cal.pub0.org/apps/api/lib/validations/schedule.ts

35 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-04-01 23:55:41 +00:00
import { z } from "zod";
import dayjs from "@calcom/dayjs";
2022-10-06 18:38:17 +00:00
import { _ScheduleModel as Schedule, _AvailabilityModel as Availability } from "@calcom/prisma/zod";
2022-10-13 20:54:38 +00:00
import { timeZone } from "./shared/timeZone";
2022-04-01 23:55:41 +00:00
2022-10-13 20:54:38 +00:00
const schemaScheduleBaseBodyParams = Schedule.omit({ id: true, timeZone: true }).partial();
2022-10-06 19:06:07 +00:00
export const schemaSingleScheduleBodyParams = schemaScheduleBaseBodyParams.merge(
2022-10-13 20:54:38 +00:00
z.object({ userId: z.number().optional(), timeZone: timeZone.optional() })
2022-10-06 19:06:07 +00:00
);
export const schemaCreateScheduleBodyParams = schemaScheduleBaseBodyParams.merge(
2022-10-13 20:54:38 +00:00
z.object({ userId: z.number().optional(), name: z.string(), timeZone })
2022-10-06 19:06:07 +00:00
);
export const schemaSchedulePublic = z
.object({ id: z.number() })
.merge(Schedule)
.merge(
z.object({
2022-10-06 18:38:17 +00:00
availability: z
.array(Availability.pick({ id: true, eventTypeId: true, days: true, startTime: true, endTime: true }))
.transform((v) =>
v.map((item) => ({
...item,
startTime: dayjs.utc(item.startTime).format("HH:mm:ss"),
endTime: dayjs.utc(item.endTime).format("HH:mm:ss"),
}))
)
2022-10-06 18:38:17 +00:00
.optional(),
})
);