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.

Dedupe connect payout reminder emails by day and batch number.

Kiran K36caf3f2d1da
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 9ca5af5f63..066bc706f4 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
@@ -4,6 +4,7 @@ import { defineJob } from "../index";
const inputSchema = z.object({
afterPartnerId: z.string().optional(),
+ batchNumber: z.number().int().positive().default(1),
});
// Sends ConnectPayoutReminder emails to partners with pending payouts
@@ -11,12 +12,16 @@ const inputSchema = z.object({
export const sendConnectPayoutRemindersJob = defineJob({
name: "send-connect-payout-reminders-job",
schema: inputSchema,
- async handle({ afterPartnerId }) {
- const nextAfterPartnerId = await sendPayoutReminder({ afterPartnerId });
+ async handle({ afterPartnerId, batchNumber }) {
+ const nextAfterPartnerId = await sendPayoutReminder({
+ afterPartnerId,
+ batchNumber,
+ });
if (nextAfterPartnerId) {
await sendConnectPayoutRemindersJob.dispatch({
afterPartnerId: nextAfterPartnerId,
+ batchNumber: batchNumber + 1,
});
}
},
diff --git a/apps/web/lib/payouts/send-payout-reminder.ts b/apps/web/lib/payouts/send-payout-reminder.ts
index 8a07e5d440..587b5b0008 100644
--- a/apps/web/lib/payouts/send-payout-reminder.ts
+++ b/apps/web/lib/payouts/send-payout-reminder.ts
@@ -1,3 +1,4 @@
+import { getUtcPeriodDate } from "@/lib/api/rewards/custom-reward-utils";
import { MIN_PAYOUT_AMOUNT_FOR_REMINDERS } from "@/lib/constants/misc";
import { PAYOUT_SUPPORTED_COUNTRIES } from "@/lib/constants/payouts-supported-countries";
import { queueBatchEmail } from "@/lib/email/queue-batch-email";
@@ -12,7 +13,7 @@ import {
ProgramPayoutMode,
} from "@prisma/client";
-const BATCH_SIZE = 100;
+const BATCH_SIZE = 500;
const EXCLUDED_PROGRAM_IDS = [
ACME_PROGRAM_ID,
@@ -111,8 +112,10 @@ function payoutReminderWhere({
// Returns the last partner id when another batch should run.
export async function sendPayoutReminder({
afterPartnerId,
+ batchNumber = 1,
}: {
afterPartnerId?: string;
+ batchNumber?: number;
} = {}) {
const partners = await prisma.payout.groupBy({
by: ["partnerId"],
@@ -216,6 +219,7 @@ export async function sendPayoutReminder({
const partnerPrograms = Array.from(partnerProgramMap.values());
const connectPayoutsLastRemindedAt = new Date();
+ const idempotencyKey = `payout-reminders-${getUtcPeriodDate()}-${batchNumber}`;
await queueBatchEmail<typeof ConnectPayoutReminder>(
partnerPrograms.map(({ partner, programs }) => ({
@@ -228,6 +232,9 @@ export async function sendPayoutReminder({
programs,
},
})),
+ {
+ idempotencyKey,
+ },
);
await prisma.partner.updateMany({
diff --git a/apps/web/tests/payouts/send-payout-reminder.test.ts b/apps/web/tests/payouts/send-payout-reminder.test.ts
index c7c28f6bb9..822f099a7d 100644
--- a/apps/web/tests/payouts/send-payout-reminder.test.ts
+++ b/apps/web/tests/payouts/send-payout-reminder.test.ts
@@ -500,48 +500,53 @@ describe("sendPayoutReminder", () => {
logo: true,
},
});
- expect(mocks.queueBatchEmail).toHaveBeenCalledWith([
- {
- variant: "notifications",
- subject: "Connect your payout details on Dub Partners",
- templateName: "ConnectPayoutReminder",
- templateProps: {
- email: "[email protected]",
- programs: [
- {
- id: "prog_a",
- name: "Program prog_a",
- logo: "https://example.com/prog_a.png",
- amount: 2500,
- },
- {
- id: "prog_b",
- name: "Program prog_b",
- logo: "https://example.com/prog_b.png",
- amount: 1500,
- },
- ],
+ expect(mocks.queueBatchEmail).toHaveBeenCalledWith(
+ [
+ {
+ variant: "notifications",
+ subject: "Connect your payout details on Dub Partners",
+ templateName: "ConnectPayoutReminder",
+ templateProps: {
+ email: "[email protected]",
+ programs: [
+ {
+ id: "prog_a",
+ name: "Program prog_a",
+ logo: "https://example.com/prog_a.png",
+ amount: 2500,
+ },
+ {
+ id: "prog_b",
+ name: "Program prog_b",
+ logo: "https://example.com/prog_b.png",
+ amount: 1500,
+ },
+ ],
+ },
},
- },
- {
- variant: "notifications",
- subject: "Connect your payout details on Dub Partners",
- templateName: "ConnectPayoutReminder",
- templateProps: {
- email: "[email protected]",
- programs: [
- {
- id: "prog_a",
- name: "Program prog_a",
- logo: "https://example.com/prog_a.png",
- amount: 1000,
- },
- ],
+ {
+ variant: "notifications",
+ subject: "Connect your payout details on Dub Partners",
+ templateName: "ConnectPayoutReminder",
+ templateProps: {
+ email: "[email protected]",
+ programs: [
+ {
+ id: "prog_a",
+ name: "Program prog_a",
+ logo: "https://example.com/prog_a.png",
+ amount: 1000,
+ },
+ ],
+ },
},
+ ],
+ {
+ idempotencyKey: "payout-reminders-2026-09-24-1",
},
- ]);
+ );
expect(mocks.partnerUpdateMany).toHaveBeenCalledWith({
where: {
id: {
@@ -586,11 +591,16 @@ describe("sendPayoutReminder", () => {
await expect(sendPayoutReminder()).resolves.toBeUndefined();
- expect(mocks.queueBatchEmail).toHaveBeenCalledWith([
- expect.objectContaining({
- }),
- ]);
+ expect(mocks.queueBatchEmail).toHaveBeenCalledWith(
+ [
+ expect.objectContaining({
+ }),
+ ],
+ {
+ idempotencyKey: "payout-reminders-2026-09-24-1",
+ },
+ );
expect(mocks.partnerUpdateMany).toHaveBeenCalledWith({
where: {
id: {
@@ -624,7 +634,21 @@ describe("sendPayoutReminder", () => {
});
it("continues after the partner id from the previous batch", async () => {
- await sendPayoutReminder({ afterPartnerId: "pn_10" });
+ mocks.groupBy
+ .mockResolvedValueOnce([{ partnerId: "pn_11" }])
+ .mockResolvedValueOnce([
+ payoutGroup({
+ partnerId: "pn_11",
+ programId: "prog_a",
+ amount: 1000,
+ }),
+ ]);
+ mocks.partnerFindMany.mockResolvedValue([
+ partner({ id: "pn_11", email: "[email protected]" }),
+ ]);
+ mocks.programFindMany.mockResolvedValue([program({ id: "prog_a" })]);
+
+ await sendPayoutReminder({ afterPartnerId: "pn_10", batchNumber: 2 });
expect(mocks.groupBy).toHaveBeenCalledWith(
expect.objectContaining({
@@ -636,5 +660,8 @@ describe("sendPayoutReminder", () => {
}),
}),
);
+ expect(mocks.queueBatchEmail).toHaveBeenCalledWith(expect.any(Array), {
+ idempotencyKey: "payout-reminders-2026-09-24-2",
+ });
});
});