Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(users): block account deletion while a subscription is past_due

Mohamed Bassem844c1f95f8e2
diff --git a/packages/trpc/models/users.ts b/packages/trpc/models/users.ts
index 77e545dc..71017afc 100644
--- a/packages/trpc/models/users.ts
+++ b/packages/trpc/models/users.ts
@@ -153,9 +153,12 @@ export class User {
},
});
+ // past_due subscriptions are still being retried by Stripe, so they can
+ // charge the user just like active ones.
if (
subscription?.status === "active" ||
- subscription?.status === "trialing"
+ subscription?.status === "trialing" ||
+ subscription?.status === "past_due"
) {
throw new TRPCError({
code: "BAD_REQUEST",
diff --git a/packages/trpc/routers/users.test.ts b/packages/trpc/routers/users.test.ts
index 1be603cd..3fffa4c1 100644
--- a/packages/trpc/routers/users.test.ts
+++ b/packages/trpc/routers/users.test.ts
@@ -716,6 +716,39 @@ describe("User Routes", () => {
expect(deletedUser).toHaveLength(1);
});
+ test<CustomTestContext>("deleteAccount refuses past_due Stripe subscription", async ({
+ db,
+ }) => {
+ const user = await createTestUser(db, {
+ name: "Test User",
+ email: "[email protected]",
+ password: "pass1234",
+ });
+ const caller = getApiCaller(db, user.id, user.email, user.role || "user");
+
+ await db.insert(subscriptions).values({
+ userId: user.id,
+ stripeCustomerId: "cus_123",
+ stripeSubscriptionId: "sub_123",
+ status: "past_due",
+ tier: "paid",
+ });
+
+ await expect(() =>
+ caller.users.deleteAccount({
+ password: "pass1234",
+ }),
+ ).rejects.toThrow(
+ /Can't delete user while subscription is active. Please cancel your subscription first and try again./,
+ );
+
+ const deletedUser = await db
+ .select()
+ .from(users)
+ .where(eq(users.id, user.id));
+ expect(deletedUser).toHaveLength(1);
+ });
+
test<CustomTestContext>("deleteAccount - wrong password", async ({
db,
}) => {