Files
calendar/lib/validations/schedule.ts
T
Alex van AndelandGitHub a506c7da33 Refactor + fix userIds filter (#179)
This fix means a behaviour change to GET calls. Instead of a JSON
payload, instead a filter param has been added to the URL itself. GET
payloads are very unexpected in API designs, even though supported.

* Todo write tests (with postman?)
* Turn isAdmin logic into common middleware

```bash
curl "http://localhost:3002/v1/schedules?apiKey=...&userId=2"
```

```bash
curl "http://localhost:3002/v1/schedules?apiKey=..." \
  -d '{"name":"Hello", "userId": 2}' \
  -H 'Content-Type: application/json'
```
2022-10-11 15:33:25 +01:00

40 lines
1.3 KiB
TypeScript

import { z } from "zod";
import dayjs from "@calcom/dayjs";
import { _ScheduleModel as Schedule, _AvailabilityModel as Availability } from "@calcom/prisma/zod";
const schemaScheduleBaseBodyParams = Schedule.omit({ id: true }).partial();
const schemaScheduleRequiredParams = z.object({
name: z.string().optional(),
userId: z.union([z.number(), z.array(z.number())]).optional(),
});
export const schemaScheduleBodyParams = schemaScheduleBaseBodyParams.merge(schemaScheduleRequiredParams);
export const schemaSingleScheduleBodyParams = schemaScheduleBaseBodyParams.merge(
z.object({ userId: z.number().optional() })
);
export const schemaCreateScheduleBodyParams = schemaScheduleBaseBodyParams.merge(
z.object({ userId: z.number().optional(), name: z.string() })
);
export const schemaSchedulePublic = z
.object({ id: z.number() })
.merge(Schedule)
.merge(
z.object({
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"),
}))
)
.optional(),
})
);