fix: Adds mandatory credentiaId in Destination Calendar API endpoint POST (#11880)

This commit is contained in:
Syed Ali Shahbaz 2023-10-17 04:09:22 +04:00 committed by GitHub
parent 401f64b986
commit 2756dff735
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 7 deletions

View File

@ -3,6 +3,7 @@ import { z } from "zod";
import { _DestinationCalendarModel as DestinationCalendar } from "@calcom/prisma/zod"; import { _DestinationCalendarModel as DestinationCalendar } from "@calcom/prisma/zod";
export const schemaDestinationCalendarBaseBodyParams = DestinationCalendar.pick({ export const schemaDestinationCalendarBaseBodyParams = DestinationCalendar.pick({
credentialId: true,
integration: true, integration: true,
externalId: true, externalId: true,
eventTypeId: true, eventTypeId: true,
@ -14,6 +15,7 @@ const schemaDestinationCalendarCreateParams = z
.object({ .object({
integration: z.string(), integration: z.string(),
externalId: z.string(), externalId: z.string(),
credentialId: z.number(),
eventTypeId: z.number().optional(), eventTypeId: z.number().optional(),
bookingId: z.number().optional(), bookingId: z.number().optional(),
userId: z.number().optional(), userId: z.number().optional(),
@ -45,4 +47,5 @@ export const schemaDestinationCalendarReadPublic = DestinationCalendar.pick({
eventTypeId: true, eventTypeId: true,
bookingId: true, bookingId: true,
userId: true, userId: true,
credentialId: true,
}); });

View File

@ -30,6 +30,7 @@ import {
* required: * required:
* - integration * - integration
* - externalId * - externalId
* - credentialId
* properties: * properties:
* integration: * integration:
* type: string * type: string
@ -37,12 +38,18 @@ import {
* externalId: * externalId:
* type: string * type: string
* description: 'The external ID of the integration' * description: 'The external ID of the integration'
* credentialId:
* type: integer
* description: 'The credential ID it is associated with'
* eventTypeId: * eventTypeId:
* type: integer * type: integer
* description: 'The ID of the eventType it is associated with' * description: 'The ID of the eventType it is associated with'
* bookingId: * bookingId:
* type: integer * type: integer
* description: 'The booking ID it is associated with' * description: 'The booking ID it is associated with'
* userId:
* type: integer
* description: 'The user it is associated with'
* tags: * tags:
* - destination-calendars * - destination-calendars
* responses: * responses:
@ -55,17 +62,33 @@ import {
*/ */
async function postHandler(req: NextApiRequest) { async function postHandler(req: NextApiRequest) {
const { userId, isAdmin, prisma, body } = req; const { userId, isAdmin, prisma, body } = req;
const parsedBody = schemaDestinationCalendarCreateBodyParams.parse(body); const parsedBody = schemaDestinationCalendarCreateBodyParams.parse(body);
await checkPermissions(req, userId); await checkPermissions(req, userId);
if (!parsedBody.eventTypeId) { const assignedUserId = isAdmin ? parsedBody.userId || userId : userId;
parsedBody.userId = userId;
}
if (isAdmin) { /* Check if credentialId data matches the ownership and integration passed in */
parsedBody.userId = parsedBody.userId || userId; const credential = await prisma.credential.findFirst({
where: { type: parsedBody.integration, userId: assignedUserId },
select: { id: true, type: true, userId: true },
});
if (!credential)
throw new HttpError({
statusCode: 400,
message: "Bad request, credential id invalid",
});
if (parsedBody.eventTypeId) {
const eventType = await prisma.eventType.findFirst({
where: { id: parsedBody.eventTypeId, userId: parsedBody.userId },
});
if (!eventType)
throw new HttpError({
statusCode: 400,
message: "Bad request, eventTypeId invalid",
});
parsedBody.userId = undefined;
} }
const destination_calendar = await prisma.destinationCalendar.create({ data: { ...parsedBody } }); const destination_calendar = await prisma.destinationCalendar.create({ data: { ...parsedBody } });