Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

prevent adding the same webhook url again

Kiran K0b4d291dccca
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.ts
index b470664f0e..aaea027307 100644
--- a/apps/web/app/api/webhooks/[webhookId]/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts
@@ -1,3 +1,4 @@
+import { DubApiError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
@@ -33,6 +34,23 @@ export const PATCH = withWorkspace(
await parseRequestBody(req),
);
+ const existingWebhook = await prisma.webhook.findFirst({
+ where: {
+ projectId: workspace.id,
+ url,
+ id: {
+ not: webhookId,
+ },
+ },
+ });
+
+ if (existingWebhook) {
+ throw new DubApiError({
+ code: "conflict",
+ message: "A Webhook with this URL already exists.",
+ });
+ }
+
const webhook = await prisma.webhook.update({
where: {
id: webhookId,
diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.ts
index a0133ca503..b4c2113f29 100644
--- a/apps/web/app/api/webhooks/route.ts
+++ b/apps/web/app/api/webhooks/route.ts
@@ -1,3 +1,4 @@
+import { DubApiError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
@@ -31,6 +32,20 @@ export const POST = withWorkspace(
await parseRequestBody(req),
);
+ const existingWebhook = await prisma.webhook.findFirst({
+ where: {
+ url,
+ projectId: workspace.id,
+ },
+ });
+
+ if (existingWebhook) {
+ throw new DubApiError({
+ code: "conflict",
+ message: "A Webhook with this URL already exists.",
+ });
+ }
+
const webhook = await prisma.webhook.create({
data: {
name,