2022-07-08 17:21:40 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
2023-02-16 21:01:40 +00:00
|
|
|
import type { z } from "zod";
|
2022-07-08 17:21:40 +00:00
|
|
|
|
2023-01-04 22:17:47 +00:00
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
2022-07-08 17:21:40 +00:00
|
|
|
import { defaultResponder } from "@calcom/lib/server";
|
|
|
|
|
2022-11-25 13:56:58 +00:00
|
|
|
import { schemaBookingEditBodyParams, schemaBookingReadPublic } from "~/lib/validations/booking";
|
|
|
|
import { schemaQueryIdParseInt } from "~/lib/validations/shared/queryIdTransformParseInt";
|
2022-07-08 17:21:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /bookings/{id}:
|
|
|
|
* patch:
|
|
|
|
* summary: Edit an existing booking
|
|
|
|
* operationId: editBookingById
|
|
|
|
* requestBody:
|
|
|
|
* description: Edit an existing booking related to one of your event-types
|
|
|
|
* required: true
|
|
|
|
* content:
|
|
|
|
* application/json:
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* properties:
|
|
|
|
* title:
|
|
|
|
* type: string
|
2023-02-07 15:41:08 +00:00
|
|
|
* description: 'Booking event title'
|
2022-07-08 17:21:40 +00:00
|
|
|
* startTime:
|
|
|
|
* type: string
|
2023-02-07 15:41:08 +00:00
|
|
|
* format: date-time
|
|
|
|
* description: 'Start time of the Event'
|
2022-07-08 17:21:40 +00:00
|
|
|
* endTime:
|
|
|
|
* type: string
|
2023-02-07 15:41:08 +00:00
|
|
|
* format: date-time
|
|
|
|
* description: 'End time of the Event'
|
|
|
|
* recurringEventId:
|
|
|
|
* type: integer
|
|
|
|
* description: 'Recurring event ID if the event is recurring'
|
|
|
|
* description:
|
|
|
|
* type: string
|
|
|
|
* description: 'Event description'
|
|
|
|
* status:
|
|
|
|
* type: string
|
|
|
|
* description: 'Acceptable values one of ["ACCEPTED", "PENDING", "CANCELLED", "REJECTED"]'
|
|
|
|
* location:
|
|
|
|
* type: string
|
|
|
|
* description: 'Meeting location'
|
|
|
|
* smsReminderNumber:
|
|
|
|
* type: number
|
|
|
|
* description: 'SMS reminder number'
|
|
|
|
* attendees:
|
|
|
|
* type: array
|
|
|
|
* description: 'List of attendees of the booking'
|
|
|
|
* items:
|
|
|
|
* type: object
|
|
|
|
* properties:
|
|
|
|
* name:
|
|
|
|
* type: string
|
|
|
|
* email:
|
|
|
|
* type: string
|
|
|
|
* format: email
|
|
|
|
* timeZone:
|
|
|
|
* type: string
|
|
|
|
* locale:
|
|
|
|
* type: string
|
|
|
|
*
|
2022-07-08 17:21:40 +00:00
|
|
|
* parameters:
|
2023-02-07 15:41:08 +00:00
|
|
|
* - in: query
|
|
|
|
* name: apiKey
|
|
|
|
* required: true
|
|
|
|
* schema:
|
|
|
|
* type: string
|
|
|
|
* description: Your API key
|
2022-07-08 17:21:40 +00:00
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: ID of the booking to edit
|
|
|
|
* tags:
|
|
|
|
* - bookings
|
|
|
|
* responses:
|
|
|
|
* 201:
|
2022-10-13 18:29:30 +00:00
|
|
|
* description: OK, booking edited successfully
|
2022-07-08 17:21:40 +00:00
|
|
|
* 400:
|
|
|
|
* description: Bad request. Booking body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
export async function patchHandler(req: NextApiRequest) {
|
|
|
|
const { prisma, query, body } = req;
|
|
|
|
const { id } = schemaQueryIdParseInt.parse(query);
|
|
|
|
const data = schemaBookingEditBodyParams.parse(body);
|
2023-01-04 22:17:47 +00:00
|
|
|
await checkPermissions(req, data);
|
2022-07-08 17:21:40 +00:00
|
|
|
const booking = await prisma.booking.update({ where: { id }, data });
|
|
|
|
return { booking: schemaBookingReadPublic.parse(booking) };
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:17:47 +00:00
|
|
|
async function checkPermissions(req: NextApiRequest, body: z.infer<typeof schemaBookingEditBodyParams>) {
|
|
|
|
const { isAdmin } = req;
|
|
|
|
if (body.userId && !isAdmin) {
|
|
|
|
// Organizer has to be a cal user and we can't allow a booking to be transfered to some other cal user's name
|
|
|
|
throw new HttpError({
|
|
|
|
statusCode: 403,
|
|
|
|
message: "Only admin can change the organizer of a booking",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 17:21:40 +00:00
|
|
|
export default defaultResponder(patchHandler);
|