The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
make periodStart and periodEnd optional but infer them from sales dates
Steven Tey3846f103c464
diff --git a/apps/web/app/api/cron/process-payouts/route.ts b/apps/web/app/api/cron/process-payouts/route.tsindex e830e49242..c4eb0acfb9 100644--- a/apps/web/app/api/cron/process-payouts/route.ts+++ b/apps/web/app/api/cron/process-payouts/route.ts@@ -24,28 +24,12 @@ export async function GET(req: Request) {return;}- const currentDate = new Date();-- const periodStart = new Date(- currentDate.getFullYear(),- currentDate.getMonth(),- 1,- );-- const periodEnd = new Date(- currentDate.getFullYear(),- currentDate.getMonth() + 1,- 0,- );-// TODO:// We need a batter way to handle this recursivelyfor (const { programId, partnerId } of partners) {await createSalesPayout({programId,partnerId,- periodStart,- periodEnd,});}diff --git a/apps/web/lib/actions/partners/create-manual-payout.ts b/apps/web/lib/actions/partners/create-manual-payout.tsindex 777ae77421..678d41e009 100644--- a/apps/web/lib/actions/partners/create-manual-payout.ts+++ b/apps/web/lib/actions/partners/create-manual-payout.ts@@ -57,8 +57,8 @@ export const createManualPayoutAction = authActionClientpayout = await createSalesPayout({programId,partnerId,- periodStart: start!,- periodEnd: end!,+ periodStart: start,+ periodEnd: end,description: description ?? undefined,});}diff --git a/apps/web/lib/partners/create-sales-payout.ts b/apps/web/lib/partners/create-sales-payout.tsindex f77181c28c..d1ce371a03 100644--- a/apps/web/lib/partners/create-sales-payout.ts+++ b/apps/web/lib/partners/create-sales-payout.ts@@ -12,8 +12,8 @@ export const createSalesPayout = async ({}: {programId: string;partnerId: string;- periodStart: Date;- periodEnd: Date;+ periodStart?: Date;+ periodEnd?: Date;description?: string;}) => {return await prisma.$transaction(async (tx) => {@@ -31,6 +31,7 @@ export const createSalesPayout = async ({select: {id: true,earnings: true,+ createdAt: true,},});@@ -52,6 +53,20 @@ export const createSalesPayout = async ({},});+ if (!periodEnd) {+ // get the end of the month of the latest sale+ // e.g. if the latest sale is 2024-12-16, the periodEnd should be 2024-12-31+ const latestSale = sales.reduce(+ (max, sale) => (sale.createdAt > max ? sale.createdAt : max),+ sales[0].createdAt,+ );+ periodEnd = new Date(+ latestSale.getFullYear(),+ latestSale.getMonth() + 1,+ 0,+ );+ }+// Update the existing payoutif (payout) {await tx.payout.update({@@ -74,6 +89,14 @@ export const createSalesPayout = async ({// Create the payoutelse {+ if (!periodStart) {+ // get the earliest sale date+ periodStart = sales.reduce(+ (min, sale) => (sale.createdAt < min ? sale.createdAt : min),+ sales[0].createdAt,+ );+ }+payout = await tx.payout.create({data: {id: createId({ prefix: "po_" }),