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

fix(api,admin): 3677 - Inscription Goal utilisation du departemet de residence #4545

Merged
merged 1 commit into from
Nov 18, 2024
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
6 changes: 2 additions & 4 deletions admin/src/scenes/phase0/YoungFooterNoRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isBefore } from "date-fns";
import { toastr } from "react-redux-toastr";
import { HiInformationCircle } from "react-icons/hi";

import { YOUNG_STATUS, YOUNG_SOURCE, YoungDto, getDepartmentForEligibility, translate, InscriptionGoalsRoutes } from "snu-lib";
import { YOUNG_STATUS, YOUNG_SOURCE, YoungDto, getDepartmentForInscriptionGoal, translate } from "snu-lib";

import { capture } from "@/sentry";
import api from "@/services/api";
Expand Down Expand Up @@ -38,9 +38,7 @@ export function YoungFooterNoRequest({ processing, young, onProcess, footerClass
const isDatePassed = young.latestCNIFileExpirationDate ? isBefore(new Date(young.latestCNIFileExpirationDate), new Date()) : false;
let isGoalReached, isLCavailable;
if (young.source !== YOUNG_SOURCE.CLE) {
// on vérifie la completion des objectifs pour la région (en fonction du département)
// schoolDepartment pour les scolarisés et HZR sinon department pour les non scolarisés
const departement = getDepartmentForEligibility(young);
const departement = getDepartmentForInscriptionGoal(young);
const tauxRemplissage = await InscriptionGoalService.getTauxRemplissage({ cohort: young.cohort!, department: departement });
isGoalReached = tauxRemplissage >= 1;
// on vérifie qu'il n'y pas de jeunes en LC
Expand Down
5 changes: 2 additions & 3 deletions admin/src/scenes/volontaires/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
PHONE_ZONES,
YOUNG_SOURCE,
getSchoolYear,
getDepartmentForEligibility,
getDepartmentForInscriptionGoal,
CohortType,
EtablissementDto,
} from "snu-lib";
Expand Down Expand Up @@ -422,8 +422,7 @@ export default function Create() {
sendData();
} else {
// on vérifie la completion des objectifs pour le département
// schoolDepartment pour les scolarisés et HZR sinon department pour les non scolarisés
const departement = getDepartmentForEligibility(values);
const departement = getDepartmentForInscriptionGoal(values);
const res = await api.get(`/inscription-goal/${values.cohort}/department/${departement}`);
if (!res.ok) {
toastr.error("Séjour non disponible: ", translate(res.code));
Expand Down
5 changes: 2 additions & 3 deletions api/src/controllers/young/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import {
translateFileStatusPhase1,
REGLEMENT_INTERIEUR_VERSION,
ReferentType,
getDepartmentForEligibility,
getDepartmentForInscriptionGoal,
FUNCTIONAL_ERRORS,
CohortDto,
} from "snu-lib";
Expand Down Expand Up @@ -276,8 +276,7 @@ router.post("/invite", passport.authenticate("referent", { session: false, failW
}

if (obj.source !== YOUNG_SOURCE.CLE && value.status === YOUNG_STATUS.VALIDATED) {
// schoolDepartment pour les scolarisés et HZR sinon department pour les non scolarisés
const departement = getDepartmentForEligibility(obj);
const departement = getDepartmentForInscriptionGoal(obj);
const completionObjectif = await getCompletionObjectifs(departement, cohort.name);
if (completionObjectif.isAtteint) {
return res.status(400).send({
Expand Down
5 changes: 2 additions & 3 deletions api/src/controllers/young/phase1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
YOUNG_STATUS_PHASE1,
REFERENT_DEPARTMENT_SUBROLE,
getDepartmentByZip,
getDepartmentForEligibility,
getDepartmentForInscriptionGoal,
FUNCTIONAL_ERRORS,
DepartmentServiceType,
} from "snu-lib";
Expand Down Expand Up @@ -88,8 +88,7 @@ router.post("/affectation", passport.authenticate("referent", { session: false,

// update youngs infos
if (young.status === "WAITING_LIST") {
// schoolDepartment pour les scolarisés et HZR sinon department pour les non scolarisés
const departement = getDepartmentForEligibility(young);
const departement = getDepartmentForInscriptionGoal(young);
const completionObjectif = await getCompletionObjectifs(departement, cohort.name);
if (completionObjectif.isAtteint) {
return res.status(400).send({
Expand Down
5 changes: 2 additions & 3 deletions api/src/referent/referentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ import {
canUpdateInscriptionGoals,
FUNCTIONAL_ERRORS,
YoungType,
getDepartmentForEligibility,
getDepartmentForInscriptionGoal,
isAdmin,
} from "snu-lib";
import { getFilteredSessions, getAllSessions } from "../utils/cohort";
Expand Down Expand Up @@ -557,8 +557,7 @@ router.put("/young/:id", passport.authenticate("referent", { session: false, fai
(!canUpdateInscriptionGoals(req.user) || !req.query.forceGoal)
) {
if (!cohort) return res.status(404).send({ ok: false, code: ERRORS.NOT_FOUND });
// schoolDepartment pour les scolarisés et HZR sinon department pour les non scolarisés
const departement = getDepartmentForEligibility(young);
const departement = getDepartmentForInscriptionGoal(young);
const completionObjectif = await getCompletionObjectifs(departement, cohort.name);
if (completionObjectif.isAtteint) {
return res.status(400).send({
Expand Down
49 changes: 48 additions & 1 deletion packages/lib/src/region-and-departments.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { YoungType } from "src";
import { getDepartmentForEligibility, getRegionForEligibility } from "./region-and-departments";
import { getDepartmentForEligibility, getRegionForEligibility, getDepartmentForInscriptionGoal } from "./region-and-departments";

describe("getDepartmentForEligibility", () => {
it("when young is schooled should return the school department", () => {
Expand Down Expand Up @@ -48,6 +48,53 @@ describe("getDepartmentForEligibility", () => {
});
});

describe("getDepartmentForInscriptionGoal", () => {
it("when young is schooled should return the department", () => {
const young: Partial<YoungType> = { _id: "123", schooled: "true", department: "department", schoolDepartment: "schoolDepartment" };
expect(getDepartmentForInscriptionGoal(young)).toBe("department");
});

it("when young is not schooled and doesn't have school department should returns the department", () => {
const young: Partial<YoungType> = { _id: "123", schooled: "false", department: "department" };
expect(getDepartmentForInscriptionGoal(young)).toBe("department");
});

it("when the both are available and the young is schooled should returns the department", () => {
const young: Partial<YoungType> = { _id: "123", schooled: "true", schoolDepartment: "schoolDepartment", department: "department" };
expect(getDepartmentForInscriptionGoal(young)).toBe("department");
});

it("when the both are available should return the school department", () => {
const young: Partial<YoungType> = { _id: "123", schoolDepartment: "SchoolDepartment", department: "Department" };
expect(getDepartmentForInscriptionGoal(young)).toBe("Department");
});

it("when school country is not FRANCE should department", () => {
const young: Partial<YoungType> = { _id: "123", department: "youngDepartment", schoolCountry: "ESPAGNE", schoolDepartment: "134" };
expect(getDepartmentForInscriptionGoal(young)).toBe("youngDepartment");
});

it("when school country is not FRANCE and schooled should department", () => {
const young: Partial<YoungType> = { _id: "123", department: "youngDepartment", schooled: "true", schoolCountry: "ESPAGNE", schoolDepartment: "134" };
expect(getDepartmentForInscriptionGoal(young)).toBe("youngDepartment");
});

it('when department not found should returns "Etranger"', () => {
const young: Partial<YoungType> = { _id: "123", schooled: "false" };
expect(getDepartmentForInscriptionGoal(young)).toBe("Etranger");
});

it("when department prepended with 0 should return good department", () => {
const young: Partial<YoungType> = { _id: "123", department: "044" };
expect(getDepartmentForInscriptionGoal(young)).toBe("Loire-Atlantique");
});

it("when department corse with return good department", () => {
const young: Partial<YoungType> = { _id: "123", department: "2A" };
expect(getDepartmentForInscriptionGoal(young)).toBe("Corse-du-Sud");
});
});

describe("getRegionForEligibility", () => {
it("should return schoolRegion if young is schooled", () => {
const young: Partial<YoungType> = {
Expand Down
14 changes: 13 additions & 1 deletion packages/lib/src/region-and-departments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function normalizeString(str) {
.replace(/-/g, " ")
.replace(/'/g, "")
.replace(/\s+/g, " ")
.trim();
.trim();
}

const departmentNameMapping = {};
Expand Down Expand Up @@ -399,6 +399,18 @@ const getDepartmentForEligibility = (
return dep;
};

export const getDepartmentForInscriptionGoal = (
young: Pick<YoungType, "schooled" | "schoolRegion" | "region" | "department" | "schoolDepartment" | "schoolCountry" | "zip"> & { _id?: YoungType["_id"] },
) => {
let dep = young?.department || getDepartmentByZip(young?.zip);
if (dep && (!isNaN(dep) || ["2A", "2B", "02A", "02B"].includes(dep))) {
if (dep.substring(0, 1) === "0" && dep.length === 3) dep = departmentLookUp[dep.substring(1)];
else dep = departmentLookUp[dep];
}
if (!dep) dep = "Etranger";
return dep;
};

const isFromMetropole = (young: YoungType) => {
const region = getRegionForEligibility(young);
return region2zone[region] === "A" || region2zone[region] === "B" || region2zone[region] === "C";
Expand Down
Loading