Skip to content

Commit

Permalink
fix(lib): vérification des objectifs pour les jeunes résidant à l'étr…
Browse files Browse the repository at this point in the history
…anger
  • Loading branch information
achorein committed Oct 11, 2024
1 parent 5ea99d1 commit baf641c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function BoxCentres({ summary, className, loading, isNational, is
<li className="mt-[12px] text-[15px] font-bold leading-[18px] text-[#171725]">{region.name}</li>
{isDepartmental && (
<li className="text-[12px], mt-[2px] leading-[14px] text-[#1F2937]">
{region.departments.map((department) => `${department} (${getDepartmentNumber(department)})`).join(", ")}
{region.departments.map((department) => `${department} (${getDepartmentNumber(department.name)})`).join(", ")}
</li>
)}
</React.Fragment>
Expand Down
38 changes: 38 additions & 0 deletions packages/lib/src/region-and-departments.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getDepartmentForEligibility } from "./region-and-departments";

describe("getDepartmentForEligibility", () => {
it("returns the school department if the young person is schooled", () => {
const young = { _id: "123", schooled: "true", schoolDepartment: "TestDepartment" };
expect(getDepartmentForEligibility(young)).toBe("TestDepartment");
});

it("returns the department if the young person is not schooled", () => {
const young = { _id: "123", schooled: "false", department: "TestDepartment" };
expect(getDepartmentForEligibility(young)).toBe("TestDepartment");
});

it("returns the school department if both are available and the young person is schooled", () => {
const young = { _id: "123", schooled: "true", schoolDepartment: "SchoolDepartment", department: "Department" };
expect(getDepartmentForEligibility(young)).toBe("SchoolDepartment");
});

it("returns the school department if both are available", () => {
const young = { _id: "123", schoolDepartment: "SchoolDepartment", department: "Department" };
expect(getDepartmentForEligibility(young)).toBe("SchoolDepartment");
});

it("returns department when schoolCountry is not FRANCE", () => {
const young = { _id: "123", department: "youngDepartment", schoolCountry: "ESPAGNE", schoolDepartment: "134" };
expect(getDepartmentForEligibility(young)).toBe("youngDepartment");
});

it('returns "Etranger" if no department is found', () => {
const young = { _id: "123", schooled: "false" };
expect(getDepartmentForEligibility(young)).toBe("Etranger");
});

it("when department prepended with 0 should return good department", () => {
const young = { _id: "123", department: "044" };
expect(getDepartmentForEligibility(young)).toBe("Loire-Atlantique");
});
});
27 changes: 16 additions & 11 deletions packages/lib/src/region-and-departments.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { YoungType } from "./mongoSchema";

const departmentLookUp = {
"01": "Ain",
"02": "Aisne",
Expand Down Expand Up @@ -112,9 +114,9 @@ const departmentLookUp = {

const departmentList = Object.values(departmentLookUp);

const getDepartmentNumber = (d) => Object.keys(departmentLookUp).find((key) => departmentLookUp[key] === d);
const getDepartmentNumber = (depNum: string | number) => Object.keys(departmentLookUp).find((key) => departmentLookUp[key] === depNum);

const getDepartmentByZip = (zip) => {
const getDepartmentByZip = (zip?: string) => {
if (!zip) return;
if (zip.length !== 5) return;

Expand All @@ -127,7 +129,7 @@ const getDepartmentByZip = (zip) => {
return departmentLookUp[departmentCode];
};

const getRegionByZip = (zip) => {
const getRegionByZip = (zip?: string) => {
if (!zip) return;
if (zip.length !== 5) return;

Expand Down Expand Up @@ -340,7 +342,7 @@ const region2zone = {
Etranger: "Etranger",
};

const getRegionForEligibility = (young) => {
const getRegionForEligibility = (young: Pick<YoungType, "schooled" | "schoolRegion" | "region" | "department" | "schoolDepartment" | "zip">) => {
let region = young.schooled === "true" ? young.schoolRegion : young.region;
if (!region) {
let dep = young?.schoolDepartment || young?.department || getDepartmentByZip(young?.zip);
Expand All @@ -354,12 +356,15 @@ const getRegionForEligibility = (young) => {
return region;
};

const getDepartmentForEligibility = (young) => {
const getDepartmentForEligibility = (
young: Pick<YoungType, "schooled" | "schoolRegion" | "region" | "department" | "schoolDepartment" | "schoolCountry" | "zip"> & { _id?: YoungType["_id"] },
) => {
let dep;
if (young._id && young.schooled === "true") dep = young.schoolDepartment;
const schoolDepartment = !young?.schoolCountry || young.schoolCountry === "FRANCE" ? young?.schoolDepartment : null;
if (young._id && young.schooled === "true") dep = schoolDepartment;
if (young._id && young.schooled === "false") dep = young.department;

if (!dep) dep = young?.schoolDepartment || young?.department || getDepartmentByZip(young?.zip);
if (!dep) dep = schoolDepartment || 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];
Expand All @@ -368,23 +373,23 @@ const getDepartmentForEligibility = (young) => {
return dep;
};

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

const isFromDOMTOM = (young) => {
const isFromDOMTOM = (young: YoungType) => {
const region = getRegionForEligibility(young);
return region2zone[region] === "DOM";
};

const isFromFrenchPolynesia = (young) => {
const isFromFrenchPolynesia = (young: YoungType) => {
const region = getRegionForEligibility(young);
return region2zone[region] === "PF";
};

// attention avant l'utilisation : depuis juillet 2023 WF est aussi attaché à la zone + region NC sur la plateforme (avant c'était region WF et zone DOM)
const isFromNouvelleCaledonie = (young) => {
const isFromNouvelleCaledonie = (young: YoungType) => {
const region = getRegionForEligibility(young);
return region2zone[region] === "NC";
};
Expand Down

0 comments on commit baf641c

Please sign in to comment.