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.

update

Kiran Ked1c439fcf91
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 1e701cb78a..f55e769040 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -1,20 +1,16 @@
import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw";
import { DubApiError, ErrorCodes } from "@/lib/api/errors";
import { createLink, getLinksForWorkspace, processLink } from "@/lib/api/links";
+import { sendToZapier } from "@/lib/api/links/send-to-zapier";
import { throwIfLinksUsageExceeded } from "@/lib/api/links/usage-checks";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
-import { qstash } from "@/lib/cron";
import { ratelimit } from "@/lib/upstash";
import {
createLinkBodySchema,
getLinksQuerySchemaExtended,
} from "@/lib/zod/schemas/links";
-import {
- APP_DOMAIN_WITH_NGROK,
- LOCALHOST_IP,
- getSearchParamsWithArray,
-} from "@dub/utils";
+import { LOCALHOST_IP, getSearchParamsWithArray } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -103,14 +99,8 @@ export const POST = withWorkspace(
waitUntil(
(async () => {
- // Publish to Zapier if the project has a Zapier hook enabled
- if (response.projectId && workspace.zapierHookEnabled) {
- await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/zapier/send-webhook`,
- body: {
- linkId: response.id,
- },
- });
+ if (response.projectId) {
+ await sendToZapier(response);
}
})(),
);
diff --git a/apps/web/app/api/zapier/send-webhook/route.ts b/apps/web/app/api/zapier/send-webhook/route.ts
deleted file mode 100644
index 682b8b4cde..0000000000
--- a/apps/web/app/api/zapier/send-webhook/route.ts
+++ /dev/null
@@ -1,66 +0,0 @@
-import { transformLink } from "@/lib/api/links";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
-import { prisma } from "@/lib/prisma";
-
-// POST /api/zapier/send-webhook - Send payload to Zapier hooks
-export async function POST(req: Request) {
- const body = await req.json();
-
- await verifyQstashSignature(req, body);
-
- const { linkId } = body;
-
- // Find the link
- const link = await prisma.link.findUniqueOrThrow({
- where: { id: linkId },
- include: {
- tags: {
- select: {
- tag: true,
- },
- },
- },
- });
-
- const transformedLink = transformLink(link);
-
- // Find the Zapier hooks for the project
- const zapierHooks = await prisma.zapierHook.findMany({
- where: {
- projectId: link.projectId!,
- },
- });
-
- if (zapierHooks.length === 0) {
- return new Response("OK");
- }
-
- // Send the response to each Zapier hook
- for (const hook of zapierHooks) {
- const response = await fetch(hook.url, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(transformedLink),
- });
-
- const json = await response.json();
-
- console.info(
- `[Zapier] Webhook to ${hook.url} for link ${link.id} responded with ${response.status}`,
- json,
- );
-
- // Zap has unsubscribed
- if (response.status === 401) {
- await prisma.zapierHook.delete({
- where: {
- id: hook.id,
- },
- });
- }
- }
-
- return new Response("OK");
-}
diff --git a/apps/web/app/api/zapier/subscribe/route.ts b/apps/web/app/api/zapier/subscribe/route.ts
index fd77559534..04c8ec4e90 100644
--- a/apps/web/app/api/zapier/subscribe/route.ts
+++ b/apps/web/app/api/zapier/subscribe/route.ts
@@ -20,18 +20,6 @@ export const POST = withWorkspace(async ({ workspace, req }) => {
},
});
- // Enable the hook on the workspace
- if (hook) {
- await prisma.project.update({
- where: {
- id: workspace.id,
- },
- data: {
- zapierHookEnabled: true,
- },
- });
- }
-
console.info(`[Zapier] Workspace ${workspace.id} subscribed to ${hook}`);
return NextResponse.json({ id: hook.id }, { status: 201 });
diff --git a/apps/web/app/api/zapier/unsubscribe/route.ts b/apps/web/app/api/zapier/unsubscribe/route.ts
index 3e2c25e363..81984f63bf 100644
--- a/apps/web/app/api/zapier/unsubscribe/route.ts
+++ b/apps/web/app/api/zapier/unsubscribe/route.ts
@@ -26,18 +26,6 @@ export const POST = withWorkspace(async ({ workspace, req }) => {
},
});
- // Disable the hook on the workspace
- if (hooks === 0) {
- await prisma.project.update({
- where: {
- id: workspace.id,
- },
- data: {
- zapierHookEnabled: false,
- },
- });
- }
-
console.info(`[Zapier] Workspace ${workspace.id} unsubscribed from ${hook}`);
return NextResponse.json({ status: "OK" });
diff --git a/apps/web/lib/api/links/send-to-zapier.ts b/apps/web/lib/api/links/send-to-zapier.ts
new file mode 100644
index 0000000000..d05b9d725b
--- /dev/null
+++ b/apps/web/lib/api/links/send-to-zapier.ts
@@ -0,0 +1,44 @@
+import { prisma } from "@/lib/prisma";
+
+export const sendToZapier = async (link) => {
+ if (!link.projectId) {
+ return;
+ }
+
+ const zapierHooks = await prisma.zapierHook.findMany({
+ where: {
+ projectId: link.projectId,
+ },
+ });
+
+ if (zapierHooks.length === 0) {
+ return;
+ }
+
+ // Send the response to each Zapier hook
+ for (const hook of zapierHooks) {
+ const response = await fetch(hook.url, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(link),
+ });
+
+ const json = await response.json();
+
+ console.info(
+ `[Zapier] Webhook to ${hook.url} for link ${link.id} responded with ${response.status}`,
+ json,
+ );
+
+ // Zap has unsubscribed
+ if (response.status === 401) {
+ await prisma.zapierHook.delete({
+ where: {
+ id: hook.id,
+ },
+ });
+ }
+ }
+};
diff --git a/apps/web/prisma/schema/workspace.prisma b/apps/web/prisma/schema/workspace.prisma
index 20210d485a..60c107823c 100644
--- a/apps/web/prisma/schema/workspace.prisma
+++ b/apps/web/prisma/schema/workspace.prisma
@@ -20,10 +20,9 @@ model Project {
tagsLimit Int @default(5)
usersLimit Int @default(1)
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
- usageLastChecked DateTime @default(now())
- zapierHookEnabled Boolean @default(false)
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+ usageLastChecked DateTime @default(now())
users ProjectUsers[]
invites ProjectInvite[]