Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle cases where the sale reward is a one-time reward #2065

Merged
merged 5 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 83 additions & 50 deletions apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { transformSaleEventData } from "@/lib/webhook/transform";
import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { differenceInMonths } from "date-fns";
import type Stripe from "stripe";

// Handle event "invoice.paid"
Expand Down Expand Up @@ -123,56 +124,88 @@ export async function invoicePaid(event: Stripe.Event) {
});

if (reward) {
const earnings = calculateSaleEarnings({
reward,
sale: {
quantity: 1,
amount: saleData.amount,
},
});

await prisma.commission.create({
data: {
id: createId({ prefix: "cm_" }),
programId: link.programId,
linkId: link.id,
partnerId: link.partnerId,
eventId,
customerId: customer.id,
quantity: 1,
type: "sale",
amount: saleData.amount,
earnings,
invoiceId,
},
});

waitUntil(
(async () => {
const program = await prisma.program.findUniqueOrThrow({
where: {
id: link.programId!,
},
select: {
id: true,
name: true,
logo: true,
},
});

await notifyPartnerSale({
program,
partner: {
id: link.partnerId!,
referralLink: link.shortLink,
},
sale: {
amount: saleData.amount,
earnings,
},
});
})(),
);
let eligibleForCommission = true;

if (typeof reward.maxDuration === "number") {
// Get the first commission (earliest sale) for this customer-partner pair
const firstCommission = await prisma.commission.findFirst({
where: {
partnerId: link.partnerId,
customerId: customer.id,
type: "sale",
},
orderBy: {
createdAt: "asc",
},
});

if (reward.maxDuration === 0 && firstCommission) {
eligibleForCommission = false;
} else if (firstCommission) {
// Calculate months difference between first commission and now
const monthsDifference = differenceInMonths(
new Date(),
firstCommission.createdAt,
);

if (monthsDifference >= reward.maxDuration) {
eligibleForCommission = false;
}
}
}

if (eligibleForCommission) {
const earnings = calculateSaleEarnings({
reward,
sale: {
quantity: 1,
amount: saleData.amount,
},
});

await prisma.commission.create({
data: {
id: createId({ prefix: "cm_" }),
programId: link.programId,
linkId: link.id,
partnerId: link.partnerId,
eventId,
customerId: customer.id,
quantity: 1,
type: "sale",
amount: saleData.amount,
earnings,
invoiceId,
},
});

waitUntil(
(async () => {
const program = await prisma.program.findUniqueOrThrow({
where: {
id: link.programId!,
},
select: {
id: true,
name: true,
logo: true,
},
});

await notifyPartnerSale({
program,
partner: {
id: link.partnerId!,
referralLink: link.shortLink,
},
sale: {
amount: saleData.amount,
earnings,
},
});
})(),
);
}
}
}

Expand Down
121 changes: 77 additions & 44 deletions apps/web/app/api/track/sale/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { prismaEdge } from "@dub/prisma/edge";
import { nanoid } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { differenceInMonths } from "date-fns";
import { NextResponse } from "next/server";
import { determinePartnerReward } from "../determine-partner-reward-edge";

Expand Down Expand Up @@ -155,52 +156,84 @@ export const POST = withWorkspaceEdge(
});

if (reward) {
const earnings = calculateSaleEarnings({
reward,
sale: {
quantity: 1,
amount: saleData.amount,
},
});

await prismaEdge.commission.create({
data: {
id: createId({ prefix: "cm_" }),
programId: link.programId,
linkId: link.id,
partnerId: link.partnerId,
eventId,
customerId: customer.id,
quantity: 1,
type: "sale",
amount: saleData.amount,
earnings,
invoiceId,
},
});
let eligibleForCommission = true;

const program = await prismaEdge.program.findUniqueOrThrow({
where: {
id: link.programId!,
},
select: {
id: true,
name: true,
logo: true,
},
});
if (typeof reward.maxDuration === "number") {
// Get the first commission (earliest sale) for this customer-partner pair
const firstCommission = await prismaEdge.commission.findFirst({
where: {
partnerId: link.partnerId,
customerId: customer.id,
type: "sale",
},
orderBy: {
createdAt: "asc",
},
});

await notifyPartnerSale({
program,
partner: {
id: link.partnerId!,
referralLink: link.shortLink,
},
sale: {
amount: saleData.amount,
earnings,
},
});
if (reward.maxDuration === 0 && firstCommission) {
eligibleForCommission = false;
} else if (firstCommission) {
// Calculate months difference between first commission and now
const monthsDifference = differenceInMonths(
new Date(),
firstCommission.createdAt,
);

if (monthsDifference >= reward.maxDuration) {
eligibleForCommission = false;
}
}
}

if (eligibleForCommission) {
const earnings = calculateSaleEarnings({
reward,
sale: {
quantity: 1,
amount: saleData.amount,
},
});

await prismaEdge.commission.create({
data: {
id: createId({ prefix: "cm_" }),
programId: link.programId,
linkId: link.id,
partnerId: link.partnerId,
eventId,
customerId: customer.id,
quantity: 1,
type: "sale",
amount: saleData.amount,
earnings,
invoiceId,
},
});

const program = await prismaEdge.program.findUniqueOrThrow({
where: {
id: link.programId!,
},
select: {
id: true,
name: true,
logo: true,
},
});

await notifyPartnerSale({
program,
partner: {
id: link.partnerId!,
referralLink: link.shortLink,
},
sale: {
amount: saleData.amount,
earnings,
},
});
}
}
}

Expand Down
Loading