Update booking file with webhook payload

pull/9078/head
Syed Ali Shahbaz 2022-06-10 12:43:41 +05:30 committed by GitHub
parent 804de67868
commit e3822c50d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 57 additions and 21 deletions

View File

@ -2,9 +2,14 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma"; import prisma from "@calcom/prisma";
import { WebhookTriggerEvents } from "@prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware"; import { withMiddleware } from "@lib/helpers/withMiddleware";
import sendPayload from "@lib/utils/sendPayload";
import getWebhooks from "@lib/utils/webhookSubscriptions";
import { BookingResponse, BookingsResponse } from "@lib/types"; import { BookingResponse, BookingsResponse } from "@lib/types";
import { schemaBookingCreateBodyParams, schemaBookingReadPublic } from "@lib/validations/booking"; import { schemaBookingCreateBodyParams, schemaBookingReadPublic } from "@lib/validations/booking";
import { schemaEventTypeReadPublic } from "@lib/validations/event-type";
async function createOrlistAllBookings( async function createOrlistAllBookings(
{ method, body, userId }: NextApiRequest, { method, body, userId }: NextApiRequest,
@ -85,28 +90,59 @@ async function createOrlistAllBookings(
res.status(201).json({ booking, message: "Booking created successfully" }); res.status(201).json({ booking, message: "Booking created successfully" });
// Create Calendar Event for webhook payload // Create Calendar Event for webhook payload
const eventType = await prisma.eventType
.findUnique({ where: { id: booking?.eventTypeId } })
.then((data) => schemaEventTypeReadPublic.parse(data))
.then((event_type) => res.status(200).json({ event_type }))
.catch((error: Error) =>
res.status(404).json({
message: `EventType with id: ${booking?.eventTypeId} not found`,
error,
})
);
const evt = {
type: eventType.title,
title: booking.title,
description: "",
additionalNotes: "",
customInputs: {},
startTime: booking.startTime,
endTime: booking.endTime,
organizer: {
name: "",
email: "",
timezone: "",
language: {
locale: "en"
}
},
attendees: [],
location: "",
destinationCalendar: null,
hideCalendar: false,
uid: booking.uid,
metadata: {}
};
// Send Webhook call if hooked to BOOKING_CREATED & BOOKING_RESCHEDULED // Send Webhook call if hooked to BOOKING_CREATED
// const eventTrigger = WebhookTriggerEvents.BOOKING_CREATED; const triggerEvent = WebhookTriggerEvents.BOOKING_CREATED;
// const subscriberOptions = { const subscriberOptions = {
// userId: user.id, userId,
// eventTypeId, eventTypeId: eventType.id,
// triggerEvent: eventTrigger, triggerEvent,
// }; };
// const subscribers = await getWebhooks(subscriberOptions); const subscribers = await getWebhooks(subscriberOptions);
// const bookingId = booking?.id; const bookingId = booking?.id;
// const promises = subscribers.map((sub) => const promises = subscribers.map((sub) =>
// sendPayload(eventTrigger, new Date().toISOString(), sub, { sendPayload(eventTrigger, new Date().toISOString(), sub, {
// ...evt, ...evt,
// bookingId, bookingId,
// rescheduleUid, }).catch((e) => {
// metadata: reqBody.metadata, console.error(`Error executing webhook for event: ${eventTrigger}, URL: ${sub.subscriberUrl}`, e);
// }).catch((e) => { })
// console.error(`Error executing webhook for event: ${eventTrigger}, URL: ${sub.subscriberUrl}`, e); );
// }) await Promise.all(promises);
// );
// await Promise.all(promises);
} }
else else
(error: Error) => { (error: Error) => {