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.

send to zapier

Kiran K449643065f73
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 195e845089..1774bc054e 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -2,13 +2,17 @@ import { DubApiError, ErrorCodes } from "@/lib/api/errors";
import { createLink, getLinksForWorkspace, processLink } from "@/lib/api/links";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
-import { prisma } from "@/lib/prisma";
+import { qstash } from "@/lib/cron";
import { ratelimit } from "@/lib/upstash";
import {
createLinkBodySchema,
getLinksQuerySchemaExtended,
} from "@/lib/zod/schemas/links";
-import { LOCALHOST_IP, getSearchParamsWithArray } from "@dub/utils";
+import {
+ APP_DOMAIN_WITH_NGROK,
+ LOCALHOST_IP,
+ getSearchParamsWithArray,
+} from "@dub/utils";
import { NextResponse } from "next/server";
// GET /api/links – get all links for a workspace
@@ -82,27 +86,16 @@ export const POST = withWorkspace(
try {
const response = await createLink(link);
- // TODO: Send this via Redis queue
- if (workspace) {
- const hooks = await prisma.zapierHook.findMany({
- where: {
- projectId: workspace.id,
+ // TODO:
+ // Send only if Zapier is enabled
+ // How do we optimize this?
+ if (response.projectId) {
+ await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/zapier/send-webhook`,
+ body: {
+ linkId: response.id,
},
});
-
- if (hooks.length) {
- for (const hook of hooks) {
- const responseHook = await fetch(hook.url, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(response),
- });
-
- console.log({ url: hook.url, status: responseHook.status });
- }
- }
}
return NextResponse.json(response, { headers });
diff --git a/apps/web/app/api/zapier/send-webhook/route.ts b/apps/web/app/api/zapier/send-webhook/route.ts
new file mode 100644
index 0000000000..b82e14ffb3
--- /dev/null
+++ b/apps/web/app/api/zapier/send-webhook/route.ts
@@ -0,0 +1,65 @@
+import { transformLink } from "@/lib/api/links";
+import { receiver } from "@/lib/cron";
+import { prisma } from "@/lib/prisma";
+
+export async function POST(req: Request) {
+ const body = await req.json();
+
+ if (process.env.VERCEL === "1") {
+ const isValid = await receiver.verify({
+ signature: req.headers.get("Upstash-Signature") || "",
+ body: JSON.stringify(body),
+ });
+
+ if (!isValid) {
+ return new Response("Unauthorized", { status: 401 });
+ }
+ }
+
+ 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,
+ );
+ }
+
+ return new Response("OK");
+}