38 lines
1.5 KiB
TypeScript
38 lines
1.5 KiB
TypeScript
import getWebhooks from "@calcom/features/webhooks/lib/getWebhooks";
|
|
import type { GetSubscriberOptions } from "@calcom/features/webhooks/lib/getWebhooks";
|
|
import sendPayload from "@calcom/features/webhooks/lib/sendOrSchedulePayload";
|
|
import { isEventPayload, type WebhookPayloadType } from "@calcom/features/webhooks/lib/sendPayload";
|
|
import logger from "@calcom/lib/logger";
|
|
import { safeStringify } from "@calcom/lib/safeStringify";
|
|
import { withReporting } from "@calcom/lib/sentryWrapper";
|
|
|
|
async function _handleWebhookTrigger(args: {
|
|
subscriberOptions: GetSubscriberOptions;
|
|
eventTrigger: string;
|
|
webhookData: WebhookPayloadType;
|
|
isDryRun?: boolean;
|
|
}) {
|
|
try {
|
|
if (args.isDryRun) return;
|
|
const subscribers = await getWebhooks(args.subscriberOptions);
|
|
|
|
const promises = subscribers.map((sub) =>
|
|
sendPayload(sub.secret, args.eventTrigger, new Date().toISOString(), sub, args.webhookData).catch(
|
|
(e) => {
|
|
if (isEventPayload(args.webhookData)) {
|
|
logger.error(
|
|
`Error executing webhook for event: ${args.eventTrigger}, URL: ${sub.subscriberUrl}, booking id: ${args.webhookData.bookingId}, booking uid: ${args.webhookData.uid}`,
|
|
safeStringify(e)
|
|
);
|
|
}
|
|
}
|
|
)
|
|
);
|
|
await Promise.all(promises);
|
|
} catch (error) {
|
|
logger.error("Error while sending webhook", error);
|
|
}
|
|
}
|
|
|
|
export const handleWebhookTrigger = withReporting(_handleWebhookTrigger, "handleWebhookTrigger");
|