Skip to content

refactor: clean up charge patch mutation APIs - #4577

Merged
turip merged 1 commit into
mainfrom
refactor/charges-cleanup
Jun 26, 2026
Merged

turip merged 1 commit into
mainfrom
refactor/charges-cleanup

Conversation

@turip

@turip turip commented Jun 26, 2026 •

Copy link
Copy Markdown
Member

What changed

This stacked cleanup removes the leftover patch trigger parameter adapter now that charge state machines receive the patch payload directly.

It also simplifies flat-fee and usage-based intent mutation callbacks so callers mutate the provided non-nil cloned mutable-field pointer directly. The mutator still owns target validation, normalization, and final validation before writing the layer back.

Why

The previous shape kept extra API surface around the transition: patches exposed TriggerParams even though the patch itself is the trigger payload, and mutate callbacks returned values/errors even though production mutations are simple in-place field edits.

Validation

  • go test -tags=dynamic ./openmeter/billing/charges/meta ./openmeter/billing/charges/flatfee/service ./openmeter/billing/charges/usagebased/service
  • go test -tags=dynamic ./openmeter/billing/charges/flatfee/adapter ./openmeter/billing/charges/usagebased/adapter ./openmeter/billing/charges/usagebased/service/rating

Summary by CodeRabbit

  • Bug Fixes
    • Improved charge update handling so delete, extend, and shrink actions apply changes more reliably.
    • Fixed trigger processing for charge patches, helping ensure the correct action is used during charge lifecycle updates.
    • Updated mutation handling to reduce the risk of inconsistent charge details being saved during updates.

@coderabbitai

coderabbitai Bot commented Jun 26, 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: 1608fbef-3f92-4488-bc16-242c41d8401c

📥 Commits

Reviewing files that changed from the base of the PR and between c16cbe1 and 8f9732f.

📒 Files selected for processing (18)
  • openmeter/billing/charges/flatfee/adapter/charge.go
  • openmeter/billing/charges/flatfee/adapter/intentoverride_test.go
  • openmeter/billing/charges/flatfee/charge.go
  • openmeter/billing/charges/flatfee/service/creditheninvoice.go
  • openmeter/billing/charges/flatfee/service/creditsonly.go
  • openmeter/billing/charges/flatfee/service/triggers.go
  • openmeter/billing/charges/meta/patch.go
  • openmeter/billing/charges/meta/patchdelete.go
  • openmeter/billing/charges/meta/patchextend.go
  • openmeter/billing/charges/meta/patchshrink.go
  • openmeter/billing/charges/usagebased/adapter/charge.go
  • openmeter/billing/charges/usagebased/adapter/intentoverride_test.go
  • openmeter/billing/charges/usagebased/charge.go
  • openmeter/billing/charges/usagebased/service/creditheninvoice.go
  • openmeter/billing/charges/usagebased/service/creditheninvoice_test.go
  • openmeter/billing/charges/usagebased/service/creditsonly.go
  • openmeter/billing/charges/usagebased/service/rating/service_test.go
  • openmeter/billing/charges/usagebased/service/triggers.go
💤 Files with no reviewable changes (4)
  • openmeter/billing/charges/meta/patch.go
  • openmeter/billing/charges/meta/patchshrink.go
  • openmeter/billing/charges/meta/patchdelete.go
  • openmeter/billing/charges/meta/patchextend.go

📝 Walkthrough

Walkthrough

This PR changes flatfee and usagebased charge intent mutations to pointer-based callbacks on cloned mutable fields. It also removes TriggerParams() from patch contracts and updates trigger dispatch to pass the patch object through state-machine activation.

Changes

Billing charge contract updates

Layer / File(s) Summary
Pointer-based mutation API
openmeter/billing/charges/flatfee/charge.go, openmeter/billing/charges/usagebased/charge.go
OverridableIntent mutation methods now accept pointer-edit callbacks and write back cloned fields only after normalization and validation.
Flatfee mutation callers
openmeter/billing/charges/flatfee/adapter/charge.go, openmeter/billing/charges/flatfee/adapter/intentoverride_test.go, openmeter/billing/charges/flatfee/service/creditheninvoice.go, openmeter/billing/charges/flatfee/service/creditsonly.go
Flatfee delete, period patch, and override test callbacks now mutate intent fields in place through pointer arguments.
Usagebased mutation callers
openmeter/billing/charges/usagebased/adapter/charge.go, openmeter/billing/charges/usagebased/adapter/intentoverride_test.go, openmeter/billing/charges/usagebased/service/creditheninvoice.go, openmeter/billing/charges/usagebased/service/creditsonly.go, openmeter/billing/charges/usagebased/service/rating/service_test.go
Usagebased delete, period patch, credits-only, and rating test callbacks now mutate intent fields in place through pointer arguments.
Patch trigger contract
openmeter/billing/charges/meta/patch.go, openmeter/billing/charges/meta/patchdelete.go, openmeter/billing/charges/meta/patchextend.go, openmeter/billing/charges/meta/patchshrink.go
The patch interface drops TriggerParams(), and delete, extend, and shrink patches now expose trigger selection through Trigger().
Trigger dispatch wiring
openmeter/billing/charges/flatfee/service/triggers.go, openmeter/billing/charges/usagebased/service/triggers.go, openmeter/billing/charges/usagebased/service/creditheninvoice_test.go, openmeter/billing/charges/flatfee/service/creditheninvoice_test.go
Flatfee and usagebased TriggerPatch calls now pass the patch object into FireAndActivate, and the corresponding tests were updated to match.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • openmeterio/openmeter#4572: Introduces the intent layering refactor that this PR continues by changing MutateEffective/Mutate to in-place pointer edits.
  • openmeterio/openmeter#4576: Covers the patch/ChangeTarget contract work that aligns with removing TriggerParams() and passing the patch object through trigger dispatch.
  • openmeterio/openmeter#4053: Touches the same TriggerPatch/FireAndActivate wiring path that this PR updates for flatfee and usagebased charges.

Suggested labels

release-note/misc, area/billing

Suggested reviewers

  • tothandras
  • GAlexIHU
🚥 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 clearly matches the main change: a refactor of charge patch mutation APIs and related callback cleanup.
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/charges-cleanup

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.

@greptile-apps

greptile-apps Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR cleans up charge patch and mutation APIs. The main changes are:

  • Patch payloads are passed directly into charge state-machine triggers.
  • Flat-fee intent mutators now edit cloned mutable fields in place.
  • Usage-based intent mutators now edit cloned mutable fields in place.
  • Tests and service call sites were updated for the new callback shape.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
openmeter/billing/charges/flatfee/charge.go Flat-fee intent mutation now passes a cloned mutable-field pointer to the callback before validation and write-back.
openmeter/billing/charges/usagebased/charge.go Usage-based intent mutation now passes a cloned mutable-field pointer to the callback before validation and write-back.
openmeter/billing/charges/meta/patch.go Patch no longer exposes a separate trigger-parameter adapter method.
openmeter/billing/charges/flatfee/service/triggers.go Flat-fee patch triggering now passes the patch object directly to the state machine.
openmeter/billing/charges/usagebased/service/triggers.go Usage-based patch triggering now passes the patch object directly to the state machine.

Reviews (2): Last reviewed commit: "refactor: clean up charge patch mutation..." | Re-trigger Greptile

@turip
turip force-pushed the feat/subssync-patch-targeting branch 2 times, most recently from 781739b to 77b8470 Compare June 26, 2026 08:38
Base automatically changed from feat/subssync-patch-targeting to main June 26, 2026 10:17
@turip
turip force-pushed the refactor/charges-cleanup branch from 991b8ef to 8f9732f Compare June 26, 2026 10:21
@turip
turip marked this pull request as ready for review June 26, 2026 10:21
@turip
turip requested a review from a team as a code owner June 26, 2026 10:21
@turip turip added release-note/misc Miscellaneous changes area/billing labels Jun 26, 2026
@turip
turip merged commit f7eb03a into main Jun 26, 2026
28 of 31 checks passed
@turip
turip deleted the refactor/charges-cleanup branch June 26, 2026 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants