Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
achorein committed Sep 18, 2024
1 parent 42965e7 commit 52e6336
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import { User } from "@/types";
import FrenchMap from "@/assets/icons/FrenchMap";
import ChevronRight from "@/assets/icons/ChevronRight";

export default function BoxCentres({ summary, className, loading, isNational, isDepartmental, user }: BoxProps & { isNational?: boolean; isDepartmental?: boolean; user: User }) {
interface BoxCentresProps extends BoxProps {
isNational?: boolean;
isDepartmental?: boolean;
user: User;
}

export default function BoxCentres({ summary, className, loading, isNational, isDepartmental, user }: BoxCentresProps) {
return (
<Box className={`overflow-hidden ${className} w-1/3`}>
<FrenchMap className="absolute right-[-40px] top-[30px] z-[0]" />
Expand All @@ -34,7 +40,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
208 changes: 182 additions & 26 deletions api/src/__tests__/cohort-session.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import request from "supertest";
import { fakerFR as faker } from "@faker-js/faker";
import { addDays, addYears } from "date-fns";
import { Types } from "mongoose";
const { ObjectId } = Types;

import { COHORT_TYPE, ERRORS, ROLES } from "snu-lib";
import { COHORT_TYPE, ERRORS, GRADES, ROLES, YOUNG_STATUS } from "snu-lib";

import { CohortModel } from "../models";
import { dbConnect, dbClose } from "./helpers/db";
Expand Down Expand Up @@ -54,9 +55,40 @@ describe("Cohort Session Controller", () => {
zip: "",
});
expect(response.status).toBe(200);
expect(response.body.ok).toBe(true);
});

it("should return 200 if young data is valid", async () => {
const young = {
department: "Loire-Atlantique",
region: "Pays de la Loire",
schoolRegion: "",
birthdateAt: faker.date.past({ years: 3, refDate: addYears(new Date(), -15) }),
grade: GRADES["2ndeGT"],
status: YOUNG_STATUS.REFUSED,
zip: faker.location.zipCode(),
};
await createCohortHelper(
getNewCohortFixture({
inscriptionEndDate: faker.date.future(),
eligibility: {
zones: [young.department!],
schoolLevels: [young.grade || ""],
bornAfter: addYears(new Date(), -18),
bornBefore: addYears(new Date(), -15),
},
}),
);
const response = await request(getAppHelper({ role: ROLES.ADMIN }))
.post("/cohort-session/eligibility/2023/")
.send(young);
expect(response.status).toBe(200);
expect(response.body.ok).toBe(true);
expect(Array.isArray(response.body.data)).toBe(true);
expect(response.body.data.length).toBe(1);
});

it("should return filtered sessions if young is valid and admin cle", async () => {
it("admin cle, should return filtered sessions if young is valid", async () => {
const young = await createYoungHelper(
getNewYoungFixture({
schooled: "false", // not HTZ
Expand All @@ -72,8 +104,8 @@ describe("Cohort Session Controller", () => {
eligibility: {
zones: [young.department!],
schoolLevels: [young.grade || ""],
bornAfter: new Date("2000-01-01"),
bornBefore: new Date("2010-01-01"),
bornAfter: addYears(new Date(), -18),
bornBefore: addYears(new Date(), -15),
},
}),
);
Expand All @@ -85,8 +117,8 @@ describe("Cohort Session Controller", () => {
eligibility: {
zones: [young.department!],
schoolLevels: [young.grade || ""],
bornAfter: new Date("2000-01-01"),
bornBefore: new Date("2010-01-01"),
bornAfter: addYears(new Date(), -18),
bornBefore: addYears(new Date(), -15),
},
}),
);
Expand All @@ -98,7 +130,7 @@ describe("Cohort Session Controller", () => {
expect(response.body.data.length).toBe(1);
});

it("should bypass sessions filter if young is valid and ref dep with getAllSessions is true", async () => {
it("referent dep, should bypass sessions filter with getAllSessions is true if young is valid", async () => {
const young = await createYoungHelper(
getNewYoungFixture({
schooled: "false", // not HTZ
Expand All @@ -114,8 +146,8 @@ describe("Cohort Session Controller", () => {
eligibility: {
zones: [young.department!],
schoolLevels: [young.grade || ""],
bornAfter: new Date("2000-01-01"),
bornBefore: new Date("2010-01-01"),
bornAfter: addYears(new Date(), -18),
bornBefore: addYears(new Date(), -15),
},
}),
);
Expand All @@ -127,8 +159,8 @@ describe("Cohort Session Controller", () => {
eligibility: {
zones: [young.department!],
schoolLevels: [young.grade || ""],
bornAfter: new Date("2000-01-01"),
bornBefore: new Date("2010-01-01"),
bornAfter: addYears(new Date(), -18),
bornBefore: addYears(new Date(), -15),
},
}),
);
Expand All @@ -140,7 +172,8 @@ describe("Cohort Session Controller", () => {
expect(response.body.data.length).toBe(2);
});

it("should return sessions if young is valid and cohort available", async () => {
it("admin, should return sessions if young is valid and cohort available", async () => {
// résidant+scolarisé dans dép X : si le dép X a un séjour, vérifier que le jeune peut candidater
const young = await createYoungHelper(
getNewYoungFixture({
schooled: "false", // not HTZ
Expand All @@ -156,8 +189,8 @@ describe("Cohort Session Controller", () => {
eligibility: {
zones: [young.department!],
schoolLevels: [young.grade || ""],
bornAfter: new Date("2000-01-01"),
bornBefore: new Date("2010-01-01"),
bornAfter: addYears(new Date(), -18),
bornBefore: addYears(new Date(), -15),
},
}),
);
Expand All @@ -168,7 +201,37 @@ describe("Cohort Session Controller", () => {
expect(response.body.data.length).toBe(1);
});

it("should return sessions if young is valid and cohort available (HTZ)", async () => {
it("should return no sessions if young is valid and cohort not available for his department", async () => {
// résidant+scolarisé dans dép X : si le dép X n’a PAS de séjour, vérifier que le jeune peut PAS candidater
const young = await createYoungHelper(
getNewYoungFixture({
schooled: "false", // not HTZ
region: "Pays de la Loire",
department: "Loire-Atlantique",
schoolDepartment: "Loire-Atlantique",
}),
);
await createCohortHelper(
getNewCohortFixture({
name: young.cohort,
inscriptionEndDate: faker.date.future(),
eligibility: {
zones: ["Morbihan"],
schoolLevels: [young.grade || ""],
bornAfter: addYears(new Date(), -18),
bornBefore: addYears(new Date(), -15),
},
}),
);
const response = await request(getAppHelper({ role: ROLES.ADMIN })).post(`/cohort-session/eligibility/2023/${young._id}`);
expect(response.status).toBe(200);
expect(response.body.ok).toBe(true);
expect(Array.isArray(response.body.data)).toBe(true);
expect(response.body.data.length).toBe(0);
});

it("admin, should return sessions if young is valid and cohort available (HTZ)", async () => {
// résidant dans dép Y + scolarisé dans dép X : si le dép X a un séjour, vérifier que le jeune peut candidater
const young = await createYoungHelper(
getNewYoungFixture({
schooled: "true", // HTZ
Expand All @@ -184,8 +247,8 @@ describe("Cohort Session Controller", () => {
eligibility: {
zones: [young.schoolDepartment!],
schoolLevels: [young.grade || ""],
bornAfter: new Date("2000-01-01"),
bornBefore: new Date("2010-01-01"),
bornAfter: addYears(new Date(), -18),
bornBefore: addYears(new Date(), -15),
},
}),
);
Expand All @@ -196,17 +259,25 @@ describe("Cohort Session Controller", () => {
expect(response.body.data.length).toBe(1);
});

it("should return no sessions if young is valid and cohort not available (wrong dep)", async () => {
const young = await createYoungHelper(getNewYoungFixture({ region: "Pays de la Loire", department: "Loire-Atlantique", schoolDepartment: "Loire-Atlantique" }));
it("admin, should not return sessions if young is valid and cohort not available in his department (HTZ)", async () => {
// résidant dans dép Y + scolarisé dans dép X : si le dép X n’a PAS de séjour, alors vérifier que le jeune ne peut PAS candidaté
const young = await createYoungHelper(
getNewYoungFixture({
schooled: "true", // HTZ
region: "Pays de la Loire",
department: "Morbihan",
schoolDepartment: "Loire-Atlantique",
}),
);
await createCohortHelper(
getNewCohortFixture({
name: young.cohort,
inscriptionEndDate: faker.date.future(),
eligibility: {
zones: ["A"],
zones: [young.department!],
schoolLevels: [young.grade || ""],
bornAfter: new Date("2000-01-01"),
bornBefore: new Date("2010-01-01"),
bornAfter: addYears(new Date(), -18),
bornBefore: addYears(new Date(), -15),
},
}),
);
Expand All @@ -217,16 +288,44 @@ describe("Cohort Session Controller", () => {
expect(response.body.data.length).toBe(0);
});

it("should return no sessions if young is valid and cohort not available (inscriptionEnded)", async () => {
it("admin, should return no sessions if young is valid and cohort not available (wrong dep)", async () => {
// Cas d’une région qui ne participe pas en totalité à un séjour (seul un département participe)
const young = await createYoungHelper(
getNewYoungFixture({
region: "Pays de la Loire",
department: "Loire-Atlantique",
schoolDepartment: "Loire-Atlantique",
}),
);
await createCohortHelper(
getNewCohortFixture({
name: young.cohort,
inscriptionEndDate: faker.date.future(),
eligibility: {
zones: ["Morbihan"],
schoolLevels: [young.grade || ""],
bornAfter: addYears(new Date(), -18),
bornBefore: addYears(new Date(), -15),
},
}),
);
const response = await request(getAppHelper({ role: ROLES.ADMIN })).post(`/cohort-session/eligibility/2023/${young._id}`);
expect(response.status).toBe(200);
expect(response.body.ok).toBe(true);
expect(Array.isArray(response.body.data)).toBe(true);
expect(response.body.data.length).toBe(0);
});

it("admin, should return no sessions if young is valid and cohort not available (inscriptionEnded)", async () => {
const young = await createYoungHelper(getNewYoungFixture({ region: "Pays de la Loire", department: "Loire-Atlantique", schoolDepartment: "Loire-Atlantique" }));
await createCohortHelper(
getNewCohortFixture({
name: young.cohort,
eligibility: {
zones: [young.department!],
schoolLevels: [young.grade || ""],
bornAfter: new Date("2000-01-01"),
bornBefore: new Date("2010-01-01"),
bornAfter: addYears(new Date(), -18),
bornBefore: addYears(new Date(), -15),
},
}),
);
Expand All @@ -236,10 +335,67 @@ describe("Cohort Session Controller", () => {
expect(Array.isArray(response.body.data)).toBe(true);
expect(response.body.data.length).toBe(0);
});

it("admin, should return no sessions if young has invalid birthdate and cohort is available", async () => {
const cohort = await createCohortHelper(
getNewCohortFixture({
inscriptionEndDate: faker.date.future(),
dateStart: addDays(addYears(new Date(), -18), -7),
dateEnd: addDays(addYears(new Date(), -18), 0),
eligibility: {
zones: ["Loire-Atlantique"],
schoolLevels: [GRADES["2ndeGT"]],
bornAfter: addYears(new Date(), -18),
bornBefore: addYears(new Date(), -15),
},
}),
);
// 15 ans à J-1
let young = await createYoungHelper(
getNewYoungFixture({
cohort: cohort.name,
region: "Pays de la Loire",
department: cohort.eligibility.zones[0],
schoolDepartment: cohort.eligibility.zones[0],
birthdateAt: addDays(addYears(new Date(), -15), 1),
}),
);
let response = await request(getAppHelper({ role: ROLES.ADMIN })).post(`/cohort-session/eligibility/2023/${young._id}`);
expect(response.status).toBe(200);
expect(response.body.data.length).toBe(0);

// 18 ans au cours du jour du séjour
young = await createYoungHelper(
getNewYoungFixture({
cohort: cohort.name,
region: "Pays de la Loire",
department: cohort.eligibility.zones[0],
schoolDepartment: cohort.eligibility.zones[0],
birthdateAt: addDays(addYears(new Date(), -18), 3),
}),
);
response = await request(getAppHelper({ role: ROLES.ADMIN })).post(`/cohort-session/eligibility/2023/${young._id}`);
expect(response.status).toBe(200);
expect(response.body.data.length).toBe(0);

// 18 au dernier jour du séjour
young = await createYoungHelper(
getNewYoungFixture({
cohort: cohort.name,
region: "Pays de la Loire",
department: cohort.eligibility.zones[0],
schoolDepartment: cohort.eligibility.zones[0],
birthdateAt: addDays(addYears(new Date(), -18), 0),
}),
);
response = await request(getAppHelper({ role: ROLES.ADMIN })).post(`/cohort-session/eligibility/2023/${young._id}`);
expect(response.status).toBe(200);
expect(response.body.data.length).toBe(0);
});
});

describe("GET /api/cohort-session/isReInscriptionOpen", () => {
it("should return 200 OK with data", async () => {
it("admin, should return 200 OK with data", async () => {
await createCohortHelper(
getNewCohortFixture({
type: COHORT_TYPE.VOLONTAIRE,
Expand Down
3 changes: 2 additions & 1 deletion api/src/__tests__/fixtures/young.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fakerFR as faker } from "@faker-js/faker";
import { addYears } from "date-fns";
import { departmentList, regionList, YoungType } from "snu-lib";

function randomDepartment() {
Expand All @@ -22,7 +23,7 @@ export default function getNewYoungFixture(fields: Partial<YoungType> = {}): Par
phone: faker.phone.number(),
phoneZone: "FRANCE",
gender: faker.person.gender(),
birthdateAt: faker.date.past({ years: 1, refDate: "01/01/2007" }),
birthdateAt: faker.date.past({ years: 3, refDate: addYears(new Date(), -15) }),
cohort: "Juillet 2023",
acceptCGU: "true",
phase: "CONTINUE",
Expand Down
Loading

0 comments on commit 52e6336

Please sign in to comment.