feat: add confirmed booking webhook event

pull/1296/head
Bill Gale 2021-12-11 16:12:32 +00:00
parent c1d90eb438
commit ff568f4aa4
No known key found for this signature in database
GPG Key ID: 09647D620D127741
4 changed files with 25 additions and 1 deletions

View File

@ -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"];

View File

@ -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,

View File

@ -0,0 +1,2 @@
-- AlterEnum
ALTER TYPE "WebhookTriggerEvents" ADD VALUE 'BOOKING_CONFIRMED';

View File

@ -313,6 +313,7 @@ enum WebhookTriggerEvents {
BOOKING_CREATED
BOOKING_RESCHEDULED
BOOKING_CANCELLED
BOOKING_CONFIRMED
}
model Webhook {