Skip to content

Commit

Permalink
record lead events
Browse files Browse the repository at this point in the history
  • Loading branch information
devkiran committed Feb 4, 2025
1 parent e78f035 commit 49bcdf5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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!,
},
}),
);
Expand Down
32 changes: 32 additions & 0 deletions apps/web/app/api/stripe/integration/webhook/customer-created.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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",
Expand Down
32 changes: 32 additions & 0 deletions apps/web/lib/integrations/shopify/create-lead.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 49bcdf5

Please sign in to comment.