Repository navigation
fix: invoice edit tax code diff - #4621
Conversation
📝 WalkthroughWalkthroughIntroduces a ChangesProvider default tax code key + diffing
Estimated code review effort: 4 (Complex) | ~60 minutes Possibly related PRs
Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ 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 |
Greptile SummaryThis PR fixes invoice edit diffs for tax configs that use provider-default omission. The main changes are:
Confidence Score: 5/5This looks safe to merge.
Important Files Changed
Reviews (3): Last reviewed commit: "fix(billing): restore invoice diff compa..." | Re-trigger Greptile |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
openmeter/taxcode/taxcode.go (1)
15-16: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider a short doc comment on this constant.
ProviderDefaultTaxCodeKeyis now referenced across config seeding, billing invoice diff resolution, and several test suites — a one-liner explaining its role (the stable key used to resolve a tax config to the provider/invoicing app's own default tax code) would help future readers who only see the usage sites.As per coding guidelines, "When adding comments or docstrings, explain intent and domain constraints that are not obvious from the code itself."
📝 Suggested doc comment
+// ProviderDefaultTaxCodeKey is the stable key used to identify the tax code that +// represents "let the invoicing provider apply its own default tax treatment". const ProviderDefaultTaxCodeKey = "default"🤖 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/taxcode/taxcode.go` around lines 15 - 16, Add a short doc comment for ProviderDefaultTaxCodeKey in taxcode.go that explains it is the stable key used to resolve the provider/invoicing app’s own default tax code in config seeding and billing invoice diff resolution. Keep it a one-liner placed directly above the constant so readers understand its intent from the symbol name alone.openmeter/taxcode/namespacehandler_test.go (1)
20-25: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueNice constant swap, but a couple lookups still use the raw string.
Seed/creation calls now use
taxcode.ProviderDefaultTaxCodeKey, butkeyToTC["default"]on Line 71 and Line 117 still hardcode the literal. Works fine today since the constant's value is"default", but it'd be more consistent (and safer if the value ever changes) to use the constant everywhere in this file.♻️ Suggested tweak
- defaultTC, ok := keyToTC["default"] + defaultTC, ok := keyToTC[taxcode.ProviderDefaultTaxCodeKey]and similarly for Line 117:
- gotDefault, ok := keyToTC["default"] + gotDefault, ok := keyToTC[taxcode.ProviderDefaultTaxCodeKey]Also applies to: 71-71, 92-96, 117-117, 141-146
🤖 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/taxcode/namespacehandler_test.go` around lines 20 - 25, The namespace handler tests still hardcode the default tax code lookup string in a few places instead of using the shared constant. Update the lookup/assertion code in namespacehandler_test.go to use taxcode.ProviderDefaultTaxCodeKey everywhere keyToTC["default"] is referenced, including the checks around the existing test cases that exercise default tax code behavior, so the tests stay consistent with the seed/creation calls and remain safe if the constant value changes.openmeter/billing/service/invoiceupdate.go (1)
293-340: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider a short docstring here.
sanitizeTaxConfigForDiffpacks in a fair bit of business logic (empty-string normalization, provider-default resolution vs. explicit-provider resolution) that isn't obvious from a glance. A short comment on the observable contract (e.g., "returns a tax config guaranteed to have a resolved, non-empty TaxCodeID for diff comparison purposes") would help future readers.🤖 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/service/invoiceupdate.go` around lines 293 - 340, Add a short docstring to sanitizeTaxConfigForDiff explaining its contract: it normalizes empty TaxCodeID values and returns a tax config guaranteed to have a resolved non-empty TaxCodeID for diff comparison. Keep it near the sanitizeTaxConfigForDiff signature so readers can understand the provider-default vs. explicit Stripe resolution paths without tracing the internal logic.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/billing/service/invoiceupdate.go`:
- Around line 212-340: The invoice edit diff sanitization path is doing repeated
namespace-scoped tax-code lookups per line, which can create N+1 database calls
in invoice updates. Memoize the provider-default tax code once in
invoiceWithSanitizedTaxConfigForDiff (similar to the syncx.OnceValues pattern
used in defaultTaxCodeResolversForInvoiceUpdate) and thread a cached getter
through sanitizeInvoiceLineTaxConfigForDiff and sanitizeTaxConfigForDiff instead
of calling s.taxCodeService.GetTaxCodeByKey directly for each line; keep
productcatalog.ResolveTaxConfig on the same memoized path where it resolves the
same default/provider tax code.
---
Nitpick comments:
In `@openmeter/billing/service/invoiceupdate.go`:
- Around line 293-340: Add a short docstring to sanitizeTaxConfigForDiff
explaining its contract: it normalizes empty TaxCodeID values and returns a tax
config guaranteed to have a resolved non-empty TaxCodeID for diff comparison.
Keep it near the sanitizeTaxConfigForDiff signature so readers can understand
the provider-default vs. explicit Stripe resolution paths without tracing the
internal logic.
In `@openmeter/taxcode/namespacehandler_test.go`:
- Around line 20-25: The namespace handler tests still hardcode the default tax
code lookup string in a few places instead of using the shared constant. Update
the lookup/assertion code in namespacehandler_test.go to use
taxcode.ProviderDefaultTaxCodeKey everywhere keyToTC["default"] is referenced,
including the checks around the existing test cases that exercise default tax
code behavior, so the tests stay consistent with the seed/creation calls and
remain safe if the constant value changes.
In `@openmeter/taxcode/taxcode.go`:
- Around line 15-16: Add a short doc comment for ProviderDefaultTaxCodeKey in
taxcode.go that explains it is the stable key used to resolve the
provider/invoicing app’s own default tax code in config seeding and billing
invoice diff resolution. Keep it a one-liner placed directly above the constant
so readers understand its intent from the symbol name alone.
🪄 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: b08bc315-772d-47a3-8434-1ea8634250fb
📒 Files selected for processing (26)
app/config/taxcode.goe2e/billinginvoice_discount_edit_test.goopenmeter/billing/httpdriver/invoiceline.goopenmeter/billing/httpdriver/invoiceline_test.goopenmeter/billing/service/gatheringinvoice.goopenmeter/billing/service/invoiceupdate.goopenmeter/billing/service/invoiceupdate_test.goopenmeter/billing/service/stdinvoice.goopenmeter/billing/worker/subscriptionsync/service/sync_test.goopenmeter/productcatalog/tax_test.goopenmeter/server/server_test.goopenmeter/taxcode/adapter/taxcode.goopenmeter/taxcode/errors.goopenmeter/taxcode/namespacehandler_test.goopenmeter/taxcode/repository.goopenmeter/taxcode/service.goopenmeter/taxcode/service/taxcode.goopenmeter/taxcode/service/taxcode_test.goopenmeter/taxcode/taxcode.goopenmeter/taxcode/testutils/env.gotest/app/stripe/invoice_test.gotest/billing/collection_test.gotest/billing/discount_test.gotest/billing/invoice_test.gotest/billing/suite.gotest/billing/tax_test.go
Overview
Fix invoice editing for charge-backed lines whose tax config is represented by provider-default omission in API edit payloads.
For API-originated invoice edits, billing now normalizes both the persisted invoice and the edited invoice before line diffing. Nil or identity-less tax configs are resolved to the namespace provider-default tax code, explicit provider codes are resolved to
TaxCodeID, and API diff equality compares only tax behavior and resolvedTaxCodeID. This prevents unrelated invoice edits from being interpreted as tax-code edits while keeping charge-managed tax config immutable.System-originated edits continue to use full
TaxConfig.Equal, so resolved tax-code snapshots are not ignored outside the API edit path.Changes
taxcode.ProviderDefaultTaxCodeKey.ChangeSourceAPIRequest; system edits use full tax config equality.Validation
env POSTGRES_HOST=127.0.0.1 make test-nocachemake etoedirenv exec . make lint-go-fastenv POSTGRES_HOST=127.0.0.1 go test -tags=dynamic -count=1 ./test/billingenv POSTGRES_HOST=127.0.0.1 go test -tags=dynamic -count=1 -run TestStripeInvoicing/TestEmptyInvoiceGenerationZeroUsage ./test/app/stripeenv POSTGRES_HOST=127.0.0.1 go test -tags=dynamic -count=1 ./test/notificationSummary by CodeRabbit
New Features
Bug Fixes