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.

record lead events

Kiran K49bcdf58e750
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 5b6f276467..92d0f86893 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
@@ -175,6 +175,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
if (!existingCustomer) {
await recordLead(leadEvent);
}
+
linkId = clickEvent.link_id;
// if it's not either a regular stripe checkout setup or a stripe checkout link,
@@ -274,7 +275,18 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
},
});
- const earningsData = prepareEarnings({
+ const leadEarnings = prepareEarnings({
+ link,
+ customer,
+ program,
+ partner,
+ event: {
+ type: "lead",
+ id: eventId,
+ },
+ });
+
+ const saleEarnings = prepareEarnings({
link,
customer,
program,
@@ -290,8 +302,8 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
},
});
- await prisma.earnings.create({
- data: earningsData,
+ await prisma.earnings.createMany({
+ data: [leadEarnings, saleEarnings],
});
waitUntil(
@@ -302,8 +314,8 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
},
program,
sale: {
- amount: earningsData.amount!,
- earnings: earningsData.earnings!,
+ amount: saleEarnings.amount!,
+ earnings: saleEarnings.earnings!,
},
}),
);
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 a0fd50cbf0..9198e246da 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,4 @@
+import { prepareEarnings } from "@/lib/api/earnings/create-earnings";
import { includeTags } from "@/lib/api/links/include-tags";
import { createId } from "@/lib/api/utils";
import { getClickEvent, recordLead } from "@/lib/tinybird";
@@ -122,6 +123,37 @@ export async function customerCreated(event: Stripe.Event) {
}),
]);
+ if (link.programId) {
+ const { program, ...partner } =
+ await prisma.programEnrollment.findFirstOrThrow({
+ where: {
+ links: {
+ some: {
+ id: link.id,
+ },
+ },
+ },
+ select: {
+ program: true,
+ partnerId: true,
+ commissionAmount: true,
+ },
+ });
+
+ await prisma.earnings.create({
+ data: prepareEarnings({
+ link,
+ customer,
+ program,
+ partner,
+ event: {
+ type: "lead",
+ id: leadData.event_id,
+ },
+ }),
+ });
+ }
+
waitUntil(
sendWorkspaceWebhook({
trigger: "lead.created",
diff --git a/apps/web/lib/integrations/shopify/create-lead.ts b/apps/web/lib/integrations/shopify/create-lead.ts
index bb91946f07..5cb20e9757 100644
--- a/apps/web/lib/integrations/shopify/create-lead.ts
+++ b/apps/web/lib/integrations/shopify/create-lead.ts
@@ -1,3 +1,4 @@
+import { prepareEarnings } from "@/lib/api/earnings/create-earnings";
import { includeTags } from "@/lib/api/links/include-tags";
import { createId } from "@/lib/api/utils";
import { generateRandomName } from "@/lib/names";
@@ -82,6 +83,37 @@ export async function createShopifyLead({
}),
]);
+ if (link.programId) {
+ const { program, ...partner } =
+ await prisma.programEnrollment.findFirstOrThrow({
+ where: {
+ links: {
+ some: {
+ id: link.id,
+ },
+ },
+ },
+ select: {
+ program: true,
+ partnerId: true,
+ commissionAmount: true,
+ },
+ });
+
+ await prisma.earnings.create({
+ data: prepareEarnings({
+ link,
+ customer,
+ program,
+ partner,
+ event: {
+ type: "lead",
+ id: leadData.event_id,
+ },
+ }),
+ });
+ }
+
waitUntil(
sendWorkspaceWebhook({
trigger: "lead.created",