diff --git a/admin/src/scenes/plan-transport/schema-repartition/components/BoxCentres.tsx b/admin/src/scenes/plan-transport/schema-repartition/components/BoxCentres.tsx
index 3256cc617e..e43b81e1ce 100644
--- a/admin/src/scenes/plan-transport/schema-repartition/components/BoxCentres.tsx
+++ b/admin/src/scenes/plan-transport/schema-repartition/components/BoxCentres.tsx
@@ -34,7 +34,7 @@ export default function BoxCentres({ summary, className, loading, isNational, is
{region.name}
{isDepartmental && (
- {region.departments.map((department) => `${department} (${getDepartmentNumber(department)})`).join(", ")}
+ {region.departments.map((department) => `${department} (${getDepartmentNumber(department.name)})`).join(", ")}
)}
diff --git a/packages/lib/src/region-and-departments.spec.ts b/packages/lib/src/region-and-departments.spec.ts
new file mode 100644
index 0000000000..6cf14b9254
--- /dev/null
+++ b/packages/lib/src/region-and-departments.spec.ts
@@ -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");
+ });
+});
diff --git a/packages/lib/src/region-and-departments.ts b/packages/lib/src/region-and-departments.ts
index f5d815f52c..9b35591ec9 100644
--- a/packages/lib/src/region-and-departments.ts
+++ b/packages/lib/src/region-and-departments.ts
@@ -1,3 +1,5 @@
+import { YoungType } from "./mongoSchema";
+
const departmentLookUp = {
"01": "Ain",
"02": "Aisne",
@@ -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;
@@ -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;
@@ -340,7 +342,7 @@ const region2zone = {
Etranger: "Etranger",
};
-const getRegionForEligibility = (young) => {
+const getRegionForEligibility = (young: Pick) => {
let region = young.schooled === "true" ? young.schoolRegion : young.region;
if (!region) {
let dep = young?.schoolDepartment || young?.department || getDepartmentByZip(young?.zip);
@@ -354,12 +356,15 @@ const getRegionForEligibility = (young) => {
return region;
};
-const getDepartmentForEligibility = (young) => {
+const getDepartmentForEligibility = (
+ young: Pick & { _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];
@@ -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";
};