2024-03-29 20:08:34 -04:00
|
|
|
const emailer = require('../config/email.js');
|
|
|
|
|
const jwt = require('../middleware/jwtToken.js');
|
|
|
|
|
|
|
|
|
|
const AppError = require('../middleware/AppError.js');
|
2024-04-05 20:10:59 -04:00
|
|
|
const { MISSING_REQUIRED_PARAMETER, LOGIN_CREDENTIALS_ERROR, GENERATE_PASSWORD_ERROR, UPDATE_PASSWORD_ERROR, DELETE_USER_ERROR } = require('../constants/errorCodes');
|
2024-03-29 20:08:34 -04:00
|
|
|
|
2025-01-27 17:38:07 -05:00
|
|
|
// controllers must use arrow functions to bind 'this' to the class instance in order to access class properties as callbacks in Express
|
2024-03-29 20:08:34 -04:00
|
|
|
class UsersController {
|
2025-01-27 17:38:07 -05:00
|
|
|
constructor(userModel) {
|
|
|
|
|
this.users = userModel;
|
|
|
|
|
}
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
async delete(req, res, next) {
|
|
|
|
|
try {
|
|
|
|
|
const { email, password } = req.body;
|
2025-01-27 17:38:07 -05:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!email || !password) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2025-01-27 17:38:07 -05:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
// verify creds first
|
2025-01-27 17:38:07 -05:00
|
|
|
const user = await this.users.login(email, password);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!user) {
|
|
|
|
|
throw new AppError(LOGIN_CREDENTIALS_ERROR);
|
|
|
|
|
}
|
2025-01-27 17:38:07 -05:00
|
|
|
|
|
|
|
|
const result = await this.users.delete(email);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!result) {
|
2025-01-27 17:38:07 -05:00
|
|
|
throw new AppError(DELETE_USER_ERROR);
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
2025-01-27 17:38:07 -05:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Utilisateur supprimé avec succès'
|
|
|
|
|
});
|
2025-01-27 17:38:07 -05:00
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-27 17:38:07 -05:00
|
|
|
module.exports = UsersController;
|