2022-10-13 20:54:38 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
2023-02-16 21:01:40 +00:00
|
|
|
import type { z } from "zod";
|
2022-10-13 20:54:38 +00:00
|
|
|
|
2023-01-04 22:17:47 +00:00
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
2022-10-13 20:54:38 +00:00
|
|
|
import { defaultResponder } from "@calcom/lib/server";
|
|
|
|
|
2022-11-25 13:56:58 +00:00
|
|
|
import { schemaSchedulePublic, schemaSingleScheduleBodyParams } from "~/lib/validations/schedule";
|
|
|
|
import { schemaQueryIdParseInt } from "~/lib/validations/shared/queryIdTransformParseInt";
|
2022-10-13 20:54:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /schedules/{id}:
|
|
|
|
* patch:
|
|
|
|
* operationId: editScheduleById
|
|
|
|
* summary: Edit an existing schedule
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: ID of the schedule to edit
|
2023-03-10 17:03:41 +00:00
|
|
|
* - in: query
|
|
|
|
* name: apiKey
|
|
|
|
* schema:
|
|
|
|
* type: string
|
|
|
|
* required: true
|
|
|
|
* description: Your API Key
|
|
|
|
* requestBody:
|
|
|
|
* description: Create a new schedule
|
|
|
|
* required: true
|
|
|
|
* content:
|
|
|
|
* application/json:
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* properties:
|
|
|
|
* name:
|
|
|
|
* type: string
|
|
|
|
* description: Name of the schedule
|
|
|
|
* timezone:
|
|
|
|
* type: string
|
|
|
|
* description: The timezone for this schedule
|
2022-10-13 20:54:38 +00:00
|
|
|
* tags:
|
|
|
|
* - schedules
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, schedule edited successfully
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Schedule body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
export async function patchHandler(req: NextApiRequest) {
|
|
|
|
const { prisma, query } = req;
|
|
|
|
const { id } = schemaQueryIdParseInt.parse(query);
|
|
|
|
const data = schemaSingleScheduleBodyParams.parse(req.body);
|
2023-01-04 22:17:47 +00:00
|
|
|
await checkPermissions(req, data);
|
2022-10-13 20:54:38 +00:00
|
|
|
const result = await prisma.schedule.update({ where: { id }, data, include: { availability: true } });
|
|
|
|
return { schedule: schemaSchedulePublic.parse(result) };
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:17:47 +00:00
|
|
|
async function checkPermissions(req: NextApiRequest, body: z.infer<typeof schemaSingleScheduleBodyParams>) {
|
|
|
|
const { isAdmin } = req;
|
|
|
|
if (isAdmin) return;
|
|
|
|
if (body.userId) {
|
|
|
|
throw new HttpError({ statusCode: 403, message: "Non admin cannot change the owner of a schedule" });
|
|
|
|
}
|
|
|
|
//_auth-middleware takes care of verifying the ownership of schedule.
|
|
|
|
}
|
|
|
|
|
2022-10-13 20:54:38 +00:00
|
|
|
export default defaultResponder(patchHandler);
|