Skip to content

fix: plan add-on assignment service - #2704

Merged
chrisgacsal merged 5 commits into
mainfrom
refactor/productcatalog
Apr 22, 2025
Merged

chrisgacsal merged 5 commits into
mainfrom
refactor/productcatalog

Conversation

@chrisgacsal

@chrisgacsal chrisgacsal commented Apr 22, 2025 •

Copy link
Copy Markdown
Collaborator

Overview

Fix validation for assignment udpate/delete operations.

Summary by CodeRabbit

  • Bug Fixes
    • Improved validation and error handling for plan add-on operations, ensuring more consistent and reliable error responses.
  • Refactor
    • Streamlined internal validation logic for product catalog mapping and service functions, resulting in cleaner and more maintainable code.

@chrisgacsal chrisgacsal self-assigned this Apr 22, 2025
@chrisgacsal
chrisgacsal requested a review from a team as a code owner April 22, 2025 12:01
@chrisgacsal chrisgacsal added the release-note/bug-fix Release note: Bug Fixes label Apr 22, 2025
@coderabbitai

coderabbitai Bot commented Apr 22, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This change removes explicit validation calls from mapping functions that convert database rows into domain model objects across several modules. Instead of invoking Validate() methods and handling their errors after object construction, the functions now return the constructed objects directly. Additionally, the planaddon service methods are refactored to streamline validation by relying on already-loaded models, simplifying error handling, and removing redundant entity fetching. There are no changes to the signatures of exported or public entities.

Changes

Files/Paths Change Summary
openmeter/productcatalog/addon/adapter/mapping.go
openmeter/productcatalog/plan/adapter/mapping.go
openmeter/productcatalog/planaddon/adapter/mapping.go
Removed explicit validation calls and related error handling from functions mapping database rows to domain objects.
openmeter/productcatalog/planaddon/service/planaddon.go Streamlined validation in service methods by validating loaded models, simplified error handling, and removed redundant entity fetching.

Possibly related PRs

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@chrisgacsal
chrisgacsal enabled auto-merge (squash) April 22, 2025 12:07

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

🧹 Nitpick comments (2)
openmeter/productcatalog/planaddon/service/planaddon.go (2)

208-216: 👍 Good addition – but consider using a specific validation error instead of “not‑found”

The newly added plan checks prevent deleting an assignment when the plan is deleted or not in Draft/Scheduled – great!
However, turning validation failures into a GenericNotFoundError may mislead API consumers (they will assume the resource is missing). A GenericValidationError (or a new domain‑specific code) would communicate the situation more accurately.

If backwards‑compatibility requires the “not‑found” semantics, add a short comment explaining the rationale.


336-336: Typo in error message (“udpate”)

- return nil, models.NewGenericNotFoundError(
-     fmt.Errorf("failed to udpate plan add-on assignment ...
+ return nil, models.NewGenericNotFoundError(
+     fmt.Errorf("failed to update plan add-on assignment ...

A minor typo, but worth fixing to keep logs and error texts professional.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6215506 and 2c8ba45.

📒 Files selected for processing (4)
  • openmeter/productcatalog/addon/adapter/mapping.go (0 hunks)
  • openmeter/productcatalog/plan/adapter/mapping.go (0 hunks)
  • openmeter/productcatalog/planaddon/adapter/mapping.go (0 hunks)
  • openmeter/productcatalog/planaddon/service/planaddon.go (6 hunks)
💤 Files with no reviewable changes (3)
  • openmeter/productcatalog/addon/adapter/mapping.go
  • openmeter/productcatalog/planaddon/adapter/mapping.go
  • openmeter/productcatalog/plan/adapter/mapping.go
🧰 Additional context used
🧬 Code Graph Analysis (1)
openmeter/productcatalog/planaddon/service/planaddon.go (6)
openmeter/productcatalog/plan/validators.go (2)
  • IsPlanDeleted (13-21)
  • HasPlanStatus (23-31)
pkg/clock/clock.go (1)
  • Now (14-21)
openmeter/productcatalog/addon/validators.go (2)
  • IsAddonDeleted (13-21)
  • HasAddonStatus (23-31)
openmeter/productcatalog/plan/plan.go (1)
  • Plan (18-30)
pkg/models/error.go (1)
  • NewGenericNotFoundError (38-40)
openmeter/productcatalog/addon/addon.go (1)
  • Addon (15-23)
⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: Test
  • GitHub Check: Quickstart
  • GitHub Check: Migration Checks
  • GitHub Check: Lint
  • GitHub Check: E2E
  • GitHub Check: Developer environment
  • GitHub Check: CI
  • GitHub Check: Build
  • GitHub Check: Commit hooks
  • GitHub Check: Analyze (go)

Comment on lines +331 to 339
if err = planAddon.Plan.ValidateWith(
plan.IsPlanDeleted(clock.Now()),
plan.HasPlanStatus(productcatalog.PlanStatusDraft, productcatalog.PlanStatusScheduled),
); err != nil {
return nil, models.NewGenericNotFoundError(
fmt.Errorf("failed to udpate plan add-on assignment [namespace=%s plan.id=%s addon.id=%s]: %w",
params.Namespace, params.PlanID, params.AddonID, 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.

⚠️ Potential issue

Missing Add‑on state validation on Update

UpdatePlanAddon used to reload and validate the add‑on; now it only validates the plan.
An add‑on can become deleted or move out of Active status after the assignment was created, so updating the assignment may silently succeed while referencing an invalid add‑on.

Consider restoring the validation, re‑using the already loaded planAddon.Addon:

+ // Validate add‑on
+ if err = planAddon.Addon.ValidateWith(
+     addon.IsAddonDeleted(clock.Now()),
+     addon.HasAddonStatus(productcatalog.AddonStatusActive),
+ ); err != nil {
+     return nil, models.NewGenericNotFoundError(
+         fmt.Errorf("failed to update plan add‑on assignment [namespace=%s plan.id=%s addon.id=%s]: %w",
+             params.Namespace, params.PlanID, params.AddonID, err),
+     )
+ }

This keeps business rules consistent across Create/Update paths.

@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 (2)
openmeter/productcatalog/planaddon/service/planaddon.go (2)

331-339: Typo in log/error messages (“udpate”) and missing capitalisation

There is a minor typo that appears twice (udpate). Fixing it improves diagnostics and avoids search discrepancies.

-               fmt.Errorf("failed to udpate plan add-on assignment [namespace=%s plan.id=%s addon.id=%s]: %w",
+               fmt.Errorf("failed to update plan add-on assignment [namespace=%s plan.id=%s addon.id=%s]: %w",

Search the file for the same misspelling to keep messages consistent.


341-354: Potential double conversion of domain models

planAddon.Plan.AsProductCatalogPlan() and planAddon.Addon.AsProductCatalogAddon() are called although planAddon was originally produced by adapter.GetPlanAddon, which (per the refactor) now already yields validated productcatalog.Plan / Addon objects. The extra conversion:

  1. Wastes a small amount of CPU/allocations.
  2. Risks silently diverging if conversion helpers evolve.

If planAddon.Plan/Addon are already the desired types, you can assign them directly:

-       Plan:  planAddon.Plan.AsProductCatalogPlan(),
-       Addon: planAddon.Addon.AsProductCatalogAddon(),
+       Plan:  planAddon.Plan,
+       Addon: planAddon.Addon,

Please verify the concrete types returned by the adapter to avoid accidental interface narrowing.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6215506 and 2c8ba45.

📒 Files selected for processing (4)
  • openmeter/productcatalog/addon/adapter/mapping.go (0 hunks)
  • openmeter/productcatalog/plan/adapter/mapping.go (0 hunks)
  • openmeter/productcatalog/planaddon/adapter/mapping.go (0 hunks)
  • openmeter/productcatalog/planaddon/service/planaddon.go (6 hunks)
💤 Files with no reviewable changes (3)
  • openmeter/productcatalog/planaddon/adapter/mapping.go
  • openmeter/productcatalog/addon/adapter/mapping.go
  • openmeter/productcatalog/plan/adapter/mapping.go
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: E2E
  • GitHub Check: Developer environment
  • GitHub Check: CI
  • GitHub Check: Analyze (go)
🔇 Additional comments (1)
openmeter/productcatalog/planaddon/service/planaddon.go (1)

208-216: Validation failure is translated to “not‑found” — is that intentional?

When the plan linked to the assignment is deleted or in an immutable status, the code maps the validation error to models.NewGenericNotFoundError. From a consumer’s standpoint this is misleading: the assignment exists, but cannot be deleted because the plan is locked, which is a 409‑Conflict‑style situation rather than 404‑Not‑Found.

Double‑check the desired API semantics; if the goal is to hide resource existence for permission reasons that should be documented. Otherwise consider surfacing a Conflict or Validation error instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants