Skip to content

Commit

Permalink
fix(api): 3443 - modification des règles pour valider un jeune (#4399)
Browse files Browse the repository at this point in the history
  • Loading branch information
C2Chandelier authored Oct 2, 2024
1 parent cd12f7d commit c750f4e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
2 changes: 0 additions & 2 deletions admin/src/scenes/classe/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ export function exportExcelSheet(classes: ClasseExport[], type: typeExport) {
studentInProgress: c.studentInProgress ?? 0,
studentWaiting: c.studentWaiting ?? 0,
studentWithdrawn: c.studentWithdrawn ?? 0,
dossierOuvert: `${Math.round(((c.studentWaiting ?? 0 + c.studentInProgress ?? 0 + c.studentValidated ?? 0) / c.totalSeats) * 100)} %`,
academy: c.academy,
region: c.region,
department: c.department,
Expand Down Expand Up @@ -281,7 +280,6 @@ export function exportExcelSheet(classes: ClasseExport[], type: typeExport) {
"Élèves en cours d'inscription",
"Élèves en attente de validation",
"Élèves désistés",
"Dossiers ouverts / effectif ajusté (%)",
"Académie",
"Région",
"Département",
Expand Down
13 changes: 11 additions & 2 deletions api/src/__tests__/referent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ describe("Referent", () => {
}
it("should not update young if goal reached", async () => {
const testName = "Juillet 2023";
const now = new Date();
const tomorrow = new Date(now);
tomorrow.setDate(now.getDate() + 1);
const cohort = await createCohortHelper(getNewCohortFixture({ name: testName, instructionEndDate: tomorrow }));
const { count } = await getInscriptionGoalStats(testName, testName);
// ajout d'un objectif à 1
const res = await request(getAppHelper())
Expand All @@ -107,7 +111,7 @@ describe("Referent", () => {
{
status: YOUNG_STATUS.VALIDATED,
},
{ region: testName, department: testName, cohort: testName },
{ region: testName, department: testName, cohort: cohort.name, cohortId: cohort._id },
{ keepYoung: true },
);
expect(responseSuccessed.statusCode).toEqual(200);
Expand Down Expand Up @@ -140,7 +144,7 @@ describe("Referent", () => {
{
status: YOUNG_STATUS.VALIDATED,
},
{ region: testName, department: testName },
{ region: testName, department: testName, cohort: cohort.name, cohortId: cohort._id },
{ queryParam: "?forceGoal=1" },
)
).response;
Expand Down Expand Up @@ -204,13 +208,18 @@ describe("Referent", () => {
});
it("should remove places when sending to cohesion center", async () => {
const sessionPhase1: any = await createSessionPhase1(getNewSessionPhase1Fixture());
const now = new Date();
const tomorrow = new Date(now);
tomorrow.setDate(now.getDate() + 1);
const cohort = await createCohortHelper(getNewCohortFixture({ name: "Juillet 2023", instructionEndDate: tomorrow }));
const placesLeft = sessionPhase1.placesLeft;
const { young, response } = await createYoungThenUpdate(
{},
{
sessionPhase1Id: sessionPhase1._id,
status: "VALIDATED",
statusPhase1: "AFFECTED",
cohortId: cohort._id,
},
);
expect(response.statusCode).toEqual(200);
Expand Down
7 changes: 5 additions & 2 deletions api/src/__tests__/young.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,11 +770,14 @@ describe("Young", () => {
});
describe("YoungModel post save hook", () => {
it("should call StateManager.Classe.compute if source is CLE", async () => {
const cohortFixture = getNewCohortFixture({ type: "CLE" });
const now = new Date();
const tomorrow = new Date(now);
tomorrow.setDate(now.getDate() + 1);
const cohortFixture = getNewCohortFixture({ type: "CLE", instructionEndDate: tomorrow });
const cohort = await createCohortHelper(cohortFixture);
const classe = createFixtureClasse({ seatsTaken: 0, status: "OPEN", cohort: cohort.name, cohortId: cohort._id });
const classeId = (await createClasse(classe))._id;
const youngFixture = getNewYoungFixture({ source: YOUNG_SOURCE.CLE, classeId });
const youngFixture = getNewYoungFixture({ source: YOUNG_SOURCE.CLE, classeId, cohort: cohort.name, cohortId: cohort._id });
const young = await createYoungHelper(youngFixture);
const updatedYoung = { ...young.toObject(), status: "VALIDATED" };
await young.set(updatedYoung).save();
Expand Down
30 changes: 24 additions & 6 deletions api/src/referent/referentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ import {
YOUNG_SITUATIONS,
CLE_FILIERE,
canValidateMultipleYoungsInClass,
canValidateYoungInClass,
ClasseSchoolYear,
canUpdateInscriptionGoals,
FUNCTIONAL_ERRORS,
Expand Down Expand Up @@ -611,11 +610,30 @@ router.put("/young/:id", passport.authenticate("referent", { session: false, fai
newYoung.reinscriptionStep2023 = "DONE";
}

if (newYoung.status === YOUNG_STATUS.VALIDATED && young.source === YOUNG_SOURCE.CLE) {
const classe = await ClasseModel.findById(young.classeId);
if (!classe) return res.status(404).send({ ok: false, code: ERRORS.NOT_FOUND });
if (!canValidateYoungInClass(req.user, classe)) {
return res.status(403).send({ ok: false, code: ERRORS.OPERATION_UNAUTHORIZED });
// verification des dates de fin d'instruction si jeune est VALIDATED
if (newYoung.status === YOUNG_STATUS.VALIDATED) {
if (young.source === YOUNG_SOURCE.CLE) {
const classe = await ClasseModel.findById(young.classeId);
if (!classe) return res.status(404).send({ ok: false, code: ERRORS.NOT_FOUND });
const cohort = await CohortModel.findById(classe.cohortId);
if (!cohort) return res.status(404).send({ ok: false, code: ERRORS.NOT_FOUND });
const now = new Date();
const isInsctructionOpen = now < cohort.instructionEndDate;
if (!isInsctructionOpen) {
return res.status(403).send({ ok: false, code: ERRORS.OPERATION_NOT_ALLOWED });
}
const remainingPlaces = classe.totalSeats - classe.seatsTaken;
if (remainingPlaces <= 0) {
return res.status(403).send({ ok: false, code: ERRORS.OPERATION_NOT_ALLOWED });
}
} else {
const cohort = await CohortModel.findById(young.cohortId);
if (!cohort) return res.status(404).send({ ok: false, code: ERRORS.NOT_FOUND });
const now = new Date();
const isInsctructionOpen = now < cohort.instructionEndDate;
if (!isInsctructionOpen) {
return res.status(400).send({ ok: false, code: ERRORS.OPERATION_NOT_ALLOWED });
}
}
}

Expand Down
5 changes: 0 additions & 5 deletions packages/lib/src/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,10 +1030,6 @@ function canCreateEtablissement(user: UserDto) {
function canValidateMultipleYoungsInClass(actor: UserDto) {
return [ROLES.ADMINISTRATEUR_CLE, ROLES.REFERENT_CLASSE].includes(actor.role);
}
function canValidateYoungInClass(actor: UserDto, classe: ClasseType) {
if (isAdmin(actor)) return true;
return [ROLES.REFERENT_DEPARTMENT, ROLES.REFERENT_REGION, ROLES.ADMINISTRATEUR_CLE, ROLES.REFERENT_CLASSE].includes(actor.role) && classe.status === STATUS_CLASSE.OPEN;
}

export {
ROLES,
Expand Down Expand Up @@ -1189,5 +1185,4 @@ export {
canUpdateReferentClasse,
canCreateEtablissement,
canValidateMultipleYoungsInClass,
canValidateYoungInClass,
};

0 comments on commit c750f4e

Please sign in to comment.