diff --git a/api/src/routes/loa.ts b/api/src/routes/loa.ts index 054977e..7bd9492 100644 --- a/api/src/routes/loa.ts +++ b/api/src/routes/loa.ts @@ -15,12 +15,6 @@ router.post("/", async (req: Request, res: Response) => { console.log(LOARequest); try { - // const result = await pool.query( - // `INSERT INTO leave_of_absences - // (member_id, filed_date, start_date, end_date, reason) - // VALUES (?, ?, ?, ?, ?)`, - // [member_id, filed_date, start_date, end_date, reason] - // ); await createNewLOA(LOARequest); res.sendStatus(201); } catch (error) { diff --git a/shared/schemas/loaSchema.ts b/shared/schemas/loaSchema.ts new file mode 100644 index 0000000..30c94c2 --- /dev/null +++ b/shared/schemas/loaSchema.ts @@ -0,0 +1,51 @@ +import * as z from "zod"; +import { LOAType } from "../types/loa"; + +export const loaTypeSchema = z.object({ + id: z.number(), + name: z.string(), + max_length_days: z.number(), +}); + +export const loaSchema = z.object({ + member_id: z.number(), + start_date: z.date(), + end_date: z.date(), + type: loaTypeSchema, + reason: z.string(), +}) + .superRefine((data, ctx) => { + const { start_date, end_date, type } = data; + + const today = new Date(); + today.setHours(0, 0, 0, 0); + + if (start_date < today) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + path: ["start_date"], + message: "Start date cannot be in the past.", + }); + } + + // 1. end > start + if (end_date <= start_date) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + path: ["end_date"], + message: "End date must be after start date.", + }); + } + + // 2. calculate max + const maxEnd = new Date(start_date); + maxEnd.setDate(maxEnd.getDate() + type.max_length_days); + + if (end_date > maxEnd) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + path: ["end_date"], + message: `This LOA type allows a maximum of ${type.max_length_days} days.`, + }); + } + }); diff --git a/ui/src/components/loa/loaForm.vue b/ui/src/components/loa/loaForm.vue index f234fb7..ec8cfd7 100644 --- a/ui/src/components/loa/loaForm.vue +++ b/ui/src/components/loa/loaForm.vue @@ -1,13 +1,15 @@ \ No newline at end of file