diff --git a/apps/dashboard/lib/trpc/routers/stripe/cancelSubscription.ts b/apps/dashboard/lib/trpc/routers/stripe/cancelSubscription.ts index 3c079e18ab..7504d87396 100644 --- a/apps/dashboard/lib/trpc/routers/stripe/cancelSubscription.ts +++ b/apps/dashboard/lib/trpc/routers/stripe/cancelSubscription.ts @@ -1,3 +1,4 @@ +import { auth } from "@/lib/auth/server"; import { stripeEnv } from "@/lib/env"; import { TRPCError } from "@trpc/server"; import Stripe from "stripe"; @@ -11,6 +12,21 @@ export const cancelSubscription = t.procedure throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: "Stripe is not set up" }); } + const memberships = await auth.getOrganizationMemberList(ctx.workspace.orgId).catch((err) => { + console.error(err); + throw new TRPCError({ + code: "INTERNAL_SERVER_ERROR", + message: "Failed to fetch organization members", + }); + }); + if (memberships.data.length > 1) { + throw new TRPCError({ + code: "PRECONDITION_FAILED", + message: + "Workspace has more than one member. You must remove all other members before downgrading to the free tier.", + }); + } + const stripe = new Stripe(e.STRIPE_SECRET_KEY, { apiVersion: "2023-10-16", typescript: true,