Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(billing): restore cancel only after Stripe seat failures

Richie McIlroya80243de13d3
diff --git a/apps/web/__tests__/unit/update-seat-quantity.test.ts b/apps/web/__tests__/unit/update-seat-quantity.test.ts
index 5c15fce60..402f701a6 100644
--- a/apps/web/__tests__/unit/update-seat-quantity.test.ts
+++ b/apps/web/__tests__/unit/update-seat-quantity.test.ts
@@ -266,4 +266,28 @@ describe("updateSeatQuantity", () => {
},
);
});
+
+ it("does not restore cancellation after Stripe applies the new quantity", async () => {
+ mockSeatLookup({ currentQuantity: 1 });
+ mockStripe.subscriptions.retrieve.mockResolvedValue({
+ id: "sub_1",
+ cancel_at_period_end: true,
+ items: {
+ data: [{ id: "si_1", quantity: 1 }],
+ },
+ });
+ mockDb.set.mockImplementation(() => {
+ throw new Error("db down");
+ });
+ const { updateSeatQuantity } = await import(
+ "@/actions/organization/update-seat-quantity"
+ );
+
+ await expect(updateSeatQuantity("org-1" as never, 2)).rejects.toThrow(
+ "local state could not be saved",
+ );
+ expect(mockStripe.subscriptions.update).not.toHaveBeenCalledWith("sub_1", {
+ cancel_at_period_end: true,
+ });
+ });
});
diff --git a/apps/web/actions/organization/update-seat-quantity.ts b/apps/web/actions/organization/update-seat-quantity.ts
index c05cdc520..415ffbda6 100644
--- a/apps/web/actions/organization/update-seat-quantity.ts
+++ b/apps/web/actions/organization/update-seat-quantity.ts
@@ -211,7 +211,6 @@ export async function updateSeatQuantity(
}
};
- let quantityUpdated = false;
try {
if (isSeatIncrease && wasCanceling) {
await stripe().subscriptions.update(subscription.id, {
@@ -243,33 +242,31 @@ export async function updateSeatQuantity(
"Payment for the added seats could not be completed. Update your payment method and try again.",
);
}
-
- quantityUpdated = true;
-
- try {
- await db()
- .update(users)
- .set({ inviteQuota: newQuantity })
- .where(eq(users.id, user.id));
- } catch (dbError) {
- console.error(
- "CRITICAL: Stripe updated to quantity",
- newQuantity,
- "but DB update failed for user",
- user.id,
- dbError,
- );
- throw new Error(
- "Billing update succeeded but local state could not be saved. Please contact support.",
- );
- }
} catch (error) {
- if (wasCanceling && !quantityUpdated) {
+ if (wasCanceling) {
await restoreScheduledCancellation();
}
throw error;
}
+ try {
+ await db()
+ .update(users)
+ .set({ inviteQuota: newQuantity })
+ .where(eq(users.id, user.id));
+ } catch (dbError) {
+ console.error(
+ "CRITICAL: Stripe updated to quantity",
+ newQuantity,
+ "but DB update failed for user",
+ user.id,
+ dbError,
+ );
+ throw new Error(
+ "Billing update succeeded but local state could not be saved. Please contact support.",
+ );
+ }
+
revalidatePath("/dashboard/settings/organization");
return { success: true, newQuantity, cancelAtPeriodEnd: false };