Skip to content

Commit

Permalink
fix: job start date (#839)
Browse files Browse the repository at this point in the history
* fix: validation sur job_start_date en création

* fix: ajout de commentaire

* fix: try fix tests

* fix: utilisation de dayjs.tz
  • Loading branch information
remy-auricoste authored and kevbarns committed Nov 21, 2023
1 parent 2218276 commit a98fa01
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
2 changes: 2 additions & 0 deletions shared/helpers/dayjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -4502,13 +4502,15 @@ 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",
},
},
"required": [
"establishment_siret",
"first_name",
"last_name",
"phone",
"email",
],
"type": "object",
Expand Down
53 changes: 53 additions & 0 deletions shared/models/job.model.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
})
10 changes: 7 additions & 3 deletions shared/models/job.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Expand Down
7 changes: 2 additions & 5 deletions shared/routes/v1Jobs.routes.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a98fa01

Please sign in to comment.