Skip to content
Closed
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
18 changes: 14 additions & 4 deletions apps/web/lib/api/partners/notify-partner-commission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import {
User,
} from "@prisma/client";

// Partners get an email only when the commission earnings reach this amount (in cents)
const MIN_PARTNER_NOTIFICATION_EARNINGS = 1000;

// Send email to partner and program owners when a commission is created
export async function notifyPartnerCommission({
program,
Expand Down Expand Up @@ -51,6 +54,13 @@ export async function notifyPartnerCommission({
// - only for the first commission per partner–customer combination
const shouldNotifyProgram = commission.type === "sale" && isFirstCommission;

const shouldNotifyPartner =
commission.earnings >= MIN_PARTNER_NOTIFICATION_EARNINGS;

if (!shouldNotifyPartner && !shouldNotifyProgram) {
return;
}

const [partner, workspaceUsers, partnerLink] = await Promise.all([
prisma.partner.findUnique({
where: {
Expand Down Expand Up @@ -144,13 +154,13 @@ export async function notifyPartnerCommission({
shortLink: partnerLink?.shortLink ?? null,
};

const partnerEmailsToNotify = partner.users
.map(({ user }) => user.email)
.filter(Boolean) as string[];
const partnerEmailsToNotify = shouldNotifyPartner
? (partner.users.map(({ user }) => user.email).filter(Boolean) as string[])
: [];

// Create all emails first, then chunk them into batches of 100
const allEmails: ResendBulkEmailOptions = [
// Partner emails (for all commission types)
// Partner emails (for all commission types with enough earnings)
...partnerEmailsToNotify.map(
(email) =>
({
Expand Down
Loading