Skip to content

Commit

Permalink
send feedback email when subscription deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Dec 4, 2023
1 parent 5c0dc04 commit c17f826
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
22 changes: 20 additions & 2 deletions apps/web/app/api/callback/stripe/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { stripe } from "@/lib/stripe";
import { getPlanFromPriceId, isUpgrade } from "@/lib/stripe/utils";
import { redis } from "@/lib/upstash";
import { log } from "@dub/utils";
import { sendEmail } from "emails";
import { resend, sendEmail } from "emails";
import UpgradeEmail from "emails/upgrade-email";
import { NextResponse } from "next/server";
import Stripe from "stripe";
Expand Down Expand Up @@ -196,6 +196,15 @@ export const POST = async (req: Request) => {
select: {
slug: true,
domains: true,
users: {
select: {
user: {
select: {
email: true,
},
},
},
},
},
});

Expand All @@ -210,6 +219,9 @@ export const POST = async (req: Request) => {
return NextResponse.json({ received: true });
}

const projectUsers = project.users.map(
({ user }) => user.email as string,
);
const projectDomains = project.domains.map((domain) => domain.slug);

const pipeline = redis.pipeline();
Expand All @@ -218,7 +230,7 @@ export const POST = async (req: Request) => {
pipeline.del(`root:${domain}`);
});

await Promise.all([
await Promise.allSettled([
prisma.project.update({
where: {
stripeId,
Expand All @@ -237,6 +249,12 @@ export const POST = async (req: Request) => {
type: "cron",
mention: true,
}),
resend.emails.send({
from: "Steven from Dub.co <steven@dub.co>",
to: projectUsers,
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\nThanks!\n\nSteven Tey\nFounder, Dub.co",
}),
]);
}
} catch (error) {
Expand Down
6 changes: 2 additions & 4 deletions apps/web/emails/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { nanoid } from "@dub/utils";
import { JSXElementConstructor, ReactElement } from "react";
import { Resend } from "resend";

export const resend = process.env.RESEND_API_KEY
? new Resend(process.env.RESEND_API_KEY)
: null;
export const resend = new Resend(process.env.RESEND_API_KEY);

export const sendEmail = async ({
email,
Expand All @@ -19,7 +17,7 @@ export const sendEmail = async ({
marketing?: boolean;
test?: boolean;
}) => {
if (!resend) {
if (!process.env.RESEND_API_KEY) {
console.log(
"Resend is not configured. You need to add a RESEND_API_KEY in your .env file for emails to work.",
);
Expand Down

0 comments on commit c17f826

Please # to comment.