diff --git a/shared/helpers/dayjs.ts b/shared/helpers/dayjs.ts index 529f1ec953..484708bc60 100644 --- a/shared/helpers/dayjs.ts +++ b/shared/helpers/dayjs.ts @@ -24,6 +24,8 @@ dayjs.extend(advancedFormat) dayjs.extend(isYesterday) dayjs.extend(localizedFormat) +dayjs.tz.setDefault("Europe/Paris") + export type TDayjs = dayjs.Dayjs export default dayjs diff --git a/shared/helpers/openapi/__snapshots__/generateOpenapi.test.ts.snap b/shared/helpers/openapi/__snapshots__/generateOpenapi.test.ts.snap index fa0c3f2342..2f5c150a32 100644 --- a/shared/helpers/openapi/__snapshots__/generateOpenapi.test.ts.snap +++ b/shared/helpers/openapi/__snapshots__/generateOpenapi.test.ts.snap @@ -4502,6 +4502,7 @@ L'email est envoyé depuis l'adresse générique \\"Ne pas répondre\\" de La bo "type": "string", }, "phone": { + "pattern": "^0[1-9]\\\\d{8}$", "type": "string", }, }, @@ -4509,6 +4510,7 @@ L'email est envoyé depuis l'adresse générique \\"Ne pas répondre\\" de La bo "establishment_siret", "first_name", "last_name", + "phone", "email", ], "type": "object", diff --git a/shared/models/job.model.test.ts b/shared/models/job.model.test.ts new file mode 100644 index 0000000000..3aae4c84ab --- /dev/null +++ b/shared/models/job.model.test.ts @@ -0,0 +1,53 @@ +import dayjs from "dayjs" +import { describe, expect, it } from "vitest" + +import { ZJobStartDateCreate } from "." + +describe("job.model", () => { + describe("job_start_date field", () => { + describe("during the day", () => { + const zJobStartDate = ZJobStartDateCreate(dayjs("2023-11-21T15:22:21.515+0100")) + it("should pass", () => { + expect(zJobStartDate.safeParse("2023-11-21").success).toBe(true) + expect(zJobStartDate.safeParse("2023-11-22").success).toBe(true) + }) + it("should fail", () => { + expect(zJobStartDate.safeParse("2023-11-19").success).toBe(false) + expect(zJobStartDate.safeParse("2023-11-20").success).toBe(false) + }) + }) + describe("at midnight in Paris", () => { + const zJobStartDate = ZJobStartDateCreate(dayjs("2023-11-21T00:00:00.000+0100")) + it("should pass", () => { + expect(zJobStartDate.safeParse("2023-11-21").success).toBe(true) + expect(zJobStartDate.safeParse("2023-11-22").success).toBe(true) + }) + it("should fail", () => { + expect(zJobStartDate.safeParse("2023-11-19").success).toBe(false) + expect(zJobStartDate.safeParse("2023-11-20").success).toBe(false) + }) + }) + describe("at 1am Paris", () => { + const zJobStartDate = ZJobStartDateCreate(dayjs("2023-11-21T01:00:00.000+0100")) + it("should pass", () => { + expect(zJobStartDate.safeParse("2023-11-21").success).toBe(true) + expect(zJobStartDate.safeParse("2023-11-22").success).toBe(true) + }) + it("should fail", () => { + expect(zJobStartDate.safeParse("2023-11-19").success).toBe(false) + expect(zJobStartDate.safeParse("2023-11-20").success).toBe(false) + }) + }) + describe("at 23pm Paris", () => { + const zJobStartDate = ZJobStartDateCreate(dayjs("2023-11-21T23:00:00.000+0100")) + it("should pass", () => { + expect(zJobStartDate.safeParse("2023-11-21").success).toBe(true) + expect(zJobStartDate.safeParse("2023-11-22").success).toBe(true) + }) + it("should fail", () => { + expect(zJobStartDate.safeParse("2023-11-19").success).toBe(false) + expect(zJobStartDate.safeParse("2023-11-20").success).toBe(false) + }) + }) + }) +}) diff --git a/shared/models/job.model.ts b/shared/models/job.model.ts index 593cdd1f3e..55115e28e9 100644 --- a/shared/models/job.model.ts +++ b/shared/models/job.model.ts @@ -75,6 +75,12 @@ export const ZJob = ZJobFields.extend({ .strict() .openapi("Job") +export const ZJobStartDateCreate = (now: dayjs.Dayjs = dayjs.tz()) => + // Le changement de jour se fait à minuit (heure de Paris) + ZJobFields.shape.job_start_date.refine((date) => dayjs.tz(date).isSameOrAfter(dayjs.tz(now).startOf("days")), { + message: "job_start_date must be greater or equal to today's date", + }) + export const ZJobWrite = ZJobFields.pick({ rome_appellation_label: true, rome_code: true, @@ -89,9 +95,7 @@ export const ZJobWrite = ZJobFields.pick({ delegations: true, }) .extend({ - job_start_date: ZJobFields.shape.job_start_date.refine((date) => dayjs(date).isSameOrAfter(dayjs().utc().startOf("days")), { - message: "job_start_date must be greater or equal to today's date", - }), + job_start_date: ZJobStartDateCreate(), }) .strict() .openapi("JobWrite") diff --git a/shared/routes/v1Jobs.routes.ts b/shared/routes/v1Jobs.routes.ts index a895968505..896b17e628 100644 --- a/shared/routes/v1Jobs.routes.ts +++ b/shared/routes/v1Jobs.routes.ts @@ -1,7 +1,6 @@ -import dayjs from "../helpers/dayjs" import { extensions } from "../helpers/zodHelpers/zodPrimitives" import { z } from "../helpers/zodWithOpenApi" -import { ZJob, ZJobFields } from "../models" +import { ZJob, ZJobFields, ZJobStartDateCreate } from "../models" import { zObjectId } from "../models/common" import { ZApiError, ZLbacError, ZLbarError } from "../models/lbacError.model" import { ZLbaItemLbaCompany, ZLbaItemLbaJob, ZLbaItemPeJob } from "../models/lbaItem.model" @@ -381,9 +380,7 @@ export const zV1JobsRoutes = { custom_geo_coordinates: true, }) .extend({ - job_start_date: ZJobFields.shape.job_start_date.refine((date) => dayjs(date).isSameOrAfter(dayjs().utc()), { - message: "job_start_date must be greater or equal to today's date", - }), + job_start_date: ZJobStartDateCreate(), appellation_code: z.string().regex(/^[0-9]+$/, "appelation code must contains only numbers"), }) .strict()