Skip to content

feat(customers): check for key and subject conflict - #3176

Merged
hekike merged 1 commit into
mainfrom
feat/customer-subject-conflict
Aug 1, 2025
Merged

hekike merged 1 commit into
mainfrom
feat/customer-subject-conflict

Conversation

@hekike

@hekike hekike commented Aug 1, 2025 •

Copy link
Copy Markdown
Contributor

Do not allow using a customer key that is in a usage attribution of an another customer.

Summary by CodeRabbit

  • Bug Fixes
    • Improved validation to prevent creating or updating a customer with a key that overlaps with another customer's subject key within the same namespace.
  • Tests
    • Added test cases to ensure that overlapping customer and subject keys are correctly detected and result in conflict errors during creation and update.

@hekike
hekike requested a review from chrisgacsal August 1, 2025 12:14
@hekike
hekike requested a review from a team as a code owner August 1, 2025 12:14
@hekike hekike added release-note/feature Release note: Exciting New Features area/billing labels Aug 1, 2025
@coderabbitai

coderabbitai Bot commented Aug 1, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

A validation step was added to the customer adapter to prevent customer keys from overlapping with subject keys of other customers in the same namespace during creation and updates. Corresponding test cases were introduced to verify these conflict scenarios for both customer creation and update operations.

Changes

Cohort / File(s) Change Summary
Customer Adapter Conflict Validation
openmeter/customer/adapter/customer.go
Added checks in CreateCustomer and UpdateCustomer to prevent customer keys from overlapping with other customers' subject keys within the same namespace. Returns a conflict error on detection.
Test Coverage for Key-Subject Overlap
test/customer/customer.go
Extended tests to cover scenarios where customer key overlaps with another customer's subject key, ensuring proper conflict error handling during creation and update.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 77ad9f7 and aa0f952.

📒 Files selected for processing (2)
  • openmeter/customer/adapter/customer.go (2 hunks)
  • test/customer/customer.go (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
test/customer/customer.go (2)
openmeter/customer/customer.go (5)
  • CreateCustomerInput (189-192)
  • CustomerMutate (48-58)
  • CustomerUsageAttribution (129-131)
  • UpdateCustomerInput (207-210)
  • CustomerID (78-78)
pkg/models/errors.go (1)
  • IsGenericConflictError (87-95)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: Developer environment
  • GitHub Check: Quickstart
  • GitHub Check: CI
  • GitHub Check: Commit hooks
  • GitHub Check: E2E
  • GitHub Check: Lint
  • GitHub Check: Test
  • GitHub Check: Migration Checks
  • GitHub Check: Build
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Analyze (go)
🔇 Additional comments (4)
openmeter/customer/adapter/customer.go (2)

155-172: LGTM! Validation logic correctly prevents key-subject conflicts.

The implementation properly checks for key conflicts with existing customer subjects:

  • Correctly scoped to namespace and non-deleted records
  • Efficient query using Select to fetch only the needed field
  • Appropriate error handling with descriptive conflict message
  • Follows the established pattern used for ID overlap validation

448-466: LGTM! Update validation correctly excludes current customer.

The implementation properly handles key-subject conflict validation for updates:

  • Correctly excludes the current customer ID with CustomerIDNEQ to prevent false positives
  • Maintains consistency with the CreateCustomer validation logic
  • Appropriate error handling and messaging
test/customer/customer.go (2)

142-158: LGTM! Test case correctly validates key-subject conflict prevention.

The test properly verifies that creating a customer with a key matching an existing customer's subject key results in a conflict error:

  • Uses realistic test data from previously created customer
  • Appropriate error type assertion using models.IsGenericConflictError
  • Clear and descriptive test logic and assertion message

222-279: LGTM! Comprehensive test coverage for update conflict scenarios.

The test cases thoroughly validate the conflict prevention logic for customer updates:

  • Proper test setup with additional customer data to create realistic conflict scenarios
  • Tests both key-key and key-subject overlap conflicts
  • Appropriate error type assertions using models.IsGenericConflictError
  • Clear, descriptive test logic and assertion messages
  • Follows established testing patterns
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/customer-subject-conflict

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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests 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.

@hekike
hekike merged commit d5f1385 into main Aug 1, 2025
@hekike
hekike deleted the feat/customer-subject-conflict branch August 1, 2025 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/billing release-note/feature Release note: Exciting New Features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants