Skip to content

refactor(viewer-rules): rename LectureTags, unify audience across collections - #296

Merged
Ardnived merged 2 commits into
mainfrom
293-viewer-rules-refactor
Apr 21, 2026
Merged

Ardnived merged 2 commits into
mainfrom
293-viewer-rules-refactor

Conversation

@Ardnived

Copy link
Copy Markdown
Contributor

Summary

  • Rename the lecture-tags collection to viewer-rules (admin group: Metadata). Drops the redundant hasRealization boolean rule — pathProgress >= 1 expresses the same condition.
  • AppCards, Lectures, and LectureClips each gain a single audience relationship to viewer-rules (nullable, hasMany: false), replacing the inline rulesField on AppCards and the tags hasMany on Lectures/LectureClips.
  • Behavior change: audience: null now hides the item from /api/app-cards/for-viewer and /api/lectures/for-viewer on all three consumers. For AppCards this is new — previously no-rules meant always-visible. Editors will recreate a catch-all ViewerRule with no conditions to restore that semantic.
  • Endpoints: appCardsForViewer reads isEligibleForViewer off the populated audience; lecturesForViewer filters by where[audience][in] against the eligible viewer-rule set.
  • ContentIndexBlock's lecture/lecture-clip filters switch from lecture-tags to viewer-rules with where[audience][in].

Migration

Standard generated migration, plus a manual fix for the known Drizzle polymorphic-FK rename bug documented in CLAUDE.md — viewer_rules_id was dropped from both sides of the payload_locked_documents_rels INSERT/SELECT since the old table only had lecture_tags_id. No data preservation per issue decisions; editors recreate audiences after the cutover.

Test Results

  • Integration tests: 529 passed, 0 skipped
  • Unit tests: 221 passed, 0 skipped
  • Build: ✓ Success
  • Lint: ✓ Clean
  • Type check: ✓ Clean
  • Pre-existing failures fixed: none

Test plan

  • Apply the migration locally (pnpm payload migrate) and confirm viewer_rules table exists and lecture_tags / lectures_rels / lecture_clips_rels are dropped
  • In admin UI, create a ViewerRule with pathProgress { min: 1 }, attach to an AppCard + Lecture; confirm /api/app-cards/for-viewer?pathProgress=0&targetSection=hero&limit=10 excludes it and ?pathProgress=3&... includes it
  • Confirm /api/lectures/for-viewer?pathProgress=3&limit=10 returns the lecture when its audience is eligible
  • Confirm a card / lecture with audience: null is excluded from the viewer endpoint responses
  • Confirm ViewerRules appears in the sidebar only when the wemeditate-app project is selected (under Metadata)

Closes #293

🤖 Generated with Claude Code

Ardnived and others added 2 commits April 21, 2026 21:00
Unifies viewer-targeting under a single reusable `viewer-rules` collection
that AppCards, Lectures, and LectureClips reference via a common `audience`
field. Removes the inline rulesField from AppCards and the hasMany `tags`
from Lectures/LectureClips.

- Collection: `lecture-tags` → `viewer-rules` (admin group: Metadata).
  Dropped `hasRealization` boolean rule — redundant with `pathProgress >= 1`.
- Fields: AppCards.rules removed; AppCards/Lectures/LectureClips each gain a
  single `audience` relationship (nullable, hasMany: false). `audience: null`
  now hides the item from /api/*/for-viewer (behavior change for AppCards).
- Endpoints: appCardsForViewer reads the virtual `isEligibleForViewer` off
  the populated audience; lecturesForViewer filters by `where[audience][in]`
  against the eligible viewer-rule set.
- ContentIndexBlock lectureFilters/lectureClipFilters switched from
  `lecture-tags` to `viewer-rules` with `where[audience][in]` query.
- Migration rebuilds payload_locked_documents_rels; worked around the known
  Drizzle polymorphic-FK rename bug (CLAUDE.md) by dropping the renamed
  column from both sides of the INSERT/SELECT. No data preservation — per
  issue #293 decisions, editors recreate audiences after the cutover.

Closes #293

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…points

Apply PR #296 review follow-ups.

- Extract VIEWER_RULE_DEFINITIONS from ViewerRules as the single source of
  truth for targeting dimensions; both for-viewer endpoints now derive
  their query schemas from it via the new buildViewerDataShape helper,
  which pulls in meditationsPerWeek on the lectures endpoint (previously
  missing) automatically.
- Add withViewerContext helper in rulesField.ts for the req.context
  threading both endpoints duplicated.
- Drop the redundant JS isAudienceEligible post-filter in lecturesForViewer
  (the DB query already filters by audience now that it's a single
  relationship, no longer hasMany tags), and replace the local idOf helper
  with extractID from payload/shared.
- Add filterOptions: () => true and align admin description wording on the
  audience relationship fields in AppCards, Lectures, and LectureClips.
- Tighten join-field docs type casts in viewer-rules tests to { id: number }
  (tests always populate at depth:1) and extend the "passes viewerData"
  lectures-for-viewer case to cover all four rule dimensions.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@Ardnived
Ardnived merged commit 8b6efb7 into main Apr 21, 2026
Ardnived added a commit that referenced this pull request Nov 5, 2026
…points

Apply PR #296 review follow-ups.

- Extract VIEWER_RULE_DEFINITIONS from ViewerRules as the single source of
  truth for targeting dimensions; both for-viewer endpoints now derive
  their query schemas from it via the new buildViewerDataShape helper,
  which pulls in meditationsPerWeek on the lectures endpoint (previously
  missing) automatically.
- Add withViewerContext helper in rulesField.ts for the req.context
  threading both endpoints duplicated.
- Drop the redundant JS isAudienceEligible post-filter in lecturesForViewer
  (the DB query already filters by audience now that it's a single
  relationship, no longer hasMany tags), and replace the local idOf helper
  with extractID from payload/shared.
- Add filterOptions: () => true and align admin description wording on the
  audience relationship fields in AppCards, Lectures, and LectureClips.
- Tighten join-field docs type casts in viewer-rules tests to { id: number }
  (tests always populate at depth:1) and extend the "passes viewerData"
  lectures-for-viewer case to cover all four rule dimensions.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
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.

Rename LectureTags → ViewerRules and make audience reusable across collections

1 participant