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.

Page connect payout reminders by partner so large runs can resume.

Kiran Kb66592fb3a77
diff --git a/apps/web/lib/jobs/handlers/send-connect-payout-reminders-job.ts b/apps/web/lib/jobs/handlers/send-connect-payout-reminders-job.ts
index f2fdb4564f..9ca5af5f63 100644
--- a/apps/web/lib/jobs/handlers/send-connect-payout-reminders-job.ts
+++ b/apps/web/lib/jobs/handlers/send-connect-payout-reminders-job.ts
@@ -2,18 +2,22 @@ import { sendPayoutReminder } from "@/lib/payouts/send-payout-reminder";
import * as z from "zod/v4";
import { defineJob } from "../index";
-const inputSchema = z.object({});
+const inputSchema = z.object({
+ afterPartnerId: z.string().optional(),
+});
// Sends ConnectPayoutReminder emails to partners with pending payouts
// who have not configured payouts yet.
export const sendConnectPayoutRemindersJob = defineJob({
name: "send-connect-payout-reminders-job",
schema: inputSchema,
- async handle() {
- const hasMore = await sendPayoutReminder();
+ async handle({ afterPartnerId }) {
+ const nextAfterPartnerId = await sendPayoutReminder({ afterPartnerId });
- if (hasMore) {
- await sendConnectPayoutRemindersJob.dispatch({});
+ if (nextAfterPartnerId) {
+ await sendConnectPayoutRemindersJob.dispatch({
+ afterPartnerId: nextAfterPartnerId,
+ });
}
},
});
diff --git a/apps/web/lib/payouts/send-payout-reminder.ts b/apps/web/lib/payouts/send-payout-reminder.ts
index 1cb7e932b1..8a07e5d440 100644
--- a/apps/web/lib/payouts/send-payout-reminder.ts
+++ b/apps/web/lib/payouts/send-payout-reminder.ts
@@ -7,11 +7,12 @@ import { ACME_PROGRAM_ID, DEMO_PROGRAM_ID, pluck } from "@dub/utils";
import {
Partner,
PayoutStatus,
+ Prisma,
Program,
ProgramPayoutMode,
} from "@prisma/client";
-const BATCH_SIZE = 1000;
+const BATCH_SIZE = 100;
const EXCLUDED_PROGRAM_IDS = [
ACME_PROGRAM_ID,
@@ -29,85 +30,114 @@ type PartnerPayoutReminder = {
})[];
};
-// This route is used to send reminders to partners who have pending payouts
-// but haven't configured payouts yet.
-export async function sendPayoutReminder() {
- // Get unsent payouts grouped by partner and program, ordered by amount desc
- const unsentPayouts = await prisma.payout.groupBy({
- by: ["partnerId", "programId"],
- where: {
- status: {
- in: [
- PayoutStatus.pending,
- PayoutStatus.processing,
- PayoutStatus.processed,
- PayoutStatus.failed,
- ],
+function payoutReminderWhere({
+ afterPartnerId,
+ partnerIds,
+}: {
+ afterPartnerId?: string;
+ partnerIds?: string[];
+} = {}): Prisma.PayoutWhereInput {
+ return {
+ status: {
+ in: [
+ PayoutStatus.pending,
+ PayoutStatus.processing,
+ PayoutStatus.processed,
+ PayoutStatus.failed,
+ ],
+ },
+ programId: {
+ notIn: EXCLUDED_PROGRAM_IDS,
+ },
+ ...(afterPartnerId && {
+ partnerId: {
+ gt: afterPartnerId,
},
- programId: {
- notIn: EXCLUDED_PROGRAM_IDS,
+ }),
+ ...(partnerIds && {
+ partnerId: {
+ in: partnerIds,
},
- partner: {
- payoutsEnabledAt: null,
- AND: [
- {
- OR: [
- { country: null },
- {
- country: {
- in: PAYOUT_SUPPORTED_COUNTRIES.map((c) => c.code),
- },
+ }),
+ partner: {
+ payoutsEnabledAt: null,
+ AND: [
+ {
+ OR: [
+ { country: null },
+ {
+ country: {
+ in: PAYOUT_SUPPORTED_COUNTRIES.map((c) => c.code),
},
- ],
- },
- {
- OR: [
- { connectPayoutsLastRemindedAt: null },
- {
- connectPayoutsLastRemindedAt: {
- lte: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000),
- },
+ },
+ ],
+ },
+ {
+ OR: [
+ { connectPayoutsLastRemindedAt: null },
+ {
+ connectPayoutsLastRemindedAt: {
+ lte: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000),
},
- ],
- },
- ],
- },
- amount: {
- gte: MIN_PAYOUT_AMOUNT_FOR_REMINDERS,
- },
- programEnrollment: {
- // Internal programs always pay through Dub, so those partners still need
- // to connect payout details even if a tenantId is set.
- // External and hybrid programs pay outside Dub once tenantId is set, so
- // skip those. Without a tenantId they are still included.
- OR: [
- {
- program: {
- payoutMode: ProgramPayoutMode.internal,
},
- },
- {
- tenantId: null,
- },
- ],
- },
+ ],
+ },
+ ],
},
- _sum: {
- amount: true,
+ amount: {
+ gte: MIN_PAYOUT_AMOUNT_FOR_REMINDERS,
},
+ programEnrollment: {
+ // Internal programs always pay through Dub, so those partners still need
+ // to connect payout details even if a tenantId is set.
+ // External and hybrid programs pay outside Dub once tenantId is set, so
+ // skip those. Without a tenantId they are still included.
+ OR: [
+ {
+ program: {
+ payoutMode: ProgramPayoutMode.internal,
+ },
+ },
+ {
+ tenantId: null,
+ },
+ ],
+ },
+ };
+}
+
+// This route is used to send reminders to partners who have pending payouts
+// but haven't configured payouts yet.
+// Returns the last partner id when another batch should run.
+export async function sendPayoutReminder({
+ afterPartnerId,
+}: {
+ afterPartnerId?: string;
+} = {}) {
+ const partners = await prisma.payout.groupBy({
+ by: ["partnerId"],
+ where: payoutReminderWhere({ afterPartnerId }),
orderBy: {
- _sum: {
- amount: "desc",
- },
+ partnerId: "asc",
},
take: BATCH_SIZE,
});
- if (!unsentPayouts.length) {
+ if (!partners.length) {
console.log("No partners need reminders.");
- return false;
+ return;
}
+ const partnerIds = pluck(partners, "partnerId");
+
+ const unsentPayouts = await prisma.payout.groupBy({
+ by: ["partnerId", "programId"],
+ where: payoutReminderWhere({ partnerIds }),
+ _sum: {
+ amount: true,
+ },
+ });
+
const [partnerData, programData] = await Promise.all([
prisma.partner.findMany({
where: {
@@ -211,7 +241,10 @@ export async function sendPayoutReminder() {
},
});
- const hasMore = unsentPayouts.length === BATCH_SIZE;
+ if (partners.length < BATCH_SIZE) {
+ console.log("No more partners to remind.");
+ return;
+ }
- return hasMore;
+ return partnerIds[partnerIds.length - 1];
}
diff --git a/apps/web/tests/payouts/send-payout-reminder.test.ts b/apps/web/tests/payouts/send-payout-reminder.test.ts
index 4e03ada70c..c7c28f6bb9 100644
--- a/apps/web/tests/payouts/send-payout-reminder.test.ts
+++ b/apps/web/tests/payouts/send-payout-reminder.test.ts
@@ -5,7 +5,7 @@ import { ACME_PROGRAM_ID, DEMO_PROGRAM_ID } from "@dub/utils";
import { PayoutStatus, ProgramPayoutMode } from "@prisma/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-const BATCH_SIZE = 1000;
+const BATCH_SIZE = 500;
const MIGRATION_PROGRAM_ID = "prog_1M1EYH84K0ZGRA70CEGB4VC72";
const NOW = new Date("2026-09-24T09:00:00.000Z");
@@ -234,8 +234,10 @@ describe("sendPayoutReminder", () => {
vi.useRealTimers();
});
- it("returns false without emailing when no payouts match", async () => {
- await expect(sendPayoutReminder()).resolves.toBe(false);
+ it("returns without emailing when no payouts match", async () => {
+ await expect(sendPayoutReminder()).resolves.toBeUndefined();
+
+ expect(mocks.groupBy).toHaveBeenCalledOnce();
expect(mocks.partnerFindMany).not.toHaveBeenCalled();
expect(mocks.queueBatchEmail).not.toHaveBeenCalled();
@@ -411,23 +413,25 @@ describe("sendPayoutReminder", () => {
);
it("sends one reminder per partner and records when they were reminded", async () => {
- mocks.groupBy.mockResolvedValue([
- payoutGroup({
- partnerId: "pn_1",
- programId: "prog_a",
- amount: 2500,
- }),
- payoutGroup({
- partnerId: "pn_1",
- programId: "prog_b",
- amount: 1500,
- }),
- payoutGroup({
- partnerId: "pn_2",
- programId: "prog_a",
- amount: 1000,
- }),
- ]);
+ mocks.groupBy
+ .mockResolvedValueOnce([{ partnerId: "pn_1" }, { partnerId: "pn_2" }])
+ .mockResolvedValueOnce([
+ payoutGroup({
+ partnerId: "pn_1",
+ programId: "prog_a",
+ amount: 2500,
+ }),
+ payoutGroup({
+ partnerId: "pn_1",
+ programId: "prog_b",
+ amount: 1500,
+ }),
+ payoutGroup({
+ partnerId: "pn_2",
+ programId: "prog_a",
+ amount: 1000,
+ }),
+ ]);
mocks.partnerFindMany.mockResolvedValue([
partner({ id: "pn_1", email: "[email protected]" }),
partner({ id: "pn_2", email: "[email protected]" }),
@@ -437,8 +441,25 @@ describe("sendPayoutReminder", () => {
program({ id: "prog_b" }),
]);
- await expect(sendPayoutReminder()).resolves.toBe(false);
+ await expect(sendPayoutReminder()).resolves.toBeUndefined();
+ expect(mocks.groupBy).toHaveBeenNthCalledWith(
+ 1,
+ expect.objectContaining({
+ by: ["partnerId"],
+ orderBy: { partnerId: "asc" },
+ take: BATCH_SIZE,
+ }),
+ );
+ expect(mocks.groupBy).toHaveBeenNthCalledWith(
+ 2,
+ expect.objectContaining({
+ by: ["partnerId", "programId"],
+ where: expect.objectContaining({
+ partnerId: { in: ["pn_1", "pn_2"] },
+ }),
+ }),
+ );
expect(mocks.partnerFindMany).toHaveBeenCalledWith({
where: {
id: {
@@ -534,30 +555,36 @@ describe("sendPayoutReminder", () => {
});
it("skips partners with no email and partners excluded by notification preferences", async () => {
- mocks.groupBy.mockResolvedValue([
- payoutGroup({
- partnerId: "pn_no_email",
- programId: "prog_a",
- amount: 2000,
- }),
- payoutGroup({
- partnerId: "pn_opted_out",
- programId: "prog_a",
- amount: 2000,
- }),
- payoutGroup({
- partnerId: "pn_ok",
- programId: "prog_a",
- amount: 2000,
- }),
- ]);
+ mocks.groupBy
+ .mockResolvedValueOnce([
+ { partnerId: "pn_no_email" },
+ { partnerId: "pn_opted_out" },
+ { partnerId: "pn_ok" },
+ ])
+ .mockResolvedValueOnce([
+ payoutGroup({
+ partnerId: "pn_no_email",
+ programId: "prog_a",
+ amount: 2000,
+ }),
+ payoutGroup({
+ partnerId: "pn_opted_out",
+ programId: "prog_a",
+ amount: 2000,
+ }),
+ payoutGroup({
+ partnerId: "pn_ok",
+ programId: "prog_a",
+ amount: 2000,
+ }),
+ ]);
mocks.partnerFindMany.mockResolvedValue([
partner({ id: "pn_no_email", email: null }),
partner({ id: "pn_ok", email: "[email protected]" }),
]);
mocks.programFindMany.mockResolvedValue([program({ id: "prog_a" })]);
- await expect(sendPayoutReminder()).resolves.toBe(false);
+ await expect(sendPayoutReminder()).resolves.toBeUndefined();
expect(mocks.queueBatchEmail).toHaveBeenCalledWith([
expect.objectContaining({
@@ -576,21 +603,38 @@ describe("sendPayoutReminder", () => {
});
});
- it("returns true when the batch is full so another run can be scheduled", async () => {
- mocks.groupBy.mockResolvedValue(
- Array.from({ length: BATCH_SIZE }, (_, index) =>
- payoutGroup({
- partnerId: `pn_${index}`,
- programId: "prog_a",
- amount: 1000,
- }),
- ),
- );
+ it("returns the last partner id when the batch is full", async () => {
+ const partnerIds = Array.from({ length: BATCH_SIZE }, (_, index) => ({
+ partnerId: `pn_${index}`,
+ }));
+
+ mocks.groupBy.mockResolvedValueOnce(partnerIds).mockResolvedValueOnce([
+ payoutGroup({
+ partnerId: "pn_0",
+ programId: "prog_a",
+ amount: 1000,
+ }),
+ ]);
mocks.partnerFindMany.mockResolvedValue([
partner({ id: "pn_0", email: "[email protected]" }),
]);
mocks.programFindMany.mockResolvedValue([program({ id: "prog_a" })]);
- await expect(sendPayoutReminder()).resolves.toBe(true);
+ await expect(sendPayoutReminder()).resolves.toBe("pn_499");
+ });
+
+ it("continues after the partner id from the previous batch", async () => {
+ await sendPayoutReminder({ afterPartnerId: "pn_10" });
+
+ expect(mocks.groupBy).toHaveBeenCalledWith(
+ expect.objectContaining({
+ by: ["partnerId"],
+ where: expect.objectContaining({
+ partnerId: {
+ gt: "pn_10",
+ },
+ }),
+ }),
+ );
});
});