Skip to content

Conversation

@sweetmantech
Copy link
Collaborator

@sweetmantech sweetmantech commented Nov 14, 2025

Summary by CodeRabbit

  • Refactor
    • Optimized artist data retrieval by consolidating multiple parallel database queries into a single unified operation, improving performance and reducing complexity.
    • Simplified the artist aggregation logic to streamline how enterprise and subscription-linked artists are fetched and deduplicated.

@coderabbitai
Copy link

coderabbitai bot commented Nov 14, 2025

Walkthrough

The changes refactor the artist retrieval logic in the ArtistsPro controller by introducing a new getEnterpriseArtists() utility function that consolidates the fetching of enterprise and subscriber accounts into a single call. A new database query helper selectAccountArtistIds() is added to reduce code duplication. Existing functions are updated to use these new utilities.

Changes

Cohort / File(s) Summary
Controller Update
controllers/ArtistsProController/getArtistsProHandler.ts
Replaced parallel retrieval of enterprise and subscriber emails with a single call to getEnterpriseArtists(). Removed direct usage of getAllEnterpriseAccounts and getSubscriberAccountEmails. Updated JSDoc to reflect that endpoint returns artists from enterprise and active subscription accounts.
New Enterprise Artist Aggregation
lib/enterprise/getEnterpriseArtists.ts
Introduces new utility function that aggregates artists from enterprise and active subscription accounts. Concurrently fetches all enterprise accounts and subscriber account emails, derives unified account IDs, fetches corresponding account-artist associations, and returns deduplicated artist IDs.
Database Query Helpers
lib/supabase/account_artist_ids/selectAccountArtistIds.ts, lib/supabase/getArtistEmails.ts
Adds selectAccountArtistIds() helper for querying account_artist_ids table with optional artist_id and account_ids[] filters. getArtistEmails() refactored to use the new helper instead of inline database query. Adjusted error handling and added defensive type narrowing for account IDs.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant H as getArtistsProHandler
    participant E as getEnterpriseArtists
    participant EA as getAllEnterpriseAccounts
    participant SA as getSubscriberAccountEmails
    participant AAI as selectAccountArtistIds
    participant AE as getArtistEmails
    
    rect rgb(200, 220, 255)
    Note over H,AE: New Consolidated Flow
    H->>E: getEnterpriseArtists()
    par Parallel Fetch
        E->>EA: getAllEnterpriseAccounts()
        E->>SA: getSubscriberAccountEmails()
    and
        EA-->>E: enterprise account IDs
        SA-->>E: subscriber account emails
    end
    E->>AAI: selectAccountArtistIds({account_ids})
    AAI-->>E: artist IDs for accounts
    E-->>H: deduplicated artist list
    H->>AE: getArtistEmails(artist_ids)
    AE->>AAI: selectAccountArtistIds({artist_id})
    AAI-->>AE: account IDs for artist
    AE-->>H: artist email data
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Areas requiring attention:
    • Logic in getEnterpriseArtists() that aggregates and deduplicates artist IDs from two separate data sources
    • Error handling changes in getArtistEmails(), particularly the "No accounts found for artist" message and removal of prior error path
    • Type safety of new SelectAccountArtistIdsParams and proper filter application in selectAccountArtistIds()
    • Verification that consolidation in getArtistsProHandler doesn't alter existing response behavior

Possibly related PRs

  • PR #172: Adds mock for getArtistsProHandler handler; this PR refactors the same handler's data-fetch logic to call the new getEnterpriseArtists() utility.
  • PR #174: Introduced parallel enterprise and subscriber account fetching with getAllEnterpriseAccounts and getSubscriberAccountEmails functions; this PR consolidates that logic into a single getEnterpriseArtists() call.

Poem

🐰 Two paths now one, the artists aligned,
Duplication solved, a cleaner design,
From enterprise halls to subscriber's delight,
One helper to fetch them all—sleek and right!
Refactored with care, the data flows true,
Hoppy hops for this code overdue!

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: refactoring the getArtistsProHandler to use the new getEnterpriseArtists library function instead of directly calling multiple database functions.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sweetmantech/myc-3461-api-apiartistspro-call-existing-libs-to-get-all-artist_ids

📜 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 a1d977c and deb4d7e.

📒 Files selected for processing (4)
  • controllers/ArtistsProController/getArtistsProHandler.ts (1 hunks)
  • lib/enterprise/getEnterpriseArtists.ts (1 hunks)
  • lib/supabase/account_artist_ids/selectAccountArtistIds.ts (1 hunks)
  • lib/supabase/getArtistEmails.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (4)
lib/enterprise/getEnterpriseArtists.ts (3)
lib/enterprise/getAllEnterpriseAccounts.ts (1)
  • getAllEnterpriseAccounts (11-21)
lib/stripe/getSubscriberAccountEmails.ts (1)
  • getSubscriberAccountEmails (13-31)
lib/supabase/account_artist_ids/selectAccountArtistIds.ts (1)
  • selectAccountArtistIds (14-35)
lib/supabase/account_artist_ids/selectAccountArtistIds.ts (1)
types/database.types.ts (1)
  • Tables (3609-3636)
lib/supabase/getArtistEmails.ts (1)
lib/supabase/account_artist_ids/selectAccountArtistIds.ts (1)
  • selectAccountArtistIds (14-35)
controllers/ArtistsProController/getArtistsProHandler.ts (1)
lib/enterprise/getEnterpriseArtists.ts (1)
  • getEnterpriseArtists (9-38)
🔇 Additional comments (12)
lib/enterprise/getEnterpriseArtists.ts (3)

1-3: LGTM! Clean imports.

The imports are well-organized and correctly reference the utility functions used in this module.


5-8: Clear documentation.

The JSDoc accurately describes the function's purpose and return type.


9-38: Well-structured consolidation of artist retrieval logic.

The function correctly:

  • Fetches enterprise and subscription accounts in parallel for efficiency
  • Deduplicates account IDs using Set
  • Returns early when no accounts are found
  • Applies type-safe filtering to ensure only valid string IDs are processed

Errors from the underlying functions propagate to the caller, which is appropriate given the handler-level error handling in getArtistsProHandler.ts.

controllers/ArtistsProController/getArtistsProHandler.ts (2)

2-2: Good refactor: simplified imports and updated documentation.

The import of the new getEnterpriseArtists() utility and the updated JSDoc clearly communicate the handler's purpose.

Also applies to: 6-6


8-27: Excellent simplification of handler logic.

Replacing the previous parallel fetching and concatenation logic with a single call to getEnterpriseArtists() makes this handler much cleaner and easier to maintain. The error handling and response structure remain appropriate.

lib/supabase/getArtistEmails.ts (3)

2-2: LGTM! Proper import of the new helper.

The import statement correctly references the newly created selectAccountArtistIds utility.


18-27: Good refactor: replaced inline query with shared helper.

Using selectAccountArtistIds() eliminates code duplication and centralizes the query logic. The early return when no accounts are found is appropriate, and errors from the helper are properly caught by the outer try-catch block.


29-31: Robust type-safe extraction of account IDs.

The map-filter chain with a type predicate correctly extracts account_id values while filtering out any null or undefined values, ensuring accountIds is typed as string[].

lib/supabase/account_artist_ids/selectAccountArtistIds.ts (4)

1-2: LGTM! Correct imports.

The imports are appropriate for a Supabase query helper function.


4-4: Well-defined types for flexible querying.

The type definitions correctly leverage the generated database types and provide a flexible parameter interface that supports optional filtering by artist_id, account_ids, or both.

Also applies to: 6-9


14-26: Flexible and correct query building.

The conditional filter application allows this function to handle various query scenarios:

  • Filter by artist_id only
  • Filter by account_ids only
  • Filter by both
  • No filters (return all)

The use of .eq() for single values and .in() for arrays is appropriate.


28-35: Appropriate error handling for a utility function.

The function correctly:

  • Executes the dynamically built query
  • Throws descriptive errors that include the original error message for debugging
  • Returns an empty array as a safe fallback when no data is found

This error handling pattern is well-suited for a shared utility function, as it allows callers to handle errors according to their specific needs.


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 and usage tips.

@sweetmantech sweetmantech merged commit edd6404 into main Nov 14, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants