Commit 9750556
fix(billing): surface subscription-past-due and cutoff-scheduled tenants in billing health (#514)
`getAtRiskTenants()` fetched a single population — `tenants.billing_status =
'past_due'` — so two of the three groups #493 §1.2 asked for were invisible:
- a tenant whose `platform_subscriptions.status` went past due without
`tenants.billing_status` being synced never appeared, which is exactly the
drift the epic wanted visible;
- a tenant over its plan limits with an `access_cutoff_at` scheduled by #494
but healthy billing never appeared either, so the cutoff column could only
ever hold a value for a tenant that was *also* past due.
Fetch the union of the three conditions and carry the `reasons` a tenant
qualified under through the pure layer, so the cases stay distinguishable
instead of collapsing into one undifferentiated list. PostgREST cannot express
a disjunction spanning two tables, hence three reads merged by tenant id
rather than a single `.or()`.
Also rank subscription rows deterministically (past_due > active > other, tie
broken by `updated_at` desc) instead of last-wins over an unordered list. To be
precise about severity: `platform_subscriptions` carries UNIQUE (tenant_id) and
every write path upserts on it, so a tenant cannot hold two rows today — this
closes a latent trap rather than a live miscount.
The metric cards keep counting past-due tenants only, so their meaning does not
silently change now that over-limit tenants are listed; scheduled cutoffs get
their own card, and the empty state no longer claims "no schools past-due" for
a list that is no longer only about payment.1 parent 69613aa commit 9750556
4 files changed
Lines changed: 329 additions & 34 deletions
File tree
- app
- [locale]/platform/billing-health
- actions/platform
- lib/billing
- tests/unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
11 | 19 | | |
12 | 20 | | |
13 | 21 | | |
14 | | - | |
15 | | - | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
16 | 31 | | |
17 | 32 | | |
18 | 33 | | |
19 | 34 | | |
20 | 35 | | |
21 | | - | |
| 36 | + | |
22 | 37 | | |
23 | 38 | | |
24 | 39 | | |
| |||
40 | 55 | | |
41 | 56 | | |
42 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
43 | 66 | | |
44 | 67 | | |
45 | 68 | | |
46 | 69 | | |
47 | 70 | | |
48 | 71 | | |
49 | 72 | | |
50 | | - | |
| 73 | + | |
| 74 | + | |
51 | 75 | | |
52 | 76 | | |
53 | 77 | | |
54 | | - | |
| 78 | + | |
55 | 79 | | |
56 | 80 | | |
57 | 81 | | |
| |||
80 | 104 | | |
81 | 105 | | |
82 | 106 | | |
83 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
84 | 110 | | |
85 | 111 | | |
86 | 112 | | |
87 | 113 | | |
88 | 114 | | |
89 | 115 | | |
90 | 116 | | |
| 117 | + | |
91 | 118 | | |
92 | 119 | | |
93 | 120 | | |
| |||
99 | 126 | | |
100 | 127 | | |
101 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
102 | 138 | | |
103 | 139 | | |
104 | 140 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
7 | 11 | | |
8 | 12 | | |
9 | 13 | | |
| |||
12 | 16 | | |
13 | 17 | | |
14 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
15 | 31 | | |
16 | 32 | | |
17 | 33 | | |
18 | 34 | | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
23 | 53 | | |
24 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
25 | 91 | | |
26 | 92 | | |
27 | 93 | | |
28 | 94 | | |
29 | | - | |
| 95 | + | |
30 | 96 | | |
31 | 97 | | |
32 | 98 | | |
33 | 99 | | |
34 | | - | |
| 100 | + | |
35 | 101 | | |
36 | 102 | | |
37 | 103 | | |
38 | 104 | | |
| 105 | + | |
39 | 106 | | |
40 | 107 | | |
41 | 108 | | |
| 109 | + | |
42 | 110 | | |
43 | 111 | | |
44 | 112 | | |
| 113 | + | |
45 | 114 | | |
46 | 115 | | |
47 | 116 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
5 | 15 | | |
6 | 16 | | |
7 | 17 | | |
| |||
20 | 30 | | |
21 | 31 | | |
22 | 32 | | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
23 | 45 | | |
24 | 46 | | |
25 | 47 | | |
26 | 48 | | |
27 | 49 | | |
| 50 | + | |
28 | 51 | | |
29 | 52 | | |
30 | 53 | | |
31 | 54 | | |
| 55 | + | |
32 | 56 | | |
33 | 57 | | |
34 | 58 | | |
| 59 | + | |
35 | 60 | | |
36 | 61 | | |
37 | 62 | | |
| |||
46 | 71 | | |
47 | 72 | | |
48 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
49 | 112 | | |
50 | 113 | | |
51 | 114 | | |
| |||
55 | 118 | | |
56 | 119 | | |
57 | 120 | | |
58 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
59 | 125 | | |
60 | 126 | | |
61 | 127 | | |
| |||
79 | 145 | | |
80 | 146 | | |
81 | 147 | | |
| 148 | + | |
82 | 149 | | |
83 | 150 | | |
84 | 151 | | |
| |||
0 commit comments