Skip to content

refactor: currencies service pkg#4638

Open
chrisgacsal wants to merge 1 commit into
mainfrom
refactor/currencies
Open

refactor: currencies service pkg#4638
chrisgacsal wants to merge 1 commit into
mainfrom
refactor/currencies

Conversation

@chrisgacsal

@chrisgacsal chrisgacsal commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Overview

Refactor currencies service package to be more aligned with the project. No functional changes.

Summary by CodeRabbit

  • New Features
    • Expanded currency capabilities to support custom currencies and cost basis records, including effective date ranges and rate handling.
    • Improved list endpoints with stronger request validation, sorting, and filtering options.
  • Bug Fixes
    • Unified currency handler/service wiring so all currency operations use the same execution path.
    • Improved backend initialization for more reliable currency functionality.
  • Refactor
    • Refined the currency service and repository layering to separate currency vs. cost-basis responsibilities.
  • Tests
    • Updated service tests to match the new construction/return behavior.

@chrisgacsal chrisgacsal self-assigned this Jul 3, 2026
@chrisgacsal chrisgacsal requested a review from a team as a code owner July 3, 2026 08:57
@chrisgacsal chrisgacsal added the release-note/ignore Ignore this change when generating release notes label Jul 3, 2026
@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

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: f060feb5-09ab-46fe-bc95-54cf14c0b3e9

📥 Commits

Reviewing files that changed from the base of the PR and between d953ff4 and 965f005.

📒 Files selected for processing (19)
  • api/v3/handlers/currencies/create.go
  • api/v3/handlers/currencies/create_cost_basis.go
  • api/v3/handlers/currencies/get_cost_bases.go
  • api/v3/handlers/currencies/handler.go
  • api/v3/handlers/currencies/list.go
  • api/v3/server/server.go
  • app/common/currency.go
  • cmd/server/wire.go
  • cmd/server/wire_gen.go
  • openmeter/currencies/adapter/adapter.go
  • openmeter/currencies/adapter/currencies.go
  • openmeter/currencies/costbasis.go
  • openmeter/currencies/currency.go
  • openmeter/currencies/models.go
  • openmeter/currencies/repository.go
  • openmeter/currencies/service.go
  • openmeter/currencies/service/service.go
  • openmeter/currencies/service/service_test.go
  • openmeter/server/router/router.go
💤 Files with no reviewable changes (2)
  • openmeter/currencies/adapter/currencies.go
  • openmeter/currencies/models.go
✅ Files skipped from review due to trivial changes (1)
  • cmd/server/wire_gen.go
🚧 Files skipped from review as they are similar to previous changes (15)
  • api/v3/handlers/currencies/create.go
  • cmd/server/wire.go
  • api/v3/handlers/currencies/list.go
  • api/v3/handlers/currencies/create_cost_basis.go
  • api/v3/handlers/currencies/get_cost_bases.go
  • openmeter/currencies/costbasis.go
  • openmeter/currencies/adapter/adapter.go
  • openmeter/server/router/router.go
  • openmeter/currencies/repository.go
  • api/v3/server/server.go
  • openmeter/currencies/currency.go
  • app/common/currency.go
  • api/v3/handlers/currencies/handler.go
  • openmeter/currencies/service/service.go
  • openmeter/currencies/service.go

📝 Walkthrough

Walkthrough

The currencies package now uses split Repository and Service interfaces, with new domain types and validation inputs. The adapter, service implementation, wiring, and v3 handlers were updated to use the new contracts.

Changes

Currencies domain, service, and handler refactor

Layer / File(s) Summary
Domain types
openmeter/currencies/currency.go, openmeter/currencies/costbasis.go, openmeter/currencies/models.go, openmeter/currencies/router.go
CurrencyType, Currency, and CostBasis move into new files, and the old models file contents are removed.
Repository and service contracts
openmeter/currencies/repository.go, openmeter/currencies/service.go
Adapter becomes Repository, repository methods are split into currency and cost-basis interfaces, and Service gains separate currency/cost-basis interfaces plus new input types and validation.
Adapter and service implementation
openmeter/currencies/adapter/adapter.go, openmeter/currencies/adapter/currencies.go, openmeter/currencies/service/service.go, openmeter/currencies/service/service_test.go
The adapter constructor now returns currencies.Repository; the service implementation is unexported and built from a repository; tests are updated to handle the new constructor result.
Dependency wiring
app/common/currency.go, cmd/server/wire.go, cmd/server/wire_gen.go, api/v3/server/server.go, openmeter/server/router/router.go
Wiring now constructs a currency repository before the service, and public config/application fields use currencies.Service.
v3 handler updates
api/v3/handlers/currencies/handler.go, create.go, create_cost_basis.go, list.go, get_cost_bases.go
The handler stores a service field, and currency/cost-basis handlers call h.service instead of h.currencyService.

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

Possibly related PRs

Suggested reviewers: mark-vass-konghq, tothandras

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title matches the main change: a refactor of the currencies service package.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ 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 refactor/currencies

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.

@chrisgacsal chrisgacsal force-pushed the refactor/currencies branch from 73d9a3c to d953ff4 Compare July 3, 2026 09:01

@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: 2

🧹 Nitpick comments (1)
openmeter/currencies/service.go (1)

40-46: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Wrap these validators with models.NewNillableGenericValidationError.

ListCurrenciesInput, CreateCurrencyInput, CreateCostBasisInput, and ListCostBasesInput should return models.NewNillableGenericValidationError(errors.Join(errs...)) instead of a raw errors.Join(...). OrderBy.Validate() should use the same validation error type for the invalid case too.

🤖 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/currencies/service.go` around lines 40 - 46, The validation paths
are returning raw errors instead of the project’s nillable generic validation
type. Update the validation logic in ListCurrenciesInput, CreateCurrencyInput,
CreateCostBasisInput, and ListCostBasesInput so they wrap the joined validation
errors with models.NewNillableGenericValidationError, and make OrderBy.Validate
return that same error type for invalid values instead of a plain fmt.Errorf.
Use the existing validation helpers and the OrderBy.Validate method as the main
points to adjust.

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.go`:
- Around line 12-19: CurrencyType.Validate currently fails fast by returning a
plain fmt.Errorf in the default branch, but it should follow the project
validation pattern. Update Validate() to build a var errs []error, append the
invalid-type error when t is not CurrencyTypeCustom or CurrencyTypeFiat, and
return models.NewNillableGenericValidationError(errors.Join(errs...)) instead of
returning immediately.

In `@openmeter/currencies/service/service_test.go`:
- Around line 71-73: `New` now returns an error, so the test helper and any
other `New(...)` call sites in this file must be updated to handle the second
return value. In `newTestService`, create the service and error from
`New(&fakeAdapter{custom: custom})`, then assert the error is nil with
`require.NoError(t, err)` before returning the service; apply the same pattern
to the other `New` usages in this test file.

---

Nitpick comments:
In `@openmeter/currencies/service.go`:
- Around line 40-46: The validation paths are returning raw errors instead of
the project’s nillable generic validation type. Update the validation logic in
ListCurrenciesInput, CreateCurrencyInput, CreateCostBasisInput, and
ListCostBasesInput so they wrap the joined validation errors with
models.NewNillableGenericValidationError, and make OrderBy.Validate return that
same error type for invalid values instead of a plain fmt.Errorf. Use the
existing validation helpers and the OrderBy.Validate method as the main points
to adjust.
🪄 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: 5bfddfd7-8889-4a81-8fe8-5975ce6bd14a

📥 Commits

Reviewing files that changed from the base of the PR and between 0eede87 and 73d9a3c.

📒 Files selected for processing (17)
  • api/v3/handlers/currencies/create.go
  • api/v3/handlers/currencies/create_cost_basis.go
  • api/v3/handlers/currencies/get_cost_bases.go
  • api/v3/handlers/currencies/handler.go
  • api/v3/handlers/currencies/list.go
  • app/common/currency.go
  • cmd/server/wire.go
  • cmd/server/wire_gen.go
  • openmeter/currencies/adapter/adapter.go
  • openmeter/currencies/adapter/currencies.go
  • openmeter/currencies/costbasis.go
  • openmeter/currencies/currency.go
  • openmeter/currencies/models.go
  • openmeter/currencies/repository.go
  • openmeter/currencies/service.go
  • openmeter/currencies/service/service.go
  • openmeter/currencies/service/service_test.go
💤 Files with no reviewable changes (2)
  • openmeter/currencies/adapter/currencies.go
  • openmeter/currencies/models.go

Comment on lines +12 to +19
func (t CurrencyType) Validate() error {
switch t {
case CurrencyTypeCustom, CurrencyTypeFiat:
return nil
default:
return fmt.Errorf("currency type: %s", t)
}
}

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.

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Validate() should collect errors, not fail fast.

As per coding guidelines, Go Validate() error methods should collect issues into var errs []error and return models.NewNillableGenericValidationError(errors.Join(errs...)) instead of failing fast. This switch-based implementation returns immediately on the first (and only) failure instead of following that convention.

♻️ Suggested fix
 func (t CurrencyType) Validate() error {
-	switch t {
-	case CurrencyTypeCustom, CurrencyTypeFiat:
-		return nil
-	default:
-		return fmt.Errorf("currency type: %s", t)
-	}
+	var errs []error
+	switch t {
+	case CurrencyTypeCustom, CurrencyTypeFiat:
+	default:
+		errs = append(errs, fmt.Errorf("currency type: %s", t))
+	}
+	return models.NewNillableGenericValidationError(errors.Join(errs...))
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (t CurrencyType) Validate() error {
switch t {
case CurrencyTypeCustom, CurrencyTypeFiat:
return nil
default:
return fmt.Errorf("currency type: %s", t)
}
}
func (t CurrencyType) Validate() error {
var errs []error
switch t {
case CurrencyTypeCustom, CurrencyTypeFiat:
default:
errs = append(errs, fmt.Errorf("currency type: %s", t))
}
return models.NewNillableGenericValidationError(errors.Join(errs...))
}
🤖 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/currencies/currency.go` around lines 12 - 19, CurrencyType.Validate
currently fails fast by returning a plain fmt.Errorf in the default branch, but
it should follow the project validation pattern. Update Validate() to build a
var errs []error, append the invalid-type error when t is not CurrencyTypeCustom
or CurrencyTypeFiat, and return
models.NewNillableGenericValidationError(errors.Join(errs...)) instead of
returning immediately.

Source: Coding guidelines

Comment thread openmeter/currencies/service/service_test.go Outdated
@greptile-apps

greptile-apps Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR refactors the currencies service package to align with project conventions: renaming AdapterRepository, splitting CurrencyService into a Service composite interface (CurrencyService + CostBasisService), making the concrete Service struct unexported, and splitting models.go into focused files (currency.go, costbasis.go, service.go). No functional logic changes.

  • Adapter is renamed Repository with sub-interfaces CurrencyRepository and CostBasisRepository; adapter.go becomes repository.go.
  • New() now returns (currencies.Service, error) with a nil-guard on the repo argument; all test call sites are correctly updated to unpack both return values.
  • Constructor in app/common/currency.go is split into NewCurrencyAdapter and NewCurrencyService, removing the unused *slog.Logger dependency and wiring them separately via Wire.

Confidence Score: 5/5

Safe to merge — purely a rename/restructure with no changes to business logic or database queries.

All interface renames and constructor signature changes are consistently propagated across handlers, server configs, wire wiring, and tests. The only items left behind are two stale comments (one referencing the deleted currencies.Adapter type, one asserting the narrower CurrencyService sub-interface instead of the full Service) — neither causes a compile error or runtime issue.

openmeter/server/server_test.go (stale compile-time assertion) and openmeter/currencies/service/service_test.go (stale comment) are the only files with minor cleanup left.

Important Files Changed

Filename Overview
openmeter/currencies/service.go Merged content of deleted models.go into this file alongside the new Service/CurrencyService/CostBasisService interface hierarchy; all input/output types and validation logic are functionally unchanged.
openmeter/currencies/service/service.go Service struct made unexported (Service→service), New() now returns (currencies.Service, error) with a nil-guard; all method receivers updated. Field still named adapter despite the Repository rename.
openmeter/currencies/service/service_test.go All New() call sites correctly unpacked to (service, err) with require.NoError; stale comment still references the deleted currencies.Adapter type.
openmeter/server/server_test.go NoopCurrencyService implements all four service methods and compiles correctly against currencies.Service, but the compile-time assertion still checks the narrower currencies.CurrencyService sub-interface.
openmeter/currencies/repository.go Adapter renamed to Repository; split into CurrencyRepository and CostBasisRepository sub-interfaces — clean structural change.
openmeter/currencies/adapter/adapter.go Return type updated from currencies.Adapter to currencies.Repository; error from Validate now wrapped with context; compile-time assertion updated.
app/common/currency.go Constructor split into NewCurrencyAdapter and NewCurrencyService; logger dependency removed; wire set updated to reflect both providers.
cmd/server/wire_gen.go Auto-generated file correctly reflects the two-step adapter+service construction with proper cleanup propagation on error.
api/v3/handlers/currencies/handler.go Field renamed from currencyService (CurrencyService) to service (Service); all handler files updated consistently.

Class Diagram

%%{init: {'theme': 'neutral'}}%%
classDiagram
    class Service {
        <<interface>>
        +ListCurrencies()
        +CreateCurrency()
        +CreateCostBasis()
        +ListCostBases()
    }
    class CurrencyService {
        <<interface>>
        +ListCurrencies()
        +CreateCurrency()
    }
    class CostBasisService {
        <<interface>>
        +CreateCostBasis()
        +ListCostBases()
    }
    Service --> CurrencyService : embeds
    Service --> CostBasisService : embeds

    class Repository {
        <<interface>>
        +Tx()
        +ListCustomCurrencies()
        +CreateCurrency()
        +CreateCostBasis()
        +ListCostBases()
    }
    class CurrencyRepository {
        <<interface>>
        +ListCustomCurrencies()
        +CreateCurrency()
    }
    class CostBasisRepository {
        <<interface>>
        +CreateCostBasis()
        +ListCostBases()
    }
    Repository --> CurrencyRepository : embeds
    Repository --> CostBasisRepository : embeds

    class service {
        -adapter Repository
        +New(repo Repository) Service
    }
    class adapter {
        -db entdb.Client
    }

    service ..|> Service : implements
    adapter ..|> Repository : implements
    service --> Repository : uses
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
classDiagram
    class Service {
        <<interface>>
        +ListCurrencies()
        +CreateCurrency()
        +CreateCostBasis()
        +ListCostBases()
    }
    class CurrencyService {
        <<interface>>
        +ListCurrencies()
        +CreateCurrency()
    }
    class CostBasisService {
        <<interface>>
        +CreateCostBasis()
        +ListCostBases()
    }
    Service --> CurrencyService : embeds
    Service --> CostBasisService : embeds

    class Repository {
        <<interface>>
        +Tx()
        +ListCustomCurrencies()
        +CreateCurrency()
        +CreateCostBasis()
        +ListCostBases()
    }
    class CurrencyRepository {
        <<interface>>
        +ListCustomCurrencies()
        +CreateCurrency()
    }
    class CostBasisRepository {
        <<interface>>
        +CreateCostBasis()
        +ListCostBases()
    }
    Repository --> CurrencyRepository : embeds
    Repository --> CostBasisRepository : embeds

    class service {
        -adapter Repository
        +New(repo Repository) Service
    }
    class adapter {
        -db entdb.Client
    }

    service ..|> Service : implements
    adapter ..|> Repository : implements
    service --> Repository : uses
Loading

Reviews (2): Last reviewed commit: "refactor: currencies service pkg" | Re-trigger Greptile

Comment thread openmeter/currencies/service/service_test.go Outdated
Comment thread openmeter/currencies/service.go
Comment on lines +20 to +27

func (t CurrencyType) Values() []CurrencyType {
return []CurrencyType{
CurrencyTypeCustom,
CurrencyTypeFiat,
}
}

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.

P2 Values() method defined on value receiver with no reference to t

CurrencyType.Values() always returns the same static slice regardless of the receiver value. This is either a utility method that belongs at the package level (not on the type), or it should be a package-level var/func. Defining it as an instance method is misleading — it implies the returned values depend on the receiver, which they don't.

Prompt To Fix With AI
This is a comment left during a code review.
Path: openmeter/currencies/currency.go
Line: 20-27

Comment:
**`Values()` method defined on value receiver with no reference to `t`**

`CurrencyType.Values()` always returns the same static slice regardless of the receiver value. This is either a utility method that belongs at the package level (not on the type), or it should be a package-level `var`/`func`. Defining it as an instance method is misleading — it implies the returned values depend on the receiver, which they don't.

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code Fix in Codex

@chrisgacsal chrisgacsal force-pushed the refactor/currencies branch from d953ff4 to 965f005 Compare July 3, 2026 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant