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

This commit is contained in:
Syed Ali Shahbaz authored and GitHub committed 2023-10-16 17:09:22 -07:00
1 parent 401f64b986
commit 2756dff735
2 files changed
+33 -7

No files matched your search

@@ -30,6 +30,7 @@ import {
* required:
* - integration
* - externalId
* - credentialId
* properties:
* integration:
* type: string
@@ -37,12 +38,18 @@ import {
* externalId:
* type: string
* description: 'The external ID of the integration'
* credentialId:
* type: integer
* description: 'The credential ID it is associated with'
* eventTypeId:
* type: integer
* description: 'The ID of the eventType it is associated with'
* bookingId:
* type: integer
* description: 'The booking ID it is associated with'
* userId:
* type: integer
* description: 'The user it is associated with'
* tags:
* - destination-calendars
* responses:
@@ -55,17 +62,33 @@ import {
*/
async function postHandler(req: NextApiRequest) {
const { userId, isAdmin, prisma, body } = req;
const parsedBody = schemaDestinationCalendarCreateBodyParams.parse(body);
await checkPermissions(req, userId);
if (!parsedBody.eventTypeId) {
parsedBody.userId = userId;
}
const assignedUserId = isAdmin ? parsedBody.userId || userId : userId;
if (isAdmin) {
parsedBody.userId = parsedBody.userId || userId;
/* Check if credentialId data matches the ownership and integration passed in */
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 } });