Skip to content

Commit

Permalink
Backfill sales for the partner's link
Browse files Browse the repository at this point in the history
  • Loading branch information
devkiran committed Nov 15, 2024
1 parent 716b59b commit 0785bc5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
paymentProcessor: saleData.payment_processor,
amount: saleData.amount,
currency: saleData.currency,
programEnrollment,
partnerId: programEnrollment.partnerId,
program: programEnrollment.program,
metadata: {
...leadEvent.data[0],
stripeMetadata: charge,
Expand Down
3 changes: 2 additions & 1 deletion apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ export async function invoicePaid(event: Stripe.Event) {
paymentProcessor: saleData.payment_processor,
amount: saleData.amount,
currency: saleData.currency,
programEnrollment,
partnerId: programEnrollment.partnerId,
program: programEnrollment.program,
metadata: {
...leadEvent.data[0],
stripeMetadata: invoice,
Expand Down
3 changes: 2 additions & 1 deletion apps/web/app/api/track/sale/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ export const POST = withWorkspaceEdge(
paymentProcessor,
amount,
currency,
programEnrollment,
partnerId: programEnrollment.partnerId,
program: programEnrollment.program,
metadata: clickData,
}),
}),
Expand Down
45 changes: 45 additions & 0 deletions apps/web/lib/actions/partners/accept-program-invite.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"use server";

import { getEvents } from "@/lib/analytics/get-events";
import { createSaleData } from "@/lib/api/sales/sale";
import { createDotsUser } from "@/lib/dots/create-dots-user";
import { retrieveDotsUser } from "@/lib/dots/retrieve-dots-user";
import { prisma } from "@/lib/prisma";
import { saleEventResponseSchema } from "@/lib/zod/schemas/sales";
import { z } from "zod";
import { authPartnerActionClient } from "../safe-action";

Expand All @@ -28,6 +31,7 @@ export const acceptProgramInviteAction = authPartnerActionClient
select: {
workspace: {
select: {
id: true,
dotsAppId: true,
},
},
Expand All @@ -45,6 +49,9 @@ export const acceptProgramInviteAction = authPartnerActionClient
partnerId: partner.id,
status: "approved",
},
include: {
program: true,
},
}),

retrieveDotsUser({
Expand Down Expand Up @@ -78,4 +85,42 @@ export const acceptProgramInviteAction = authPartnerActionClient
data: { dotsUserId: newDotsUser.id },
});
}

// Backfill sales for the partner's link
const saleEvents = await getEvents({
workspaceId: workspace.id,
linkId: programInvite.linkId,
event: "sales",
interval: "all",
page: 1,
limit: 5000,
order: "desc",
sortBy: "timestamp",
});

const data = saleEvents.map(
(e: z.infer<typeof saleEventResponseSchema>) => ({
...createSaleData({
customerId: e.customer.id,
linkId: e.link.id,
clickId: e.click.id,
invoiceId: e.invoice_id,
eventId: e.eventId,
paymentProcessor: e.payment_processor,
amount: e.sale.amount,
currency: "usd",
partnerId: partner.id,
program: programEnrollment.program,
metadata: e.click,
}),
createdAt: new Date(e.timestamp),
}),
);

if (data.length > 0) {
await prisma.sale.createMany({
data,
skipDuplicates: true,
});
}
});
10 changes: 5 additions & 5 deletions apps/web/lib/api/sales/sale.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Prisma, Program, ProgramEnrollment, SaleStatus } from "@prisma/client";
import { Prisma, Program, SaleStatus } from "@prisma/client";
import { createId } from "../utils";
import { calculateEarnings } from "./commission";

Expand All @@ -11,7 +11,8 @@ export const createSaleData = ({
paymentProcessor,
amount,
currency,
programEnrollment,
partnerId,
program,
metadata,
}: {
customerId: string;
Expand All @@ -22,11 +23,10 @@ export const createSaleData = ({
paymentProcessor: string;
amount: number;
currency: string;
programEnrollment: ProgramEnrollment & { program: Program };
partnerId: string;
program: Program;
metadata: Record<string, any>;
}) => {
const { program, partnerId } = programEnrollment;

const earnings = calculateEarnings({
program,
sales: 1,
Expand Down
6 changes: 2 additions & 4 deletions apps/web/scripts/backfill-sales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ async function main() {
paymentProcessor: e.payment_processor,
amount: e.sale.amount,
currency: "usd",
programEnrollment: {
program,
partnerId,
} as any,
partnerId,
program,
metadata: e.click,
}),
createdAt: new Date(e.timestamp),
Expand Down

0 comments on commit 0785bc5

Please sign in to comment.