Files
cal.diy/pages/api/schedules/_post.ts
T
Alex van Andel 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

59 lines
1.7 KiB
TypeScript

import { HttpError } from "@/../../packages/lib/http-error";
import type { NextApiRequest } from "next";
import { DEFAULT_SCHEDULE, getAvailabilityFromSchedule } from "@calcom/lib/availability";
import { defaultResponder } from "@calcom/lib/server";
import { schemaCreateScheduleBodyParams, schemaSchedulePublic } from "@lib/validations/schedule";
/**
* @swagger
* /schedules:
* post:
* operationId: addSchedule
* summary: Creates a new schedule
* tags:
* - schedules
* responses:
* 201:
* description: OK, schedule created
* 400:
* description: Bad request. Schedule body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function postHandler({ body, userId, isAdmin, prisma }: NextApiRequest) {
const parsedBody = schemaCreateScheduleBodyParams.parse(body);
if (parsedBody.userId && !isAdmin) {
throw new HttpError({ statusCode: 403 });
}
const data = await prisma.schedule.create({
data: {
...parsedBody,
userId: parsedBody.userId || userId,
availability: {
createMany: {
data: getAvailabilityFromSchedule(DEFAULT_SCHEDULE).map((schedule) => ({
days: schedule.days,
startTime: schedule.startTime,
endTime: schedule.endTime,
})),
},
},
},
});
const createSchedule = schemaSchedulePublic.safeParse(data);
if (!createSchedule.success) {
throw new HttpError({ statusCode: 400, message: "Could not create new schedule" });
}
return {
schedule: createSchedule.data,
message: "Schedule created succesfully",
};
}
export default defaultResponder(postHandler);