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

feat(api): 3853 - Notifier le jeune et les ref si un ref désiste un jeune #4669

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion api/src/__tests__/referent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe("Referent", () => {
);
expect(response.statusCode).toEqual(200);
expect(young?.status).toEqual("WITHDRAWN");
expect(young?.statusPhase1).toEqual("AFFECTED");
expect(young?.statusPhase1).toEqual("WAITING_AFFECTATION");
expect(young?.statusPhase2).toEqual("WAITING_REALISATION");
expect(young?.statusPhase3).toEqual("WAITING_REALISATION");
});
Expand Down
67 changes: 66 additions & 1 deletion api/src/referent/referentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ import {
getDepartmentForInscriptionGoal,
isAdmin,
isReferentReg,
isReferentDep,
canValidateYoungToLP,
WITHRAWN_REASONS,
formatDateFRTimezoneUTC,
} from "snu-lib";
import { getFilteredSessions, getAllSessions, getFilteredSessionsForCLE } from "../utils/cohort";
import scanFile from "../utils/virusScanner";
Expand Down Expand Up @@ -675,6 +676,12 @@ router.put("/young/:id", passport.authenticate("referent", { session: false, fai
}
}

if (newYoung.status === YOUNG_STATUS.WITHDRAWN && young.status !== YOUNG_STATUS.WITHDRAWN) {
const { withdrawnReason } = newYoung;
await handleNotifForWithdrawn(young, cohort, withdrawnReason);
newYoung.statusPhase1 = young.statusPhase1 === YOUNG_STATUS_PHASE1.AFFECTED ? YOUNG_STATUS_PHASE1.WAITING_AFFECTATION : young.statusPhase1;
}

young.set(newYoung);
await young.save({ fromUser: req.user });

Expand Down Expand Up @@ -707,6 +714,64 @@ router.put("/young/:id", passport.authenticate("referent", { session: false, fai
}
});

async function handleNotifForWithdrawn(young, cohort, withdrawnReason) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tu peux mettre ce code dans un service

const oldStatusPhase1 = young.statusPhase1;

// We notify the ref dep and the young
try {
const youngFullName = young.firstName + " " + young.lastName;
const referents: ReferentDocument[] = await ReferentModel.find({ role: ROLES.REFERENT_DEPARTMENT, department: young.department });
const SUB_ROLES_PRIORITY = [SUB_ROLES.manager_department, SUB_ROLES.assistant_manager_department, SUB_ROLES.secretariat, SUB_ROLES.manager_phase2];
let selectedReferent: ReferentDocument | undefined = referents.find((referent) => referent.subRole && SUB_ROLES_PRIORITY.includes(referent.subRole));
if (!selectedReferent && referents.length > 0) {
selectedReferent = referents[0];
}
if (selectedReferent) {
await sendTemplate(SENDINBLUE_TEMPLATES.referent.YOUNG_WITHDRAWN_NOTIFICATION, {
emailTo: [{ name: `${selectedReferent.firstName} ${selectedReferent.lastName}`, email: selectedReferent.email }],
params: { student_name: youngFullName, message: WITHRAWN_REASONS.find((r) => r.value === withdrawnReason)?.label || "" },
});
}
// If they are CLE, we notify the class referent.
if (cohort?.type === YOUNG_SOURCE.CLE) {
const classe = await ClasseModel.findById(young.classeId);
const referent = await ReferentModel.findById(classe?.referentClasseIds[0]);
const datecohorte = `du ${formatDateFRTimezoneUTC(cohort.dateStart)} au ${formatDateFRTimezoneUTC(cohort.dateEnd)}`;
if (referent) {
await sendTemplate(SENDINBLUE_TEMPLATES.referent.YOUNG_WITHDRAWN_CLE, {
emailTo: [{ name: `${referent.firstName} ${referent.lastName}`, email: referent.email }],
params: {
youngFirstName: young.firstName,
youngLastName: young.lastName,
datecohorte,
raisondesistement: WITHRAWN_REASONS.find((r) => r.value === withdrawnReason)?.label || "",
},
});
}
}

// If young affected, we notify the head center
if (oldStatusPhase1 === YOUNG_STATUS_PHASE1.AFFECTED && young.sessionPhase1Id != null) {
const session = await SessionPhase1Model.findById(young.sessionPhase1Id);
const headCenter = await ReferentModel.findById(session?.headCenterId);

if (headCenter) {
await sendTemplate(SENDINBLUE_TEMPLATES.headCenter.YOUNG_WITHDRAWN, {
emailTo: [{ name: `${headCenter.firstName} ${headCenter.lastName}`, email: headCenter.email }],
params: { contact_name: youngFullName, message: WITHRAWN_REASONS.find((r) => r.value === withdrawnReason)?.label || "" },
});
}
}

await sendTemplate(SENDINBLUE_TEMPLATES.young.WITHDRAWN, {
emailTo: [{ name: `${young.firstName} ${young.lastName}`, email: young.email }],
params: { message: WITHRAWN_REASONS.find((r) => r.value === withdrawnReason)?.label || "" },
});
} catch (e) {
capture(e);
}
}

/**
* Permet de valider plusieurs jeunes CLE à la fois (pas d'objectifs pour les CLE).
* Gère uniquement le changement de status vers VALIDATED et n'est pas compatible HTS
Expand Down
Loading