2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-31 20:14:37 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
|
|
import type { ScheduleResponse } from "@lib/types";
|
|
|
|
import { schemaScheduleBodyParams, schemaSchedulePublic, withValidSchedule } from "@lib/validations/schedule";
|
2022-03-30 12:17:55 +00:00
|
|
|
import {
|
|
|
|
schemaQueryIdParseInt,
|
|
|
|
withValidQueryIdTransformParseInt,
|
|
|
|
} from "@lib/validations/shared/queryIdTransformParseInt";
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-31 20:14:37 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /api/schedules/:id/edit:
|
|
|
|
* patch:
|
|
|
|
* summary: Edits an existing schedule
|
|
|
|
* tags:
|
|
|
|
* - schedules
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, schedule edited successfuly
|
|
|
|
* model: Schedule
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Schedule body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
export async function editSchedule(req: NextApiRequest, res: NextApiResponse<ScheduleResponse>) {
|
|
|
|
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
|
|
|
|
const safeBody = await schemaScheduleBodyParams.safeParse(req.body);
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-31 20:14:37 +00:00
|
|
|
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
|
|
|
|
const schedule = await prisma.schedule.update({
|
|
|
|
where: { id: safeQuery.data.id },
|
|
|
|
data: safeBody.data,
|
|
|
|
});
|
|
|
|
const data = schemaSchedulePublic.parse(schedule);
|
2022-03-28 22:27:14 +00:00
|
|
|
|
2022-03-31 20:14:37 +00:00
|
|
|
if (data) res.status(200).json({ data });
|
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
|
|
|
|
error,
|
|
|
|
});
|
2022-03-28 22:27:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 20:14:37 +00:00
|
|
|
export default withMiddleware("HTTP_PATCH")(
|
|
|
|
withValidQueryIdTransformParseInt(withValidSchedule(editSchedule))
|
|
|
|
);
|