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.

fix PartnerPayoutSent email link, add to chargeSucceeded webhook

Steven Tey7f4459bd26e1
diff --git a/apps/web/app/api/stripe/webhook/charge-succeeded.ts b/apps/web/app/api/stripe/webhook/charge-succeeded.ts
index 9d6f27d3e5..b7166cc7fb 100644
--- a/apps/web/app/api/stripe/webhook/charge-succeeded.ts
+++ b/apps/web/app/api/stripe/webhook/charge-succeeded.ts
@@ -1,5 +1,9 @@
+import { limiter } from "@/lib/cron/limiter";
import { stripe } from "@/lib/stripe";
import { prisma } from "@dub/prisma";
+import { formatDate } from "@dub/utils";
+import { sendEmail } from "emails";
+import PartnerPayoutSent from "emails/partner-payout-sent";
import Stripe from "stripe";
export async function chargeSucceeded(event: Stripe.Event) {
@@ -24,8 +28,20 @@ export async function chargeSucceeded(event: Stripe.Event) {
include: {
payouts: {
include: {
- partner: true,
program: true,
+ partner: {
+ include: {
+ users: {
+ select: {
+ user: {
+ select: {
+ email: true,
+ },
+ },
+ },
+ },
+ },
+ },
},
},
},
@@ -47,25 +63,56 @@ export async function chargeSucceeded(event: Stripe.Event) {
console.log("Transfer created", transfer);
- await prisma.$transaction(async (tx) => {
- await tx.payout.update({
- where: {
- id: payout.id,
- },
- data: {
- stripeTransferId: transfer.id,
- status: "completed",
- },
- });
+ const partnerUsers = payout.partner.users.map(({ user }) => user);
- await tx.sale.updateMany({
- where: {
- payoutId: payout.id,
- },
- data: {
- status: "paid",
- },
- });
- });
+ await Promise.all([
+ prisma.$transaction(async (tx) => {
+ await tx.payout.update({
+ where: {
+ id: payout.id,
+ },
+ data: {
+ stripeTransferId: transfer.id,
+ status: "completed",
+ },
+ });
+
+ await tx.sale.updateMany({
+ where: {
+ payoutId: payout.id,
+ },
+ data: {
+ status: "paid",
+ },
+ });
+ }),
+ partnerUsers.map((user) =>
+ limiter.schedule(() =>
+ sendEmail({
+ subject: "You've been paid!",
+ email: user.email!,
+ from: "Dub Partners <[email protected]>",
+ react: PartnerPayoutSent({
+ email: user.email!,
+ program: payout.program,
+ payout: {
+ id: payout.id,
+ amount: payout.amount,
+ startDate: formatDate(payout.periodStart!, {
+ year: "numeric",
+ month: "short",
+ day: "numeric",
+ }),
+ endDate: formatDate(payout.periodEnd!, {
+ year: "numeric",
+ month: "short",
+ day: "numeric",
+ }),
+ },
+ }),
+ }),
+ ),
+ ),
+ ]);
}
}
diff --git a/apps/web/emails/partner-payout-sent.tsx b/apps/web/emails/partner-payout-sent.tsx
index e99daba251..24e0a889bb 100644
--- a/apps/web/emails/partner-payout-sent.tsx
+++ b/apps/web/emails/partner-payout-sent.tsx
@@ -41,7 +41,7 @@ export default function PartnerPayoutSent({
endDate: string;
};
}) {
- const linkToPayout = `https://partners.dub.co/${program.id}/payouts?payoutId=${payout.id}`;
+ const linkToPayout = `https://partners.dub.co/settings/payouts?payoutId=${payout.id}`;
const saleAmountInDollars = currencyFormatter(payout.amount / 100, {
minimumFractionDigits: 2,
@@ -75,7 +75,7 @@ export default function PartnerPayoutSent({
<strong className="text-black">{payout.endDate}</strong>.
</Text>
<Text className="text-sm leading-6 text-gray-600">
- Your wallet has been updated to reflect this payout.
+ The funds are on their way to your account.
</Text>
<Section className="mb-12 mt-8">