From 6ecb9a25709e73e02a1be952b598a583fd8bbcaa Mon Sep 17 00:00:00 2001 From: Kiran K Date: Wed, 15 Jan 2025 22:20:42 +0530 Subject: [PATCH] Implement unsubscribe functionality for Zapier webhooks. --- apps/web/app/api/webhooks/callback/route.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/apps/web/app/api/webhooks/callback/route.ts b/apps/web/app/api/webhooks/callback/route.ts index 36a4373df..82539ec50 100644 --- a/apps/web/app/api/webhooks/callback/route.ts +++ b/apps/web/app/api/webhooks/callback/route.ts @@ -41,6 +41,21 @@ export const POST = async (req: Request) => { const response = Buffer.from(body, "base64").toString("utf-8"); const isFailed = status >= 400; + // Unsubscribe Zapier webhook + if ( + webhook.receiver === "zapier" && + webhook.installationId && + status === 410 + ) { + await prisma.webhook.delete({ + where: { + id: webhookId, + }, + }); + + return new Response(`Unsubscribed Zapier webhook ${webhookId}`); + } + await Promise.all([ // Record the webhook event recordWebhookEvent({ @@ -63,5 +78,5 @@ export const POST = async (req: Request) => { : []), ]); - return new Response("OK"); + return new Response(`Webhook ${webhookId} processed`); };