diff --git a/apps/dashboard/app/(app)/settings/billing/stripe/success/page.tsx b/apps/dashboard/app/(app)/settings/billing/stripe/success/page.tsx index bda76f02ab..7618ed7fee 100644 --- a/apps/dashboard/app/(app)/settings/billing/stripe/success/page.tsx +++ b/apps/dashboard/app/(app)/settings/billing/stripe/success/page.tsx @@ -1,10 +1,14 @@ import { EmptyPlaceholder } from "@/components/dashboard/empty-placeholder"; import { Code } from "@/components/ui/code"; +import { insertAuditLogs } from "@/lib/audit"; import { getTenantId } from "@/lib/auth"; import { db, eq, schema } from "@/lib/db"; import { stripeEnv } from "@/lib/env"; import { PostHogClient } from "@/lib/posthog"; +import { ingestAuditLogsTinybird } from "@/lib/tinybird"; import { currentUser } from "@clerk/nextjs"; +import { defaultProSubscriptions } from "@unkey/billing"; +import { headers } from "next/headers"; import { redirect } from "next/navigation"; import Stripe from "stripe"; @@ -18,10 +22,10 @@ type Props = { export default async function StripeSuccess(props: Props) { const { session_id, new_plan } = props.searchParams; const tenantId = getTenantId(); - if (!tenantId) { + const user = await currentUser(); + if (!tenantId || !user) { return redirect("/auth/sign-in"); } - const _user = await currentUser(); const ws = await db.query.workspaces.findFirst({ where: (table, { and, eq, isNull }) => @@ -72,19 +76,64 @@ export default async function StripeSuccess(props: Props) { ); } - const isChangingPlan = new_plan && new_plan !== ws.plan; + const isUpgradingPlan = new_plan && new_plan !== ws.plan && new_plan === "pro"; + const h = headers(); - await db - .update(schema.workspaces) - .set({ - stripeCustomerId: customer.id, - stripeSubscriptionId: session.subscription as string, - trialEnds: null, - ...(isChangingPlan ? { plan: new_plan } : {}), - }) - .where(eq(schema.workspaces.id, ws.id)); + await db.transaction(async (tx) => { + await tx + .update(schema.workspaces) + .set({ + stripeCustomerId: customer.id, + stripeSubscriptionId: session.subscription as string, + trialEnds: null, + ...(isUpgradingPlan + ? { + plan: new_plan, + planChanged: new Date(), + subscriptions: defaultProSubscriptions(), + planDowngradeRequest: null, + } + : {}), + }) + .where(eq(schema.workspaces.id, ws.id)); - if (isChangingPlan) { + if (isUpgradingPlan) { + await insertAuditLogs(tx, { + workspaceId: ws.id, + actor: { type: "user", id: user.id }, + event: "workspace.update", + description: "Changed plan to 'pro'", + resources: [ + { + type: "workspace", + id: ws.id, + }, + ], + context: { + location: h.get("x-forwarded-for") ?? process.env.VERCEL_REGION ?? "unknown", + userAgent: h.get("user-agent") ?? undefined, + }, + }); + await ingestAuditLogsTinybird({ + workspaceId: ws.id, + actor: { type: "user", id: user.id }, + event: "workspace.update", + description: "Changed plan to 'pro'", + resources: [ + { + type: "workspace", + id: ws.id, + }, + ], + context: { + location: h.get("x-forwarded-for") ?? process.env.VERCEL_REGION ?? "unknown", + userAgent: h.get("user-agent") ?? undefined, + }, + }); + } + }); + + if (isUpgradingPlan) { PostHogClient.capture({ distinctId: tenantId, event: "plan_changed",