Repository navigation
feat: add db support for flatfee/usagebased charge intent overrides - #4556
Conversation
|
Note Reviews pausedIt 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 Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThis PR adds explicit intent-override storage and adapter handling for flat-fee and usage-based charges, updates effective deletion timestamp resolution, refreshes the search view and Ent schemas, and adds integration coverage for override create/update/delete flows. ChangesCharge Intent Override Feature
Sequence Diagram(s)sequenceDiagram
participant Caller
participant ChargeAdapter
participant EntDB
participant Postgres
Caller->>ChargeAdapter: CreateChargeOverride(ctx, charge)
ChargeAdapter->>ChargeAdapter: validate charge and IntentOverride
ChargeAdapter->>EntDB: createIntentOverride(...)
EntDB->>Postgres: INSERT override row
ChargeAdapter->>EntDB: update charge DeletedAt from GetIntentDeletedAt()
EntDB->>Postgres: UPDATE charge row
ChargeAdapter-->>Caller: ChargeBase with IntentOverride
Caller->>ChargeAdapter: DeleteChargeOverride(ctx, charge)
ChargeAdapter->>EntDB: delete override row
EntDB->>Postgres: DELETE override row
ChargeAdapter->>EntDB: persist effective DeletedAt
EntDB->>Postgres: UPDATE charge row
ChargeAdapter-->>Caller: ChargeBase with DeletedAt
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
e09d4a5 to
1aa2076
Compare
9c50c77 to
98290d6
Compare
5de2f2c to
156e06f
Compare
98290d6 to
c73becd
Compare
c73becd to
557ccda
Compare
Greptile SummaryThis PR introduces dedicated
Confidence Score: 5/5The change is safe to merge; the override model is correctly wired end-to-end with no data-corrupting paths found. All previously flagged issues (affected-row guard in DeleteChargeOverride, nil-pointer dereferences on Price/Discounts, ambiguous error messages) have been addressed. The intent-deletion routing through GetIntentDeletedAt() is consistent across CreateChargeOverride, UpdateCharge, and DeleteCharge in both adapters. The migration correctly drops the stale override_kind column and adds the new intent_deleted_at column; the down migration is symmetric. No new logic regressions were found in the creditheninvoice.go rename, and the test coverage spans the full create/update/delete lifecycle for both charge types. No files require special attention. Important Files Changed
|
There was a problem hiding this comment.
Actionable comments posted: 9
🧹 Nitpick comments (2)
openmeter/billing/charges/usagebased/adapter/intentoverride_test.go (1)
290-291: 📐 Maintainability & Code Quality | 🔵 TrivialTiny nit: these
s.True(...Equal(...))checks are a bit opaque on failure. If you want richer diffs here,s.Equal(...)would be easier to read.🤖 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/usagebased/adapter/intentoverride_test.go` around lines 290 - 291, The test assertions in the intent override check are using opaque boolean equality calls, which make failures harder to read. Update the assertions in the test that validates override pricing/discounts to use the test suite’s direct equality helper instead of wrapping Equal inside s.True, so failures show richer diffs while keeping the same verification for Price and Discounts.openmeter/billing/charges/flatfee/charge.go (1)
75-81: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDocument the override-vs-base precedence here.
A short docstring would make this a lot easier to read at call sites, since the method quietly prefers the override intent timestamp over the base intent timestamp. As per coding guidelines, "Add a docstring to Go domain helpers when the name compresses important business semantics that are easy to misread at call sites"; this helper now carries the new effective-deletion contract from the override feature.
🤖 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/flatfee/charge.go` around lines 75 - 81, Add a short docstring to ChargeBase.GetIntentDeletedAt explaining the effective deletion timestamp contract: it returns IntentOverride.IntentDeletedAt when IntentOverride is present, otherwise it falls back to Intent.IntentDeletedAt. This helper in ChargeBase should explicitly document the override-vs-base precedence so call sites understand the business semantics without reading the implementation.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 @.agents/skills/ent/SKILL.md:
- Line 43: The regeneration section has two small grammar issues that should be
corrected for readability. Update the wording in the sentence with generators
needs and the later sentence with something have changed so both read naturally,
while keeping the rest of the instructions in SKILL.md unchanged.
- Around line 43-49: The regeneration guidance in SKILL.md should be updated to
match the repo’s preferred workflow: make `make generate` the primary command
after Ent schema changes instead of presenting `go generate ./openmeter/ent/...`
as the usual path. Adjust the instructions around the Ent schema regeneration
section so they direct users to edit files under `openmeter/ent/schema/` and run
`make generate`, keeping `go generate` only if you need to mention a lower-level
fallback or removing it entirely if not needed.
In `@openmeter/billing/charges/flatfee/adapter/intentoverride.go`:
- Around line 111-211: The shared Ent helper methods createIntentOverride and
updateIntentOverride should be transaction-aware like the rest of the adapter
layer. Wrap the Ent access in these helpers with entutils.TransactingRepo(...)
so they reuse an existing transaction from ctx instead of opening direct DB
operations. Keep the change localized to the adapter methods in
intentoverride.go and ensure the create/save/query flow still uses the same
symbols (createIntentOverride, updateIntentOverride, a.db.ChargeFlatFeeOverride,
and mapIntentOverrideFromDB) under the repo transaction wrapper.
- Around line 97-105: The DeletedAt value returned from intentoverride.go is not
the same UTC-normalized timestamp that gets persisted. In ChargeBase update
flow, use the same converted value produced by
convert.TimePtrIn(charge.Intent.IntentDeletedAt, time.UTC) when assigning
charge.DeletedAt, so the returned response matches the database representation
even if the input timestamp was non-UTC.
In `@openmeter/billing/charges/usagebased/adapter/intentoverride.go`:
- Around line 109-199: The shared Ent helper methods createIntentOverride and
updateIntentOverride are doing direct DB writes/queries without being
transaction-aware. Wrap the Ent access in entutils.TransactingRepo(...) so these
helpers participate in the existing transaction from ctx and can be safely
reused without bypassing the adapter transaction pattern. Keep the transaction
boundary inside these helper functions rather than only at their callers, and
ensure both the create/save path and the update/query path use the
transaction-aware repo.
- Around line 95-103: The delete-override flow in intentoverride.go is
persisting a UTC-normalized timestamp via UpdateOneID/SetOrClearDeletedAt, but
ChargeBase is still returned with the original IntentDeletedAt pointer. Update
the assignment after the save so the returned charge.DeletedAt uses the same
UTC-normalized value produced by convert.TimePtrIn (or the saved value), keeping
the immediate response consistent with a later DB read.
In `@openmeter/ent/schema/chargesflatfee.go`:
- Around line 193-198: Mirror the TaxCode foreign key on the flat-fee override
schema by updating the tax_code_id field in chargesflatfee.go and the related
override definition to use the same referential constraint as the base flat-fee
charge TaxCode relationship. Keep the field optional/nillable as-is, but add the
FK/edge wiring so overrides cannot reference a missing tax code, matching the
existing TaxCode intent validation on the main charge model.
In `@openmeter/ent/schema/chargesusagebased.go`:
- Around line 195-200: The usage-based override schema currently exposes
tax_code_id as a plain field without the TaxCode relationship, so it can point
to a missing record. Update the override schema in the charges usage-based
definition to add the same tax code FK/edge used by the base usage-based charge
schema, and apply the same fix to the other override block referenced by the
comment so both override paths enforce the TaxCode relationship consistently.
In `@tools/migrate/migrations/20260624043300_charge_intent_override_ddl.up.sql`:
- Around line 2-4: Add a guard or backfill around the ALTER TABLE changes in
20260624043300_charge_intent_override_ddl.up.sql before dropping override_kind.
In the migration for charge_flat_fees and charge_usage_based, either
preflight-check for any rows where override_kind is not NULL and fail fast, or
populate the new override tables/columns first via the relevant migration path
before removing the old marker. Make sure the change is tied to the
charge_flat_fees and charge_usage_based DDL so existing override data is not
silently lost.
---
Nitpick comments:
In `@openmeter/billing/charges/flatfee/charge.go`:
- Around line 75-81: Add a short docstring to ChargeBase.GetIntentDeletedAt
explaining the effective deletion timestamp contract: it returns
IntentOverride.IntentDeletedAt when IntentOverride is present, otherwise it
falls back to Intent.IntentDeletedAt. This helper in ChargeBase should
explicitly document the override-vs-base precedence so call sites understand the
business semantics without reading the implementation.
In `@openmeter/billing/charges/usagebased/adapter/intentoverride_test.go`:
- Around line 290-291: The test assertions in the intent override check are
using opaque boolean equality calls, which make failures harder to read. Update
the assertions in the test that validates override pricing/discounts to use the
test suite’s direct equality helper instead of wrapping Equal inside s.True, so
failures show richer diffs while keeping the same verification for Price and
Discounts.
🪄 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: d3165e81-f068-417c-86d2-93f9231ded6f
⛔ Files ignored due to path filters (42)
openmeter/ent/db/chargeflatfee.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee/chargeflatfee.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee_query.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfee_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeoverride.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeoverride/chargeflatfeeoverride.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeoverride/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeoverride_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeoverride_delete.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeoverride_query.gois excluded by!**/ent/db/**openmeter/ent/db/chargeflatfeeoverride_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargessearchv1.gois excluded by!**/ent/db/**openmeter/ent/db/chargessearchv1/chargessearchv1.gois excluded by!**/ent/db/**openmeter/ent/db/chargessearchv1/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebased.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebased/chargeusagebased.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebased/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebased_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebased_query.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebased_update.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedoverride.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedoverride/chargeusagebasedoverride.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedoverride/where.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedoverride_create.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedoverride_delete.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedoverride_query.gois excluded by!**/ent/db/**openmeter/ent/db/chargeusagebasedoverride_update.gois excluded by!**/ent/db/**openmeter/ent/db/client.gois excluded by!**/ent/db/**openmeter/ent/db/ent.gois excluded by!**/ent/db/**openmeter/ent/db/entmixinaccessor.gois excluded by!**/ent/db/**openmeter/ent/db/expose.gois excluded by!**/ent/db/**openmeter/ent/db/hook/hook.gois excluded by!**/ent/db/**openmeter/ent/db/migrate/schema.gois excluded by!**/ent/db/**openmeter/ent/db/mutation.gois excluded by!**/ent/db/**openmeter/ent/db/paginate.gois excluded by!**/ent/db/**openmeter/ent/db/predicate/predicate.gois excluded by!**/ent/db/**openmeter/ent/db/runtime.gois excluded by!**/ent/db/**openmeter/ent/db/setorclear.gois excluded by!**/ent/db/**openmeter/ent/db/tx.gois excluded by!**/ent/db/**tools/migrate/migrations/atlas.sumis excluded by!**/*.sum,!**/*.sum
📒 Files selected for processing (26)
.agents/skills/charges/SKILL.md.agents/skills/ent/SKILL.mdopenmeter/billing/charges/flatfee/adapter.goopenmeter/billing/charges/flatfee/adapter/charge.goopenmeter/billing/charges/flatfee/adapter/intentoverride.goopenmeter/billing/charges/flatfee/adapter/intentoverride_test.goopenmeter/billing/charges/flatfee/adapter/mapper.goopenmeter/billing/charges/flatfee/charge.goopenmeter/billing/charges/flatfee/service/creditheninvoice.goopenmeter/billing/charges/models/chargemeta/mixin.goopenmeter/billing/charges/models/intentoverride/flatfee.goopenmeter/billing/charges/models/intentoverride/mixin.goopenmeter/billing/charges/models/intentoverride/usagebased.goopenmeter/billing/charges/usagebased/adapter.goopenmeter/billing/charges/usagebased/adapter/charge.goopenmeter/billing/charges/usagebased/adapter/intentoverride.goopenmeter/billing/charges/usagebased/adapter/intentoverride_test.goopenmeter/billing/charges/usagebased/adapter/mapper.goopenmeter/billing/charges/usagebased/charge.goopenmeter/ent/schema/charges.goopenmeter/ent/schema/chargesflatfee.goopenmeter/ent/schema/chargesusagebased.gopkg/timeutil/closedperiod.gotools/migrate/migrations/20260624043300_charge_intent_override_ddl.down.sqltools/migrate/migrations/20260624043300_charge_intent_override_ddl.up.sqltools/migrate/views.sql
💤 Files with no reviewable changes (1)
- openmeter/billing/charges/models/chargemeta/mixin.go
|
Want your agent to iterate on Greptile's feedback? Try greploops. |
borosr
left a comment
There was a problem hiding this comment.
Other then the two question I sent the code looks good
Summary
Adds type-owned charge intent overrides for flat-fee and usage-based charges.
Charge intents are the durable instructions used to produce charge state and invoice lines. Overrides are API-owned replacement intents for the same charge, so API edits can remain effective while subscription sync continues reconciling the base intent.
Overrides are full snapshots, not sparse patches. If no override row exists, the base intent is effective. If an override row exists, the override intent is effective.
Rules
invoice_at; invoice-state calculations still use the base/original intent timing in this PR.Deletion Rules
Flat-fee and usage-based charges track deletion intent separately for the base intent and override intent:
Intent.IntentDeletedAtmarks the base/original intent as deleted.IntentOverride.IntentDeletedAtmarks the override intent as deleted.DeletedAtfromIntent.IntentDeletedAt.DeletedAtfromIntentOverride.IntentDeletedAt.This allows subscription/system state to delete the base intent while an API override remains effective, and allows API override deletion without mutating the base intent.
Summary by CodeRabbit
DeletedAtand intent-deleted timestamps consistently (UTC-normalized), including when overrides exist.