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.

determine reward eligibility

Steven Teyb740c092e130
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 aabf92f928..11c914c311 100644
--- a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
+++ b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
@@ -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"
@@ -126,18 +127,30 @@ export async function invoicePaid(event: Stripe.Event) {
let eligibleForCommission = true;
if (typeof reward.maxDuration === "number") {
- const commissionCount = await prisma.commission.count({
+ // 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 && commissionCount > 0) {
- eligibleForCommission = false;
- } else if (commissionCount >= reward.maxDuration) {
+ 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;
+ }
}
}
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index e96e91e451..0197290ef0 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -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";
@@ -158,18 +159,30 @@ export const POST = withWorkspaceEdge(
let eligibleForCommission = true;
if (typeof reward.maxDuration === "number") {
- const commissionCount = await prismaEdge.commission.count({
+ // 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",
+ },
});
- if (reward.maxDuration === 0 && commissionCount > 0) {
- eligibleForCommission = false;
- } else if (commissionCount >= reward.maxDuration) {
+ 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;
+ }
}
}