Skip to content

feat: Add allSiteIdsByTier and allByEnrollmentAndTier collection meth…#1569

Open
sandsinh wants to merge 4 commits into
mainfrom
SITES-43769
Open

feat: Add allSiteIdsByTier and allByEnrollmentAndTier collection meth…#1569
sandsinh wants to merge 4 commits into
mainfrom
SITES-43769

Conversation

@sandsinh

@sandsinh sandsinh commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

What

Adds two new collection methods to spacecat-shared-data-access for querying sites by entitlement tier:

  • SiteEnrollmentCollection.allSiteIdsByTier(tier, productCode?) — single JOIN query against site_enrollments/entitlements returning matching siteIds, optionally narrowed to a product code.
  • SiteCollection.allByEnrollmentAndTier(tier, productCode?, options?) — chains through SiteEnrollmentCollection via entityRegistry and batch-fetches full Site objects for those IDs.

Why

Callers need to look up sites by entitlement tier (e.g. PAID, FREE_TRIAL, PLG), optionally scoped to a product code (e.g. LLMO), without fetching every enrollment and filtering in memory.

Testing

Unit and integration tests added for both new methods (site.collection.test.js, site.test.js, site-enrollment.collection.test.js, site-enrollment.test.js).

Related Issues

SITES-43769

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown

This PR will trigger a minor release when merged.

sandsinh and others added 2 commits July 19, 2026 01:13
Monorepo uses npm workspaces with a single root lockfile; this
per-package lockfile was added by mistake.
@sandsinh
sandsinh requested a review from MysticatBot July 18, 2026 20:25

@MysticatBot MysticatBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @sandsinh,

Verdict: Request changes - one deduplication gap to address before merge.
Complexity: MEDIUM - medium diff, new collection methods following existing patterns.
Changes: Adds tier-based site enrollment lookup methods (allSiteIdsByTier and allByEnrollmentAndTier) with full test coverage (8 files).

Must fix before merge

  1. [Important] Duplicate site IDs not deduplicated before batch fetch - packages/spacecat-shared-data-access/src/models/site-enrollment/site-enrollment.collection.js:79 (details inline)
Non-blocking (2): minor issues and suggestions
  • nit: Inconsistent input validation - allSiteIdsByTier uses if (!tier) while allByEnrollmentAndTier uses if (!hasText(tier)). The falsy check accepts whitespace-only strings; consider aligning to hasText for consistency - site-enrollment.collection.js:59
  • nit: TypeScript declarations for allSiteIdsByProductCode and allByEnrollmentProductCode are added to the .d.ts files but their implementations are not visible in this diff. Confirm these are pre-existing methods getting their missing types backfilled alongside the new code - site-enrollment/index.d.ts:9, site/index.d.ts:66

Skill: pr-review | Model: us.anthropic.claude-opus-4-6-v1[1m] | Duration: 2m 41s | Cost: $4.78 | Commit: 71632b2245ae3b16aa6e6f6926c90ebf477b7b20
If this code review was useful, please react with 👍. Otherwise, react with 👎.

this.log.error(`[SiteEnrollmentCollection] Failed to query site_enrollments by tier - ${error.message}`, error);
throw new DataAccessError('Failed to query site_enrollments by tier', { entityName: 'SiteEnrollment', tableName: 'site_enrollments' }, error);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (blocking): allSiteIdsByTier returns raw (data || []).map((row) => row.site_id) without deduplication. If a site is enrolled in multiple entitlements at the same tier (e.g., two PAID entitlements for different product codes when no productCode filter is supplied), the query returns duplicate site_id values. These duplicates flow into allByEnrollmentAndTier and cause redundant batchGetByKeys lookups - wasted I/O on the same primary keys.

The existing allSiteIdsByProductCode sibling has the same pattern but is less likely to produce duplicates in practice (unique on (site_id, entitlement_id) plus a specific product code narrows to one entitlement per site). Tier is a broader classifier and more likely to hit this.

Fix:

return [...new Set((data || []).map((row) => row.site_id))];

@MysticatBot MysticatBot added ai-reviewed Reviewed by AI complexity:medium AI-assessed PR complexity: MEDIUM labels Jul 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-reviewed Reviewed by AI complexity:medium AI-assessed PR complexity: MEDIUM

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants