summaryrefslogtreecommitdiffhomepage
path: root/packages/console/core/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/console/core/src')
-rw-r--r--packages/console/core/src/billing.ts25
1 files changed, 25 insertions, 0 deletions
diff --git a/packages/console/core/src/billing.ts b/packages/console/core/src/billing.ts
index 44f12db9e..2c1cdb068 100644
--- a/packages/console/core/src/billing.ts
+++ b/packages/console/core/src/billing.ts
@@ -335,4 +335,29 @@ export namespace Billing {
return subscription.id
},
)
+
+ export const unsubscribe = fn(
+ z.object({
+ subscriptionID: z.string(),
+ }),
+ async ({ subscriptionID }) => {
+ const workspaceID = await Database.use((tx) =>
+ tx
+ .select({ workspaceID: BillingTable.workspaceID })
+ .from(BillingTable)
+ .where(eq(BillingTable.subscriptionID, subscriptionID))
+ .then((rows) => rows[0]?.workspaceID),
+ )
+ if (!workspaceID) throw new Error("Workspace ID not found for subscription")
+
+ await Database.transaction(async (tx) => {
+ await tx
+ .update(BillingTable)
+ .set({ subscriptionID: null, subscription: null })
+ .where(eq(BillingTable.workspaceID, workspaceID))
+
+ await tx.delete(SubscriptionTable).where(eq(SubscriptionTable.workspaceID, workspaceID))
+ })
+ },
+ )
}