The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
identify webhook receiver from url
Kiran K5515d41672b7
diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.tsindex 36151ae249..c7f2a64194 100644--- a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts+++ b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts@@ -135,5 +135,15 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {sendCancellationFeedback({owners: workspaceUsers,}),++ // Disable the webhooks+ prisma.webhook.updateMany({+ where: {+ projectId: workspace.id,+ },+ data: {+ disabled: true,+ },+ }),]);}diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts b/apps/web/app/api/stripe/webhook/customer-subscription-updated.tsindex 00bd060457..79f1d91a4f 100644--- a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts+++ b/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts@@ -84,6 +84,14 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {rateLimit: plan.limits.api,},});++ // Disable the webhooks if the plan is downgraded to free+ if (newPlan === "free") {+ await prisma.webhook.updateMany({+ where: { projectId: workspace.id },+ data: { disabled: true },+ });+ }}const subscriptionCanceled =diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.tsindex 059f4b0062..0cf3d99f03 100644--- a/apps/web/app/api/webhooks/route.ts+++ b/apps/web/app/api/webhooks/route.ts@@ -4,11 +4,13 @@ import { parseRequestBody } from "@/lib/api/utils";import { withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";import { webhookCache } from "@/lib/webhook/cache";-import { WEBHOOK_ID_PREFIX, WEBHOOK_RECEIVER } from "@/lib/webhook/constants";+import { WEBHOOK_ID_PREFIX } from "@/lib/webhook/constants";import { createWebhookSecret } from "@/lib/webhook/secret";import { transformWebhook } from "@/lib/webhook/transform";-import { isLinkLevelWebhook } from "@/lib/webhook/utils";-import z from "@/lib/zod";+import {+ identifyWebhookReceiver,+ isLinkLevelWebhook,+} from "@/lib/webhook/utils";import { createWebhookSchema } from "@/lib/zod/schemas/webhooks";import { nanoid } from "@dub/utils";import { waitUntil } from "@vercel/functions";@@ -16,10 +18,6 @@ import { sendEmail } from "emails";import WebhookAdded from "emails/webhook-added";import { NextResponse } from "next/server";-const createWebhookBodySchema = createWebhookSchema.extend({- receiver: z.enum(WEBHOOK_RECEIVER).default("user"),-});-// GET /api/webhooks - get all webhooks for the given workspaceexport const GET = withWorkspace(async ({ workspace }) => {@@ -60,10 +58,10 @@ export const GET = withWorkspace(// POST /api/webhooks/ - create a new webhookexport const POST = withWorkspace(async ({ req, workspace, session }) => {- const body = createWebhookBodySchema.parse(await parseRequestBody(req));+ const body = createWebhookSchema.parse(await parseRequestBody(req));let { secret } = body;- const { name, url, triggers, linkIds, receiver } = body;+ const { name, url, triggers, linkIds } = body;if (!secret) {secret = createWebhookSecret();@@ -111,7 +109,7 @@ export const POST = withWorkspace(secret,triggers,projectId: workspace.id,- receiver,+ receiver: identifyWebhookReceiver(url),links: {...(linkIds &&linkIds.length > 0 && {diff --git a/apps/web/lib/webhook/utils.ts b/apps/web/lib/webhook/utils.tsindex b56263bed8..bfe566501d 100644--- a/apps/web/lib/webhook/utils.ts+++ b/apps/web/lib/webhook/utils.ts@@ -1,4 +1,4 @@-import type { Webhook } from "@prisma/client";+import type { Webhook, WebhookReceiver } from "@prisma/client";import { LINK_LEVEL_WEBHOOK_TRIGGERS } from "./constants";export const isLinkLevelWebhook = (webhook: Pick<Webhook, "triggers">) => {@@ -13,3 +13,15 @@ export const isLinkLevelWebhook = (webhook: Pick<Webhook, "triggers">) => {LINK_LEVEL_WEBHOOK_TRIGGERS.includes(trigger),);};++export const identifyWebhookReceiver = (url: string): WebhookReceiver => {+ if (url.includes("zapier.com")) {+ return "zapier";+ }++ if (url.includes("make.com")) {+ return "make";+ }++ return "user";+};