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.

Use Resend batch endpoint

Steven Tey1f711038228c
diff --git a/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts b/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts
index 9e175ba510..101139a29d 100644
--- a/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/confirm/confirm-payouts.ts
@@ -1,14 +1,15 @@
import { createId } from "@/lib/api/create-id";
-import { limiter } from "@/lib/cron/limiter";
import { PAYOUT_FEES } from "@/lib/partners/constants";
import {
CUTOFF_PERIOD,
CUTOFF_PERIOD_TYPES,
} from "@/lib/partners/cutoff-period";
import { stripe } from "@/lib/stripe";
-import { sendEmail } from "@dub/email";
+import { resend } from "@dub/email/resend";
+import { VARIANT_TO_FROM_MAP } from "@dub/email/resend/constants";
import PartnerPayoutConfirmed from "@dub/email/templates/partner-payout-confirmed";
import { prisma } from "@dub/prisma";
+import { chunk, log } from "@dub/utils";
import { Program, Project } from "@prisma/client";
const allowedPaymentMethods = ["us_bank_account", "card", "link"];
@@ -156,28 +157,38 @@ export async function confirmPayouts({
// Send emails to all the partners involved in the payouts if the payout method is ACH
// This is because ACH takes 4 business days to process, so we want to give partners a heads up
if (newInvoice && paymentMethod.type === "us_bank_account") {
- await Promise.allSettled(
- payouts
- .filter((payout) => payout.partner.email)
- .map((payout) =>
- limiter.schedule(() =>
- sendEmail({
- subject: "You've got money coming your way!",
- email: payout.partner.email!,
- react: PartnerPayoutConfirmed({
- email: payout.partner.email!,
- program,
- payout: {
- id: payout.id,
- amount: payout.amount,
- startDate: payout.periodStart,
- endDate: payout.periodEnd,
- },
- }),
- variant: "notifications",
- }),
- ),
- ),
+ if (!resend) {
+ // this should never happen, but just in case
+ await log({
+ message: "Resend is not configured, skipping email sending.",
+ type: "errors",
+ });
+ console.log("Resend is not configured, skipping email sending.");
+ return;
+ }
+
+ const payoutChunks = chunk(
+ payouts.filter((payout) => payout.partner.email),
+ 100,
);
+ for (const payoutChunk of payoutChunks) {
+ await resend!.batch.send(
+ payoutChunk.map((payout) => ({
+ from: VARIANT_TO_FROM_MAP.notifications,
+ to: [payout.partner.email!],
+ subject: "You've got money coming your way!",
+ react: PartnerPayoutConfirmed({
+ email: payout.partner.email!,
+ program,
+ payout: {
+ id: payout.id,
+ amount: payout.amount,
+ startDate: payout.periodStart,
+ endDate: payout.periodEnd,
+ },
+ }),
+ })),
+ );
+ }
}
}