Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin functionalities #17

Merged
merged 7 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 99 additions & 10 deletions app/controllers/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,123 @@ import {OffreDePoste} from "../entity/OffreDePoste";
import {UserRepository} from "../repository/UserRepository";
import {User} from "../entity/User";
import {loggedInNoRedirection} from "../passport/passportFunctions";
import {Alert} from "../utils/Alert";
import {OrganisationRepository} from "../repository/OrganisationRepository";
import {Organisation} from "../entity/Organisation";

export class AdminController {
static index(req: express.Request, res: express.Response) {
res.render("admin/index", { title: "Home", user: loggedInNoRedirection(req, res)});
res.render("admin/index", { title: "Home", userLogged: loggedInNoRedirection(req, res)});
}

static utilisateurs(req: express.Request, res: express.Response) {
res.render("admin/utilisateurs", { title: "Utilisateurs", user: loggedInNoRedirection(req, res)});
UserRepository.getAll().then((users: User[]) => {
OrganisationRepository.getAll().then((organisations : Organisation[]) => {
console.log(users);
res.render("admin/utilisateurs", { title: "Utilisateurs", organisations: organisations, users, userLogged: loggedInNoRedirection(req, res)});
});
});
}

static utilisateur(req: express.Request, res: express.Response) {
let email = req.params.email;
UserRepository.getById(email).then((user: User) => {
console.log(user);
res.render("admin/utilisateur", {title: "Utilisateur", user: user, userLogged: loggedInNoRedirection(req, res)});
})
}

static async modifierUtilisateur(req: express.Request, res: express.Response) {
if (req.method === "POST") {
const alerts: Alert[] = [];
let user = new User(
req.body.email,
req.body.nom,
req.body.prenom,
req.body.telephone,
req.body.date_creation,
req.body.statut,
"",
req.body.role,
null,
null,
undefined,
);

await UserRepository.update(user).then((user: User) => {
let alert = new Alert("success", "L'utilisateur a été modifié");
alerts.push(alert);
})
.catch((err) => {
let alert = new Alert("danger", "L'utilisateur n'a pas été modifié");
alerts.push(alert);
console.log(err);
});
UserRepository.getById(req.body.email).then((user: User) => {
console.log(user);
res.render("admin/utilisateur", {title: "Utilisateur", user: user, alerts: alerts, userLogged: loggedInNoRedirection(req, res)});
})
}else{
let email = req.params.email;
UserRepository.getById(email).then((user: User) => {
console.log(user);
res.render("admin/modifierUtilisateur", {
title: "Modifier un utilisateur",
user: user,
userLogged: loggedInNoRedirection(req, res)
});
})
}
}

static async supprimerUtilisateur(req: express.Request, res: express.Response) {
let email = req.params.email;
const alerts: Alert[] = [];

await UserRepository.supprimerUtilisateur(email).then((user: User) => {
let alert = new Alert("success", "L'utilisateur a bien été supprimé");
alerts.push(alert);
})
.catch((err) => {
let alert = new Alert("danger", "L'utilisateur n'a pas été supprimé");
alerts.push(alert);
console.log(err);
});

UserRepository.getAll().then((users: User[]) => {
res.render("admin/utilisateurs", { title: "Utilisateurs", users, alerts: alerts, userLogged: loggedInNoRedirection(req, res)});
});
}

static demandes(req: express.Request, res: express.Response) {
UserRepository.getRecruiterDemand().then((users: User[]) => {
console.log(users);
res.render("admin/demandes", {title: "Demandes", users: users, user: loggedInNoRedirection(req, res)});
UserRepository.getOldRecruiterDemand().then((oldUsers: User[]) => {
OrganisationRepository.getAll().then((organisations : Organisation[]) => {
res.render("admin/demandes", {title: "Demandes", users: users, organisations: organisations, oldUsers: oldUsers, userLogged: loggedInNoRedirection(req, res)});
});
});
});
}

static accepterDemande(req: express.Request, res: express.Response) {
static demande(req: express.Request, res: express.Response) {
let email = req.params.email;
UserRepository.getById(email).then((user: User) => {
console.log(user);
res.render("admin/demande", {title: "Demande", user: user, userLogged: loggedInNoRedirection(req, res)});
})
}

static async accepterDemande(req: express.Request, res: express.Response) {
let email = req.params.email;
UserRepository.setDemandAccepted(email).then((email) => {
await UserRepository.setDemandAccepted(email).then((email) => {
console.log(email);
});
res.redirect("/admin/demandes");
}

static refuserDemande(req: express.Request, res: express.Response) {
static async refuserDemande(req: express.Request, res: express.Response) {
let email = req.params.email;
UserRepository.setDemandRefused(email).then((email) => {
await UserRepository.setDemandRefused(email).then((email) => {
console.log(email);
});
res.redirect("/admin/demandes");
Expand All @@ -40,15 +129,15 @@ export class AdminController {
static offres(req: express.Request, res: express.Response) {
OfferRepository.getAll().then((offers: OffreDePoste[]) => {
console.log(offers);
res.render("admin/offres", {title: "Offres", offers: offers, user: loggedInNoRedirection(req, res)});
res.render("admin/offres", {title: "Offres", offers: offers, userLogged: loggedInNoRedirection(req, res)});
});
}

static offre(req: express.Request, res: express.Response) {
let numero = req.params.numero;
OfferRepository.getById(Number.parseInt(numero)).then((offer: OffreDePoste) => {
console.log(offer);
res.render("admin/offre", {title: "Offre", offer: offer, user: loggedInNoRedirection(req, res)});
res.render("admin/offre", {title: "Offre", offer: offer, userLogged: loggedInNoRedirection(req, res)});
})
}
}
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/ApiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {OfferRepository} from "../repository/OfferRepository";
import {OffreDePoste} from "../entity/OffreDePoste";
import express from "express";
import {FilterOffer} from "../utils/FilterOffer";
import {UserRepository} from "../repository/UserRepository";
import {User} from "../entity/User";

export class ApiController {
static getOffers(req: express.Request, res: express.Response) {
Expand All @@ -16,4 +18,4 @@ export class ApiController {
res.json(offers);
});
}
}
}
10 changes: 5 additions & 5 deletions app/controllers/CandidatureController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class CandidatureController {
static candidater(req: express.Request, res: express.Response) {

//TODO Vérifier que l'utilisateur est bien un candidat
let user: User = new User('[email protected]', 'Tillai', 'Soudarsane', '0652645299', new Date('2020-10-10'), false, 'mdp', 'Candidat', "", "123456");
let user: User = new User('[email protected]', 'Tillai', 'Soudarsane', '0652645299', new Date('2020-10-10'), false, 'mdp', 'Candidat', "", null);

let alerts: Alert[] = [];
let numero: number = Number.parseInt(req.params.numero);
Expand All @@ -26,7 +26,7 @@ export class CandidatureController {
let alert = new Alert("danger", "La motivation doit faire plus de 20 caractères.");
alerts.push(alert);
} else {
let user: User = new User('[email protected]', 'Doe', 'John', '123456789', new Date(), true, 'password123', 'Candidat', 'En attente', '1566');
let user: User = new User('[email protected]', 'Doe', 'John', '123456789', new Date(), true, 'password123', 'Candidat', 'En attente', null);
let candidature: Candidature = new Candidature(new Date(), user, offer, StatutCandidatureEnum.EN_ATTENTE, req.body.motivation);
await CandidatureRepository.create(candidature).then(async (candidature) => {
//upload file:
Expand Down Expand Up @@ -64,7 +64,7 @@ export class CandidatureController {

}
}
res.render("candidater", {title: "Candidater", offer: offer, alerts: alerts, user: loggedInNoRedirection(req, res)});
res.render("candidater", {title: "Candidater", offer: offer, alerts: alerts, userLogged: loggedInNoRedirection(req, res)});

}
)
Expand All @@ -76,9 +76,9 @@ export class CandidatureController {

static candidatures(req: express.Request, res: express.Response) {
//TODO Vérifier que l'utilisateur est bien un candidat
let user: User = new User('[email protected]', 'Doe', 'John', '123456789', new Date(), true, 'password123', 'Candidat', 'En attente', '1566');
let user: User = new User('[email protected]', 'Doe', 'John', '123456789', new Date(), true, 'password123', 'Candidat', 'En attente', null);
CandidatureRepository.getByUser(user).then((candidatures) => {
res.render("candidatures", {title: "Mes candidatures", candidatures: candidatures, user: loggedInNoRedirection(req, res)});
res.render("candidatures", {title: "Mes candidatures", candidatures: candidatures, userLogged: loggedInNoRedirection(req, res)});

}).catch((err) => {
console.log(err);
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/FicheController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class FicheController {
//TODO get the siren from the recruiter
//random number 9 digits

let siren: string = req.user.siren as string;
let siren: string = req.user.organisation?.siren as string;
let nbHeures: number = parseInt(req.body.nbHeures);


Expand Down Expand Up @@ -42,7 +42,7 @@ export class FicheController {

}
//TODO get the siren from the recruiter
res.render("fiche/creation", {title: "Créer une fiche de poste", alerts: alerts, user: loggedInNoRedirection(req, res)});
res.render("fiche/creation", {title: "Créer une fiche de poste", alerts: alerts, userLogged: loggedInNoRedirection(req, res)});

}
}
4 changes: 2 additions & 2 deletions app/controllers/HomeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class HomeController {
//For testing mock getAll() method
FicheDePosteRepository.getDistinctRegion().then((regions: string[]) => {
OfferRepository.getAll().then((offers: OffreDePoste[]) => {
res.render("index", {title: "Home", offers: offers, regions: regions, user: loggedInNoRedirection(req, res)});
res.render("index", {title: "Home", offers: offers, regions: regions, userLogged: loggedInNoRedirection(req, res)});
});
});

Expand Down Expand Up @@ -75,7 +75,7 @@ export class HomeController {
}

OrganisationRepository.getAll().then((organisations: Organisation[]) => {
res.render("demandeRecruteur", {title: "Recruteur", organisations: organisations, alerts: alerts, user: loggedInNoRedirection(req, res)});
res.render("demandeRecruteur", {title: "Recruteur", organisations: organisations, alerts: alerts, userLogged: loggedInNoRedirection(req, res)});
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/OfferController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class OfferController {
title: "Créer une offre",
ficheDePostes: ficheDePostes,
alerts: alerts,
user: loggedInNoRedirection(req, res),
userLogged: loggedInNoRedirection(req, res),
csrfToken: req.session.csrfSecret
});
});
Expand Down
18 changes: 8 additions & 10 deletions app/controllers/RecruteurController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {OfferRepository} from "../repository/OfferRepository";
export class RecruteurController {
static index(req: express.Request, res: express.Response) {
//Get candidatures by siren

res.render("recruteur/index", {title: "Home", user: loggedInNoRedirection(req, res)});
res.render("recruteur/index", {title: "Home", userLogged: loggedInNoRedirection(req, res)});
}

static candidatures(req: express.Request, res: express.Response) {
Expand All @@ -17,19 +16,18 @@ export class RecruteurController {
let alerts: Alert[] = [];

CandidatureRepository.getBySiren(siren).then((candidatures) => {
res.render("recruteur/candidatures", {title: "Candidatures", candidatures: candidatures});
res.render("recruteur/candidatures", {title: "Candidatures", candidatures: candidatures, userLogged: loggedInNoRedirection(req, res)});
}).catch((err) => {
alerts.push(new Alert("danger", "Erreur lors de la récupération des candidatures"));
res.redirect("/recruteur");
});
}

static offres(req: express.Request, res: express.Response) {
let siren = '123456';
let alerts: Alert[] = [];
OfferRepository.getBySiren(siren).then((offers) => {
res.render("recruteur/offres", {title: "Offres", offers: offers});
});
}
let siren = '123456';
let alerts: Alert[] = [];
OfferRepository.getBySiren(siren).then((offers) => {
res.render("recruteur/offres", {title: "Offres", offers: offers, userLogged: loggedInNoRedirection(req, res)});
});
}
}

8 changes: 5 additions & 3 deletions app/entity/User.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {Organisation} from "./Organisation";

export class User {
email: string;
nom: string;
Expand All @@ -9,10 +11,10 @@ export class User {
role: string;
demande_organisation: string | null;
//TODO replace siren by organisation
siren: string|null;
organisation: Organisation|null;
id?: string;

constructor(email: string, nom: string, prenom: string, telephone: string, dateCreation: Date, statut: boolean, passwordHash: string, role: string, demande_organisation: string|null, siren: string|null, id?: string) {
constructor(email: string, nom: string, prenom: string, telephone: string, dateCreation: Date, statut: boolean, passwordHash: string, role: string, demande_organisation: string|null, organisation: Organisation|null, id?: string) {
this.email = email;
this.nom = nom;
this.prenom = prenom;
Expand All @@ -22,7 +24,7 @@ export class User {
this.passwordHash = passwordHash;
this.role = role;
this.demande_organisation = demande_organisation;
this.siren = siren;
this.organisation = organisation;
this.id=id;
}

Expand Down
23 changes: 13 additions & 10 deletions app/passport/passportFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const localStrategy = require("passport-local").Strategy;
const bcrypt = require("bcrypt");

passport.serializeUser((user:User, done:any) => {
console.log("in serialize user: ", user);
//console.log("in serialize user: ", user);
done(null, user);
});

passport.deserializeUser((user:User, done:any) => {
console.log("in deserialize user: ", user);
//console.log("in deserialize user: ", user);
done(null, user);
});

Expand All @@ -23,9 +23,15 @@ passport.use(
async (req:any, email:string, password:string, done:any, res:any)=>{
const { nom, prenom, telephone} = req.body;
try{
if(password.length <= 6 || !email){
const regex = /^(?=.*[a-z].*[a-z])(?=.*[A-Z].*[A-Z])(?=.*\d.*\d)(?=.*[$@!%*?&#].*[$@!%*?&#])[A-Za-z\d$@!%*?&#]{12,}$/;

if(!email){
done(null, false, {
message: "Veuillez saisir une adresse email",
})
}else if(!regex.test(password)){
done(null, false, {
message: "Veuillez saisir un mot de passe de 7 caractères minimum",
message: "Veuillez saisir un mot de passe de 12 caractères minimum comprenant des majuscules, des minuscules, des chiffres et des caractères spéciaux",
})
}else{
const hashedPass = await bcrypt.hash(password, 10);
Expand Down Expand Up @@ -60,18 +66,15 @@ passport.use(
}
UserRepository.getById(email)
.then(async(user) => {
//console.log(user);
//console.log(user.length==0);
//console.log(!user[0]);
if (!user[0]) {
if (!user) {
return done(null, false, { message: "Vérifiez vos identifiants et mot de passe" });
}
/*let userLogged: User = new User(user[0].email, user[0].nom, user[0].prenom, user[0].telephone, user[0].date_creation, user[0].statut, user[0].password, user[0].role, user[0].demande_organisation, user[0].siren);
let userLogged: User = new User(user.email, user.nom, user.prenom, user.telephone, user.dateCreation, user.statut, user.passwordHash, user.role, user.demande_organisation, user.organisation);
const passwordMatches = await bcrypt.compare(password, userLogged.passwordHash);

if (!passwordMatches) {
return done(null, false, { message: "Mot de passe incorrect" });
}*/
}

return done(null, user, { message: "Vous êtes connecté!" });
})
Expand Down
2 changes: 0 additions & 2 deletions app/public/js/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ function filterOffer() {
offersDiv.append(...generateOffers(offers));
}
);


}


Expand Down
Loading