2022-03-06 14:38:26 +01:00
|
|
|
import Joi from "joi";
|
|
|
|
|
|
|
|
import UsersModel from "../models/users";
|
2024-06-19 11:22:21 +02:00
|
|
|
import AlbumsModel from "../models/albums";
|
|
|
|
import WantlistModel from "../models/wantlist";
|
2022-04-10 15:21:15 +02:00
|
|
|
import Pages from "./Pages";
|
2022-03-06 14:38:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Classe permettant la gestion de l'utilisateur connecté
|
|
|
|
*/
|
2022-04-10 15:21:15 +02:00
|
|
|
class Me extends Pages {
|
2022-03-06 14:38:26 +01:00
|
|
|
/**
|
|
|
|
* Méthode permettant de modifier le profil d'un utilisateur
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
async patchMe() {
|
2023-08-02 16:11:56 +02:00
|
|
|
const { body } = this.req;
|
|
|
|
const { _id } = this.req.user;
|
2022-03-06 14:38:26 +01:00
|
|
|
|
|
|
|
const schema = Joi.object({
|
2024-02-01 08:47:33 +01:00
|
|
|
pagination: Joi.number(),
|
2022-03-06 14:38:26 +01:00
|
|
|
isPublicCollection: Joi.boolean(),
|
2023-08-02 16:11:56 +02:00
|
|
|
oldPassword: Joi.string(),
|
|
|
|
password: Joi.string(),
|
|
|
|
passwordConfirm: Joi.ref("password"),
|
|
|
|
mastodon: {
|
|
|
|
publish: Joi.boolean(),
|
|
|
|
url: Joi.string().uri().allow(null, ""),
|
|
|
|
token: Joi.string().allow(null, ""),
|
|
|
|
message: Joi.string().allow(null, ""),
|
2024-06-15 10:13:22 +02:00
|
|
|
wantlist: Joi.string().allow(null, ""),
|
2023-08-02 16:11:56 +02:00
|
|
|
},
|
2022-03-06 14:38:26 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const value = await schema.validateAsync(body);
|
2023-08-02 16:11:56 +02:00
|
|
|
const user = await UsersModel.findById(_id);
|
|
|
|
|
|
|
|
if (value.oldPassword) {
|
|
|
|
if (!user.validPassword(value.oldPassword)) {
|
|
|
|
throw new Error("Votre ancien mot de passe n'est pas valide");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-28 18:14:30 +01:00
|
|
|
if (value.mastodon !== undefined) {
|
|
|
|
user.mastodon = value.mastodon;
|
|
|
|
}
|
2023-08-02 16:11:56 +02:00
|
|
|
|
|
|
|
if (value.password) {
|
|
|
|
user.salt = value.password;
|
|
|
|
}
|
|
|
|
|
2024-02-01 08:47:33 +01:00
|
|
|
if (value.pagination) {
|
|
|
|
user.pagination = value.pagination;
|
|
|
|
}
|
|
|
|
|
2024-01-28 17:17:07 +01:00
|
|
|
if (value.isPublicCollection !== undefined) {
|
|
|
|
user.isPublicCollection = value.isPublicCollection;
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:11:56 +02:00
|
|
|
user.save();
|
2022-03-06 14:38:26 +01:00
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
2023-08-02 16:11:56 +02:00
|
|
|
this.req.login(user, (err) => {
|
2022-03-06 14:38:26 +01:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-02 16:11:56 +02:00
|
|
|
return user;
|
2022-04-10 15:21:15 +02:00
|
|
|
}
|
2024-06-19 11:22:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Méthode permettant de supprimer un utilisateur et toutes ses données
|
|
|
|
*
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
async deleteMe() {
|
|
|
|
const { _id } = this.req.user;
|
|
|
|
|
|
|
|
await AlbumsModel.deleteMany({ User: _id });
|
|
|
|
await WantlistModel.deleteMany({ User: _id });
|
|
|
|
await UsersModel.deleteOne({ _id });
|
|
|
|
|
|
|
|
return {
|
|
|
|
deleted: true,
|
|
|
|
};
|
|
|
|
}
|
2022-03-06 14:38:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Me;
|