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 duplicate cancellation feedback emails (#4030)

Pedro Ladeirac15eae7976cc
diff --git a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts
index cff4b65fdf..d3e961dc09 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts
@@ -15,7 +15,10 @@ import AdvancedPlanDowngradeNotice from "@dub/email/templates/advanced-plan-down
import { prisma } from "@dub/prisma";
import { capitalize, FREE_PLAN, log } from "@dub/utils";
import Stripe from "stripe";
-import { sendCancellationFeedback } from "./utils/send-cancellation-feedback";
+import {
+ CANCELLATION_FEEDBACK_EMAIL_TYPE,
+ sendCancellationFeedback,
+} from "./utils/send-cancellation-feedback";
import { updateWorkspacePlan } from "./utils/update-workspace-plan";
export async function customerSubscriptionDeleted(
@@ -175,6 +178,7 @@ export async function customerSubscriptionDeleted(
}),
sendCancellationFeedback({
+ workspace,
owners: workspaceUsers,
}),
@@ -203,6 +207,14 @@ export async function customerSubscriptionDeleted(
}),
]);
+ // Reset cancellation feedback dedupe so a future resubscribe + cancel can send again
+ await prisma.sentEmail.deleteMany({
+ where: {
+ projectId: workspace.id,
+ type: CANCELLATION_FEEDBACK_EMAIL_TYPE,
+ },
+ });
+
// Update the webhooks cache
const webhooks = await prisma.webhook.findMany({
where: {
diff --git a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-updated.ts b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-updated.ts
index f50534443f..bd5846b5e9 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-updated.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-updated.ts
@@ -107,6 +107,7 @@ export async function customerSubscriptionUpdated(
const cancelReason = updatedSubscription.cancellation_details?.feedback;
await sendCancellationFeedback({
+ workspace,
owners,
reason: cancelReason,
});
diff --git a/apps/web/app/(ee)/api/stripe/webhook/utils/send-cancellation-feedback.ts b/apps/web/app/(ee)/api/stripe/webhook/utils/send-cancellation-feedback.ts
index f42a358534..9356c61e2c 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/utils/send-cancellation-feedback.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/utils/send-cancellation-feedback.ts
@@ -1,7 +1,10 @@
import { isBlacklistedEmail } from "@/lib/edge-config";
import { sendEmail } from "@dub/email";
+import { prisma } from "@dub/prisma";
import Stripe from "stripe";
+export const CANCELLATION_FEEDBACK_EMAIL_TYPE = "cancellationFeedbackEmail";
+
const cancellationReasonMap = {
customer_service: "you had a bad experience with our customer service",
low_quality: "the product didn't meet your expectations",
@@ -13,15 +16,33 @@ const cancellationReasonMap = {
};
export async function sendCancellationFeedback({
+ workspace,
owners,
reason,
}: {
+ workspace: {
+ id: string;
+ };
owners: {
name: string | null;
email: string | null;
}[];
reason?: Stripe.Subscription.CancellationDetails.Feedback | null;
}) {
+ const alreadySent = await prisma.sentEmail.findFirst({
+ where: {
+ projectId: workspace.id,
+ type: CANCELLATION_FEEDBACK_EMAIL_TYPE,
+ },
+ });
+
+ if (alreadySent) {
+ console.log(
+ `Cancellation feedback email already sent for workspace ${workspace.id}, skipping...`,
+ );
+ return;
+ }
+
const isBlacklistedCancellation = await isBlacklistedEmail(
owners.filter(({ email }) => email).map(({ email }) => email!),
);
@@ -31,6 +52,13 @@ export async function sendCancellationFeedback({
return;
}
+ await prisma.sentEmail.create({
+ data: {
+ projectId: workspace.id,
+ type: CANCELLATION_FEEDBACK_EMAIL_TYPE,
+ },
+ });
+
const reasonText = reason ? cancellationReasonMap[reason] : "";
return await Promise.all(