Commit c568a3c
authored
feat(billing): meter service, extras balance, weekly reset (PR-N2) (#3536)
* feat(billing): add meter service, extras balance, weekly reset (PR-N2)
Add compute attribution layer built on PR-N1 plan versioning.
New files:
- billing.extraBalance model + schema (ledger-based, atomic topup/debit/expiration)
- billing.extraBalance repository (creditPack, debit, addExpirationEntries, getBalance)
- billing.extra.service (creditPack, debit, expireOldEntries, refundPartial, listLedger)
- billing.meter.service (unitsFromCosts via frozen ratios, attribute with replay protection)
- billing.reset.service (resetWeek archive-then-upsert, resetAllDue, isoWeekKey)
Modified files:
- billing.usage.repository: add findByWeek, incrementMeter (atomic, idempotency via $ne)
- billing.usage.service: add currentWeekKey, incrementMeter (overflow + thresholds), getMeter
- billing.plan.service: add bumpVersionWithRetry (exponential backoff on E11000)
All new code paths gated behind config.billing.meterMode=false (backward compat).
79 new unit tests (739 total, up from 660).
Refs #3533
* refactor(billing): apply critical-review fixes — repository pattern + edge guards
HIGH: Move all mongoose access out of the 3 target services into repositories.
- extraBalance.repository: add refundPartial() atomic ledger write
- usage.repository: add markThreshold(), archiveOtherWeeks(), upsertWeekSnapshot()
- subscription.repository: add findAllDueForReset()
(also fixes BillingSubscription->Subscription model name bug in resetAllDue)
MEDIUM: refundPartial ambiguous-pack guard — return applied=false+reason
when 0 or >1 packs share same meterUnits; TODO PR-N3 for packId from metadata.
resetAllDue window query limitation documented with TODO PR-N5.
LOW: Remove no-op sparse:true from LedgerEntry subdocument fields.
Add z.refine(n !== 0) on LedgerEntry.amount to reject zero-amount entries.
Tests: 749 passing (+14 new tests for new repo methods + edge cases).
* fix(billing): objectId guards, sign-convention docs, $setOnInsert fields
- add ObjectId.isValid guard to all BillingExtraBalance repo methods
- fix $setOnInsert to init alertedAt80/100 and meterBreakdown on upsert
- fix month fallback: weekKey.slice produced invalid YYYY-W string
- reconcile sign-convention docs: refund entries are negative clawbacks
- update attribute() JSDoc: no MeterQuotaExhausted throw, best-effort
- expand getBillingEvents JSDoc with async and @returns annotation
- add regression tests for all above fixes
* fix(billing): add biome-ignore comment to isValidOrgId for Codacy false positive
* fix(billing): correct organization field name in findAllDueForReset
The Subscription model uses 'organization' (not 'organizationId').
Projection + service consumer were both broken, silently making
resetAllDue a no-op (sub.organizationId always undefined).
Caught by critical-review pass 2 on PR-N2.
* fix(billing): apply CodeRabbit pass 2 — guards, sign-by-kind, idempotency, threshold gating
- CRITICAL: null-guard getOrCreate in refundPartial + listLedger
- Mongoose: validator rejecting zero ledger amounts
- Zod: superRefine sign-by-kind (topup/adjustment > 0; debit/expiry/refund < 0)
- Repository: input guards on creditPack/debit/refundPartial (amount > 0, non-empty key)
- Repository: breakdown $inc filter (only Number.isFinite && > 0 entries)
- Service: refund idempotency key includes topupEntry._id to avoid collision
- Service: attribute() reports extrasConsumed=0 when debit returns applied=false
- Service: threshold event emitted only when markThreshold modifiedCount > 0
- Service: bumpVersionWithRetry maxAttempts guard (must be positive integer)
- JSDoc: resetWeek @returns includes null; all named test helpers documented
- Tests: fixtures use sub.organization not organizationId; regression guard added
- Tests: new tests for every added guard1 parent 0f2320d commit c568a3c
17 files changed
Lines changed: 3349 additions & 0 deletions
File tree
- modules/billing
- models
- repositories
- services
- tests
Lines changed: 147 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 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 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 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 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
0 commit comments