Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add expiring balance credits for administrator grants and usage-mode redemption codes. This feature is independent of the file-response fix in #905.
Behavior and approach
Keep the existing account balance as the total balance and track the expirable portion in billing_expiring_credits. All balance mutations lock the account before its credit batches. Balance-sensitive operations capture one timestamp after acquiring the account lock, so a lock wait crossing a credit's expiration cannot leave expired funds available.
Balance-sensitive reads, deposits, redemptions, reservations and settlement expire due credits before calculating funds. A cancellable background worker also sweeps on startup and every minute. Administrator account lists prefetch due user IDs in one query and expire only the requested users, avoiding one expiration lookup per account.
A grant first repays existing debt; only its unspent remainder can expire. Administrative reductions also reduce active batches, preventing a later expiration from charging the same funds again. A reservation does not extend a grant's lifetime. Actual usage settling after expiration continues to follow the existing full-cost settlement behavior and may leave a negative balance.
Usage-mode redemption codes use optional balanceDurationDays; subscription codes retain integer durationDays. The code's own redemption deadline remains independent of the awarded balance's lifetime.
Change type
Affected areas
Verification
Backend checks used Go 1.26.10. Frontend and contract checks used Node 26.12.1 with pnpm 10.17.0.
Screenshots, API examples, or logs
Administrator grant:
POST /api/v1/admin/billing/accounts/42/expiring-credits
{
"creditUSD": 20,
"durationDays": 0.1,
"idempotencyKey": "campaign-user-42-001",
"description": "Trial credit"
}
The response contains the original grant and the current account balance. Retrying with identical details does not deposit again. Changing the amount, lifetime, administrator or description while reusing the key returns a conflict.
A usage-mode redemption code can set balanceDurationDays=0.1. Omission or zero keeps its award permanent. The balance lifetime starts when the code is redeemed.
Configuration, migration, and compatibility notes
Documentation
Security and privacy
Checklist