'use strict'; const Joi = require('joi'); const { Model } = require('@hapipal/schwifty'); const { encryptSha1 } = require('iut-encrypt-cemal'); // Importe les fonctions d'encryption module.exports = class User extends Model { static get tableName() { return 'user'; } static get joiSchema() { return Joi.object({ id: Joi.number().integer().greater(0), scope: Joi.string().required().example('user').description('Scope of the user'), username: Joi.string().min(3).required().example('john_doe').description('Username of the user'), firstName: Joi.string().min(3).required().example('John').description('Firstname of the user'), lastName: Joi.string().min(3).required().example('Doe').description('Lastname of the user'), email: Joi.string().email().required().example('john@example.com').description('Email address of the user'), password: Joi.string().min(8).required().example('password123').description('Password of the user'), createdAt: Joi.date(), updatedAt: Joi.date() }); } $beforeInsert(queryContext) { this.scope = 'user'; this.updatedAt = new Date(); this.createdAt = this.updatedAt; this.password = encryptSha1(this.password); // Encrypte le mot de passe avant insertion } $beforeUpdate(queryContext) { this.updatedAt = new Date(); // Si le mot de passe a été modifié, ré-encrypte-le avant la mise à jour if (this.password && this.old && this.password !== this.old.password) { this.password = encryptSha1(this.password); } } };