diff --git a/lib/webhooks/constants.ts b/lib/webhooks/constants.ts index ddd5a44507..cd3df9826f 100644 --- a/lib/webhooks/constants.ts +++ b/lib/webhooks/constants.ts @@ -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"]; diff --git a/pages/api/book/confirm.ts b/pages/api/book/confirm.ts index 6c72466dea..31720bb9da 100644 --- a/pages/api/book/confirm.ts +++ b/pages/api/book/confirm.ts @@ -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, diff --git a/prisma/migrations/20211211155527_add_booking_confirmed_to_webhook_trigger_event/migration.sql b/prisma/migrations/20211211155527_add_booking_confirmed_to_webhook_trigger_event/migration.sql new file mode 100644 index 0000000000..0f4c205765 --- /dev/null +++ b/prisma/migrations/20211211155527_add_booking_confirmed_to_webhook_trigger_event/migration.sql @@ -0,0 +1,2 @@ +-- AlterEnum +ALTER TYPE "WebhookTriggerEvents" ADD VALUE 'BOOKING_CONFIRMED'; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 1605caba0b..648ddd8272 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -313,6 +313,7 @@ enum WebhookTriggerEvents { BOOKING_CREATED BOOKING_RESCHEDULED BOOKING_CANCELLED + BOOKING_CONFIRMED } model Webhook {