Skip to content

Commit

Permalink
Merge pull request #20 from BaptisteBuvron/amélioration_admin
Browse files Browse the repository at this point in the history
Amélioration admin
  • Loading branch information
BaptisteBuvron authored Jun 21, 2023
2 parents 4a71929 + 76b086d commit b71a6ec
Show file tree
Hide file tree
Showing 15 changed files with 538 additions and 115 deletions.
87 changes: 87 additions & 0 deletions app/controllers/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {loggedInNoRedirection} from "../passport/passportFunctions";
import {Alert} from "../utils/Alert";
import {OrganisationRepository} from "../repository/OrganisationRepository";
import {Organisation} from "../entity/Organisation";
import {FicheDePoste} from "../entity/FicheDePoste";
import {FicheDePosteRepository} from "../repository/FicheDePosteRepository";

export class AdminController {
static index(req: express.Request, res: express.Response) {
Expand Down Expand Up @@ -140,5 +142,90 @@ export class AdminController {
res.render("admin/offre", {title: "Offre", offer: offer, userLogged: loggedInNoRedirection(req, res)});
})
}

static async modifierOffre(req: express.Request, res: express.Response) {
if (req.method === "POST") {
const alerts: Alert[] = [];

let idOffre = req.body.idOffre;
let listePieces: string[] = [];
if (req.body.pieces && Array.isArray(req.body.pieces)) {
listePieces = req.body.pieces;
}
let nb_piece = listePieces.length
let listePiecesConcat = listePieces.join(",");
let date = req.body.dateValidite.split('/');
let dateformat = `${date[2]}-${date[1]}-${date[0]}`;
let dateSQL = new Date(dateformat);

let ficheDePoste = new FicheDePoste(
req.body.idFiche,
req.body.status,
req.body.responsable,
req.body.typeMetier,
req.body.lieu,
req.body.teletravail,
Number.parseInt(req.body.nbHeures),
req.body.salaire,
req.body.description,
"",
)

let offreDePoste = new OffreDePoste(
req.body.idFiche,
req.body.etatPublication,
dateSQL,
nb_piece,
listePiecesConcat,
ficheDePoste
)

await FicheDePosteRepository.update(ficheDePoste).then((fiche: FicheDePoste) => {
let alert = new Alert("success", "La fiche a été modifié");
alerts.push(alert);
}).catch((err) => {
console.log(err);
let alert = new Alert("danger", "La fiche n'a pas été modifié");
alerts.push(alert);
});

await OfferRepository.update(offreDePoste).then((offre: OffreDePoste) => {
let alert = new Alert("success", "L'offre a été modifié");
alerts.push(alert);
}).catch((err) => {
console.log(err);
let alert = new Alert("danger", "L'offre n'a pas été modifié");
alerts.push(alert);
});

OfferRepository.getById(Number.parseInt(idOffre)).then((offer: OffreDePoste) => {
console.log(offer);
res.render("admin/offre", {title: "Offre", offer: offer, alerts: alerts, userLogged: loggedInNoRedirection(req, res)});
})
}else{
let numero = req.params.numero;
OfferRepository.getById(Number.parseInt(numero)).then((offer: OffreDePoste) => {
res.render("admin/modifierOffre", {title: "Modifier une offre", offer: offer, userLogged: loggedInNoRedirection(req, res)});
})
}
}

static async supprimerOffre(req: express.Request, res: express.Response) {
const alerts: Alert[] = [];
let numero = req.params.numero;
await OfferRepository.supprimer(Number.parseInt(numero)).then((suppression: boolean) => {
console.log(suppression);
let alert = new Alert("success", "L'offre a été supprimée");
alerts.push(alert);
}).catch((err) => {
console.log(err);
let alert = new Alert("danger", "L'offre n'a été supprimée");
alerts.push(alert);
});
OfferRepository.getAll().then((offers: OffreDePoste[]) => {
console.log(offers);
res.render("admin/offres", {title: "Offres", alerts: alerts, offers: offers, userLogged: loggedInNoRedirection(req, res)});
});
}
}

36 changes: 32 additions & 4 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
"bcrypt": "^5.1.0",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"date-fns": "^2.30.0",
"dotenv": "^16.0.3",
"ejs": "^3.1.9",
"express": "^4.18.2",
"express-session": "^1.17.3",
"moment": "^2.29.4",
"multer": "^1.4.5-lts.1",
"mysql": "^2.18.1",
"passport": "^0.6.0",
Expand Down
20 changes: 19 additions & 1 deletion app/passport/passportFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@ export function loggedInNoRedirection(req:any, res:any) {
}
}

// Middleware pour vérifier la connexion + le rôle de l'utilisateur pouvant être de 2 profils différents
function checkRoleTwoProfile(role:any, role2:any) {
return function (req:any, res:any, next:any) {
if (req.isAuthenticated() && (req.user.role === role || req.user.role === role2)) {
return next();
}
let message;
if (!(req.isAuthenticated())) {
message ="Vous n'êtes pas connecté";
}
else if (!(req.user.role === role || req.user.role === role2)) {
message = "Vous n'avez pas les accès nécéssaires pour cet onglet";
}
res.redirect(`/login?message=${message}`);

};
}

// Middleware pour vérifier la connexion + le rôle de l'utilisateur
function checkRole(role:any) {
Expand All @@ -133,5 +150,6 @@ module.exports = {
passport,
loggedIn,
checkRole,
loggedInNoRedirection
loggedInNoRedirection,
checkRoleTwoProfile
};
21 changes: 19 additions & 2 deletions app/repository/FicheDePosteRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,25 @@ export class FicheDePosteRepository {
});
}

update(id: number, entity: FicheDePoste): Promise<FicheDePoste | null> {
throw new Error("Method not implemented.");
static update(fiche: FicheDePoste): Promise<FicheDePoste> {
const query = `UPDATE FicheDePoste
SET status = ?,
responsable = ?,
type_metier = ?,
lieu = ?,
teletravail = ?,
nb_heures = ?,
salaire = ?,
description = ?
WHERE numero = ?`;
return new Promise<FicheDePoste>((resolve, reject) => {
pool.query(query, [fiche.status, fiche.responsable, fiche.typeMetier, fiche.lieu, fiche.teletravail, fiche.nbHeures, fiche.salaire, fiche.description, fiche.id], (err, result) => {
if (err) {
return reject(err);
}
return resolve(result);
});
});
}

}
36 changes: 31 additions & 5 deletions app/repository/OfferRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ export class OfferRepository {
if (result.length == 0 || err) {
return reject("Not found");
}
result[0].date_validite = new Date(result[0].date_validite).toLocaleDateString('fr-FR')

let organisation = new Organisation(result[0].siren, result[0].nom, result[0].type, result[0].siege);
let ficheDePoste = new FicheDePoste(result[0].fiche, result[0].status, result[0].responsable, result[0].type_metier, result[0].lieu, result[0].teletravail, result[0].nbheure, result[0].salaire, result[0].description, result[0].siren, organisation);
let ficheDePoste = new FicheDePoste(result[0].fiche, result[0].status, result[0].responsable, result[0].type_metier, result[0].lieu, result[0].teletravail, result[0].nb_heures, result[0].salaire, result[0].description, result[0].siren, organisation);
let offer = new OffreDePoste(result[0].numero, result[0].etat, result[0].date_validite, result[0].nb_piece, result[0].liste_piece, ficheDePoste);
return resolve(offer);
}
Expand Down Expand Up @@ -121,12 +123,36 @@ export class OfferRepository {

}

update(id: number, entity: OffreDePoste): Promise<OffreDePoste | null> {
throw new Error("Method not implemented.");
static update(offre: OffreDePoste): Promise<OffreDePoste> {
const query = `UPDATE OffreDePoste
SET etat = ?,
date_validite = ?,
nb_piece = ?,
liste_piece = ?
WHERE numero = ?`;
return new Promise<OffreDePoste>(
(resolve, reject) => {
pool.query(query, [offre.etat, offre.dateValidite, offre.nbPiece, offre.listePiece, offre.numero], (err, result) => {
if (err) {
return reject(err);
}
return resolve(result);
});
});
}

delete(id: number): Promise<boolean> {
throw new Error("Method not implemented.");
static supprimer(id: number): Promise<boolean> {
const query = `DELETE FROM OffreDePoste
WHERE numero = ?`;
return new Promise<boolean>(
(resolve, reject) => {
pool.query(query, [id], (err, result) => {
if (err) {
return reject(err);
}
return resolve(result);
});
});
}

}
3 changes: 3 additions & 0 deletions app/routes/AdminRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ adminRouter.get("/accepterDemande/:email", AdminController.accepterDemande);
adminRouter.get("/refuserDemande/:email", AdminController.refuserDemande);
adminRouter.get("/offres", AdminController.offres);
adminRouter.get("/offre/:numero", AdminController.offre);
adminRouter.get("/modifierOffre/:numero", AdminController.modifierOffre);
adminRouter.post("/modifierOffre/:numero", AdminController.modifierOffre);
adminRouter.get("/supprimerOffre/:numero", AdminController.supprimerOffre);


12 changes: 1 addition & 11 deletions app/routes/MainRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,7 @@ defaultRouter.post(
return res.redirect(`/login?message=${info.message}`);
}
req.login(user, async () => {
let role = user.role;
let url;
if (role == "Administrateur") {
url = "admin";
} else if (role == "Recruteur") {
url = "recruteur";
} else {
url = "";
}

return res.redirect(`/${url}`);
return res.redirect(`/`);
});
})(req, res, next);
}
Expand Down
Loading

0 comments on commit b71a6ec

Please sign in to comment.