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.

notifyPartnerSale

Steven Tey588ad93c6b4c
diff --git a/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts b/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts
index 7efa488005..20f3cdd5f6 100644
--- a/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts
+++ b/apps/web/app/api/stripe/connect/webhook/checkout-session-completed.ts
@@ -1,3 +1,4 @@
+import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";
import { createSaleData } from "@/lib/api/sales/sale";
import { prisma } from "@/lib/prisma";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
@@ -7,8 +8,6 @@ import { transformSaleEventData } from "@/lib/webhook/transform";
import { nanoid } from "@dub/utils";
import { Customer } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
-import { sendEmail } from "emails";
-import NewSaleCreated from "emails/new-sale-created";
import type Stripe from "stripe";
// Handle event "checkout.session.completed"
@@ -141,19 +140,6 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
},
});
- const { user } = await prisma.partnerUser.findFirstOrThrow({
- where: {
- partnerId: partner.id,
- },
- include: {
- user: {
- select: {
- email: true,
- },
- },
- },
- });
-
const saleRecord = createSaleData({
customerId: saleData.customer_id,
linkId: saleData.link_id,
@@ -171,26 +157,20 @@ export async function checkoutSessionCompleted(event: Stripe.Event) {
},
});
- await Promise.all([
+ await Promise.allSettled([
prisma.sale.create({
data: saleRecord,
}),
-
- sendEmail({
- subject: "You just made a referral sale!",
- email: user.email!,
- react: NewSaleCreated({
- email: user.email!,
- program,
- partner: {
- id: partner.id,
- referralLink: link.shortLink,
- },
- sale: {
- amount: saleRecord.amount,
- earnings: saleRecord.earnings,
- },
- }),
+ notifyPartnerSale({
+ partner: {
+ id: partner.id,
+ referralLink: link.shortLink,
+ },
+ program,
+ sale: {
+ amount: saleRecord.amount,
+ earnings: saleRecord.earnings,
+ },
}),
]);
}
diff --git a/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts b/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
index 560071be2d..b53154440d 100644
--- a/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
+++ b/apps/web/app/api/stripe/connect/webhook/invoice-paid.ts
@@ -1,3 +1,4 @@
+import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";
import { createSaleData } from "@/lib/api/sales/sale";
import { prisma } from "@/lib/prisma";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
@@ -6,8 +7,6 @@ import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { transformSaleEventData } from "@/lib/webhook/transform";
import { nanoid } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
-import { sendEmail } from "emails";
-import NewSaleCreated from "emails/new-sale-created";
import type Stripe from "stripe";
// Handle event "invoice.paid"
@@ -123,19 +122,6 @@ export async function invoicePaid(event: Stripe.Event) {
},
});
- const { user } = await prisma.partnerUser.findFirstOrThrow({
- where: {
- partnerId: partner.id,
- },
- include: {
- user: {
- select: {
- email: true,
- },
- },
- },
- });
-
const saleRecord = createSaleData({
customerId: saleData.customer_id,
linkId: saleData.link_id,
@@ -153,26 +139,20 @@ export async function invoicePaid(event: Stripe.Event) {
},
});
- await Promise.all([
+ await Promise.allSettled([
prisma.sale.create({
data: saleRecord,
}),
-
- sendEmail({
- subject: "You just made a referral sale!",
- email: user.email!,
- react: NewSaleCreated({
- email: user.email!,
- program,
- partner: {
- id: partner.id,
- referralLink: link.shortLink,
- },
- sale: {
- amount: saleRecord.amount,
- earnings: saleRecord.earnings,
- },
- }),
+ notifyPartnerSale({
+ partner: {
+ id: partner.id,
+ referralLink: link.shortLink,
+ },
+ program,
+ sale: {
+ amount: saleRecord.amount,
+ earnings: saleRecord.earnings,
+ },
}),
]);
}
diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.ts
index 820d042897..4e5e50c7a7 100644
--- a/apps/web/app/api/track/sale/route.ts
+++ b/apps/web/app/api/track/sale/route.ts
@@ -1,4 +1,5 @@
import { DubApiError } from "@/lib/api/errors";
+import { notifyPartnerSale } from "@/lib/api/partners/notify-partner-sale";
import { createSaleData } from "@/lib/api/sales/sale";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspaceEdge } from "@/lib/auth/workspace-edge";
@@ -13,8 +14,6 @@ import {
} from "@/lib/zod/schemas/sales";
import { nanoid } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
-import NewSaleCreated from "emails/new-sale-created";
-import { sendEmailViaResend } from "emails/send-via-resend";
import { NextResponse } from "next/server";
export const runtime = "edge";
@@ -137,19 +136,6 @@ export const POST = withWorkspaceEdge(
},
});
- const { user } = await prismaEdge.partnerUser.findFirstOrThrow({
- where: {
- partnerId: partner.id,
- },
- include: {
- user: {
- select: {
- email: true,
- },
- },
- },
- });
-
const saleRecord = createSaleData({
customerId: customer.id,
linkId: link.id,
@@ -164,26 +150,20 @@ export const POST = withWorkspaceEdge(
metadata: clickData,
});
- await Promise.all([
+ await Promise.allSettled([
prismaEdge.sale.create({
data: saleRecord,
}),
-
- sendEmailViaResend({
- subject: "You just made a referral sale!",
- email: user.email!,
- react: NewSaleCreated({
- email: user.email!,
- program,
- partner: {
- id: partner.id,
- referralLink: link.shortLink,
- },
- sale: {
- amount: saleRecord.amount,
- earnings: saleRecord.earnings,
- },
- }),
+ notifyPartnerSale({
+ partner: {
+ id: partner.id,
+ referralLink: link.shortLink,
+ },
+ program,
+ sale: {
+ amount: saleRecord.amount,
+ earnings: saleRecord.earnings,
+ },
}),
]);
}
diff --git a/apps/web/emails/new-referral-sale.tsx b/apps/web/emails/new-referral-sale.tsx
deleted file mode 100644
index e9ca49c603..0000000000
--- a/apps/web/emails/new-referral-sale.tsx
+++ /dev/null
@@ -1,85 +0,0 @@
-import { DUB_WORDMARK, getPrettyUrl } 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 default function NewReferralSale({
- email = "[email protected]",
- workspace = {
- name: "Acme, Inc",
- slug: "acme",
- },
- saleAmount = 4900,
-}: {
- email: string;
- workspace: {
- name: string;
- slug: string;
- };
- saleAmount: number;
-}) {
- const referralLink = `https://refer.dub.co/${workspace.slug}`;
- const saleAmountInDollars = Math.round(saleAmount / 100).toLocaleString();
- return (
- <Html>
- <Head />
- <Preview>
- You just made a ${saleAmountInDollars} sale via your referral link{" "}
- {getPrettyUrl(referralLink)}
- </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-gray-200 px-10 py-5">
- <Section className="mt-8">
- <Img
- src={DUB_WORDMARK}
- height="40"
- alt="Dub.co"
- className="mx-auto my-0"
- />
- </Section>
- <Heading className="mx-0 my-7 p-0 text-center text-xl font-semibold text-black">
- You just made a ${saleAmountInDollars} referral sale!
- </Heading>
- <Text className="text-sm leading-6 text-black">
- Congratulations – someone just upgraded their Dub plan using your
- referral link:{" "}
- <a
- href={referralLink}
- className="text-semibold font-medium text-black underline"
- >
- {getPrettyUrl(referralLink)}
- </a>
- </Text>
- <Text className="text-sm leading-6 text-black">
- As a thank you from us for spreading the word about Dub, you will
- receive{" "}
- <strong>10% of every successful payment from this sale</strong> –
- for the first 12 months.
- </Text>
- <Section className="my-8 text-center">
- <Link
- className="rounded-full bg-black px-6 py-3 text-center text-[12px] font-semibold text-white no-underline"
- href={`https://app.dub.co/${workspace.slug}/settings/referrals`}
- >
- View your referral stats
- </Link>
- </Section>
- <Footer email={email} />
- </Container>
- </Body>
- </Tailwind>
- </Html>
- );
-}
diff --git a/apps/web/lib/api/partners/notify-partner-sale.ts b/apps/web/lib/api/partners/notify-partner-sale.ts
new file mode 100644
index 0000000000..4bbb69af3c
--- /dev/null
+++ b/apps/web/lib/api/partners/notify-partner-sale.ts
@@ -0,0 +1,55 @@
+import { limiter } from "@/lib/cron/limiter";
+import { prismaEdge } from "@/lib/prisma/edge";
+import NewSaleCreated from "emails/new-sale-created";
+import { sendEmailViaResend } from "emails/send-via-resend";
+
+export async function notifyPartnerSale({
+ 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: NewSaleCreated({
+ email: user.email!,
+ program,
+ partner,
+ sale,
+ }),
+ }),
+ ),
+ ),
+ );
+}