From adf4efd9243d7a96a3772009693c5c826472241b Mon Sep 17 00:00:00 2001 From: Baptiste Buvron Date: Thu, 22 Jun 2023 00:36:04 +0200 Subject: [PATCH] =?UTF-8?q?Fix:=20-=20Tentative=20de=20connexion=20avec=20?= =?UTF-8?q?un=20email=20non=20existant=20-=20Supression=20d'une=20offre=20?= =?UTF-8?q?qui=20poss=C3=A8de=20des=20candidatures=20(ON=20DELETE=20CASCAD?= =?UTF-8?q?E)=20-=20Fix=20du=20message=20invitant=20=C3=A0=20se=20connecte?= =?UTF-8?q?r=20page=20d'accueil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/AdminController.ts | 4 +- app/passport/passportFunctions.ts | 1 + app/repository/UserRepository.ts | 227 +++++++++++++++++------------ app/table.sql | 14 +- app/views/index.ejs | 2 +- 5 files changed, 143 insertions(+), 105 deletions(-) diff --git a/app/controllers/AdminController.ts b/app/controllers/AdminController.ts index e5b0529..f064ec6 100644 --- a/app/controllers/AdminController.ts +++ b/app/controllers/AdminController.ts @@ -225,9 +225,7 @@ export class AdminController { let alert = new Alert("danger", "L'offre n'a été supprimée"); alerts.push(alert); }); - OfferRepository.getAll().then((offers: OffreDePoste[]) => { - res.render("admin/offres", {title: "Offres", alerts: alerts, offers: offers, userLogged: loggedInNoRedirection(req, res)}); - }); + res.redirect("/admin/offres"); } } diff --git a/app/passport/passportFunctions.ts b/app/passport/passportFunctions.ts index 6e7f6de..53cddc7 100644 --- a/app/passport/passportFunctions.ts +++ b/app/passport/passportFunctions.ts @@ -80,6 +80,7 @@ passport.use( }) .catch((err) => { console.log(err); + return done(null, false, {message: "Erreur lors de la connexion"}); }); } catch (error) { return done(error); diff --git a/app/repository/UserRepository.ts b/app/repository/UserRepository.ts index 89e0b0b..4b8c792 100644 --- a/app/repository/UserRepository.ts +++ b/app/repository/UserRepository.ts @@ -8,86 +8,117 @@ const db = pool; export class UserRepository { static tableName = "Utilisateur"; - static getById(email:string): Promise { - const query = `SELECT u.email, u.nom, u.prenom, u.telephone, DATE_FORMAT(u.date_creation, '%d-%m-%Y') as date_creation, u.statut, u.password, u.role, u.demande_organisation, o.siren, o.nom as organisation, o.type, o.siege - FROM ${UserRepository.tableName} u LEFT JOIN ${OrganisationRepository.tableName} o using (siren) + static getById(email: string): Promise { + const query = `SELECT u.email, + u.nom, + u.prenom, + u.telephone, + DATE_FORMAT(u.date_creation, '%d-%m-%Y') as date_creation, + u.statut, + u.password, + u.role, + u.demande_organisation, + o.siren, + o.nom as organisation, + o.type, + o.siege + FROM ${UserRepository.tableName} u + LEFT JOIN ${OrganisationRepository.tableName} o using (siren) WHERE u.email = ?`; return new Promise( (resolve, reject) => - pool.query(query,[email], (err, result) => { - if (err) { - return reject(err); - } - let organisation = new Organisation( - result[0].siren, - result[0].organisation, - result[0].type, - result[0].siege - ); - let user = new User( - result[0].email, - result[0].nom, - result[0].prenom, - result[0].telephone, - result[0].date_creation, - result[0].statut, - result[0].password, - result[0].role, - result[0].demande_organisation, - organisation, - undefined - ); - - return resolve(user); + pool.query(query, [email], (err, result) => { + if (err) { + return reject(err); + } + if (result.length === 0) { + return reject(new Error("User not found")); + } + let organisation = new Organisation( + result[0].siren, + result[0].organisation, + result[0].type, + result[0].siege + ); + let user = new User( + result[0].email, + result[0].nom, + result[0].prenom, + result[0].telephone, + result[0].date_creation, + result[0].statut, + result[0].password, + result[0].role, + result[0].demande_organisation, + organisation, + undefined + ); + + return resolve(user); } ) ); } static getAll(): Promise<[User]> { - const query = `SELECT u.email, u.nom, u.prenom, u.telephone, DATE_FORMAT(u.date_creation, '%d-%m-%Y') as date_creation, u.statut, u.password, u.role, u.demande_organisation, o.siren, o.nom as organisation, o.type, o.siege - FROM ${UserRepository.tableName} u LEFT JOIN ${OrganisationRepository.tableName} o using (siren)`; + const query = `SELECT u.email, + u.nom, + u.prenom, + u.telephone, + DATE_FORMAT(u.date_creation, '%d-%m-%Y') as date_creation, + u.statut, + u.password, + u.role, + u.demande_organisation, + o.siren, + o.nom as organisation, + o.type, + o.siege + FROM ${UserRepository.tableName} u + LEFT JOIN ${OrganisationRepository.tableName} o using (siren)`; return new Promise<[User]>( (resolve, reject) => pool.query(query, (err, result) => { - if (err) { - return reject(err); - } + if (err) { + return reject(err); + } - let organisation; - let user; - for (let i = 0; i < result.length; i++) { - organisation = new Organisation( - result[i].siren, - result[i].organisation, - result[i].type, - result[i].siege - ); - user = new User( - result[i].email, - result[i].nom, - result[i].prenom, - result[i].telephone, - result[i].date_creation, - result[i].statut, - result[i].password, - result[i].role, - result[i].demande_organisation, - organisation, - undefined - ); - result[i] = user; - } + let organisation; + let user; + for (let i = 0; i < result.length; i++) { + organisation = new Organisation( + result[i].siren, + result[i].organisation, + result[i].type, + result[i].siege + ); + user = new User( + result[i].email, + result[i].nom, + result[i].prenom, + result[i].telephone, + result[i].date_creation, + result[i].statut, + result[i].password, + result[i].role, + result[i].demande_organisation, + organisation, + undefined + ); + result[i] = user; + } - return resolve(result); + return resolve(result); } ) ); } static create(entity: User): Promise { - const query = `INSERT INTO ${UserRepository.tableName} (email, nom, prenom, telephone, date_creation, statut, password, role, demande_organisation, siren) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`; + const query = `INSERT INTO ${UserRepository.tableName} (email, nom, prenom, telephone, date_creation, statut, + password, role, demande_organisation, siren) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`; return new Promise((resolve, reject) => { pool.query(query, [entity.email, entity.nom, entity.prenom, entity.telephone, entity.dateCreation, entity.statut, entity.passwordHash, entity.role, entity.demande_organisation, entity.organisation?.siren], (err, result) => { if (err) { @@ -98,13 +129,13 @@ export class UserRepository { }); } - static update(user : User): Promise { + static update(user: User): Promise { const query = `UPDATE ${UserRepository.tableName} - SET nom = ?, - prenom = ?, + SET nom = ?, + prenom = ?, telephone = ?, - statut = ?, - role = ? + statut = ?, + role = ? WHERE email = ?`; return new Promise( (resolve, reject) => @@ -118,8 +149,9 @@ export class UserRepository { ); } - static supprimerUtilisateur(email:string): Promise { - const query = `DELETE FROM ${UserRepository.tableName} + static supprimerUtilisateur(email: string): Promise { + const query = `DELETE + FROM ${UserRepository.tableName} WHERE email = ?`; return new Promise( (resolve, reject) => @@ -141,15 +173,15 @@ export class UserRepository { return new Promise<[User]>( (resolve, reject) => pool.query(query, (err, result) => { - if (err) { - return reject(err); - } - if(result[0]){ - console.log(result[0]); - let organisation = new Organisation(result[0].siren, result[0].organisation, result[0].type, result[0].siege); - result[0].organisation = organisation; - console.log(result); - } + if (err) { + return reject(err); + } + if (result[0]) { + console.log(result[0]); + let organisation = new Organisation(result[0].siren, result[0].organisation, result[0].type, result[0].siege); + result[0].organisation = organisation; + console.log(result); + } return resolve(result); } ) @@ -160,19 +192,19 @@ export class UserRepository { const query = `SELECT u.email, u.nom, u.prenom, o.siren, o.nom as organisation, o.type, o.siege FROM ${UserRepository.tableName} u INNER JOIN ${OrganisationRepository.tableName} o using (siren) - WHERE u.demande_organisation = 'En cours' - OR u.demande_organisation = 'refus' - OR u.demande_organisation = 'acceptation'`; + WHERE u.demande_organisation = 'En cours' + OR u.demande_organisation = 'refus' + OR u.demande_organisation = 'acceptation'`; return new Promise<[User]>( (resolve, reject) => pool.query(query, (err, result) => { - if (err) { - return reject(err); - } - if(result[0]){ - let organisation = new Organisation(result[0].siren, result[0].organisation, result[0].type, result[0].siege); - result[0].organisation = organisation; - } + if (err) { + return reject(err); + } + if (result[0]) { + let organisation = new Organisation(result[0].siren, result[0].organisation, result[0].type, result[0].siege); + result[0].organisation = organisation; + } return resolve(result); } ) @@ -180,21 +212,28 @@ export class UserRepository { } static getOldRecruiterDemand(): Promise<[User]> { - const query = `SELECT u.email, u.nom, u.prenom, u.demande_organisation, o.siren, o.nom as organisation, o.type, o.siege + const query = `SELECT u.email, + u.nom, + u.prenom, + u.demande_organisation, + o.siren, + o.nom as organisation, + o.type, + o.siege FROM ${UserRepository.tableName} u INNER JOIN ${OrganisationRepository.tableName} o using (siren) WHERE u.demande_organisation = 'refus' - OR u.demande_organisation = 'acceptation'`; + OR u.demande_organisation = 'acceptation'`; return new Promise<[User]>( (resolve, reject) => pool.query(query, (err, result) => { - if (err) { - return reject(err); - } - if(result[0]){ - let organisation = new Organisation(result[0].siren, result[0].organisation, result[0].type, result[0].siege); - result[0].organisation = organisation; - } + if (err) { + return reject(err); + } + if (result[0]) { + let organisation = new Organisation(result[0].siren, result[0].organisation, result[0].type, result[0].siege); + result[0].organisation = organisation; + } return resolve(result); } ) diff --git a/app/table.sql b/app/table.sql index bfdd177..f4b3501 100644 --- a/app/table.sql +++ b/app/table.sql @@ -48,7 +48,7 @@ CREATE TABLE `FicheDePoste` `salaire` int(11) NOT NULL, `description` text NOT NULL, `siren` varchar(11) NOT NULL, - FOREIGN KEY (siren) REFERENCES Organisation (siren), + FOREIGN KEY (siren) REFERENCES Organisation (siren) ON DELETE CASCADE, PRIMARY KEY (numero) ); @@ -66,7 +66,7 @@ CREATE TABLE `OffreDePoste` `nb_piece` int(11) NOT NULL, `liste_piece` text NOT NULL, `fiche` int(11) NOT NULL, - FOREIGN KEY (fiche) REFERENCES FicheDePoste (numero), + FOREIGN KEY (fiche) REFERENCES FicheDePoste (numero) ON DELETE CASCADE, PRIMARY KEY (numero) ); @@ -125,9 +125,11 @@ CREATE TABLE `Candidature` `offre` int(11) NOT NULL, `statut` ENUM ('En analyse', 'Acceptée', 'Refusée', 'En attente'), `motivation` text NOT NULL, - FOREIGN KEY (candidat) REFERENCES Utilisateur (email), - FOREIGN KEY (offre) REFERENCES OffreDePoste (numero), + FOREIGN KEY (candidat) REFERENCES Utilisateur (email) ON DELETE CASCADE, + FOREIGN KEY (offre) REFERENCES OffreDePoste (numero) ON DELETE CASCADE, PRIMARY KEY (candidat, offre) + ##cascade delete for candidat and offre + ); @@ -145,7 +147,7 @@ CREATE TABLE `Piece` `url` varchar(128) NOT NULL, `candidat` varchar(128) NOT NULL, `offre` int(11) NOT NULL, - FOREIGN KEY (candidat, offre) REFERENCES Candidature (candidat, offre), + FOREIGN KEY (candidat, offre) REFERENCES Candidature (candidat, offre) ON DELETE CASCADE, PRIMARY KEY (id) ); @@ -153,8 +155,6 @@ CREATE TABLE `Piece` -- Déchargement des données de la table `Piece` -- - - -- -------------------------------------------------------- #Insertion des données diff --git a/app/views/index.ejs b/app/views/index.ejs index b015506..3a6fc89 100644 --- a/app/views/index.ejs +++ b/app/views/index.ejs @@ -9,7 +9,7 @@

Site de recrutement

- <% if(typeof userLogged === 'undefined'){ %> + <% if (!user) { %>