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.

Fix webhook payload: Include Tags, Shopify support

Steven Teyd307e12a9980
diff --git a/apps/web/app/api/links/[linkId]/route.ts b/apps/web/app/api/links/[linkId]/route.ts
index 6de774b657..550363db14 100644
--- a/apps/web/app/api/links/[linkId]/route.ts
+++ b/apps/web/app/api/links/[linkId]/route.ts
@@ -179,13 +179,13 @@ export const DELETE = withWorkspace(
});
}
- await deleteLink(link.id);
+ const response = await deleteLink(link.id);
waitUntil(
sendWorkspaceWebhook({
trigger: "link.deleted",
workspace,
- data: linkEventSchema.parse(transformLink(link)),
+ data: linkEventSchema.parse(response),
}),
);
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.ts
index 0f2a757d31..c31590cf27 100644
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@ -15,13 +15,6 @@ import { LOCALHOST_IP, getSearchParamsWithArray } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
-const CORS_HEADERS = {
- "Access-Control-Allow-Origin": "*",
- "Access-Control-Allow-Methods": "POST, OPTIONS",
- "Access-Control-Allow-Headers":
- "Content-Type, Authorization, dub-anonymous-link-creation",
-};
-
// GET /api/links – get all links for a workspace
export const GET = withWorkspace(
async ({ req, headers, workspace }) => {
@@ -95,10 +88,7 @@ export const POST = withWorkspace(
}
return NextResponse.json(response, {
- headers: {
- ...headers,
- ...CORS_HEADERS,
- },
+ headers,
});
} catch (error) {
throw new DubApiError({
@@ -111,10 +101,3 @@ export const POST = withWorkspace(
requiredPermissions: ["links.write"],
},
);
-
-export const OPTIONS = () => {
- return new Response(null, {
- status: 204,
- headers: CORS_HEADERS,
- });
-};
diff --git a/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts
index de907fb112..d2a2b7bbc6 100644
--- a/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts
+++ b/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts
@@ -1,3 +1,4 @@
+import { includeTags } from "@/lib/api/links/include-tags";
import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";
import { createSaleData } from "@/lib/api/sales/create-sale-data";
import { createId } from "@/lib/api/utils";
@@ -234,6 +235,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
increment: charge.amount_total!,
},
},
+ include: includeTags,
}),
// update workspace sales usage
diff --git a/apps/web/app/api/stripe/integration/webhook/customer-created.ts b/apps/web/app/api/stripe/integration/webhook/customer-created.ts
index 8bb9e16526..fd3eba6e64 100644
--- a/apps/web/app/api/stripe/integration/webhook/customer-created.ts
+++ b/apps/web/app/api/stripe/integration/webhook/customer-created.ts
@@ -1,3 +1,5 @@
+import { transformLink } from "@/lib/api/links";
+import { includeTags } from "@/lib/api/links/include-tags";
import { createId } from "@/lib/api/utils";
import { getClickEvent, recordLead } from "@/lib/tinybird";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
@@ -105,6 +107,7 @@ export async function customerCreated(event: Stripe.Event) {
increment: 1,
},
},
+ include: includeTags,
}),
// update workspace usage
@@ -126,7 +129,7 @@ export async function customerCreated(event: Stripe.Event) {
workspace,
data: transformLeadEventData({
...leadData,
- link,
+ link: transformLink(link),
customerId: customer.id,
customerExternalId: customer.externalId,
customerName: customer.name,
diff --git a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
index 1950ccaa0f..3a16603465 100644
--- a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
+++ b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
@@ -1,3 +1,4 @@
+import { includeTags } from "@/lib/api/links/include-tags";
import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";
import { createSaleData } from "@/lib/api/sales/create-sale-data";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
@@ -90,6 +91,7 @@ export async function invoicePaid(event: Stripe.Event) {
increment: invoice.amount_paid,
},
},
+ include: includeTags,
}),
// update workspace sales usage
prisma.project.update({
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.ts
index 07ef34d70a..900a179a9d 100644
--- a/apps/web/app/api/track/lead/route.ts
+++ b/apps/web/app/api/track/lead/route.ts
@@ -1,4 +1,5 @@
import { DubApiError } from "@/lib/api/errors";
+import { includeTags } from "@/lib/api/links/include-tags";
import { createId, parseRequestBody } from "@/lib/api/utils";
import { withWorkspaceEdge } from "@/lib/auth/workspace-edge";
import { generateRandomName } from "@/lib/names";
@@ -104,6 +105,7 @@ export const POST = withWorkspaceEdge(
increment: 1,
},
},
+ include: includeTags,
}),
// update workspace usage
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index 35e7ae32f4..70c6bd5965 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -1,4 +1,5 @@
import { DubApiError } from "@/lib/api/errors";
+import { includeTags } from "@/lib/api/links/include-tags";
import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";
import { createSaleData } from "@/lib/api/sales/create-sale-data";
import { parseRequestBody } from "@/lib/api/utils";
@@ -103,6 +104,7 @@ export const POST = withWorkspaceEdge(
increment: amount,
},
},
+ include: includeTags,
}),
// update workspace sales usage
prismaEdge.project.update({
diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.ts
index f451d3ec4a..2e838b94b6 100644
--- a/apps/web/lib/api/links/create-link.ts
+++ b/apps/web/lib/api/links/create-link.ts
@@ -16,6 +16,7 @@ import { waitUntil } from "@vercel/functions";
import { combineTagIds } from "../tags/combine-tag-ids";
import { createId } from "../utils";
import { linkCache } from "./cache";
+import { includeTags } from "./include-tags";
import { updateLinksUsage } from "./update-links-usage";
import { transformLink } from "./utils";
@@ -112,20 +113,7 @@ export async function createLink(link: ProcessedLinkProps) {
}),
},
include: {
- tags: {
- select: {
- tag: {
- select: {
- id: true,
- name: true,
- color: true,
- },
- },
- },
- orderBy: {
- createdAt: "asc",
- },
- },
+ ...includeTags,
webhooks: webhookIds ? true : false,
},
});
diff --git a/apps/web/lib/api/links/delete-link.ts b/apps/web/lib/api/links/delete-link.ts
index 28b8876cad..e703294ff4 100644
--- a/apps/web/lib/api/links/delete-link.ts
+++ b/apps/web/lib/api/links/delete-link.ts
@@ -4,6 +4,8 @@ import { prisma } from "@dub/prisma";
import { R2_URL } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { linkCache } from "./cache";
+import { includeTags } from "./include-tags";
+import { transformLink } from "./utils";
export async function deleteLink(linkId: string) {
const link = await prisma.link.delete({
@@ -11,11 +13,7 @@ export async function deleteLink(linkId: string) {
id: linkId,
},
include: {
- tags: {
- select: {
- tag: true,
- },
- },
+ ...includeTags,
},
});
@@ -37,5 +35,5 @@ export async function deleteLink(linkId: string) {
]),
);
- return link;
+ return transformLink(link);
}
diff --git a/apps/web/lib/api/links/include-tags.ts b/apps/web/lib/api/links/include-tags.ts
new file mode 100644
index 0000000000..5b460c9539
--- /dev/null
+++ b/apps/web/lib/api/links/include-tags.ts
@@ -0,0 +1,18 @@
+import { Prisma } from "@dub/prisma/client";
+
+export const includeTags = {
+ tags: {
+ select: {
+ tag: {
+ select: {
+ id: true,
+ name: true,
+ color: true,
+ },
+ },
+ },
+ orderBy: {
+ createdAt: "asc",
+ },
+ },
+} satisfies Prisma.LinkInclude;
diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.ts
index 951b836a00..c224f323a6 100644
--- a/apps/web/lib/api/links/update-link.ts
+++ b/apps/web/lib/api/links/update-link.ts
@@ -15,6 +15,7 @@ import { waitUntil } from "@vercel/functions";
import { combineTagIds } from "../tags/combine-tag-ids";
import { createId } from "../utils";
import { linkCache } from "./cache";
+import { includeTags } from "./include-tags";
import { transformLink } from "./utils";
export async function updateLink({
@@ -140,20 +141,7 @@ export async function updateLink({
}),
},
include: {
- tags: {
- select: {
- tag: {
- select: {
- id: true,
- name: true,
- color: true,
- },
- },
- },
- orderBy: {
- createdAt: "asc",
- },
- },
+ ...includeTags,
webhooks: webhookIds ? true : false,
},
});
diff --git a/apps/web/lib/integrations/shopify/create-lead.ts b/apps/web/lib/integrations/shopify/create-lead.ts
index 5a415c3a3b..ce041dafc1 100644
--- a/apps/web/lib/integrations/shopify/create-lead.ts
+++ b/apps/web/lib/integrations/shopify/create-lead.ts
@@ -1,9 +1,14 @@
+import { includeTags } from "@/lib/api/links/include-tags";
+import { transformLink } from "@/lib/api/links/utils";
import { createId } from "@/lib/api/utils";
import { generateRandomName } from "@/lib/names";
import { getClickEvent, recordLead } from "@/lib/tinybird";
+import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
+import { transformLeadEventData } from "@/lib/webhook/transform";
import { leadEventSchemaTB } from "@/lib/zod/schemas/leads";
import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
+import { waitUntil } from "@vercel/functions";
import { orderSchema } from "./schema";
export async function createShopifyLead({
@@ -48,7 +53,7 @@ export async function createShopifyLead({
customer_id: customer.id,
});
- await Promise.all([
+ const [_lead, link, workspace] = await Promise.all([
// record lead
recordLead(leadData),
@@ -62,6 +67,7 @@ export async function createShopifyLead({
increment: 1,
},
},
+ include: includeTags,
}),
// update workspace usage
@@ -77,5 +83,22 @@ export async function createShopifyLead({
}),
]);
+ waitUntil(
+ sendWorkspaceWebhook({
+ trigger: "lead.created",
+ workspace,
+ data: transformLeadEventData({
+ ...leadData,
+ link: transformLink(link),
+ customerId: customer.id,
+ customerExternalId: customer.externalId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ customerCreatedAt: customer.createdAt,
+ }),
+ }),
+ );
+
return leadData;
}
diff --git a/apps/web/lib/integrations/shopify/create-sale.ts b/apps/web/lib/integrations/shopify/create-sale.ts
index 958d534452..14c3c0dd98 100644
--- a/apps/web/lib/integrations/shopify/create-sale.ts
+++ b/apps/web/lib/integrations/shopify/create-sale.ts
@@ -1,11 +1,15 @@
+import { includeTags } from "@/lib/api/links/include-tags";
import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";
import { createSaleData } from "@/lib/api/sales/create-sale-data";
import { recordSale } from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
+import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
+import { transformSaleEventData } from "@/lib/webhook/transform";
import z from "@/lib/zod";
import { leadEventSchemaTB } from "@/lib/zod/schemas/leads";
import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
+import { waitUntil } from "@vercel/functions";
import { orderSchema } from "./schema";
export async function createShopifySale({
@@ -54,7 +58,7 @@ export async function createShopifySale({
metadata: JSON.stringify(order),
};
- const [_sale, link, _project] = await Promise.all([
+ const [_sale, link, workspace, customer] = await Promise.all([
// record sale
recordSale(saleData),
@@ -71,6 +75,7 @@ export async function createShopifySale({
increment: amount,
},
},
+ include: includeTags,
}),
// update workspace sales usage
@@ -88,9 +93,36 @@ export async function createShopifySale({
},
}),
+ prisma.customer.findUnique({
+ where: {
+ id: customerId,
+ },
+ }),
+
redis.del(`shopify:checkout:${checkoutToken}`),
]);
+ waitUntil(
+ sendWorkspaceWebhook({
+ trigger: "sale.created",
+ workspace,
+ data: transformSaleEventData({
+ ...saleData,
+ link,
+ ...(customer
+ ? {
+ customerId: customer.id,
+ customerExternalId: customer.externalId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ customerCreatedAt: customer.createdAt,
+ }
+ : {}),
+ }),
+ }),
+ );
+
// for program links
if (link.programId) {
const { program, partnerId, commissionAmount } =
@@ -128,22 +160,24 @@ export async function createShopifySale({
},
});
- await Promise.allSettled([
- prisma.sale.create({
- data: saleRecord,
- }),
+ waitUntil(
+ Promise.allSettled([
+ prisma.sale.create({
+ data: saleRecord,
+ }),
- notifyPartnerSale({
- partner: {
- id: partnerId,
- referralLink: link.shortLink,
- },
- program,
- sale: {
- amount: saleRecord.amount,
- earnings: saleRecord.earnings,
- },
- }),
- ]);
+ notifyPartnerSale({
+ partner: {
+ id: partnerId,
+ referralLink: link.shortLink,
+ },
+ program,
+ sale: {
+ amount: saleRecord.amount,
+ earnings: saleRecord.earnings,
+ },
+ }),
+ ]),
+ );
}
}