Skip to content

refactor: rename flat fee credit allocation handler#4362

Merged
turip merged 2 commits into
mainfrom
codex/flatfee-handler-renames
May 15, 2026
Merged

refactor: rename flat fee credit allocation handler#4362
turip merged 2 commits into
mainfrom
codex/flatfee-handler-renames

Conversation

@turip
Copy link
Copy Markdown
Member

@turip turip commented May 15, 2026

Summary

  • rename the flat-fee credit allocation handler to OnAllocateCredits
  • route credit-only flat-fee allocation through the same handler
  • rename flat-fee credit allocation correction handler to OnCorrectCreditAllocations

Verification

  • direnv exec . env POSTGRES_HOST=127.0.0.1 GOCACHE=/tmp/go-build-openmeter-handler-renames go test -count=1 -tags=dynamic ./openmeter/billing/charges/flatfee/... ./openmeter/billing/charges/service ./openmeter/ledger/chargeadapter ./test/credits
  • direnv exec . env GOCACHE=/tmp/go-build-openmeter-handler-renames make lint-go-fast

Summary by CodeRabbit

  • Refactor

    • Consolidated credit allocation and correction flows into unified allocation events, simplifying billing behavior during invoice and payment processing.
    • Standardized allocation input handling and validation to prevent negative pre-tax allocations.
  • Tests

    • Updated and reorganized test coverage to exercise the new allocation and correction flows end-to-end.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 37749b69-a8fb-4981-b3ed-43a5f4be235d

📥 Commits

Reviewing files that changed from the base of the PR and between 268dcd4 and afc4fca.

📒 Files selected for processing (1)
  • openmeter/billing/charges/testutils/handlers.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • openmeter/billing/charges/testutils/handlers.go

📝 Walkthrough

Walkthrough

This PR consolidates flat-fee credit allocation and correction handling by replacing three handler methods with two unified ones, introducing OnAllocateCreditsInput (PreTaxAmountToAllocate) and CorrectCreditAllocationsInput (AllocateAt), and updating services, ledger adapter, mocks, and tests to the new contract.

Changes

Flat-Fee Handler Consolidation

Layer / File(s) Summary
Handler contract and input types
openmeter/billing/charges/flatfee/handler.go
New OnAllocateCreditsInput validates non-negative PreTaxAmountToAllocate; CorrectCreditAllocationsInput adds AllocateAt. Handler interface replaces OnAssignedToInvoice, OnCreditsOnlyUsageAccrued, and OnCreditsOnlyUsageAccruedCorrection with OnAllocateCredits and OnCorrectCreditAllocations.
Service layer handler invocations
openmeter/billing/charges/flatfee/service/realizations/creditsonly.go, .../credittheninvoice.go, .../realizations/correct.go
Service code now calls OnAllocateCredits for allocations and OnCorrectCreditAllocations for corrections; input validation and error wrapping updated, downstream realization/lineage wiring unchanged.
Ledger adapter handler implementation
openmeter/ledger/chargeadapter/flatfee.go
flatFeeHandler implements OnAllocateCredits (zero short-circuit, validation, accepts CreditThenInvoice/CreditOnly) and OnCorrectCreditAllocations; collector uses PreTaxAmountToAllocate.
Test mock implementations
openmeter/billing/charges/testutils/handlers.go, openmeter/billing/charges/service/handlers_test.go
Mocks and test handler doubles implement OnAllocateCredits and OnCorrectCreditAllocations with nil-callback guards; return allocation/correction inputs using ServicePeriod and PreTaxAmountToAllocate.
Ledger adapter tests
openmeter/ledger/chargeadapter/flatfee_test.go
Tests reorganized around TestOnAllocateCredits and TestOnCorrectCreditAllocations; helpers updated to construct OnAllocateCreditsInput (PreTaxAmountToAllocate).
Service-level invoicable tests
openmeter/billing/charges/service/invoicable_test.go
Multiple flat-fee tests wired to onAllocateCredits and updated assertions to validate PreTaxAmountToAllocate and callback invocation counts across scenarios.
Additional service tests and integration
openmeter/billing/charges/service/lineage_test.go, taxcode_test.go, test/credits/sanity_test.go
Lineage and tax-code tests switched to onAllocateCredits; sanity comments updated to reflect the new allocation hook.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested labels

release-note/misc

Suggested reviewers

  • tothandras
  • GAlexIHU
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately describes the main change: renaming flat-fee credit allocation handlers from generic/assignment-based names (OnAssignedToInvoice, OnCreditsOnlyUsageAccrued) to more semantically precise ones (OnAllocateCredits, OnCorrectCreditAllocations).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/flatfee-handler-renames

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@turip turip marked this pull request as ready for review May 15, 2026 08:15
@turip turip requested a review from a team as a code owner May 15, 2026 08:15
@turip turip marked this pull request as draft May 15, 2026 08:16
@turip turip marked this pull request as ready for review May 15, 2026 08:17
@turip turip added release-note/ignore Ignore this change when generating release notes area/billing labels May 15, 2026
@turip turip requested review from GAlexIHU and tothandras May 15, 2026 08:21
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@openmeter/billing/charges/testutils/handlers.go`:
- Around line 34-41: The mockFlatFeeHandler.OnAllocateCredits currently always
returns one allocation entry; change it to mirror the production handler by
returning nil, nil when input.PreTaxAmountToAllocate is zero. Update the
mockFlatFeeHandler.OnAllocateCredits implementation to check
input.PreTaxAmountToAllocate and only build and return
creditrealization.CreateAllocationInputs when the amount is non-zero, otherwise
return nil, nil; reference the mockFlatFeeHandler.OnAllocateCredits function and
the input.PreTaxAmountToAllocate field when making the change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4af9315e-d4af-410a-a59e-196b18625c44

📥 Commits

Reviewing files that changed from the base of the PR and between 6161e62 and 268dcd4.

📒 Files selected for processing (12)
  • openmeter/billing/charges/flatfee/handler.go
  • openmeter/billing/charges/flatfee/service/realizations/correct.go
  • openmeter/billing/charges/flatfee/service/realizations/creditsonly.go
  • openmeter/billing/charges/flatfee/service/realizations/credittheninvoice.go
  • openmeter/billing/charges/service/handlers_test.go
  • openmeter/billing/charges/service/invoicable_test.go
  • openmeter/billing/charges/service/lineage_test.go
  • openmeter/billing/charges/service/taxcode_test.go
  • openmeter/billing/charges/testutils/handlers.go
  • openmeter/ledger/chargeadapter/flatfee.go
  • openmeter/ledger/chargeadapter/flatfee_test.go
  • test/credits/sanity_test.go

Comment thread openmeter/billing/charges/testutils/handlers.go
@turip turip merged commit f67a8a1 into main May 15, 2026
26 checks passed
@turip turip deleted the codex/flatfee-handler-renames branch May 15, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/billing release-note/ignore Ignore this change when generating release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants