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.

revert the deleteWebhook

Kiran Kcbfb5a949aa9
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.ts
index b79b609d8f..1fd6d2f1c1 100644
--- a/apps/web/app/api/webhooks/[webhookId]/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts
@@ -3,8 +3,8 @@ import { linkCache } from "@/lib/api/links/cache";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { webhookCache } from "@/lib/webhook/cache";
-import { deleteWebhook } from "@/lib/webhook/delete-webhook";
import { transformWebhook } from "@/lib/webhook/transform";
+import { updateWebhookStatusForWorkspace } from "@/lib/webhook/update-webhook";
import { isLinkLevelWebhook } from "@/lib/webhook/utils";
import { updateWebhookSchema } from "@/lib/zod/schemas/webhooks";
import { prisma } from "@dub/prisma";
@@ -258,11 +258,73 @@ export const DELETE = withWorkspace(
async ({ workspace, params }) => {
const { webhookId } = params;
- await deleteWebhook({
- webhookId,
- workspaceId: workspace.id,
+ const webhook = await prisma.webhook.findUniqueOrThrow({
+ where: {
+ id: webhookId,
+ projectId: workspace.id,
+ },
+ select: {
+ installationId: true,
+ },
+ });
+
+ if (webhook.installationId) {
+ throw new DubApiError({
+ code: "bad_request",
+ message:
+ "This webhook is managed by an integration, hence cannot be deleted manually.",
+ });
+ }
+
+ const linkWebhooks = await prisma.linkWebhook.findMany({
+ where: {
+ webhookId,
+ },
+ select: {
+ linkId: true,
+ },
+ });
+
+ await prisma.webhook.delete({
+ where: {
+ id: webhookId,
+ },
});
+ waitUntil(
+ (async () => {
+ const links = await prisma.link.findMany({
+ where: {
+ id: { in: linkWebhooks.map(({ linkId }) => linkId) },
+ },
+ include: {
+ webhooks: {
+ select: {
+ webhookId: true,
+ },
+ },
+ },
+ });
+
+ const formatedLinks = links.map((link) => {
+ return {
+ ...link,
+ webhookIds: link.webhooks.map((webhook) => webhook.webhookId),
+ };
+ });
+
+ await Promise.all([
+ updateWebhookStatusForWorkspace({
+ workspaceId: workspace.id,
+ }),
+
+ linkCache.mset(formatedLinks),
+
+ webhookCache.delete(webhookId),
+ ]);
+ })(),
+ );
+
return NextResponse.json({
id: webhookId,
});
diff --git a/apps/web/lib/webhook/delete-webhook.ts b/apps/web/lib/webhook/delete-webhook.ts
deleted file mode 100644
index 21f4a8257f..0000000000
--- a/apps/web/lib/webhook/delete-webhook.ts
+++ /dev/null
@@ -1,81 +0,0 @@
-import { linkCache } from "@/lib/api/links/cache";
-import { webhookCache } from "@/lib/webhook/cache";
-import { prisma } from "@dub/prisma";
-import { waitUntil } from "@vercel/functions";
-import { DubApiError } from "../api/errors";
-import { updateWebhookStatusForWorkspace } from "./update-webhook";
-
-export const deleteWebhook = async ({
- webhookId,
- workspaceId,
-}: {
- webhookId: string;
- workspaceId: string;
-}) => {
- const webhook = await prisma.webhook.findUniqueOrThrow({
- where: {
- id: webhookId,
- projectId: workspaceId,
- },
- select: {
- installationId: true,
- },
- });
-
- if (webhook.installationId) {
- throw new DubApiError({
- code: "bad_request",
- message:
- "This webhook is managed by an integration, hence cannot be deleted manually.",
- });
- }
-
- const linkWebhooks = await prisma.linkWebhook.findMany({
- where: {
- webhookId,
- },
- select: {
- linkId: true,
- },
- });
-
- await prisma.webhook.delete({
- where: {
- id: webhookId,
- },
- });
-
- waitUntil(
- (async () => {
- const links = await prisma.link.findMany({
- where: {
- id: { in: linkWebhooks.map(({ linkId }) => linkId) },
- },
- include: {
- webhooks: {
- select: {
- webhookId: true,
- },
- },
- },
- });
-
- const formatedLinks = links.map((link) => {
- return {
- ...link,
- webhookIds: link.webhooks.map((webhook) => webhook.webhookId),
- };
- });
-
- await Promise.all([
- updateWebhookStatusForWorkspace({
- workspaceId,
- }),
-
- linkCache.mset(formatedLinks),
-
- webhookCache.delete(webhookId),
- ]);
- })(),
- );
-};