diff --git a/app/api/webhooks/stripe/route.ts b/app/api/webhooks/stripe/route.ts index 9ef00a1..7137d8a 100644 --- a/app/api/webhooks/stripe/route.ts +++ b/app/api/webhooks/stripe/route.ts @@ -204,6 +204,7 @@ const buildSubscriptionUpsertData = ( subscription: Stripe.Subscription, firstItem: Stripe.SubscriptionItem | undefined, serverId: string, + internalBillingCustomerId: string, promoCode: string | null, couponAmountOff: number | null, periodStart: Date | null, @@ -220,7 +221,7 @@ const buildSubscriptionUpsertData = ( const status = toSubscriptionStatus(subscription.status); return { create: { - billingCustomerId: subscription.customer as string, + billingCustomerId: internalBillingCustomerId, canceledAt, currency: subscription.currency.toUpperCase(), currentPeriodEnd: periodEnd, @@ -260,6 +261,16 @@ const handleSubscriptionChanged = async ( return; } + // Resolve internal BillingCustomer.id from the Stripe customer ID + const stripeCustomerId = subscription.customer as string; + const billingCustomer = await prisma.billingCustomer.findUnique({ + select: { id: true }, + where: { stripeCustomerId }, + }); + if (!billingCustomer) { + return; + } + const { promoCode, couponAmountOff, couponPercentOff } = resolveSubscriptionDiscount(subscription); const { periodStart, periodEnd } = resolveSubscriptionPeriod(subscription); @@ -275,6 +286,7 @@ const handleSubscriptionChanged = async ( subscription, firstItem, serverId, + billingCustomer.id, promoCode, couponAmountOff, periodStart, @@ -409,9 +421,21 @@ const upsertCheckoutSubscriptionAndAddons = async ( ? firstItem.price.product : ((firstItem?.price.product as Stripe.Product)?.id ?? ""); + // Resolve the internal BillingCustomer.id from the Stripe customer ID — + // billingCustomerId is a FK to BillingCustomer.id (not stripeCustomerId) + const billingCustomer = await prisma.billingCustomer.findUnique({ + select: { id: true }, + where: { stripeCustomerId: customerId }, + }); + if (!billingCustomer) { + throw new Error( + `BillingCustomer not found for Stripe customer ${customerId}` + ); + } + await prisma.subscription.upsert({ create: { - billingCustomerId: customerId, + billingCustomerId: billingCustomer.id, currency: (subscription.currency ?? "gbp").toUpperCase(), id: subscriptionId, planName: subscription.metadata?.serverType ?? "unknown", diff --git a/lib/mail.ts b/lib/mail.ts index c6d9049..0fe0b7d 100644 --- a/lib/mail.ts +++ b/lib/mail.ts @@ -797,3 +797,105 @@ export const sendSuspensionWarningEmail = ( to: toEmail, }); }; + +// --------------------------------------------------------------------------- +// Server ready email +// --------------------------------------------------------------------------- + +interface ServerReadyParams { + toEmail: string; + userName: string | null | undefined; + serverName: string; + game: string; + region: string; + serverUrl: string; +} + +export const sendServerReadyEmail = ( + params: ServerReadyParams +): Promise => { + const { toEmail, userName, serverName, game, region, serverUrl } = params; + const displayName = userName ?? "there"; + const gameLabel = + game.charAt(0).toUpperCase() + game.slice(1).replaceAll("-", " "); + + const html = baseTemplate(` +

Your server is ready! 🎮

+

Hi ${displayName}, your game server has finished provisioning and is ready to use.

+
+
Server name
${serverName}
+
Game
${gameLabel}
+
Region
${region}
+
+ Go to server +

+ You'll find the connection details and server controls on your server page. +

+ `); + + const text = [ + `Your NodeByte server "${serverName}" is ready!`, + ``, + `Game: ${gameLabel}`, + `Region: ${region}`, + ``, + `Go to your server: ${serverUrl}`, + ].join("\n"); + + return sendMail({ + html, + subject: `Your server "${serverName}" is ready to play`, + text, + to: toEmail, + }); +}; + +// --------------------------------------------------------------------------- +// Server provision failed email +// --------------------------------------------------------------------------- + +interface ServerProvisionFailedParams { + toEmail: string; + userName: string | null | undefined; + serverName: string; + reason: string; + supportUrl: string; +} + +export const sendServerProvisionFailedEmail = ( + params: ServerProvisionFailedParams +): Promise => { + const { toEmail, userName, serverName, reason, supportUrl } = params; + const displayName = userName ?? "there"; + + const html = baseTemplate(` +

Server provisioning failed

+

Hi ${displayName}, we were unable to provision your server ${serverName}.

+
+
Reason
${reason}
+
+

Our team has been notified. Please contact support and we'll get this resolved for you.

+ Contact support +

+ Your subscription will not be charged for a server that failed to provision. +

+ `); + + const text = [ + `Server provisioning failed — NodeByte`, + ``, + `Hi ${displayName}, we were unable to provision "${serverName}".`, + ``, + `Reason: ${reason}`, + ``, + `Please contact support: ${supportUrl}`, + `Your subscription will not be charged for a server that failed to provision.`, + ].join("\n"); + + return sendMail({ + html, + subject: `Server "${serverName}" failed to provision`, + text, + to: toEmail, + }); +}; diff --git a/lib/workflows/steps.ts b/lib/workflows/steps.ts index 8d4239c..c7ba509 100644 --- a/lib/workflows/steps.ts +++ b/lib/workflows/steps.ts @@ -7,6 +7,11 @@ import { enqueueCommand } from "@/lib/agent/commands"; import { prisma } from "@/lib/db"; import { API_URL, env, SNAPSHOT_ENVIRONMENT } from "@/lib/env"; import { emitActivity } from "@/lib/events/emit"; +import { + sendServerDeletedEmail, + sendServerProvisionFailedEmail, + sendServerReadyEmail, +} from "@/lib/mail"; import { getProviderForUser, getProviderForUserWithImage, @@ -285,6 +290,25 @@ export const stepMarkReady = async (serverId: string) => { phase: "ready", serverId, }); + const server = await prisma.server.findUnique({ + select: { + game: true, + location: true, + name: true, + user: { select: { email: true, name: true } }, + }, + where: { id: serverId }, + }); + if (server?.user) { + void sendServerReadyEmail({ + game: server.game, + region: server.location, + serverName: server.name, + serverUrl: `${API_URL}/dashboard/${serverId}`, + toEmail: server.user.email, + userName: server.user.name, + }); + } }; export const stepMarkFailed = async (input: { @@ -305,6 +329,19 @@ export const stepMarkFailed = async (input: { phase: "errored", serverId: input.serverId, }); + const server = await prisma.server.findUnique({ + select: { name: true, user: { select: { email: true, name: true } } }, + where: { id: input.serverId }, + }); + if (server?.user) { + void sendServerProvisionFailedEmail({ + reason: input.reason, + serverName: server.name, + supportUrl: "https://nodebyte.host/contact", + toEmail: server.user.email, + userName: server.user.name, + }); + } }; const AGENT_LIVENESS_WINDOW_MS = 60_000; @@ -353,6 +390,10 @@ export const stepDeleteProviderServer = async (serverId: string) => { export const stepMarkDeleted = async (serverId: string) => { "use step"; + const server = await prisma.server.findUnique({ + select: { name: true, user: { select: { email: true, name: true } } }, + where: { id: serverId }, + }); const { count } = await prisma.server.updateMany({ data: { deletedAt: new Date(), @@ -369,6 +410,13 @@ export const stepMarkDeleted = async (serverId: string) => { phase: "deleted", serverId, }); + if (server?.user) { + void sendServerDeletedEmail({ + serverName: server.name, + toEmail: server.user.email, + userName: server.user.name, + }); + } }; export const stepReadPhase = async (