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 cancellation feedback email

Steven Tey4722046d3cd0
diff --git a/apps/web/app/api/cron/emails/cancellation-feedback/route.ts b/apps/web/app/api/cron/emails/cancellation-feedback/route.ts
new file mode 100644
index 0000000000..f6dbb89d88
--- /dev/null
+++ b/apps/web/app/api/cron/emails/cancellation-feedback/route.ts
@@ -0,0 +1,56 @@
+import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { sendEmail } from "emails";
+
+export const dynamic = "force-dynamic";
+
+const cancellationReasonMap = {
+ customer_service: "you had a bad experience with our customer service",
+ low_quality: "the product didn't meet your expectations",
+ missing_features: "you were expecting more features",
+ switched_service: "you switched to a different service",
+ too_complex: "the product was too complex",
+ too_expensive: "the product was too expensive",
+ unused: "you didn't use the product",
+};
+
+export async function POST(req: Request) {
+ try {
+ const body = await req.json();
+ // await verifyQstashSignature(req, body);
+ const { owners, reason } = body as {
+ owners: {
+ name: string | null;
+ email: string | null;
+ }[];
+ reason?:
+ | "customer_service"
+ | "low_quality"
+ | "missing_features"
+ | "other"
+ | "switched_service"
+ | "too_complex"
+ | "too_expensive"
+ | "unused";
+ };
+
+ const reasonText = reason ? cancellationReasonMap[reason] : "";
+
+ await Promise.all(
+ owners.map(
+ (owner) =>
+ owner.email &&
+ sendEmail({
+ email: owner.email,
+ from: "Steven Tey <[email protected]>",
+ replyToFromEmail: true,
+ subject: "Feedback for Dub.co?",
+ text: `Hey ${owner.name ? owner.name.split(" ")[0] : "there"}!\n\nSaw you canceled your Dub subscription${reasonText ? ` and mentioned that ${reasonText}` : ""} – do you mind sharing if there's anything we could've done better on our side?\n\nWe're always looking to improve our product offering so any feedback would be greatly appreciated!\n\nThank you so much in advance!\n\nBest,\nSteven Tey\nFounder, Dub.co`,
+ }),
+ ),
+ );
+
+ return new Response("Feedback email sent.", { status: 200 });
+ } catch (error) {
+ return handleAndReturnErrorResponse(error);
+ }
+}
diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
index c2126c763e..cc4ff29a3e 100644
--- a/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
+++ b/apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
@@ -1,8 +1,8 @@
+import { qstash } from "@/lib/cron";
import { prisma } from "@/lib/prisma";
import { recordLink } from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
-import { FREE_PLAN, log } from "@dub/utils";
-import { sendEmail } from "emails";
+import { APP_DOMAIN_WITH_NGROK, FREE_PLAN, log } from "@dub/utils";
import { NextResponse } from "next/server";
import Stripe from "stripe";
@@ -33,11 +33,13 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {
select: {
user: {
select: {
+ name: true,
email: true,
},
},
},
where: {
+ role: "owner",
user: {
isMachine: false,
},
@@ -59,9 +61,7 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {
const workspaceLinks = workspace.links;
- const workspaceUsers = workspace.users.map(
- ({ user }) => user.email as string,
- );
+ const workspaceUsers = workspace.users.map(({ user }) => user);
const pipeline = redis.pipeline();
// remove root domain redirect for all domains from Redis
@@ -132,13 +132,14 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {
type: "cron",
mention: true,
}),
- workspaceUsers.map((email) =>
- sendEmail({
- email,
- subject: "Feedback on your Dub.co experience?",
- text: "Hey!\n\nI noticed you recently cancelled your Dub.co subscription – we're sorry to see you go!\n\nI'd love to hear your feedback on your experience with Dub – what could we have done better?\n\nThank you so much in advance!\n\nSteven Tey\nFounder, Dub.co",
- }),
- ),
+ qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/emails/cancellation-feedback`,
+ // artificially delay anywhere between 3-5 hours to make it feel more like a human
+ delay:
+ Math.floor(Math.random() * 1000 * 60 * 60 * 3) + 3 * 1000 * 60 * 60,
+ body: {
+ owners: workspaceUsers,
+ },
+ }),
]);
}
diff --git a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts b/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts
index b011de8904..53f23950b5 100644
--- a/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts
+++ b/apps/web/app/api/stripe/webhook/customer-subscription-updated.ts
@@ -1,5 +1,6 @@
+import { qstash } from "@/lib/cron";
import { prisma } from "@/lib/prisma";
-import { getPlanFromPriceId, log } from "@dub/utils";
+import { APP_DOMAIN_WITH_NGROK, getPlanFromPriceId, log } from "@dub/utils";
import { NextResponse } from "next/server";
import Stripe from "stripe";
@@ -23,6 +24,26 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {
where: {
stripeId,
},
+ select: {
+ id: true,
+ plan: true,
+ users: {
+ select: {
+ user: {
+ select: {
+ email: true,
+ name: true,
+ },
+ },
+ },
+ where: {
+ role: "owner",
+ user: {
+ isMachine: false,
+ },
+ },
+ },
+ },
});
if (!workspace) {
@@ -64,4 +85,24 @@ export async function customerSubscriptionUpdated(event: Stripe.Event) {
},
});
}
+
+ const subscriptionCanceled =
+ subscriptionUpdated.status === "active" &&
+ subscriptionUpdated.cancel_at_period_end;
+
+ if (subscriptionCanceled) {
+ const owners = workspace.users.map(({ user }) => user);
+ const cancelReason = subscriptionUpdated.cancellation_details?.feedback;
+
+ await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/emails/cancellation-feedback`,
+ // artificially delay anywhere between 3-5 hours to make it feel more like a human
+ delay:
+ Math.floor(Math.random() * 1000 * 60 * 60 * 3) + 3 * 1000 * 60 * 60,
+ body: {
+ owners,
+ reason: cancelReason,
+ },
+ });
+ }
}
diff --git a/apps/web/emails/index.ts b/apps/web/emails/index.ts
index bbf0a94062..330bc37495 100644
--- a/apps/web/emails/index.ts
+++ b/apps/web/emails/index.ts
@@ -11,6 +11,7 @@ export const sendEmail = async ({
subject,
from,
bcc,
+ replyToFromEmail,
text,
react,
marketing,
@@ -19,6 +20,7 @@ export const sendEmail = async ({
subject: string;
from?: string;
bcc?: string;
+ replyToFromEmail?: boolean;
text?: string;
react?: ReactElement<any, string | JSXElementConstructor<any>>;
marketing?: boolean;
@@ -48,9 +50,11 @@ export const sendEmail = async ({
: `${process.env.NEXT_PUBLIC_APP_NAME} <system@${process.env.NEXT_PUBLIC_APP_DOMAIN}>`),
To: email,
Bcc: bcc,
- ReplyTo: process.env.NEXT_PUBLIC_IS_DUB
- : `support@${process.env.NEXT_PUBLIC_APP_DOMAIN}`,
+ ...(!replyToFromEmail && {
+ ReplyTo: process.env.NEXT_PUBLIC_IS_DUB
+ : `support@${process.env.NEXT_PUBLIC_APP_DOMAIN}`,
+ }),
Subject: subject,
...(text && { TextBody: text }),
...(react && { HtmlBody: render(react) }),