Skip to content

fix(ledger): nit comments from #4731 - #4839

Merged
mark-vass-konghq merged 2 commits into
mainfrom
fix/ledger-nit-comments-on-credit-only
Aug 4, 2026
Merged

fix(ledger): nit comments from #4731#4839
mark-vass-konghq merged 2 commits into
mainfrom
fix/ledger-nit-comments-on-credit-only

Conversation

@mark-vass-konghq

@mark-vass-konghq mark-vass-konghq commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Currency references can now be restored from JSON and parsed from text with validation.
    • Added helpers for creating fiat and custom currency test fixtures.
  • Bug Fixes

    • Improved handling of missing or promotional settlement currencies.
    • Currency conversion now distinguishes unavailable currencies from invalid settlements.
  • Tests

    • Updated currency test utilities and expanded serialization round-trip coverage.

Greptile Summary

The PR refines currency serialization and settlement-currency handling while consolidating currency test helpers.

  • Adds JSON and text unmarshalling support for currency references while preserving immutable custom-currency precision snapshots.
  • Represents absent promotional settlement currency as nil and validates payment settlement currencies before ledger booking.
  • Moves currency fixtures into consolidated test utility packages and updates affected tests.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
openmeter/currencies/currency.go Adds transactional JSON/text unmarshalling for fiat and versioned custom currency references while retaining the existing serialized format.
openmeter/billing/charges/creditpurchase/settlement.go Changes settlement currency retrieval to explicitly distinguish an absent promotional currency from invalid settlement state.
openmeter/ledger/chargeadapter/creditpurchase.go Adapts ledger currency selection to the optional settlement currency and validates currencies used by payment events.
openmeter/currencies/testutils/currency.go Consolidates fiat and custom currency fixture constructors into the main currency test utility package.
openmeter/currencies/testutils/env/env.go Relocates the currency integration-test environment into a dedicated subpackage without changing its service wiring.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  S[Credit purchase settlement] --> G[GetCurrency]
  G -->|Invoice or external| F[Fiat currency pointer]
  G -->|Promotional| N[nil currency]
  F --> P[Payment and cost-basis ledger routing]
  N --> C[Promotional credit issuance without payment currency]
Loading

Reviews (5): Last reviewed commit: "fix: settlementcurrency validation" | Re-trigger Greptile

Context used:

@mark-vass-konghq mark-vass-konghq self-assigned this Aug 3, 2026
@mark-vass-konghq
mark-vass-konghq requested a review from a team as a code owner August 3, 2026 10:27
@mark-vass-konghq mark-vass-konghq added release-note/feature Release note: Exciting New Features area/billing labels Aug 3, 2026
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The change adds JSON and text unmarshalling for CurrencyReference, consolidates currency test helpers under currencies/testutils, updates environment test helpers, and changes settlement currency resolution to use nullable pointers.

Changes

Currency handling

Layer / File(s) Summary
Currency reference decoding and coverage
openmeter/currencies/currency.go, openmeter/currencies/currency_test.go
CurrencyReference now supports JSON and text unmarshalling. Tests cover fiat, custom, resolved, and unresolved references.
Currency test utility consolidation
openmeter/currencies/testutils/*, openmeter/currencies/adapter/currencies_test.go, openmeter/currencies/currencyresolver/resolver_test.go, openmeter/currencies/service/service_test.go, api/v3/..., openmeter/billing/..., openmeter/ledger/..., test/...
Currency fixture helpers move to currencies/testutils. Test imports use the consolidated package. Environment helpers use dedicated environment packages.
Nullable settlement currency handling
openmeter/billing/charges/creditpurchase/settlement.go, openmeter/ledger/chargeadapter/creditpurchase.go
Settlement.GetCurrency returns a pointer. Credit-purchase adapters handle missing and present settlement currencies accordingly.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested labels: kind/refactor

Suggested reviewers: galexihu

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 18.75% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ⚠️ Warning The title refers to 'nit comments from #4731' but the changeset contains major structural changes including method signature updates, package reorganization, and currency handling logic changes that go well beyond minor nit fixes. Update the title to accurately reflect the main changes, such as 'refactor: consolidate currency testutils and update settlement currency handling' or similar.
✅ Passed checks (3 passed)
Check name Status Explanation
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.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/ledger-nit-comments-on-credit-only

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
openmeter/billing/charges/creditpurchase/settlement.go (1)

335-352: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document the nullable settlement-currency contract.

GetCurrency now returns (nil, nil) for promotional settlements, returns a non-nil pointer for invoice and external settlements, and returns errors for invalid or incomplete state. Add a Go doc comment that explains these semantics and why promotional settlements have no fiat currency.

Suggested documentation
+// GetCurrency returns the fiat currency for invoice and external settlements.
+// It returns nil, nil for promotional settlements, which have no fiat
+// settlement currency, and returns an error for invalid or incomplete state.
 func (s Settlement) GetCurrency() (*currencyx.FiatCode, error) {
🤖 Prompt for 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.

In `@openmeter/billing/charges/creditpurchase/settlement.go` around lines 335 -
352, Add a Go doc comment directly above Settlement.GetCurrency describing that
it returns a non-nil fiat currency for invoice and external settlements, returns
nil with no error for promotional settlements because they have no fiat
currency, and returns an error for invalid or incomplete settlement state.

Source: Coding guidelines

🤖 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/currencies/currency_test.go`:
- Around line 163-165: Strengthen the test around
CurrencyReference.UnmarshalText by asserting that the unmarshaled reference is
resolved and retains the original custom precision, in addition to the existing
equality check. Update the test using the visible reference and unmarshaled
symbols so a regression dropping the custom resolution snapshot fails.

---

Nitpick comments:
In `@openmeter/billing/charges/creditpurchase/settlement.go`:
- Around line 335-352: Add a Go doc comment directly above
Settlement.GetCurrency describing that it returns a non-nil fiat currency for
invoice and external settlements, returns nil with no error for promotional
settlements because they have no fiat currency, and returns an error for invalid
or incomplete settlement state.
🪄 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 Plus

Run ID: e23147cd-a2a2-4e64-b19b-0b958e60c70b

📥 Commits

Reviewing files that changed from the base of the PR and between 913c436 and 9edbf45.

📒 Files selected for processing (50)
  • api/v3/handlers/customers/charges/convert_test.go
  • api/v3/handlers/customers/credits/convert_test.go
  • openmeter/billing/charges/creditpurchase/charge_test.go
  • openmeter/billing/charges/creditpurchase/service/external_test.go
  • openmeter/billing/charges/creditpurchase/service/promotional_test.go
  • openmeter/billing/charges/creditpurchase/settlement.go
  • openmeter/billing/charges/flatfee/adapter/detailedline_test.go
  • openmeter/billing/charges/flatfee/adapter/intentoverride_test.go
  • openmeter/billing/charges/flatfee/adapter/realizationrun_test.go
  • openmeter/billing/charges/flatfee/charge_test.go
  • openmeter/billing/charges/lineage/lineage_test.go
  • openmeter/billing/charges/service/base_test.go
  • openmeter/billing/charges/service/creditpurchase_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/service/truncation_test.go
  • openmeter/billing/charges/usagebased/adapter/detailedline_test.go
  • openmeter/billing/charges/usagebased/adapter/intentoverride_test.go
  • openmeter/billing/charges/usagebased/charge_test.go
  • openmeter/billing/charges/usagebased/service/creditheninvoice_test.go
  • openmeter/billing/charges/usagebased/service/creditsonly_test.go
  • openmeter/billing/charges/usagebased/service/rating/service_test.go
  • openmeter/billing/charges/usagebased/service/rating/subtract/subtract_test.go
  • openmeter/billing/charges/usagebased/service/rating/testutils/testutils.go
  • openmeter/billing/charges/usagebased/service/run/payment_test.go
  • openmeter/billing/worker/subscriptionsync/service/reconciler/patchcharge_test.go
  • openmeter/billing/worker/subscriptionsync/service/sync_credittheninvoice_test.go
  • openmeter/currencies/adapter/currencies_test.go
  • openmeter/currencies/currency.go
  • openmeter/currencies/currency_test.go
  • openmeter/currencies/currencyresolver/resolver_test.go
  • openmeter/currencies/service/service_test.go
  • openmeter/currencies/testutils/currency.go
  • openmeter/currencies/testutils/currency/currency.go
  • openmeter/currencies/testutils/env/env.go
  • openmeter/ledger/chargeadapter/creditpurchase.go
  • openmeter/ledger/chargeadapter/creditpurchase_customcurrency_test.go
  • openmeter/ledger/chargeadapter/creditpurchase_test.go
  • openmeter/ledger/chargeadapter/flatfee_test.go
  • openmeter/ledger/chargeadapter/usagebased_customcurrency_test.go
  • openmeter/ledger/chargeadapter/usagebased_test.go
  • openmeter/ledger/collector/collection_fbo_test.go
  • openmeter/ledger/customerbalance/creditvoid_customcurrency_test.go
  • openmeter/ledger/customerbalance/expired_loader_test.go
  • openmeter/ledger/customerbalance/service_test.go
  • openmeter/ledger/customerbalance/testenv_test.go
  • openmeter/ledger/recognizer/service_test.go
  • test/app/stripe/invoice_credits_test.go
  • test/credits/base.go
💤 Files with no reviewable changes (1)
  • openmeter/currencies/testutils/currency/currency.go

Comment thread openmeter/currencies/currency_test.go
@@ -1,4 +1,4 @@
package testutils

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why do we need the subpackage?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To keep currencies/testutils lightweight. It is imported widely for pure currency fixtures, while NewTestEnv pulls in the DB, adapter, and service setup and is only used by the currencies package tests

@GAlexIHU GAlexIHU Aug 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

testutils generally arent lightweight and arent meant to be lightweight, are you concerned with this because of test-compile times? those are bad as but thats a more complex topic

Comment thread openmeter/currencies/currency.go Outdated
return reference, nil
}

func parseCurrencyReference(value []byte) (CurrencyReference, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i think there's no point in having this separate if you've implemented UnmarshalText

Comment thread openmeter/currencies/currency.go Outdated
Comment on lines +176 to +179
var serialized struct {
Code currencyx.Code `json:"code"`
CustomCurrencyID *string `json:"custom_currency_id,omitempty"`
}

@chrisgacsal chrisgacsal Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I do not think you need this. You can unmarshalJSON directly to CurrencyReference as the JSON encoder will skip the private fields anyway.

Comment thread openmeter/currencies/currency.go Outdated
return reference, nil
}

func parseCurrencyReference(value []byte) (CurrencyReference, error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think the implementation of parseCurrencyReference should be moved to UnmarshaText method, no need for a separate parser helper. The ParseCurrencyReference could simple construct an empty CurrecnyReference and unmarshal the byte stream to it. So the parseCurrencyReference feels redundant to me.

@mark-vass-konghq
mark-vass-konghq force-pushed the fix/ledger-nit-comments-on-credit-only branch from a80e04e to fc4a965 Compare August 3, 2026 13:10
fix: active entitlement validation blocking meter delete (#4835)

Co-authored-by: Robert Borbely <robert.borbely@konghq.com>

fix: meter delete in tx (#4838)

Co-authored-by: Alex Goth <64845621+GAlexIHU@users.noreply.github.com>
@mark-vass-konghq
mark-vass-konghq force-pushed the fix/ledger-nit-comments-on-credit-only branch from fc4a965 to 4c9fb8d Compare August 3, 2026 13:10
Comment on lines +465 to +469
if settlementCurrency == nil {
return "", fmt.Errorf("settlement currency is required for a custom currency purchase")
}

return currencyx.Code(settlementCurrency), nil
return currencyx.Code(lo.FromPtr(settlementCurrency)), nil

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if settlementCurrency == nil {
return "", fmt.Errorf("settlement currency is required for a custom currency purchase")
}
return currencyx.Code(settlementCurrency), nil
return currencyx.Code(lo.FromPtr(settlementCurrency)), nil
sc := lo.FromPtr(settlementCurrency)
if sc == "" {
return "", fmt.Errorf("settlement currency is required for a custom currency purchase")
}
return currencyx.Code(sc), nil

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also we might want to validate the currencyx.Code before returning it.

@mark-vass-konghq
mark-vass-konghq merged commit 0db8fd1 into main Aug 4, 2026
27 checks passed
@mark-vass-konghq
mark-vass-konghq deleted the fix/ledger-nit-comments-on-credit-only branch August 4, 2026 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/billing release-note/feature Release note: Exciting New Features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants