Skip to content

fix(customer): subject service hook - #3114

Merged
chrisgacsal merged 2 commits into
mainfrom
feat/subject-hooks
Jul 18, 2025
Merged

chrisgacsal merged 2 commits into
mainfrom
feat/subject-hooks

Conversation

@chrisgacsal

@chrisgacsal chrisgacsal commented Jul 17, 2025 •

Copy link
Copy Markdown
Collaborator

Overview

  • fix Customer lookup by usage attribution where deleted Customers were also returned
  • ensure that deleted Customers are ignored by the Subject service hook
  • fix nil pointer dereference when metadata from Subject and Customer are merged
  • do not export methods internal methods of the CustomerSubjectProvisioner

Summary by CodeRabbit

  • Bug Fixes

    • Excluded deleted customers from certain queries to ensure only active customers are considered.
    • Improved handling of deleted customers during customer provisioning to avoid conflicts.
  • Refactor

    • Renamed several internal methods for clarity and consistency.
    • Enhanced metadata merging logic to better handle empty values.
  • Tests

    • Updated tests to align with internal method renaming and improved test clarity.

@chrisgacsal
chrisgacsal requested a review from tothandras July 17, 2025 14:56
@chrisgacsal chrisgacsal self-assigned this Jul 17, 2025
@chrisgacsal
chrisgacsal requested a review from a team as a code owner July 17, 2025 14:56
@chrisgacsal chrisgacsal added the release-note/misc Miscellaneous changes label Jul 17, 2025
@coderabbitai

coderabbitai Bot commented Jul 17, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This change adds a filter to exclude soft-deleted customers in the GetCustomerByUsageAttribution query and refactors several method names in the customer provisioning logic for brevity. It also updates metadata handling and test code to use the new method names, with no changes to core control flow or public API signatures.

Changes

Files/Paths Change Summary
openmeter/customer/adapter/customer.go Added a filter to exclude soft-deleted customers in the GetCustomerByUsageAttribution query.
openmeter/customer/service/hooks/subject.go Renamed several methods for brevity; added deleted customer check; updated metadata merging logic and error msgs.
openmeter/customer/service/hooks/subject_test.go Updated tests to use new unexported method names; removed redundant metadata initialization.

Possibly related PRs


📜 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 f776334 and 02f155c.

📒 Files selected for processing (3)
  • openmeter/customer/adapter/customer.go (1 hunks)
  • openmeter/customer/service/hooks/subject.go (11 hunks)
  • openmeter/customer/service/hooks/subject_test.go (4 hunks)
🧰 Additional context used
🧠 Learnings (2)
openmeter/customer/adapter/customer.go (1)
Learnt from: chrisgacsal
PR: openmeterio/openmeter#2699
File: openmeter/productcatalog/planaddon/service/service_test.go:210-211
Timestamp: 2025-04-21T08:32:31.689Z
Learning: In `productcatalog.UsageBasedRateCard`, the `BillingCadence` field is a non-pointer `isodate.Period`, while in `productcatalog.FlatFeeRateCard`, `BillingCadence` is a pointer type (`*isodate.Period`). This means `MonthPeriod` should be used directly for `UsageBasedRateCard` (not `&MonthPeriod`).
openmeter/customer/service/hooks/subject_test.go (1)
Learnt from: GAlexIHU
PR: openmeterio/openmeter#2383
File: openmeter/entitlement/metered/lateevents_test.go:37-45
Timestamp: 2025-03-07T12:17:43.129Z
Learning: In the OpenMeter codebase, test files like `openmeter/entitlement/metered/lateevents_test.go` may use variables like `meterSlug` and `namespace` without explicit declarations visible in the same file. This appears to be an accepted pattern in their test structure.
🧬 Code Graph Analysis (2)
openmeter/customer/adapter/customer.go (3)
openmeter/ent/db/customer/where.go (2)
  • HasSubjectsWith (1330-1339)
  • DeletedAtIsNil (348-350)
openmeter/ent/db/customersubjects/where.go (2)
  • SubjectKey (69-71)
  • DeletedAtIsNil (359-361)
openmeter/ent/db/subscription/where.go (1)
  • DeletedAtIsNil (323-325)
openmeter/customer/service/hooks/subject.go (2)
openmeter/customer/customer.go (1)
  • UpdateCustomerInput (207-210)
pkg/models/metadata.go (1)
  • Metadata (9-9)
⏰ 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). (12)
  • GitHub Check: Artifacts / Benthos Collector Container image
  • GitHub Check: Artifacts / Container image
  • GitHub Check: Commit hooks
  • GitHub Check: Quickstart
  • GitHub Check: Developer environment
  • GitHub Check: E2E
  • GitHub Check: CI
  • GitHub Check: Lint
  • GitHub Check: Build
  • GitHub Check: Test
  • GitHub Check: Migration Checks
  • GitHub Check: Analyze (go)
🔇 Additional comments (19)
openmeter/customer/adapter/customer.go (1)

376-377: LGTM! Consistent implementation of deleted customer filtering.

This change correctly adds the DeletedAtIsNil() filter to exclude soft-deleted customers from usage attribution lookups, which aligns with the PR objective and follows the established pattern used elsewhere in the file.

openmeter/customer/service/hooks/subject_test.go (5)

64-64: LGTM! Test updated to reflect method renaming.

The test correctly uses the new unexported method name getCustomerForSubject instead of the previous GetCustomerForSubject, aligning with the refactoring in the main implementation.


68-68: LGTM! Test updated to reflect method renaming.

The test correctly uses the new unexported method name ensureCustomer instead of the previous EnsureCustomer, aligning with the refactoring in the main implementation.


123-123: LGTM! Test updated to reflect method renaming.

The test correctly uses the new unexported method name ensureCustomer instead of the previous EnsureCustomer, aligning with the refactoring in the main implementation.


178-178: LGTM! Test updated to reflect method renaming.

The test correctly uses the new unexported method name ensureCustomer instead of the previous EnsureCustomer, aligning with the refactoring in the main implementation.


231-231: LGTM! Test updated to reflect method renaming.

The test correctly uses the new unexported method name ensureCustomer instead of the previous EnsureCustomer, aligning with the refactoring in the main implementation.

openmeter/customer/service/hooks/subject.go (13)

39-40: LGTM! Method renamed for better encapsulation.

The method has been appropriately renamed from ProvisionCustomerForSubject to provision (unexported) for better encapsulation within the package, while maintaining the same functionality.


57-57: LGTM! Method call updated consistently.

The method call has been correctly updated to use the renamed provision method.


61-61: LGTM! Method call updated consistently.

The method call has been correctly updated to use the renamed provision method.


190-190: LGTM! Method renamed for better encapsulation.

The method has been appropriately renamed from GetCustomerForSubject to getCustomerForSubject (unexported) for better encapsulation within the package.


227-227: LGTM! Method renamed for better encapsulation.

The method has been appropriately renamed from EnsureCustomer to ensureCustomer (unexported) for better encapsulation within the package.


234-234: LGTM! Method call updated consistently.

The method call has been correctly updated to use the renamed getCustomerForSubject method.


243-247: Excellent fix for handling deleted customers!

This change correctly addresses the PR objective by ignoring customers marked as deleted (DeletedAt != nil), which prevents nil pointer dereference issues and ensures consistency with the adapter layer filtering. The logic appropriately resets both the customer and key conflict flags when a deleted customer is encountered.


263-266: Good refactoring for code clarity.

Introducing the local customerID variable improves code readability and reduces duplication in the error messages and method calls that follow.


279-287: Improved metadata handling with explicit nil check.

This change enhances the metadata merging logic by explicitly checking for empty metadata maps and returning nil when both subject and customer metadata are empty, rather than always attempting to merge them. This prevents unnecessary empty metadata objects and provides cleaner data handling.


335-335: LGTM! Method renamed for better encapsulation.

The method has been appropriately renamed from EnsureStripeCustomer to ensureStripeCustomer (unexported) for better encapsulation within the package.


373-373: LGTM! Method renamed for clarity.

The method has been appropriately renamed from ProvisionCustomerForSubject to Provision for better clarity and brevity while maintaining its exported status.


374-374: LGTM! Method call updated consistently.

The method call has been correctly updated to use the renamed ensureCustomer method.


386-386: LGTM! Method call updated consistently.

The method call has been correctly updated to use the renamed ensureStripeCustomer method.

✨ 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.
    • 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 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.

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

Labels

release-note/misc Miscellaneous changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants