93 lines
2.4 KiB
JavaScript
93 lines
2.4 KiB
JavaScript
import Joi from "joi";
|
|
|
|
import UsersModel from "../models/users";
|
|
import AlbumsModel from "../models/albums";
|
|
import WantlistModel from "../models/wantlist";
|
|
import Pages from "./Pages";
|
|
|
|
/**
|
|
* Classe permettant la gestion de l'utilisateur connecté
|
|
*/
|
|
class Me extends Pages {
|
|
/**
|
|
* Méthode permettant de modifier le profil d'un utilisateur
|
|
* @return {Object}
|
|
*/
|
|
async patchMe() {
|
|
const { body } = this.req;
|
|
const { _id } = this.req.user;
|
|
|
|
const schema = Joi.object({
|
|
pagination: Joi.number(),
|
|
isPublicCollection: Joi.boolean(),
|
|
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, ""),
|
|
wantlist: Joi.string().allow(null, ""),
|
|
},
|
|
});
|
|
|
|
const value = await schema.validateAsync(body);
|
|
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");
|
|
}
|
|
}
|
|
|
|
if (value.mastodon !== undefined) {
|
|
user.mastodon = value.mastodon;
|
|
}
|
|
|
|
if (value.password) {
|
|
user.salt = value.password;
|
|
}
|
|
|
|
if (value.pagination) {
|
|
user.pagination = value.pagination;
|
|
}
|
|
|
|
if (value.isPublicCollection !== undefined) {
|
|
user.isPublicCollection = value.isPublicCollection;
|
|
}
|
|
|
|
user.save();
|
|
|
|
await new Promise((resolve, reject) => {
|
|
this.req.login(user, (err) => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
|
|
return resolve(null);
|
|
});
|
|
});
|
|
|
|
return user;
|
|
}
|
|
|
|
/**
|
|
* 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,
|
|
};
|
|
}
|
|
}
|
|
|
|
export default Me;
|