#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
feat(billing): report Team and Processor as independent holdings (#7868)
ConnorYohb29fef286355
diff --git a/app/saas/src/main/java/stirling/software/saas/payg/api/PaygWalletController.java b/app/saas/src/main/java/stirling/software/saas/payg/api/PaygWalletController.javaindex 77b6b13f5d..1d87514190 100644--- a/app/saas/src/main/java/stirling/software/saas/payg/api/PaygWalletController.java+++ b/app/saas/src/main/java/stirling/software/saas/payg/api/PaygWalletController.java@@ -36,6 +36,7 @@ import stirling.software.proprietary.model.TeamMembership;import stirling.software.proprietary.security.database.repository.UserRepository;import stirling.software.proprietary.security.model.User;import stirling.software.proprietary.security.repository.TeamMembershipRepository;+import stirling.software.saas.model.SaasTeamExtensions;import stirling.software.saas.payg.api.WalletSnapshotResponse.ActivityRow;import stirling.software.saas.payg.api.WalletSnapshotResponse.CategoryBreakdown;import stirling.software.saas.payg.api.WalletSnapshotResponse.MemberRow;@@ -53,6 +54,7 @@ import stirling.software.saas.payg.repository.WalletLedgerRepository;import stirling.software.saas.payg.repository.WalletPolicyRepository;import stirling.software.saas.payg.wallet.WalletLedgerEntry;import stirling.software.saas.payg.wallet.WalletPolicy;+import stirling.software.saas.repository.SaasTeamExtensionsRepository;import stirling.software.saas.security.UserTeamResolver;import stirling.software.saas.util.AuthenticationUtils;@@ -110,6 +112,7 @@ public class PaygWalletController {private final UserRepository userRepository;private final PrepaidBundleService prepaidBundleService;private final UserTeamResolver userTeamResolver;+ private final SaasTeamExtensionsRepository teamExtensionsRepository;public PaygWalletController(EntitlementService entitlementService,@@ -121,7 +124,8 @@ public class PaygWalletController {PaygShadowChargeRepository shadowRepo,UserRepository userRepository,PrepaidBundleService prepaidBundleService,- UserTeamResolver userTeamResolver) {+ UserTeamResolver userTeamResolver,+ SaasTeamExtensionsRepository teamExtensionsRepository) {this.entitlementService = Objects.requireNonNull(entitlementService, "entitlementService");this.billingService = Objects.requireNonNull(billingService, "billingService");this.memberRepo = Objects.requireNonNull(memberRepo, "memberRepo");@@ -133,6 +137,8 @@ public class PaygWalletController {this.prepaidBundleService =Objects.requireNonNull(prepaidBundleService, "prepaidBundleService");this.userTeamResolver = Objects.requireNonNull(userTeamResolver, "userTeamResolver");+ this.teamExtensionsRepository =+ Objects.requireNonNull(teamExtensionsRepository, "teamExtensionsRepository");}/** The single wallet fetch the frontend makes; every figure on the Plan page comes from it. */@@ -165,6 +171,9 @@ public class PaygWalletController {EntitlementSnapshot snap = entitlementService.getSnapshot(teamId);String status = billing.subscribed() ? STATUS_SUBSCRIBED : STATUS_FREE;+ WalletSnapshotResponse.ProcessorHolding processor =+ new WalletSnapshotResponse.ProcessorHolding(billing.subscribed());+ WalletSnapshotResponse.TeamHolding team = teamHolding(teamId);boolean noCap = billing.subscribed() && billing.capMoneyMinor() == null;Integer capMajor =@@ -214,6 +223,8 @@ public class PaygWalletController {new WalletSnapshotResponse(teamId,status,+ team,+ processor,isLeader ? ROLE_LEADER : ROLE_MEMBER,ISO_DATE.format(snap.periodStart().toLocalDate()),ISO_DATE.format(snap.periodEnd().toLocalDate()),@@ -243,6 +254,25 @@ public class PaygWalletController {return ResponseEntity.ok(body);}+ /**+ * The team's user-capacity holding.+ *+ * <p>The cap is written from the Team subscription, so a team holds Team exactly when it+ * carries a real one. Integer.MAX_VALUE is the sentinel a team carries before it ever holds a+ * Team plan, and it reports as no holding and no limit rather than as a number, so nothing+ * downstream does arithmetic on it.+ */+ private WalletSnapshotResponse.TeamHolding teamHolding(Long teamId) {+ int usersInUse = Math.toIntExact(memberRepo.countByTeamId(teamId));+ Integer licensed =+ teamExtensionsRepository+ .findByTeamId(teamId)+ .map(SaasTeamExtensions::getMaxSeats)+ .filter(max -> max != null && max > 0 && max < Integer.MAX_VALUE)+ .orElse(null);+ return new WalletSnapshotResponse.TeamHolding(licensed != null, licensed, usersInUse);+ }+/** Per-category size-scaled units + input-file counts for the same window. */private record BreakdownPair(CategoryBreakdown units, CategoryBreakdown docs) {}@@ -475,6 +505,8 @@ public class PaygWalletController {return new WalletSnapshotResponse(null, // teamId — unknown when the caller has no team membershipSTATUS_FREE,+ new WalletSnapshotResponse.TeamHolding(false, null, 0),+ new WalletSnapshotResponse.ProcessorHolding(false),ROLE_MEMBER,ISO_DATE.format(window[0].toLocalDate()),ISO_DATE.format(window[1].toLocalDate()),diff --git a/app/saas/src/main/java/stirling/software/saas/payg/api/WalletSnapshotResponse.java b/app/saas/src/main/java/stirling/software/saas/payg/api/WalletSnapshotResponse.javaindex baf3081edc..344656fe6b 100644--- a/app/saas/src/main/java/stirling/software/saas/payg/api/WalletSnapshotResponse.java+++ b/app/saas/src/main/java/stirling/software/saas/payg/api/WalletSnapshotResponse.java@@ -18,8 +18,12 @@ import java.util.List;* @param teamId the caller's primary team_id. Needed by the frontend so it can pass it to the* Supabase edge functions that create Stripe Checkout / portal sessions — those run outside* Spring Security and have no other way to resolve the caller's team.- * @param status {@code "free"} when the team has no Stripe subscription; {@code "subscribed"} once- * a card is on file and the engine bills meter events.+ * @param status the old single billing axis. Superseded by {@code team} and {@code processor},+ * which say which products the team holds independently; kept until the frontend stops reading+ * it. {@code "free"} when the team has no Stripe subscription; {@code "subscribed"} once a card+ * is on file and the engine bills meter events.+ * @param team the Team (user capacity) holding — see {@link TeamHolding}.+ * @param processor the Processor (metered automation) holding — see {@link ProcessorHolding}.* @param role the current caller's role within their team — {@code "leader"} or {@code "member"}.* Controls which UI variant the frontend renders.* @param billingPeriodStart inclusive ISO date (yyyy-MM-dd) for the current cycle — the Stripe@@ -64,6 +68,8 @@ import java.util.List;public record WalletSnapshotResponse(Long teamId,String status,+ TeamHolding team,+ ProcessorHolding processor,String role,String billingPeriodStart,String billingPeriodEnd,@@ -130,4 +136,28 @@ public record WalletSnapshotResponse(* @param docUnits absolute document count of the entry*/public record ActivityRow(long id, String kind, String label, String ts, int docUnits) {}++ /**+ * The Team holding: paid user capacity.+ *+ * <p>Reported separately from {@link ProcessorHolding} because the two products are orthogonal+ * — a team may hold either, both, or neither — and a single {@code free | subscribed} axis+ * cannot say which. A caller decides what to offer from {@code held}, not from {@code status}.+ *+ * @param held the team pays for user capacity. False means no Team plan, so a caller offers it,+ * rather than meaning capacity is unknown.+ * @param licensedUsers how many users the holding covers; {@code null} when the team has no+ * user limit.+ * @param usersInUse team members occupying capacity right now.+ */+ public record TeamHolding(boolean held, Integer licensedUsers, int usersInUse) {}++ /**+ * The Processor holding: metered document automation beyond the free grant.+ *+ * @param active the team has a live metered subscription. This is the fact the old {@code+ * status == "subscribed"} actually carried; the rate, spend, cap and grant figures for it+ * stay on the enclosing record.+ */+ public record ProcessorHolding(boolean active) {}}diff --git a/app/saas/src/test/java/stirling/software/saas/payg/api/PaygWalletControllerTest.java b/app/saas/src/test/java/stirling/software/saas/payg/api/PaygWalletControllerTest.javaindex 644b7edd8f..8f56c253cd 100644--- a/app/saas/src/test/java/stirling/software/saas/payg/api/PaygWalletControllerTest.java+++ b/app/saas/src/test/java/stirling/software/saas/payg/api/PaygWalletControllerTest.java@@ -35,6 +35,7 @@ import stirling.software.proprietary.model.TeamMembership;import stirling.software.proprietary.security.database.repository.UserRepository;import stirling.software.proprietary.security.model.User;import stirling.software.proprietary.security.repository.TeamMembershipRepository;+import stirling.software.saas.model.SaasTeamExtensions;import stirling.software.saas.payg.api.PaygWalletController.UpdateCapRequest;import stirling.software.saas.payg.api.WalletSnapshotResponse.MemberRow;import stirling.software.saas.payg.billing.TeamBillingContext;@@ -52,6 +53,7 @@ import stirling.software.saas.payg.repository.PaygTeamExtensionsRepository;import stirling.software.saas.payg.repository.WalletLedgerRepository;import stirling.software.saas.payg.repository.WalletPolicyRepository;import stirling.software.saas.payg.wallet.WalletPolicy;+import stirling.software.saas.repository.SaasTeamExtensionsRepository;import stirling.software.saas.security.EnhancedJwtAuthenticationToken;import stirling.software.saas.security.UserTeamResolver;@@ -72,6 +74,7 @@ class PaygWalletControllerTest {@Mock private PaygShadowChargeRepository shadowRepo;@Mock private UserRepository userRepository;@Mock private PrepaidBundleService prepaidBundleService;+ @Mock private SaasTeamExtensionsRepository teamExtensionsRepository;private PaygWalletController controller;@@ -88,7 +91,8 @@ class PaygWalletControllerTest {shadowRepo,userRepository,prepaidBundleService,- new UserTeamResolver(memberRepo));+ new UserTeamResolver(memberRepo),+ teamExtensionsRepository);}/**@@ -144,6 +148,87 @@ class PaygWalletControllerTest {// GET /wallet// -----------------------------------------------------------------------------------------+ /**+ * Team and Credits are independently purchasable, so the snapshot reports them as separate+ * facts. A free team holds neither; the collapsed status string cannot express that difference.+ */+ @Test+ void getWallet_freeTeam_holdsNeitherProduct() {+ User user = userWithId(60L, UUID.randomUUID());+ Team team = teamWithId(60L);+ when(userRepository.findBySupabaseId(any())).thenReturn(Optional.of(user));+ user.setTeam(team);+ when(memberRepo.findByTeamIdAndUserId(team.getId(), 60L))+ .thenReturn(Optional.of(membership(team, user, TeamRole.MEMBER)));+ when(memberRepo.countByTeamId(60L)).thenReturn(3L);+ when(billingService.forTeam(60L)).thenReturn(freeBilling(500L));+ when(entitlementService.getSnapshot(60L)).thenReturn(snapshot(0L, 500L));+ stubEmptyLedgerReads(60L);++ WalletSnapshotResponse body = controller.getWallet(jwtAuth(user.getSupabaseId())).getBody();++ assertThat(body).isNotNull();+ assertThat(body.processor().active()).isFalse();+ assertThat(body.team().held()).isFalse();+ // The member count is real, so the capacity meter already has a numerator.+ assertThat(body.team().usersInUse()).isEqualTo(3);+ // Cloud has no user limit today, so there is no denominator to report.+ assertThat(body.team().licensedUsers()).isNull();+ }++ /** A team carrying a real cap holds Team, and reports it as the meter's denominator. */+ @Test+ void getWallet_teamPlan_reportsTheLicensedUsers() {+ User user = userWithId(62L, UUID.randomUUID());+ Team team = teamWithId(62L);+ when(userRepository.findBySupabaseId(any())).thenReturn(Optional.of(user));+ user.setTeam(team);+ when(memberRepo.findByTeamIdAndUserId(team.getId(), 62L))+ .thenReturn(Optional.of(membership(team, user, TeamRole.MEMBER)));+ when(memberRepo.countByTeamId(62L)).thenReturn(40L);+ SaasTeamExtensions ext = new SaasTeamExtensions();+ ext.setMaxSeats(100);+ when(teamExtensionsRepository.findByTeamId(62L)).thenReturn(Optional.of(ext));+ when(billingService.forTeam(62L)).thenReturn(freeBilling(500L));+ when(entitlementService.getSnapshot(62L)).thenReturn(snapshot(0L, 500L));+ stubEmptyLedgerReads(62L);++ WalletSnapshotResponse body = controller.getWallet(jwtAuth(user.getSupabaseId())).getBody();++ assertThat(body).isNotNull();+ assertThat(body.team().held()).isTrue();+ assertThat(body.team().licensedUsers()).isEqualTo(100);+ assertThat(body.team().usersInUse()).isEqualTo(40);+ // Team and Processor stay independent: a Team plan says nothing about the meter.+ assertThat(body.processor().active()).isFalse();+ }++ /**+ * A metered subscription switches Credits on and says nothing about Team: the two axes move+ * independently, which is the whole point of reporting them separately.+ */+ @Test+ void getWallet_subscribedTeam_holdsCreditsButNotTeam() {+ User user = userWithId(61L, UUID.randomUUID());+ Team team = teamWithId(61L);+ when(userRepository.findBySupabaseId(any())).thenReturn(Optional.of(user));+ user.setTeam(team);+ when(memberRepo.findByTeamIdAndUserId(team.getId(), 61L))+ .thenReturn(Optional.of(membership(team, user, TeamRole.MEMBER)));+ when(memberRepo.countByTeamId(61L)).thenReturn(8L);+ when(billingService.forTeam(61L))+ .thenReturn(subscribedBilling("sub_decoupled", 2500L, 1250L));+ when(entitlementService.getSnapshot(61L)).thenReturn(snapshot(100L, 1250L));+ stubEmptyLedgerReads(61L);++ WalletSnapshotResponse body = controller.getWallet(jwtAuth(user.getSupabaseId())).getBody();++ assertThat(body).isNotNull();+ assertThat(body.processor().active()).isTrue();+ assertThat(body.team().held()).isFalse();+ assertThat(body.team().usersInUse()).isEqualTo(8);+ }+@Testvoid getWallet_freeTier_returnsFreeShape() {User user = userWithId(7L, UUID.randomUUID());diff --git a/app/saas/src/test/java/stirling/software/saas/payg/api/WalletSnapshotResponseTest.java b/app/saas/src/test/java/stirling/software/saas/payg/api/WalletSnapshotResponseTest.javaindex 7960def2a9..7a0c0e7866 100644--- a/app/saas/src/test/java/stirling/software/saas/payg/api/WalletSnapshotResponseTest.java+++ b/app/saas/src/test/java/stirling/software/saas/payg/api/WalletSnapshotResponseTest.java@@ -19,6 +19,8 @@ class WalletSnapshotResponseTest {return new WalletSnapshotResponse(7L,"subscribed",+ new WalletSnapshotResponse.TeamHolding(true, 100, 12),+ new WalletSnapshotResponse.ProcessorHolding(true),"leader","2026-06-01","2026-07-01",@@ -100,6 +102,8 @@ class WalletSnapshotResponseTest {new WalletSnapshotResponse(7L,"free",+ new WalletSnapshotResponse.TeamHolding(false, null, 1),+ new WalletSnapshotResponse.ProcessorHolding(false),"member","2026-06-01","2026-07-01",diff --git a/frontend/editor/src/portal/components/billing/walletFixtures.ts b/frontend/editor/src/portal/components/billing/walletFixtures.tsindex eb9c8fc02f..81f630adae 100644--- a/frontend/editor/src/portal/components/billing/walletFixtures.ts+++ b/frontend/editor/src/portal/components/billing/walletFixtures.ts@@ -4,6 +4,8 @@ import type { Wallet } from "@portal/api/billing";export const freeWallet: Wallet = {teamId: 42,status: "free",+ team: { held: false, licensedUsers: null, usersInUse: 1 },+ processor: { active: false },role: "leader",billingPeriodStart: "2026-06-01",billingPeriodEnd: "2026-06-30",@@ -36,6 +38,8 @@ export const freeWallet: Wallet = {export const subscribedWallet: Wallet = {teamId: 42,status: "subscribed",+ team: { held: false, licensedUsers: null, usersInUse: 4 },+ processor: { active: true },role: "leader",billingPeriodStart: "2026-06-01",billingPeriodEnd: "2026-06-30",diff --git a/frontend/editor/src/proprietary/billing/types.ts b/frontend/editor/src/proprietary/billing/types.tsindex 279ae503bf..47f33a60b7 100644--- a/frontend/editor/src/proprietary/billing/types.ts+++ b/frontend/editor/src/proprietary/billing/types.ts@@ -32,10 +32,40 @@ export interface WalletActivityRow {docUnits: number;}+/**+ * The Team holding: paid user capacity. Independent of {@link ProcessorHolding} — a team may hold+ * either product, both, or neither, which `status` alone cannot express.+ */+export interface TeamHolding {+ /**+ * The team pays for user capacity. False means no Team plan, so a caller offers it, rather than+ * meaning capacity is unknown.+ */+ held: boolean;+ /** Users the holding covers; null when the team has no user limit. */+ licensedUsers: number | null;+ /** Members occupying capacity right now — the capacity meter's numerator. */+ usersInUse: number;+}++/** The Processor holding: metered automation beyond the free grant. */+export interface ProcessorHolding {+ /** The team has a live metered subscription. The fact `status: "subscribed"` actually carried. */+ active: boolean;+}+export interface Wallet {/** Caller's primary team_id; null on the synthetic empty snapshot for team-less callers. */teamId: number | null;+ /**+ * The old single billing axis. Superseded by `team` and `processor`, which say which products the+ * team holds independently; kept until every consumer stops branching on it.+ */status: WalletStatus;+ /** Paid user capacity, reported independently of Credits. */+ team: TeamHolding;+ /** Metered automation, reported independently of Team. */+ processor: ProcessorHolding;role: WalletRole;/** ISO yyyy-mm-dd. Stripe period when subscribed; calendar month when free. */billingPeriodStart: string;diff --git a/frontend/editor/src/saas/components/shared/FreeLimitReachedModal.test.tsx b/frontend/editor/src/saas/components/shared/FreeLimitReachedModal.test.tsxindex 8a342b1297..b30d4aefbb 100644--- a/frontend/editor/src/saas/components/shared/FreeLimitReachedModal.test.tsx+++ b/frontend/editor/src/saas/components/shared/FreeLimitReachedModal.test.tsx@@ -12,6 +12,8 @@ import type { Wallet } from "@app/hooks/useWallet";const wallet: Wallet = {teamId: 1,status: "free",+ team: { held: false, licensedUsers: null, usersInUse: 1 },+ processor: { active: false },role: "leader",billingPeriodStart: "2026-06-01",billingPeriodEnd: "2026-06-30",diff --git a/frontend/editor/src/saas/hooks/walletDevPreview.ts b/frontend/editor/src/saas/hooks/walletDevPreview.tsindex 1bce8eb2f8..98e0ce0aa3 100644--- a/frontend/editor/src/saas/hooks/walletDevPreview.ts+++ b/frontend/editor/src/saas/hooks/walletDevPreview.ts@@ -47,6 +47,8 @@ function buildDevPreviewWallet(role: WalletRole): Wallet {return {teamId: null,status: subscribed ? "subscribed" : "free",+ team: { held: false, licensedUsers: null, usersInUse: 1 },+ processor: { active: subscribed },role,billingPeriodStart: isoDay(periodStart),billingPeriodEnd: isoDay(periodEnd),