feat: add confirmed booking webhook event
parent
c1d90eb438
commit
ff568f4aa4
|
@ -5,4 +5,5 @@ export const WEBHOOK_TRIGGER_EVENTS = [
|
|||
WebhookTriggerEvents.BOOKING_CANCELLED,
|
||||
WebhookTriggerEvents.BOOKING_CREATED,
|
||||
WebhookTriggerEvents.BOOKING_RESCHEDULED,
|
||||
] as ["BOOKING_CANCELLED", "BOOKING_CREATED", "BOOKING_RESCHEDULED"];
|
||||
WebhookTriggerEvents.BOOKING_CONFIRMED,
|
||||
] as ["BOOKING_CANCELLED", "BOOKING_CREATED", "BOOKING_RESCHEDULED", "BOOKING_CONFIRMED"];
|
||||
|
|
|
@ -11,6 +11,8 @@ import EventManager from "@lib/events/EventManager";
|
|||
import logger from "@lib/logger";
|
||||
import prisma from "@lib/prisma";
|
||||
import { BookingConfirmBody } from "@lib/types/booking";
|
||||
import sendPayload from "@lib/webhooks/sendPayload";
|
||||
import getSubscribers from "@lib/webhooks/subscriptions";
|
||||
|
||||
import { getTranslation } from "@server/lib/i18n";
|
||||
|
||||
|
@ -84,6 +86,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||
id: bookingId,
|
||||
},
|
||||
select: {
|
||||
userId: true,
|
||||
title: true,
|
||||
description: true,
|
||||
startTime: true,
|
||||
|
@ -153,6 +156,23 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||
await sendScheduledEmails({ ...evt, additionInformation: metadata });
|
||||
}
|
||||
|
||||
// Hook up the webhook logic here
|
||||
const eventTrigger = "BOOKING_CONFIRMED";
|
||||
// Send Webhook call if hooked to BOOKING.CONFIRMED
|
||||
const subscribers = await getSubscribers(booking.userId, eventTrigger);
|
||||
const promises = subscribers.map((sub) =>
|
||||
sendPayload(
|
||||
eventTrigger,
|
||||
new Date().toISOString(),
|
||||
sub.subscriberUrl,
|
||||
evt,
|
||||
sub.payloadTemplate
|
||||
).catch((e) => {
|
||||
console.error(`Error executing webhook for event: ${eventTrigger}, URL: ${sub.subscriberUrl}`, e);
|
||||
})
|
||||
);
|
||||
await Promise.all(promises);
|
||||
|
||||
await prisma.booking.update({
|
||||
where: {
|
||||
id: bookingId,
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
-- AlterEnum
|
||||
ALTER TYPE "WebhookTriggerEvents" ADD VALUE 'BOOKING_CONFIRMED';
|
|
@ -313,6 +313,7 @@ enum WebhookTriggerEvents {
|
|||
BOOKING_CREATED
|
||||
BOOKING_RESCHEDULED
|
||||
BOOKING_CANCELLED
|
||||
BOOKING_CONFIRMED
|
||||
}
|
||||
|
||||
model Webhook {
|
||||
|
|
Loading…
Reference in New Issue