diff --git a/apps/web/src/server/billing/payments.ts b/apps/web/src/server/billing/payments.ts index 41cec92..00459e3 100644 --- a/apps/web/src/server/billing/payments.ts +++ b/apps/web/src/server/billing/payments.ts @@ -1,7 +1,9 @@ import Stripe from "stripe"; import { env } from "~/env"; import { db } from "../db"; +import { sendSubscriptionConfirmationEmail } from "../mailer"; import { TeamService } from "../service/team-service"; +import { logger } from "../logger/log"; export function getStripe() { if (!env.STRIPE_SECRET_KEY) { @@ -123,6 +125,8 @@ export async function syncStripeData(customerId: string) { return; } + const wasPaid = team.isActive && team.plan !== "FREE"; + const subscriptions = await stripe.subscriptions.list({ customer: customerId, limit: 1, @@ -144,6 +148,10 @@ export async function syncStripeData(customerId: string) { .map((item) => item.price?.id) .filter((id): id is string => Boolean(id)); + const nextPlan = getPlanFromPriceIds(priceIds); + const isNowPaid = subscription.status === "active" && nextPlan !== "FREE"; + const shouldSendSubscriptionConfirmation = !wasPaid && isNowPaid; + await db.subscription.upsert({ where: { id: subscription.id }, update: { @@ -182,10 +190,24 @@ export async function syncStripeData(customerId: string) { }); await TeamService.updateTeam(team.id, { - plan: - subscription.status === "canceled" - ? "FREE" - : getPlanFromPriceIds(priceIds), + plan: subscription.status === "canceled" ? "FREE" : nextPlan, isActive: subscription.status === "active", }); + + if (shouldSendSubscriptionConfirmation) { + try { + const teamUsers = await TeamService.getTeamUsers(team.id); + await Promise.all( + teamUsers + .map((tu) => tu.user?.email) + .filter((email): email is string => Boolean(email)) + .map((email) => sendSubscriptionConfirmationEmail(email)) + ); + } catch (err) { + logger.error( + { err, teamId: team.id }, + "[Billing]: Failed sending subscription confirmation email" + ); + } + } } diff --git a/apps/web/src/server/mailer.ts b/apps/web/src/server/mailer.ts index 2a7f843..bc360e6 100644 --- a/apps/web/src/server/mailer.ts +++ b/apps/web/src/server/mailer.ts @@ -69,6 +69,19 @@ export async function sendTeamInviteEmail( await sendMail(email, subject, text, html); } +export async function sendSubscriptionConfirmationEmail(email: string) { + if (!env.FOUNDER_EMAIL) { + logger.error("FOUNDER_EMAIL not configured"); + return; + } + + const subject = "Thanks for subscribing to useSend"; + const text = `Hey,\n\nThanks for subscribing to useSend, just wanted to let you know you can join the discord server to have a dedicated support channel for your team. So that we can address your queries / bugs asap.\n\nYou can join over using the link: https://discord.com/invite/BU8n8pJv8S\n\nIf you prefer slack, please let me know\n\ncheers,\nkoushik - useSend`; + const html = text.replace(/\n/g, "
"); + + await sendMail(email, subject, text, html, undefined, env.FOUNDER_EMAIL); +} + export async function sendMail( email: string, subject: string, diff --git a/apps/web/src/server/service/team-service.ts b/apps/web/src/server/service/team-service.ts index b268eff..596ce51 100644 --- a/apps/web/src/server/service/team-service.ts +++ b/apps/web/src/server/service/team-service.ts @@ -563,6 +563,7 @@ export class TeamService { "[TeamService]: Set warning notification cooldown" ); } + } async function getLimitReachedEmail(