feat(billing): add creditGrant idempotent method + source field on entries#3662
Conversation
…tries Adds creditGrant(orgId, amount, source) to BillingExtraBalanceRepository — mirrors creditPack's 2-step pattern but uses a synthetic refId (source-orgId) for idempotency instead of stripeSessionId. Adds source enum field to LedgerEntrySchema + ExtraBalanceCreditGrant Zod schema for input validation.
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (4)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3662 +/- ##
==========================================
+ Coverage 89.21% 89.43% +0.21%
==========================================
Files 137 137
Lines 4683 4714 +31
Branches 1455 1466 +11
==========================================
+ Hits 4178 4216 +38
+ Misses 393 389 -4
+ Partials 112 109 -3
Flags with carried forward coverage won't be shown. Click here to find out more. Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds support in the billing “extra balance” ledger for non-Stripe credit grants (e.g. free-tier signup grants) with idempotent application semantics, plus schema/index support to tag ledger entries with a source.
Changes:
- Added
BillingExtraBalanceRepository.creditGrant(orgId, amount, source)implementing a 2-step get-or-create + idempotent ledger push. - Extended the ExtraBalance ledger entry schemas (Mongoose + Zod) with a
sourceenum and introduced anExtraBalanceCreditGrantZod input schema. - Added a sparse compound index on
ledger.refId+ledger.sourceand expanded unit tests for the new grant behavior and schema.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| modules/billing/repositories/billing.extraBalance.repository.js | Adds creditGrant() repository method and exports it. |
| modules/billing/models/billing.extraBalance.schema.js | Adds GrantSource + source on ledger entries and ExtraBalanceCreditGrant schema. |
| modules/billing/models/billing.extraBalance.model.mongoose.js | Adds source field to LedgerEntrySchema and adds an index on ledger.refId/ledger.source. |
| modules/billing/tests/billing.extraBalance.unit.tests.js | Adds unit tests for creditGrant behavior and ExtraBalanceCreditGrant schema validation. |
…ents - creditGrant now validates source via Zod (ExtraBalanceCreditGrant.parse) before write — findOneAndUpdate does not run validators so the explicit parse is necessary - Add test for Zod throw on invalid source enum value - Fix index comment: $ne predicate does not use tight index bounds (noted actual benefit) - Fix GrantSource/LedgerEntry comments: creditCompensation does NOT set source; source is exclusively set by creditGrant and future grant methods
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
Summary
creditGrant(orgId, amount, source)toBillingExtraBalanceRepository— mirrorscreditPack2-step pattern (getOrCreate → idempotency-guarded push) but uses a syntheticrefId(source-orgId) instead ofstripeSessionId, enabling non-Stripe grants (e.g. free tier signup)sourceenum field (signup_grant | adjustment) to MongooseLedgerEntrySchema+ ZodLedgerEntryschema (mirrors all 3 perfeedback_enum_all_schemas)ExtraBalanceCreditGrantZod input schema (nostripeSessionIdrequired){ledger.refId, ledger.source}for grant idempotency lookupscreditPackunchanged — Stripe topup entries omitsourcefieldPart of N2 Free Tier Grant
Task 2 of 6. Depends on: Task 1 (#3661 merged) —
signupGrant/oneShotconfig fields on Free plan.Next: Task 3 —
BillingSignupGrantService+ signup hook.Test plan
billing.extraBalance.unit.tests.js— 14 new tests: creditGrant idempotency (duplicate returns applied:false + reason:duplicate_grant), step-1 upsert, step-2 no-upsert, refId filter shape, invalid orgId guard, zero/negative/empty-source guards; ExtraBalanceCreditGrant schema: valid no-stripeSessionId, valid sources, rejects 0 amount, rejects invalid orgId, rejects unknown source$sortArrayrequires MongoDB 5.2+, not available in CI)