cal.pub0.org/packages/features/bookings/lib/handleBookingRequested.ts

70 lines
2.0 KiB
TypeScript
Raw Normal View History

import { sendAttendeeRequestEmail, sendOrganizerRequestEmail } from "@calcom/emails";
import { getWebhookPayloadForBooking } from "@calcom/features/bookings/lib/getWebhookPayloadForBooking";
import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
import sendPayload from "@calcom/features/webhooks/lib/sendPayload";
import logger from "@calcom/lib/logger";
import { WebhookTriggerEvents } from "@calcom/prisma/enums";
import type { CalendarEvent } from "@calcom/types/Calendar";
const log = logger.getChildLogger({ prefix: ["[handleConfirmation] book:user"] });
/**
* Supposed to do whatever is needed when a booking is requested.
*/
export async function handleBookingRequested(args: {
evt: CalendarEvent;
booking: {
eventType: {
currency: string;
description: string | null;
id: number;
length: number;
price: number;
requiresConfirmation: boolean;
title: string;
teamId?: number | null;
} | null;
eventTypeId: number | null;
userId: number | null;
id: number;
};
}) {
const { evt, booking } = args;
await sendOrganizerRequestEmail({ ...evt });
await sendAttendeeRequestEmail({ ...evt }, evt.attendees[0]);
try {
const subscribersBookingRequested = await getWebhooks({
userId: booking.userId,
eventTypeId: booking.eventTypeId,
triggerEvent: WebhookTriggerEvents.BOOKING_REQUESTED,
teamId: booking.eventType?.teamId,
});
const webhookPayload = getWebhookPayloadForBooking({
booking,
evt,
});
const promises = subscribersBookingRequested.map((sub) =>
sendPayload(
sub.secret,
WebhookTriggerEvents.BOOKING_REQUESTED,
new Date().toISOString(),
sub,
webhookPayload
).catch((e) => {
console.error(
`Error executing webhook for event: ${WebhookTriggerEvents.BOOKING_REQUESTED}, URL: ${sub.subscriberUrl}`,
e
);
})
);
await Promise.all(promises);
} catch (error) {
// Silently fail
log.error(error);
}
}