2024-06-05 08:52:41 +02:00
|
|
|
import { db } from "@link-stack/bridge-common";
|
2024-04-30 13:13:49 +02:00
|
|
|
|
|
|
|
|
export interface NotifyWebhooksOptions {
|
|
|
|
|
backendId: string;
|
|
|
|
|
payload: any;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const notifyWebhooksTask = async (
|
|
|
|
|
options: NotifyWebhooksOptions,
|
|
|
|
|
): Promise<void> => {
|
|
|
|
|
const { backendId, payload } = options;
|
2025-07-07 20:02:54 +02:00
|
|
|
|
|
|
|
|
console.log(`[notify-webhooks] Processing webhook notification:`, {
|
|
|
|
|
backendId,
|
|
|
|
|
payloadKeys: Object.keys(payload),
|
|
|
|
|
payload: JSON.stringify(payload, null, 2),
|
|
|
|
|
});
|
2024-04-30 13:13:49 +02:00
|
|
|
|
|
|
|
|
const webhooks = await db
|
|
|
|
|
.selectFrom("Webhook")
|
|
|
|
|
.selectAll()
|
|
|
|
|
.where("backendId", "=", backendId)
|
|
|
|
|
.execute();
|
|
|
|
|
|
2025-07-07 20:02:54 +02:00
|
|
|
console.log(`[notify-webhooks] Found ${webhooks.length} webhooks for backend ${backendId}`);
|
|
|
|
|
|
2024-04-30 13:13:49 +02:00
|
|
|
for (const webhook of webhooks) {
|
|
|
|
|
const { endpointUrl, httpMethod, headers } = webhook;
|
|
|
|
|
const finalHeaders = { "Content-Type": "application/json", ...headers };
|
2025-07-07 20:02:54 +02:00
|
|
|
const body = JSON.stringify(payload);
|
|
|
|
|
|
|
|
|
|
console.log(`[notify-webhooks] Sending webhook:`, {
|
|
|
|
|
url: endpointUrl,
|
2024-04-30 13:13:49 +02:00
|
|
|
method: httpMethod,
|
2025-07-07 20:02:54 +02:00
|
|
|
bodyLength: body.length,
|
|
|
|
|
headers: Object.keys(finalHeaders),
|
|
|
|
|
payload: body,
|
2024-04-30 13:13:49 +02:00
|
|
|
});
|
2025-07-07 20:02:54 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const result = await fetch(endpointUrl, {
|
|
|
|
|
method: httpMethod,
|
|
|
|
|
headers: finalHeaders,
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(`[notify-webhooks] Webhook response:`, {
|
|
|
|
|
url: endpointUrl,
|
|
|
|
|
status: result.status,
|
|
|
|
|
statusText: result.statusText,
|
|
|
|
|
ok: result.ok,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!result.ok) {
|
|
|
|
|
const responseText = await result.text();
|
|
|
|
|
console.error(`[notify-webhooks] Webhook error response:`, {
|
|
|
|
|
url: endpointUrl,
|
|
|
|
|
status: result.status,
|
|
|
|
|
response: responseText.substring(0, 500), // First 500 chars
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(`[notify-webhooks] Webhook request failed:`, {
|
|
|
|
|
url: endpointUrl,
|
|
|
|
|
error: error instanceof Error ? error.message : error,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-04-30 13:13:49 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default notifyWebhooksTask;
|