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.

Update coupon-deleted.ts

Kiran K18b0011f6fc8
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/coupon-deleted.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/coupon-deleted.ts
index 73d1a266ea..8e8a6f96d7 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/coupon-deleted.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/coupon-deleted.ts
@@ -1,8 +1,11 @@
+import { getWorkspaceUsers } from "@/lib/api/get-workspace-users";
import { qstash } from "@/lib/cron";
-import { sendEmail } from "@dub/email";
+import { resend } from "@dub/email/resend";
+import { VARIANT_TO_FROM_MAP } from "@dub/email/resend/constants";
import DiscountDeleted from "@dub/email/templates/discount-deleted";
import { prisma } from "@dub/prisma";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
+
import { waitUntil } from "@vercel/functions";
import type Stripe from "stripe";
@@ -92,41 +95,12 @@ export async function couponDeleted(event: Stripe.Event) {
waitUntil(
(async () => {
- const workspaceUsers = await prisma.projectUsers.findFirst({
- where: {
- projectId: workspace.id,
- role: "owner",
- user: {
- email: {
- not: null,
- },
- },
- },
- include: {
- user: {
- select: {
- email: true,
- },
- },
- },
+ const { users } = await getWorkspaceUsers({
+ workspaceId: workspace.id,
+ role: "owner",
});
- if (workspaceUsers) {
- const { user } = workspaceUsers;
-
- await sendEmail({
- subject: `${process.env.NEXT_PUBLIC_APP_NAME}: Discount has been deleted`,
- email: user.email!,
- react: DiscountDeleted({
- email: user.email!,
- coupon: {
- id: coupon.id,
- },
- }),
- });
- }
-
- Promise.allSettled([
+ await Promise.allSettled([
...groupIds.map((groupId) =>
qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/invalidate-for-discounts`,
@@ -135,6 +109,20 @@ export async function couponDeleted(event: Stripe.Event) {
},
}),
),
+
+ resend.batch.send(
+ users.map((user) => ({
+ from: VARIANT_TO_FROM_MAP.notifications,
+ to: user.email,
+ subject: `${process.env.NEXT_PUBLIC_APP_NAME}: Discount has been deleted`,
+ react: DiscountDeleted({
+ email: user.email,
+ coupon: {
+ id: coupon.id,
+ },
+ }),
+ })),
+ ),
]);
})(),
);