The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
move to individual files
Kiran K2005e672393a
diff --git a/apps/web/app/api/integrations/uninstall/route.ts b/apps/web/app/api/integrations/uninstall/route.tsindex 336e381dfa..d9c5a531af 100644--- a/apps/web/app/api/integrations/uninstall/route.ts+++ b/apps/web/app/api/integrations/uninstall/route.ts@@ -2,7 +2,7 @@ import { DubApiError } from "@/lib/api/errors";import { withWorkspace } from "@/lib/auth";import { uninstallSlackIntegration } from "@/lib/integrations/slack/uninstall";import { prisma } from "@/lib/prisma";-import { deleteWebhook } from "@/lib/webhook/api";+import { deleteWebhook } from "@/lib/webhook/delete-webhook";import { NextResponse } from "next/server";// DELETE /api/integrations/uninstall - uninstall an installation by iddiff --git a/apps/web/app/api/slack/callback/route.ts b/apps/web/app/api/slack/callback/route.tsindex f012bb2538..7b341bed72 100644--- a/apps/web/app/api/slack/callback/route.ts+++ b/apps/web/app/api/slack/callback/route.ts@@ -5,7 +5,7 @@ import { getSlackEnv } from "@/lib/integrations/slack/env";import { SlackCredential } from "@/lib/integrations/slack/type";import { prisma } from "@/lib/prisma";import { redis } from "@/lib/upstash";-import { addWebhook } from "@/lib/webhook/api";+import { createWebhook } from "@/lib/webhook/create-webhook";import z from "@/lib/zod";import {APP_DOMAIN_WITH_NGROK,@@ -75,7 +75,7 @@ export const GET = async (req: Request) => {const data = await response.json();- const webhook = await addWebhook({+ const webhook = await createWebhook({name: "Slack",url: data.incoming_webhook.url,triggers: ["link.created"],diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.tsindex c2eb1961b1..55debe8d33 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 { prisma } from "@/lib/prisma";-import { deleteWebhook } from "@/lib/webhook/api";import { webhookCache } from "@/lib/webhook/cache";+import { deleteWebhook } from "@/lib/webhook/delete-webhook";import { transformWebhook } from "@/lib/webhook/transform";import { isLinkLevelWebhook } from "@/lib/webhook/utils";import { updateWebhookSchema } from "@/lib/zod/schemas/webhooks";diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.tsindex ac9b68983a..a1c51d2d8d 100644--- a/apps/web/app/api/webhooks/route.ts+++ b/apps/web/app/api/webhooks/route.ts@@ -2,8 +2,9 @@ import { DubApiError } from "@/lib/api/errors";import { parseRequestBody } from "@/lib/api/utils";import { withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";-import { addWebhook, updateWebhookStatusForWorkspace } from "@/lib/webhook/api";+import { createWebhook } from "@/lib/webhook/create-webhook";import { transformWebhook } from "@/lib/webhook/transform";+import { updateWebhookStatusForWorkspace } from "@/lib/webhook/update-webhook";import { createWebhookSchema } from "@/lib/zod/schemas/webhooks";import { waitUntil } from "@vercel/functions";import { sendEmail } from "emails";@@ -87,7 +88,7 @@ export const POST = withWorkspace(}}- const webhook = await addWebhook({+ const webhook = await createWebhook({name,url,triggers,diff --git a/apps/web/lib/actions/enable-disable-webhook.ts b/apps/web/lib/actions/enable-disable-webhook.tsindex 4a5c89d792..2f7df029d8 100644--- a/apps/web/lib/actions/enable-disable-webhook.ts+++ b/apps/web/lib/actions/enable-disable-webhook.ts@@ -2,8 +2,8 @@import { prisma } from "@/lib/prisma";import { waitUntil } from "@vercel/functions";-import { updateWebhookStatusForWorkspace } from "../webhook/api";import { webhookCache } from "../webhook/cache";+import { updateWebhookStatusForWorkspace } from "../webhook/update-webhook";import z from "../zod";import { authActionClient } from "./safe-action";diff --git a/apps/web/lib/integrations/segment/install.ts b/apps/web/lib/integrations/segment/install.tsindex 943ede9640..bfc450bcc1 100644--- a/apps/web/lib/integrations/segment/install.ts+++ b/apps/web/lib/integrations/segment/install.ts@@ -1,7 +1,7 @@"use server";import { authActionClient } from "@/lib/actions/safe-action";-import { addWebhook } from "@/lib/webhook/api";+import { createWebhook } from "@/lib/webhook/create-webhook";import { SEGMENT_INTEGRATION_ID } from "@dub/utils";import { revalidatePath } from "next/cache";import { z } from "zod";@@ -18,7 +18,7 @@ export const installSegmentAction = authActionClientconst { workspace, user } = ctx;const { writeKey } = parsedInput;- const webhook = await addWebhook({+ const webhook = await createWebhook({name: "Segment",url: "https://api.segment.io/v1/track",triggers: ["link.clicked"],diff --git a/apps/web/lib/webhook/api.ts b/apps/web/lib/webhook/create-webhook.tssimilarity index 58%rename from apps/web/lib/webhook/api.tsrename to apps/web/lib/webhook/create-webhook.tsindex 9787b613f2..4c639cdf44 100644--- a/apps/web/lib/webhook/api.ts+++ b/apps/web/lib/webhook/create-webhook.ts@@ -12,9 +12,7 @@ import { waitUntil } from "@vercel/functions";import { WebhookTrigger } from "../types";import { createWebhookSecret } from "./secret";-// TODO (Kiran): move the methods to individual files--export async function addWebhook({+export async function createWebhook({name,url,secret,@@ -102,82 +100,3 @@ export async function addWebhook({return webhook;}--export const updateWebhookStatusForWorkspace = async ({- workspaceId,-}: {- workspaceId: string;-}) => {- const activeWebhooksCount = await prisma.webhook.count({- where: {- projectId: workspaceId,- disabledAt: null,- },- });-- await prisma.project.update({- where: {- id: workspaceId,- },- data: {- webhookEnabled: activeWebhooksCount > 0,- },- });-};--export const deleteWebhook = async ({- webhookId,- workspaceId,-}: {- webhookId: string;- workspaceId: string;-}) => {- const linkWebhooks = await prisma.linkWebhook.findMany({- where: {- webhookId,- },- select: {- linkId: true,- },- });-- await prisma.webhook.delete({- where: {- id: webhookId,- projectId: workspaceId,- },- });-- 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),- ]);- })(),- );-};diff --git a/apps/web/lib/webhook/delete-webhook.ts b/apps/web/lib/webhook/delete-webhook.tsnew file mode 100644index 0000000000..fcf092d1b9--- /dev/null+++ b/apps/web/lib/webhook/delete-webhook.ts@@ -0,0 +1,63 @@+import { linkCache } from "@/lib/api/links/cache";+import { prisma } from "@/lib/prisma";+import { webhookCache } from "@/lib/webhook/cache";+import { waitUntil } from "@vercel/functions";+import { updateWebhookStatusForWorkspace } from "./update-webhook";++export const deleteWebhook = async ({+ webhookId,+ workspaceId,+}: {+ webhookId: string;+ workspaceId: string;+}) => {+ const linkWebhooks = await prisma.linkWebhook.findMany({+ where: {+ webhookId,+ },+ select: {+ linkId: true,+ },+ });++ await prisma.webhook.delete({+ where: {+ id: webhookId,+ projectId: workspaceId,+ },+ });++ 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),+ ]);+ })(),+ );+};diff --git a/apps/web/lib/webhook/failure.ts b/apps/web/lib/webhook/failure.tsindex 9e04d9c1a2..019bbd0d5c 100644--- a/apps/web/lib/webhook/failure.ts+++ b/apps/web/lib/webhook/failure.ts@@ -2,9 +2,9 @@ import { Webhook } from "@prisma/client";import { sendEmail } from "emails";import WebhookDisabled from "emails/webhook-disabled";import { prisma } from "../prisma";-import { updateWebhookStatusForWorkspace } from "./api";import { webhookCache } from "./cache";import { WEBHOOK_FAILURE_NOTIFY_THRESHOLD } from "./constants";+import { updateWebhookStatusForWorkspace } from "./update-webhook";export const handleWebhookFailure = async (webhookId: string) => {const webhook = await prisma.webhook.update({diff --git a/apps/web/lib/webhook/update-webhook.ts b/apps/web/lib/webhook/update-webhook.tsnew file mode 100644index 0000000000..24a69015e4--- /dev/null+++ b/apps/web/lib/webhook/update-webhook.ts@@ -0,0 +1,23 @@+import { prisma } from "@/lib/prisma";++export const updateWebhookStatusForWorkspace = async ({+ workspaceId,+}: {+ workspaceId: string;+}) => {+ const activeWebhooksCount = await prisma.webhook.count({+ where: {+ projectId: workspaceId,+ disabledAt: null,+ },+ });++ await prisma.project.update({+ where: {+ id: workspaceId,+ },+ data: {+ webhookEnabled: activeWebhooksCount > 0,+ },+ });+};