Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,21 @@ export async function sendPaypalPayouts(invoice: Pick<Invoice, "id">) {
paidAt: new Date(),
},
});

console.log(`Updated ${updatedPayouts.count} payouts to "sent" status`);

const batchEmails = await sendBatchEmail(
payouts
.filter((payout) => payout.partner.email)
.map((payout) => ({
variant: "notifications",
to: payout.partner.email!,
subject: `You've received a ${currencyFormatter(payout.amount)} payout from ${payout.program.name}`,
react: PartnerPayoutProcessed({
email: payout.partner.email!,
program: payout.program,
payout,
variant: "paypal",
}),
})),
payouts.map((payout) => ({
variant: "notifications",
to: payout.partner.email!,
subject: `You've received a ${currencyFormatter(payout.amount)} payout from ${payout.program.name}`,
react: PartnerPayoutProcessed({
email: payout.partner.email!,
program: payout.program,
payout,
variant: "paypal",
}),
})),
);

console.log("Resend batch emails sent", JSON.stringify(batchEmails, null, 2));
Expand Down
36 changes: 17 additions & 19 deletions apps/web/lib/actions/partners/bulk-ban-partners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,23 @@ export const bulkBanPartnersAction = authActionClient
});

await sendBatchEmail(
programEnrollments
.filter(({ partner }) => partner.email)
.map(({ partner }) => ({
to: partner.email!,
subject: `You've been banned from the ${program.name} Partner Program`,
variant: "notifications",
replyTo: program.supportEmail || "noreply",
react: PartnerBanned({
partner: {
name: partner.name,
email: partner.email!,
},
program: {
name: program.name,
slug: program.slug,
},
bannedReason: BAN_PARTNER_REASONS[parsedInput.reason],
}),
})),
programEnrollments.map(({ partner }) => ({
to: partner.email!,
subject: `You've been banned from the ${program.name} Partner Program`,
variant: "notifications",
replyTo: program.supportEmail || "noreply",
react: PartnerBanned({
partner: {
name: partner.name,
email: partner.email!,
},
program: {
name: program.name,
slug: program.slug,
},
bannedReason: BAN_PARTNER_REASONS[parsedInput.reason],
}),
})),
);
})(),
);
Expand Down
15 changes: 4 additions & 11 deletions apps/web/lib/api/domains/claim-dot-link-domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { createLink } from "@/lib/api/links";
import { registerDomain } from "@/lib/dynadot/register-domain";
import { WorkspaceWithUsers } from "@/lib/types";
import { sendBatchEmail } from "@dub/email";
import { ResendBulkEmailOptions } from "@dub/email/resend/types";
import DomainClaimed from "@dub/email/templates/domain-claimed";
import { prisma } from "@dub/prisma";
import { DEFAULT_LINK_PROPS } from "@dub/utils";
Expand Down Expand Up @@ -175,9 +174,8 @@ export const sendDomainClaimedEmails = async ({
},
});

const emails: ResendBulkEmailOptions = workspaceWithOwner.users
.filter(({ user }) => user.email)
.map(({ user }) => ({
return await sendBatchEmail(
workspaceWithOwner.users.map(({ user }) => ({
variant: "notifications",
to: user.email!,
subject: "Successfully claimed your .link domain!",
Expand All @@ -186,11 +184,6 @@ export const sendDomainClaimedEmails = async ({
domain,
workspaceSlug: workspace.slug,
}),
}));

if (emails.length > 0) {
return await sendBatchEmail(emails);
}

return null;
})),
);
};
6 changes: 3 additions & 3 deletions packages/email/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export const sendEmail = async (opts: ResendEmailOptions) => {
};

export const sendBatchEmail = async (
payload: ResendBulkEmailOptions,
emails: ResendBulkEmailOptions,
options?: { idempotencyKey?: string },
) => {
if (resend) {
return await sendBatchEmailViaResend(payload, options);
return await sendBatchEmailViaResend(emails, options);
}

// Fallback to SMTP if Resend is not configured
Expand All @@ -43,7 +43,7 @@ export const sendBatchEmail = async (

if (smtpConfigured) {
await Promise.all(
payload.map((p) =>
emails.map((p) =>
sendViaNodeMailer({
to: p.to,
subject: p.subject,
Expand Down
28 changes: 24 additions & 4 deletions packages/email/src/send-via-resend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const sendEmailViaResend = async (opts: ResendEmailOptions) => {
};

export const sendBatchEmailViaResend = async (
opts: ResendBulkEmailOptions,
emails: ResendBulkEmailOptions,
options?: { idempotencyKey?: string },
) => {
if (!resend) {
Expand All @@ -69,19 +69,39 @@ export const sendBatchEmailViaResend = async (
};
}

if (opts.length === 0) {
if (emails.length === 0) {
return {
data: null,
error: null,
};
}

const payload = opts.map(resendEmailForOptions);
// Filter out emails without to address
// and format the emails for Resend
const filteredBatch = emails.reduce(
(acc, email) => {
if (!email?.to) {
return acc;
}

acc.push(resendEmailForOptions(email));

return acc;
},
[] as ReturnType<typeof resendEmailForOptions>[],
);

if (filteredBatch.length === 0) {
return {
data: null,
error: null,
};
}

const idempotencyKey = options?.idempotencyKey || undefined;

return await resend.batch.send(
payload,
filteredBatch,
idempotencyKey ? { idempotencyKey } : undefined,
);
};