2022-10-11 14:25:57 +00:00
|
|
|
import type { Prisma } from "@prisma/client";
|
|
|
|
import type { NextApiRequest } from "next";
|
|
|
|
|
|
|
|
import { defaultResponder } from "@calcom/lib/server";
|
|
|
|
|
|
|
|
import {
|
|
|
|
schemaBookingEditBodyParams,
|
|
|
|
schemaBookingReferenceReadPublic,
|
2022-11-25 13:56:58 +00:00
|
|
|
} from "~/lib/validations/booking-reference";
|
|
|
|
import { schemaQueryIdParseInt } from "~/lib/validations/shared/queryIdTransformParseInt";
|
2022-10-11 14:25:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /booking-references/{id}:
|
|
|
|
* patch:
|
|
|
|
* operationId: editBookingReferenceById
|
|
|
|
* summary: Edit an existing booking reference
|
|
|
|
* requestBody:
|
|
|
|
* description: Edit an existing booking reference related to one of your bookings
|
|
|
|
* required: true
|
|
|
|
* content:
|
|
|
|
* application/json:
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* properties:
|
2023-02-13 12:52:11 +00:00
|
|
|
* type:
|
2022-10-11 14:25:57 +00:00
|
|
|
* type: string
|
2023-02-13 12:52:11 +00:00
|
|
|
* meetingId:
|
2022-10-11 14:25:57 +00:00
|
|
|
* type: string
|
2023-02-13 12:52:11 +00:00
|
|
|
* meetingPassword:
|
|
|
|
* type: string
|
|
|
|
* externalCalendarId:
|
|
|
|
* type: string
|
|
|
|
* deleted:
|
|
|
|
* type: boolean
|
|
|
|
* credentialId:
|
|
|
|
* type: integer
|
2022-10-11 14:25:57 +00:00
|
|
|
* parameters:
|
2023-02-13 12:52:11 +00:00
|
|
|
* - in: query
|
|
|
|
* name: apiKey
|
|
|
|
* required: true
|
|
|
|
* schema:
|
|
|
|
* type: string
|
|
|
|
* description: Your API key
|
2022-10-11 14:25:57 +00:00
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: ID of the booking reference to edit
|
|
|
|
* tags:
|
2023-02-13 12:52:11 +00:00
|
|
|
* - booking-references
|
2022-10-11 14:25:57 +00:00
|
|
|
* responses:
|
|
|
|
* 201:
|
2023-02-13 12:52:11 +00:00
|
|
|
* description: OK, BookingReference edited successfully
|
2022-10-11 14:25:57 +00:00
|
|
|
* 400:
|
|
|
|
* description: Bad request. BookingReference body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
export async function patchHandler(req: NextApiRequest) {
|
|
|
|
const { prisma, query, body, isAdmin, userId } = req;
|
|
|
|
const { id } = schemaQueryIdParseInt.parse(query);
|
|
|
|
const data = schemaBookingEditBodyParams.parse(body);
|
|
|
|
/* If user tries to update bookingId, we run extra checks */
|
|
|
|
if (data.bookingId) {
|
|
|
|
const args: Prisma.BookingFindFirstOrThrowArgs = isAdmin
|
|
|
|
? /* If admin, we only check that the booking exists */
|
|
|
|
{ where: { id: data.bookingId } }
|
|
|
|
: /* For non-admins we make sure the booking belongs to the user */
|
|
|
|
{ where: { id: data.bookingId, userId } };
|
|
|
|
await prisma.booking.findFirstOrThrow(args);
|
|
|
|
}
|
|
|
|
const booking_reference = await prisma.bookingReference.update({ where: { id }, data });
|
|
|
|
return { booking_reference: schemaBookingReferenceReadPublic.parse(booking_reference) };
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultResponder(patchHandler);
|