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.

Add program owner sale notification emails

Kiran Kda40b63ba944
diff --git a/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts
index 7a2e43e2d7..0c6edd8c71 100644
--- a/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts
+++ b/apps/web/app/api/stripe/integration/webhook/checkout-session-completed.ts
@@ -1,6 +1,7 @@
import { createId } from "@/lib/api/create-id";
import { includeTags } from "@/lib/api/links/include-tags";
import { sendPartnerSaleNotification } from "@/lib/api/partners/partner-sale-notification";
+import { sendProgramOwnerSaleNotification } from "@/lib/api/partners/program-owner-sale-notification";
import { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings";
import { determinePartnerReward } from "@/lib/partners/determine-partner-reward";
import {
@@ -313,7 +314,7 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
},
});
- await sendPartnerSaleNotification({
+ const emailData = {
program,
partner: {
id: link.partnerId!,
@@ -323,7 +324,10 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
amount: saleData.amount,
earnings,
},
- });
+ };
+
+ await sendPartnerSaleNotification(emailData);
+ await sendProgramOwnerSaleNotification(emailData);
})(),
);
}
diff --git a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
index d2d93151c1..91dfbd74b3 100644
--- a/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
+++ b/apps/web/app/api/stripe/integration/webhook/invoice-paid.ts
@@ -1,6 +1,7 @@
import { createId } from "@/lib/api/create-id";
import { includeTags } from "@/lib/api/links/include-tags";
import { sendPartnerSaleNotification } from "@/lib/api/partners/partner-sale-notification";
+import { sendProgramOwnerSaleNotification } from "@/lib/api/partners/program-owner-sale-notification";
import { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings";
import { determinePartnerReward } from "@/lib/partners/determine-partner-reward";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
@@ -194,7 +195,7 @@ export async function invoicePaid(event: Stripe.Event) {
},
});
- await sendPartnerSaleNotification({
+ const emailData = {
program,
partner: {
id: link.partnerId!,
@@ -204,7 +205,10 @@ export async function invoicePaid(event: Stripe.Event) {
amount: saleData.amount,
earnings,
},
- });
+ };
+
+ await sendPartnerSaleNotification(emailData);
+ await sendProgramOwnerSaleNotification(emailData);
})(),
);
}
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index cbfb4ac3ca..ae5b89ffd8 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -2,6 +2,7 @@ import { createId } from "@/lib/api/create-id";
import { DubApiError } from "@/lib/api/errors";
import { includeTags } from "@/lib/api/links/include-tags";
import { sendPartnerSaleNotification } from "@/lib/api/partners/partner-sale-notification";
+import { sendProgramOwnerSaleNotification } from "@/lib/api/partners/program-owner-sale-notification";
import { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
@@ -252,7 +253,7 @@ export const POST = withWorkspace(
},
});
- await sendPartnerSaleNotification({
+ const emailData = {
program,
partner: {
id: link.partnerId!,
@@ -262,7 +263,10 @@ export const POST = withWorkspace(
amount: saleData.amount,
earnings,
},
- });
+ };
+
+ await sendPartnerSaleNotification(emailData);
+ await sendProgramOwnerSaleNotification(emailData);
}
}
}
diff --git a/apps/web/lib/api/partners/partner-sale-notification.ts b/apps/web/lib/api/partners/partner-sale-notification.ts
index 4ca655e15b..0191e9a540 100644
--- a/apps/web/lib/api/partners/partner-sale-notification.ts
+++ b/apps/web/lib/api/partners/partner-sale-notification.ts
@@ -1,6 +1,6 @@
import { limiter } from "@/lib/cron/limiter";
import { sendEmailViaResend } from "@dub/email/resend";
-import { NewSaleCreated } from "@dub/email/templates/new-sale-created";
+import { NewSaleAlertPartner } from "@dub/email/templates/new-sale-alert-partner";
import { prismaEdge } from "@dub/prisma/edge";
export async function sendPartnerSaleNotification({
@@ -42,7 +42,7 @@ export async function sendPartnerSaleNotification({
subject: "You just made a sale via Dub Partners!",
from: "Dub Partners <[email protected]>",
email: user.email!,
- react: NewSaleCreated({
+ react: NewSaleAlertPartner({
email: user.email!,
program,
partner,
diff --git a/apps/web/lib/api/partners/program-owner-sale-notification.ts b/apps/web/lib/api/partners/program-owner-sale-notification.ts
new file mode 100644
index 0000000000..f7b8489706
--- /dev/null
+++ b/apps/web/lib/api/partners/program-owner-sale-notification.ts
@@ -0,0 +1,55 @@
+import { limiter } from "@/lib/cron/limiter";
+import { sendEmailViaResend } from "@dub/email/resend";
+import { NewSaleAlertProgramOwner } from "@dub/email/templates/new-sale-alert-program-owner";
+import { prismaEdge } from "@dub/prisma/edge";
+
+export async function sendProgramOwnerSaleNotification({
+ partner,
+ program,
+ sale,
+}: {
+ partner: {
+ id: string;
+ referralLink: string;
+ };
+ program: {
+ id: string;
+ name: string;
+ logo: string | null;
+ };
+ sale: {
+ amount: number;
+ earnings: number;
+ };
+}) {
+ const partnerUsers = await prismaEdge.partnerUser.findMany({
+ where: {
+ partnerId: partner.id,
+ },
+ include: {
+ user: {
+ select: {
+ email: true,
+ },
+ },
+ },
+ });
+
+ return await Promise.all(
+ partnerUsers.map(({ user }) =>
+ limiter.schedule(() =>
+ sendEmailViaResend({
+ subject: "You just made a sale via Dub Partners!",
+ from: "Dub Partners <[email protected]>",
+ email: user.email!,
+ react: NewSaleAlertProgramOwner({
+ email: user.email!,
+ program,
+ partner,
+ sale,
+ }),
+ }),
+ ),
+ ),
+ );
+}
diff --git a/apps/web/lib/integrations/shopify/create-sale.ts b/apps/web/lib/integrations/shopify/create-sale.ts
index 98785273be..6a16648af6 100644
--- a/apps/web/lib/integrations/shopify/create-sale.ts
+++ b/apps/web/lib/integrations/shopify/create-sale.ts
@@ -1,6 +1,7 @@
import { createId } from "@/lib/api/create-id";
import { includeTags } from "@/lib/api/links/include-tags";
import { sendPartnerSaleNotification } from "@/lib/api/partners/partner-sale-notification";
+import { sendProgramOwnerSaleNotification } from "@/lib/api/partners/program-owner-sale-notification";
import { calculateSaleEarnings } from "@/lib/api/sales/calculate-sale-earnings";
import { determinePartnerReward } from "@/lib/partners/determine-partner-reward";
import { recordSale } from "@/lib/tinybird";
@@ -168,7 +169,7 @@ export async function createShopifySale({
},
});
- await sendPartnerSaleNotification({
+ const emailData = {
program,
partner: {
id: link.partnerId!,
@@ -178,7 +179,10 @@ export async function createShopifySale({
amount: saleData.amount,
earnings,
},
- });
+ };
+
+ await sendPartnerSaleNotification(emailData);
+ await sendProgramOwnerSaleNotification(emailData);
})(),
);
}
diff --git a/packages/email/src/templates/new-sale-created.tsx b/packages/email/src/templates/new-sale-alert-partner.tsx
similarity index 97%
rename from packages/email/src/templates/new-sale-created.tsx
rename to packages/email/src/templates/new-sale-alert-partner.tsx
index 09486fe226..ffca9ea79a 100644
--- a/packages/email/src/templates/new-sale-created.tsx
+++ b/packages/email/src/templates/new-sale-alert-partner.tsx
@@ -14,7 +14,7 @@ import {
} from "@react-email/components";
import { Footer } from "../components/footer";
-export function NewSaleCreated({
+export function NewSaleAlertPartner({
email = "[email protected]",
partner = {
id: "pn_OfewI1Faaf5pV8QH3mha8L7S",
@@ -115,4 +115,4 @@ export function NewSaleCreated({
);
}
-export default NewSaleCreated;
+export default NewSaleAlertPartner;
diff --git a/packages/email/src/templates/new-sale-alert-program-owner.tsx b/packages/email/src/templates/new-sale-alert-program-owner.tsx
new file mode 100644
index 0000000000..9e2fd5869d
--- /dev/null
+++ b/packages/email/src/templates/new-sale-alert-program-owner.tsx
@@ -0,0 +1,166 @@
+import { currencyFormatter, DUB_WORDMARK } from "@dub/utils";
+import {
+ Body,
+ Container,
+ Head,
+ Heading,
+ Html,
+ Img,
+ Link,
+ Preview,
+ Section,
+ Tailwind,
+ Text,
+} from "@react-email/components";
+import { Footer } from "../components/footer";
+
+export function NewSaleAlertProgramOwner({
+ email = "[email protected]",
+ workspace = {
+ name: "Acme",
+ slug: "acme",
+ },
+ program = {
+ id: "prog_CYCu7IMAapjkRpTnr8F1azjN",
+ name: "Acme",
+ logo: DUB_WORDMARK,
+ holdingPeriodDays: 30,
+ },
+ partner = {
+ id: "pn_OfewI1Faaf5pV8QH3mha8L7S",
+ name: "Steven",
+ referralLink: "https://refer.dub.co/steven",
+ },
+ sale = {
+ amount: 1330,
+ earnings: 399,
+ commissionRate: 30,
+ },
+}: {
+ email: string;
+ workspace: {
+ name: string;
+ slug: string;
+ };
+ program: {
+ id: string;
+ name: string;
+ logo: string | null;
+ holdingPeriodDays: number;
+ };
+ partner: {
+ id: string;
+ name: string;
+ referralLink: string;
+ };
+ sale: {
+ amount: number;
+ earnings: number;
+ commissionRate: number;
+ };
+}) {
+ const linkToProgram = `https://app.dub.co/${workspace.slug}/programs/${program.id}/sales?partnerId=${partner.id}`;
+
+ const saleAmountInDollars = currencyFormatter(sale.amount / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ });
+
+ const earningsInDollars = currencyFormatter(sale.earnings / 100, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ });
+
+ const profitInDollars = currencyFormatter(
+ (sale.amount - sale.earnings) / 100,
+ {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ },
+ );
+
+ let formattedDueDate = "";
+
+ if (program.holdingPeriodDays > 0) {
+ const dueDate = new Date();
+ dueDate.setDate(dueDate.getDate() + program.holdingPeriodDays);
+
+ formattedDueDate = dueDate.toLocaleDateString("en-US", {
+ month: "long",
+ day: "numeric",
+ year: "numeric",
+ });
+ }
+
+ return (
+ <Html>
+ <Head />
+ <Preview>You received a payment from a customer referred! 💰</Preview>
+ <Tailwind>
+ <Body className="mx-auto my-auto bg-white font-sans">
+ <Container className="mx-auto my-10 max-w-[500px] rounded border border-solid border-neutral-200 px-10 py-5">
+ <Section className="my-8">
+ <Img
+ src={program.logo || "https://assets.dub.co/logo.png"}
+ height="32"
+ alt={program.name}
+ />
+ </Section>
+
+ <Heading className="mx-0 my-6 p-0 text-lg font-medium text-black">
+ Hi {email.split("@")[0]},
+ </Heading>
+
+ <Text className="text-sm leading-6 text-neutral-600">
+ You received a payment from a customer referred by{" "}
+ <strong>{partner.name || partner.referralLink}</strong>.
+ </Text>
+
+ <Section className="my-4">
+ <div>
+ <Text className="my-2 flex justify-between text-sm leading-6 text-neutral-600">
+ <strong>Amount</strong>
+ <span>{saleAmountInDollars} USD</span>
+ </Text>
+
+ <Text className="my-2 flex justify-between text-sm leading-6 text-neutral-600">
+ <strong>Commission</strong>
+ <span>-{earningsInDollars} USD</span>
+ </Text>
+
+ <div className="h-px w-full bg-neutral-200" />
+
+ <Text className="my-2 flex justify-between text-sm leading-6 text-neutral-600">
+ <strong>Profit</strong>
+ <strong>{profitInDollars} USD</strong>
+ </Text>
+ </div>
+ </Section>
+
+ {formattedDueDate && (
+ <Text className="text-sm leading-6 text-neutral-600">
+ Payment for this commission is not due until{" "}
+ <strong>{formattedDueDate}</strong>, based on your campaign
+ settings.
+ </Text>
+ )}
+
+ <Text className="text-sm leading-6 text-neutral-600">
+ You can view sales and commissions in the{" "}
+ <Link href={linkToProgram} className="text-blue-600 underline">
+ program dashboard
+ </Link>
+ .
+ </Text>
+
+ <Text className="text-sm leading-6 text-neutral-600">Thanks!</Text>
+
+ <Footer email={email} />
+ </Container>
+ </Body>
+ </Tailwind>
+ </Html>
+ );
+}
+
+export default NewSaleAlertProgramOwner;