Repository navigation
Description
Context
LectureTags and the inline rulesField on AppCards both implement viewer-targeting today, but as two disconnected systems. Each collection that wants visibility rules inlines its own rulesField() call, with drift in available rule keys — AppCards has hasRealization + meditationsPerWeek; LectureTags has neither. This refactor pulls rule configuration into a single first-class collection (ViewerRules) that other collections reference via relationship, making rules reusable and editable independently.
Second goal: drop hasRealization. It's redundant with pathProgress >= 1 and forces rulesField to carry both a boolean and a range type to express "has the user started the path."
Resolved Decisions
- Field name on consuming collections:
audience(notviewerRule/visibility). - No-audience behavior:
audience === null⇒ always hidden on both AppCards and Lectures/LectureClips. This is a behavior change for AppCards (previously no-rules = always visible). Editors will manually author a catch-all ViewerRule with no conditions to restore always-visible semantics for cards that don't need targeting; we do not seed that rule as part of this issue. - Data migration: none. Use whatever migration
pnpm db:migrations:creategenerates by default — data loss on existinglecture-tags,lectures.tags,lecture-clips.tags, andapp-cards.rulesis acceptable. Editors will recreate ViewerRules and reassign audiences after the change lands. - Admin group: keep ViewerRules under
Metadata.
Related Work
- PR feat(lectures): split Lectures into Lectures + LectureClips #292 — split Lectures into Lectures + LectureClips; introduced
lecture-clipswithhasManyrelationship tolecture-tags. - PR feat: add LectureTags collection with targeting rules #267 — introduced
lecture-tagsand therulesField/isEligibleForViewerpattern. - PR Dynamic cards - overlay checkbox #238 / feat(app-cards): add overlay checkbox (#238) #284 — added inline
rulesFieldon AppCards plusappCardsForViewer+ weighted sampling.
Current Architecture
src/collections/tags/LectureTags.ts— sluglecture-tags,label+rulesField({ pathProgress, totalMeditationsViewed, totalLecturesViewed })+ joins.src/collections/content/AppCards.ts(lines 169-177) — inlinerulesField({ hasRealization, pathProgress, meditationsPerWeek, totalMeditationsViewed, totalLecturesViewed }).src/collections/resources/Lectures.ts(lines 76-84) —tags: relationship → lecture-tags, hasMany: true.src/collections/resources/LectureClips.ts(lines 94-102) — same as Lectures.src/fields/rulesField.ts— JSON field + virtualisEligibleForViewercheckbox readingreq.context.viewerData(VIEWER_DATA_CONTEXT_KEY).src/endpoints/appCardsForViewer.ts— filters by card-inlineisEligibleForViewer.src/endpoints/lecturesForViewer.ts— evaluates tags, filters lectures/clips where all tags pass.src/lib/access/config/projects.ts(lines 48-70) —lecture-tagsandapp-cardsare both inwemeditate-apponly.- Tests:
tests/int/lecture-tags.int.spec.ts,tests/int/app-cards-for-viewer.int.spec.ts,tests/int/lectures-for-viewer.int.spec.ts.
Target Architecture
viewer-rules(renamed fromlecture-tags) — reusable audience/visibility rules. Fields:label(text, required).rulesField({ pathProgress, meditationsPerWeek, totalMeditationsViewed, totalLecturesViewed })— union minushasRealization.- Joins:
lectures,lectureClips,appCards(all keyed on the newaudiencefield).
- AppCards — delete inline
rulesField(...); addaudience: relationship → viewer-rules, hasMany: false. - Lectures / LectureClips — change
tags(hasMany →lecture-tags) toaudience(single →viewer-rules). - Drop
hasRealization— from rule definitions, endpoint query schemas, and tests. UsepathProgress >= 1. - No-audience = hidden (consistent across AppCards, Lectures, LectureClips).
- Project visibility: swap
'lecture-tags'→'viewer-rules'in thewemeditate-appproject.
Implementation Plan
1. Rename the collection and adjust fields
- Rename
src/collections/tags/LectureTags.ts→src/collections/tags/ViewerRules.ts.slug: 'viewer-rules',labels: { singular: 'Viewer Rule', plural: 'Viewer Rules' }.admin.group: 'Metadata'(unchanged).- Keep
labelfield. rulesFieldconfig:rules: [ { name: 'pathProgress', type: 'range' }, { name: 'meditationsPerWeek', type: 'range' }, { name: 'totalMeditationsViewed', type: 'range' }, { name: 'totalLecturesViewed', type: 'range' }, ]
- Join fields:
lectures(onaudience),lectureClips(onaudience),appCards(onaudience).
- Update barrel exports:
src/collections/tags/index.ts,src/collections/index.ts. - Register in
src/payload.config.ts(replaceLectureTagsimport).
2. Update consuming collections
- AppCards (
src/collections/content/AppCards.ts): deleterulesField(...)spread at lines 169-177. Add{ name: 'audience', type: 'relationship', relationTo: 'viewer-rules', hasMany: false, admin: { description: 'Controls which viewers see this card. If empty, the card is hidden.' } }in the Rules tab aboveweight. - Lectures (
src/collections/resources/Lectures.ts, lines 76-84): renametags→audience,relationTo: 'viewer-rules',hasMany: false, update admin description. - LectureClips (
src/collections/resources/LectureClips.ts, lines 94-102): same as Lectures.
3. Update endpoints
appCardsForViewer(src/endpoints/appCardsForViewer.ts):- Remove
hasRealizationfromquerySchema. - Keep
depth: 1to populateaudience; the populated object carries the virtualisEligibleForViewerflag because viewer data is threaded viareq.context. - Change the filter from
card.isEligibleForViewer === truetocard.audience && typeof card.audience === 'object' && card.audience.isEligibleForViewer === true(null audience = hidden).
- Remove
lecturesForViewer(src/endpoints/lecturesForViewer.ts):- Step 1: change
collection: 'lecture-tags'→'viewer-rules'; rename variables (tagDocs→ruleDocs,eligibleSet→eligibleRuleIds). - Steps 2-3: replace "all tags in set" with "single
audienceis in set." Items withaudience: nullare excluded. - Drop the
allTagsEligiblehelper; replace with a single-id check. - Rename
tagsfield references toaudience.
- Step 1: change
4. Drop hasRealization
Covered in steps 1 and 3. Additionally:
- Grep the codebase for
hasRealization— remove from any seed scripts, test factories, or type consumers. - Update
.claude/docs/architecture.mdrules listing under the AppCards description.
5. Project visibility
src/lib/access/config/projects.ts — wemeditate-app.collections: replace 'lecture-tags' with 'viewer-rules'.
6. Database migration
Run pnpm db:migrations:create after the schema changes in steps 1-5 are in place, accept whatever it generates, and commit both the .ts migration and .json snapshot. No manual migration logic — data loss on the affected tables is acceptable per the decisions above.
7. Type regeneration and docs
pnpm generate:typesafter schema changes.- Update
.claude/docs/architecture.md— "Tag Collections" entry for LectureTags; AppCards entry (removerulesFieldmention, addaudience). - Update
.claude/docs/components/project-visibility.md— collection visibility matrix rowlecture-tags→viewer-rules.
8. Tests
- Rename
tests/int/lecture-tags.int.spec.ts→viewer-rules.int.spec.ts: update slug, factory (createLectureTag→createViewerRule), assertions. tests/int/app-cards-for-viewer.int.spec.ts:- Remove all
hasRealizationcoverage. - Replace inline
rules: {...}setup withcreateViewerRule(...)+audience: rule.idon the card. - Add: card with
audience: nullis hidden (behavior change). - Add: card with
audiencepointing to an ineligible rule is hidden. - Keep: card with
audiencepointing to an always-match rule (empty rules) is returned.
- Remove all
tests/int/lectures-for-viewer.int.spec.ts:- Replace tag setup with ViewerRule docs.
- Collapse "all tags eligible" assertions into "audience eligible."
- Add: lecture/clip with
audience: nullis excluded.
- Update
tests/utils/testData.tsfactories: rename, replacehasRealizationusages withpathProgress.
Critical Files
src/collections/tags/LectureTags.ts(rename target)src/collections/content/AppCards.tssrc/collections/resources/Lectures.tssrc/collections/resources/LectureClips.tssrc/endpoints/appCardsForViewer.tssrc/endpoints/lecturesForViewer.tssrc/lib/access/config/projects.tssrc/payload.config.ts- New migration file under
src/migrations/ - Test files and
tests/utils/testData.ts
Verification
pnpm generate:types→ no errors.pnpm lint→ no errors.pnpm build→ success.pnpm test:int— all green, new tests cover:- AppCard with
audience: null→ excluded. - AppCard with eligible
audience→ returned. - AppCard with ineligible
audience→ excluded. - Lecture / LectureClip with
audience: null→ excluded. pathProgress >= 1on a ViewerRule behaves ashasRealizationused to.
- AppCard with
- Manual admin-UI smoke test:
- Create a ViewerRule with
pathProgress { min: 1 }, attach to an AppCard; confirm/api/app-cards/for-viewer?pathProgress=0&targetSection=hero&limit=10excludes it and?pathProgress=3&...includes it. - Same for
/api/lectures/for-viewer.
- Create a ViewerRule with
Metadata
Metadata
Assignees
Labels
Type
Fields
Hold Until
Activity
refactor(viewer-rules): rename LectureTags, unify audience across col…
feat(atlas): groom legacy event listings and group shared venues befo…
refactor(viewer-rules): rename LectureTags → ViewerRules, unify audience