Skip to content

feat(taxcode): prevent deletion of plan-referenced tax codes and validate tax codes on plan publish - #4557

Merged
borbelyr-kong merged 3 commits into
fix/tax-code-deletefrom
feat/taxcode-plan-publish-validation
Jul 3, 2026
Merged

borbelyr-kong merged 3 commits into
fix/tax-code-deletefrom
feat/taxcode-plan-publish-validation

Conversation

@borbelyr-kong

@borbelyr-kong borbelyr-kong commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

@coderabbitai

Summary by CodeRabbit

  • New Features

    • Plan publishing now validates referenced tax codes and surfaces missing or deleted tax-code issues clearly.
    • Rate cards can now report their linked tax-code reference for validation.
  • Bug Fixes

    • Publishing a plan now fails when a referenced tax code no longer exists or has been deleted.
    • Validation errors now point to the specific phase or rate card involved.
  • Tests

    • Added coverage for tax-code resolution, missing references, and publish-time validation behavior.

@coderabbitai

coderabbitai Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1d7a0ed9-e488-44a5-b089-52f4379b473f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/taxcode-plan-publish-validation

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.

@borbelyr-kong

Copy link
Copy Markdown
Contributor Author

@greptileai review this draft

@greptile-apps

greptile-apps Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds tax code validation to the plan publishing flow and creates a TaxCodeResolver abstraction (mirroring the existing FeatureResolver pattern) for namespace-scoped tax code lookup. Publishing a plan now fails with a user-friendly validation error when a referenced tax code is missing or soft-deleted.

  • Introduces TaxCodeResolver / NamespacedTaxCodeResolver interfaces and a concrete implementation in taxcoderesolver/resolver.go that delegates to taxcode.Service.
  • Adds ValidateRateCardsWithTaxCodes and ValidatePlanWithTaxCodes (mirroring their feature counterparts) wired into PublishPlan; soft-deleted and missing tax codes surface as ErrRateCardTaxCodeNotFound with field-prefixed paths.
  • Updates all three wire-gen entrypoints and test harnesses to inject the new resolver; covers the new paths with both unit tests (stub resolver) and an integration test that uses a deliberately dangling tax code reference.

Confidence Score: 5/5

Safe to merge — the change adds a publish-time guard with no mutations to existing data paths.

The new tax code validation follows the existing feature-resolver pattern precisely, all error branches are covered by both unit and integration tests, the wire-gen changes are consistent across all three entrypoints, and no existing behavior is altered. The only new failure mode is a clean validation rejection during plan publishing, which is the intended behavior.

No files require special attention.

Important Files Changed

Filename Overview
openmeter/productcatalog/taxcoderesolver.go New interfaces TaxCodeResolver and NamespacedTaxCodeResolver — clean, mirrors FeatureResolver pattern
openmeter/productcatalog/taxcoderesolver/resolver.go Concrete resolver wrapping taxcode.Service; namespace guard present, properly delegates to GetTaxCode
openmeter/productcatalog/ratecard.go Adds TaxCodeReference accessor and ValidateRateCardsWithTaxCodes validator; empty-ID guard and nil check are correct
openmeter/productcatalog/plan.go ValidatePlanWithTaxCodes mirrors ValidatePlanWithFeatures exactly; field-prefix nesting is correct
openmeter/productcatalog/plan/service/plan.go Tax code validation inserted correctly after feature validation in PublishPlan; errors collected and wrapped consistently
openmeter/productcatalog/plan/service/service.go TaxCodeResolver added to Config with nil-guard; cleanly wired into service struct
openmeter/productcatalog/errors.go ErrRateCardTaxCodeNotFound error variable and error code constant added consistently with existing pattern
openmeter/productcatalog/taxcoderesolver_test.go Unit tests cover valid code, deleted code, not-found error, no-TaxConfig skip, and empty-ID guard; good breadth
openmeter/productcatalog/plan/service/taxcode_test.go Integration test correctly simulates dangling tax code reference and verifies publish-time validation error
app/common/productcatalog.go NewTaxCodeResolver provider and TaxCodeResolver wiring added correctly to Plan wire set and NewPlanService
cmd/server/wire_gen.go taxcoderesolver.New injected before planService creation with correct error-cleanup ordering

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant Client
    participant PlanService
    participant ValidatePlanWithTaxCodes
    participant ValidateRateCardsWithTaxCodes
    participant NamespacedTaxCodeResolver
    participant TaxCodeService

    Client->>PlanService: PublishPlan(params)
    PlanService->>PlanService: pp.ValidateWith(ValidatePlanWithTaxCodes)
    loop each phase
        ValidatePlanWithTaxCodes->>ValidateRateCardsWithTaxCodes: validate(phase.RateCards)
        loop each rate card
            ValidateRateCardsWithTaxCodes->>ValidateRateCardsWithTaxCodes: check TaxCodeReference()
            alt no TaxConfig
                ValidateRateCardsWithTaxCodes-->>ValidateRateCardsWithTaxCodes: skip
            else empty TaxCodeID
                ValidateRateCardsWithTaxCodes-->>ValidatePlanWithTaxCodes: ErrRateCardTaxCodeNotFound
            else valid TaxCodeID
                ValidateRateCardsWithTaxCodes->>NamespacedTaxCodeResolver: Resolve(ctx, id)
                NamespacedTaxCodeResolver->>TaxCodeService: GetTaxCode(namespace, id)
                alt not found / deleted
                    TaxCodeService-->>NamespacedTaxCodeResolver: error
                    NamespacedTaxCodeResolver-->>ValidateRateCardsWithTaxCodes: error
                    ValidateRateCardsWithTaxCodes-->>ValidatePlanWithTaxCodes: ErrRateCardTaxCodeNotFound
                else found and active
                    TaxCodeService-->>NamespacedTaxCodeResolver: "TaxCode{DeletedAt nil}"
                    NamespacedTaxCodeResolver-->>ValidateRateCardsWithTaxCodes: TaxCode ptr
                    ValidateRateCardsWithTaxCodes-->>ValidatePlanWithTaxCodes: nil
                end
            end
        end
    end
    ValidatePlanWithTaxCodes-->>PlanService: joined errs
    alt validation errors
        PlanService-->>Client: GenericValidationError wrapping field-prefixed ValidationIssues
    else no errors
        PlanService->>PlanService: proceed with publish
        PlanService-->>Client: published Plan
    end
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"}}}%%
sequenceDiagram
    participant Client
    participant PlanService
    participant ValidatePlanWithTaxCodes
    participant ValidateRateCardsWithTaxCodes
    participant NamespacedTaxCodeResolver
    participant TaxCodeService

    Client->>PlanService: PublishPlan(params)
    PlanService->>PlanService: pp.ValidateWith(ValidatePlanWithTaxCodes)
    loop each phase
        ValidatePlanWithTaxCodes->>ValidateRateCardsWithTaxCodes: validate(phase.RateCards)
        loop each rate card
            ValidateRateCardsWithTaxCodes->>ValidateRateCardsWithTaxCodes: check TaxCodeReference()
            alt no TaxConfig
                ValidateRateCardsWithTaxCodes-->>ValidateRateCardsWithTaxCodes: skip
            else empty TaxCodeID
                ValidateRateCardsWithTaxCodes-->>ValidatePlanWithTaxCodes: ErrRateCardTaxCodeNotFound
            else valid TaxCodeID
                ValidateRateCardsWithTaxCodes->>NamespacedTaxCodeResolver: Resolve(ctx, id)
                NamespacedTaxCodeResolver->>TaxCodeService: GetTaxCode(namespace, id)
                alt not found / deleted
                    TaxCodeService-->>NamespacedTaxCodeResolver: error
                    NamespacedTaxCodeResolver-->>ValidateRateCardsWithTaxCodes: error
                    ValidateRateCardsWithTaxCodes-->>ValidatePlanWithTaxCodes: ErrRateCardTaxCodeNotFound
                else found and active
                    TaxCodeService-->>NamespacedTaxCodeResolver: "TaxCode{DeletedAt nil}"
                    NamespacedTaxCodeResolver-->>ValidateRateCardsWithTaxCodes: TaxCode ptr
                    ValidateRateCardsWithTaxCodes-->>ValidatePlanWithTaxCodes: nil
                end
            end
        end
    end
    ValidatePlanWithTaxCodes-->>PlanService: joined errs
    alt validation errors
        PlanService-->>Client: GenericValidationError wrapping field-prefixed ValidationIssues
    else no errors
        PlanService->>PlanService: proceed with publish
        PlanService-->>Client: published Plan
    end
Loading

Reviews (2): Last reviewed commit: "fix(taxcode): fix lint" | Re-trigger Greptile

Comment on lines +23 to +27
func (s stubTaxCodeResolver) Namespace() string { return s.namespace }

func (s stubTaxCodeResolver) ResolveTaxCode(_ context.Context, _ string) (*taxcode.TaxCode, error) {
return s.result, s.err
}

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 Stub resolver ignores the id argument — test can't verify correct lookup key

stubTaxCodeResolver.ResolveTaxCode discards the id parameter and always returns the preset result/err. This means the tests cannot catch a bug where the wrong field (e.g. rc.TaxConfig.TaxCodeID vs. some other field) is passed to the resolver. Adding an expectedID string field to the stub and asserting on it (or using require.Equal(t, taxCodeID, capturedID)) would make the tests meaningful for the lookup-key path.

Fix in Claude Code

Comment on lines +43 to +46
func (r *resolver) ResolveTaxCode(ctx context.Context, namespace string, id string) (*taxcode.TaxCode, error) {
if namespace == "" {
return nil, errors.New("namespace is not set")
}

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 Empty id produces a misleading system error instead of a user-friendly not-found error

When TaxCodeID is a non-nil pointer to an empty string, GetTaxCodeInput.Validate() returns ErrResourceIDEmpty — a validation issue that is neither IsGenericNotFoundError nor IsTaxCodeNotFoundError. The caller in ValidateRateCardsWithTaxCodes therefore falls into the generic "failed to resolve tax code for ratecard: ..." branch rather than surfacing ErrRateCardTaxCodeNotFound. An early guard here keeps the error classification consistent.

Suggested change
func (r *resolver) ResolveTaxCode(ctx context.Context, namespace string, id string) (*taxcode.TaxCode, error) {
if namespace == "" {
return nil, errors.New("namespace is not set")
}
func (r *resolver) ResolveTaxCode(ctx context.Context, namespace string, id string) (*taxcode.TaxCode, error) {
if namespace == "" {
return nil, errors.New("namespace is not set")
}
if id == "" {
return nil, errors.New("id is not set")
}

Fix in Claude Code

Comment on lines +970 to +972
if tc == nil || tc.DeletedAt != nil {
errs = append(errs, models.ErrorWithFieldPrefix(rateCardFieldSelector, ErrRateCardTaxCodeNotFound))
}

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 tc == nil branch is unreachable with the concrete resolver

resolver.ResolveTaxCode either returns an error or &tc (a non-nil pointer). A (nil, nil) return is impossible from the production implementation, so tc == nil can never be true here. The DeletedAt check alone is sufficient. Since the NamespacedTaxCodeResolver interface doesn't prohibit (nil, nil) from other implementations, keeping the guard is defensible — but a comment explaining the intent would help future readers.

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

@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.

🧹 Nitpick comments (1)
openmeter/productcatalog/taxcoderesolver_test.go (1)

84-85: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use t.Context() instead of context.Background() in this test.

Line 84 should use the test-scoped context so cancellation/deadline stays tied to the test harness.

🔧 Proposed change
-	ctx := context.Background()
+	ctx := t.Context()

As per coding guidelines, "Use t.Context() in Go tests when a testing.T or testing.TB is available instead of introducing context.Background() to keep cancellation tied to the test harness."

🤖 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/productcatalog/taxcoderesolver_test.go` around lines 84 - 85,
Replace the `context.Background()` call with `t.Context()` in the ctx variable
initialization to ensure the context is properly tied to the test harness for
correct cancellation and deadline management. The ctx variable assignment should
use t.Context() instead of context.Background() to align with the coding
guidelines for Go tests.

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.

Nitpick comments:
In `@openmeter/productcatalog/taxcoderesolver_test.go`:
- Around line 84-85: Replace the `context.Background()` call with `t.Context()`
in the ctx variable initialization to ensure the context is properly tied to the
test harness for correct cancellation and deadline management. The ctx variable
assignment should use t.Context() instead of context.Background() to align with
the coding guidelines for Go tests.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0ec3f2f6-65e0-432f-97b6-52cd84a58039

📥 Commits

Reviewing files that changed from the base of the PR and between a46a043 and 8a31078.

📒 Files selected for processing (17)
  • app/common/productcatalog.go
  • cmd/billing-worker/wire_gen.go
  • cmd/jobs/internal/wire_gen.go
  • cmd/server/wire_gen.go
  • openmeter/productcatalog/errors.go
  • openmeter/productcatalog/plan.go
  • openmeter/productcatalog/plan/service/plan.go
  • openmeter/productcatalog/plan/service/service.go
  • openmeter/productcatalog/ratecard.go
  • openmeter/productcatalog/taxcoderesolver.go
  • openmeter/productcatalog/taxcoderesolver/resolver.go
  • openmeter/productcatalog/taxcoderesolver_test.go
  • openmeter/productcatalog/testutils/env.go
  • openmeter/subscription/testutils/service.go
  • openmeter/taxcode/service/hooks/planvalidator.go
  • test/billing/subscription_suite.go
  • test/customer/testenv.go

@borbelyr-kong
borbelyr-kong force-pushed the feat/taxcode-plan-publish-validation branch 3 times, most recently from 2f9c5ab to 3a58a03 Compare June 24, 2026 13:10

@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.

🧹 Nitpick comments (2)
openmeter/productcatalog/plan/service/taxcode_test.go (1)

892-893: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Small nudge: derive from t.Context().

You can build the cancelable context off the test context so cancellation stays tied to the harness (and t.Context() is already canceled at cleanup, so this stays nicely scoped):

♻️ Suggested tweak
-	ctx, cancel := context.WithCancel(context.Background())
+	ctx, cancel := context.WithCancel(t.Context())
 	defer cancel()

As per coding guidelines: "Use t.Context() in Go tests when a testing.T or testing.TB is available instead of introducing context.Background()".

🤖 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/productcatalog/plan/service/taxcode_test.go` around lines 892 -
893, The test context setup is using context.Background() directly instead of
the test harness context. Update the cancelable context creation in
taxcode_test.go to derive from t.Context() so it stays scoped to the test
lifecycle, keeping the existing cancel behavior while tying cancellation to the
test cleanup.

Source: Coding guidelines

openmeter/productcatalog/taxcoderesolver_test.go (1)

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

Tiny one: prefer t.Context() here.

Since t is right there, t.Context() keeps cancellation tied to the test harness. Quick swap:

♻️ Suggested tweak
-	ctx := context.Background()
+	ctx := t.Context()

As per coding guidelines: "Use t.Context() in Go tests when a testing.T or testing.TB is available instead of introducing context.Background()".

🤖 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/productcatalog/taxcoderesolver_test.go` at line 84, The test setup
currently creates a standalone context with context.Background() instead of
using the test-scoped context. Update the test in taxcoderesolver_test.go to use
t.Context() wherever the local testing.T is available, so cancellation and
deadlines stay tied to the test harness; this should be applied at the ctx
initialization near the TaxCodeResolver test setup.

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.

Nitpick comments:
In `@openmeter/productcatalog/plan/service/taxcode_test.go`:
- Around line 892-893: The test context setup is using context.Background()
directly instead of the test harness context. Update the cancelable context
creation in taxcode_test.go to derive from t.Context() so it stays scoped to the
test lifecycle, keeping the existing cancel behavior while tying cancellation to
the test cleanup.

In `@openmeter/productcatalog/taxcoderesolver_test.go`:
- Line 84: The test setup currently creates a standalone context with
context.Background() instead of using the test-scoped context. Update the test
in taxcoderesolver_test.go to use t.Context() wherever the local testing.T is
available, so cancellation and deadlines stay tied to the test harness; this
should be applied at the ctx initialization near the TaxCodeResolver test setup.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2eb85a87-7d87-4680-bbb4-0807a71c8fa2

📥 Commits

Reviewing files that changed from the base of the PR and between 8a31078 and 3a58a03.

📒 Files selected for processing (17)
  • app/common/productcatalog.go
  • cmd/billing-worker/wire_gen.go
  • cmd/jobs/internal/wire_gen.go
  • cmd/server/wire_gen.go
  • openmeter/productcatalog/errors.go
  • openmeter/productcatalog/plan.go
  • openmeter/productcatalog/plan/service/plan.go
  • openmeter/productcatalog/plan/service/service.go
  • openmeter/productcatalog/plan/service/taxcode_test.go
  • openmeter/productcatalog/ratecard.go
  • openmeter/productcatalog/taxcoderesolver.go
  • openmeter/productcatalog/taxcoderesolver/resolver.go
  • openmeter/productcatalog/taxcoderesolver_test.go
  • openmeter/productcatalog/testutils/env.go
  • openmeter/subscription/testutils/service.go
  • test/billing/subscription_suite.go
  • test/customer/testenv.go
✅ Files skipped from review due to trivial changes (2)
  • cmd/billing-worker/wire_gen.go
  • cmd/jobs/internal/wire_gen.go
🚧 Files skipped from review as they are similar to previous changes (11)
  • openmeter/productcatalog/taxcoderesolver.go
  • test/billing/subscription_suite.go
  • openmeter/productcatalog/errors.go
  • openmeter/productcatalog/plan/service/service.go
  • cmd/server/wire_gen.go
  • openmeter/productcatalog/plan.go
  • openmeter/productcatalog/testutils/env.go
  • test/customer/testenv.go
  • openmeter/productcatalog/ratecard.go
  • openmeter/productcatalog/plan/service/plan.go
  • app/common/productcatalog.go

@borbelyr-kong
borbelyr-kong force-pushed the fix/tax-code-delete branch 2 times, most recently from 181cff6 to 96f7d97 Compare July 3, 2026 09:34
@borbelyr-kong
borbelyr-kong force-pushed the feat/taxcode-plan-publish-validation branch from f5bc13d to d50dfbb Compare July 3, 2026 09:47
@borbelyr-kong
borbelyr-kong marked this pull request as ready for review July 3, 2026 11:57
@borbelyr-kong
borbelyr-kong requested a review from a team as a code owner July 3, 2026 11:57
@borbelyr-kong
borbelyr-kong merged commit e193df7 into fix/tax-code-delete Jul 3, 2026
23 checks passed
@borbelyr-kong
borbelyr-kong deleted the feat/taxcode-plan-publish-validation branch July 3, 2026 11:59
borbelyr-kong added a commit that referenced this pull request Jul 16, 2026
borbelyr-kong added a commit that referenced this pull request Oct 14, 2026
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