Files
sign/packages/lib/universal/trigger-webhook.ts

31 lines
1000 B
TypeScript
Raw Normal View History

2024-02-16 11:04:11 +02:00
import type { Document, WebhookTriggerEvents } from '@documenso/prisma/client';
2024-02-24 11:18:58 +02:00
import { getAllWebhooksByEventTrigger } from '../server-only/webhooks/get-all-webhooks-by-event-trigger';
2024-02-16 11:04:11 +02:00
import { postWebhookPayload } from './post-webhook-payload';
export type TriggerWebhookOptions = {
eventTrigger: WebhookTriggerEvents;
documentData: Document;
};
export const triggerWebhook = async ({ eventTrigger, documentData }: TriggerWebhookOptions) => {
try {
2024-02-24 11:18:58 +02:00
const allWebhooks = await getAllWebhooksByEventTrigger({ eventTrigger });
2024-02-16 11:04:11 +02:00
const webhookPromises = allWebhooks.map((webhook) => {
const { webhookUrl, secret } = webhook;
postWebhookPayload({
webhookData: { webhookUrl, secret, eventTriggers: [eventTrigger] },
documentData,
}).catch((_err) => {
throw new Error(`Failed to send webhook to ${webhookUrl}`);
});
});
return Promise.all(webhookPromises);
} catch (err) {
throw new Error(`Failed to trigger webhook`);
}
};