Skip to content

Commit

Permalink
fix(billing): add missing subscription fields and audit logging to up…
Browse files Browse the repository at this point in the history
…grade flow (#2179)

* fix(billing): add missing subscription fields and audit logging to upgrade flow

* fix context properties
  • Loading branch information
mcstepp committed Oct 8, 2024
1 parent f45291a commit daecfe6
Showing 1 changed file with 62 additions and 13 deletions.
75 changes: 62 additions & 13 deletions apps/dashboard/app/(app)/settings/billing/stripe/success/page.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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 }) =>
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit daecfe6

Please sign in to comment.