Skip to content

Rename LectureTags → ViewerRules and make audience reusable across collections #293

Closed

Description

@Ardnived

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 (not viewerRule / 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:create generates by default — data loss on existing lecture-tags, lectures.tags, lecture-clips.tags, and app-cards.rules is acceptable. Editors will recreate ViewerRules and reassign audiences after the change lands.
  • Admin group: keep ViewerRules under Metadata.

Related Work

Current Architecture

  • src/collections/tags/LectureTags.ts — slug lecture-tags, label + rulesField({ pathProgress, totalMeditationsViewed, totalLecturesViewed }) + joins.
  • src/collections/content/AppCards.ts (lines 169-177) — inline rulesField({ 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 + virtual isEligibleForViewer checkbox reading req.context.viewerData (VIEWER_DATA_CONTEXT_KEY).
  • src/endpoints/appCardsForViewer.ts — filters by card-inline isEligibleForViewer.
  • src/endpoints/lecturesForViewer.ts — evaluates tags, filters lectures/clips where all tags pass.
  • src/lib/access/config/projects.ts (lines 48-70) — lecture-tags and app-cards are both in wemeditate-app only.
  • 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

  1. viewer-rules (renamed from lecture-tags) — reusable audience/visibility rules. Fields:
    • label (text, required).
    • rulesField({ pathProgress, meditationsPerWeek, totalMeditationsViewed, totalLecturesViewed }) — union minus hasRealization.
    • Joins: lectures, lectureClips, appCards (all keyed on the new audience field).
  2. AppCards — delete inline rulesField(...); add audience: relationship → viewer-rules, hasMany: false.
  3. Lectures / LectureClips — change tags (hasMany → lecture-tags) to audience (single → viewer-rules).
  4. Drop hasRealization — from rule definitions, endpoint query schemas, and tests. Use pathProgress >= 1.
  5. No-audience = hidden (consistent across AppCards, Lectures, LectureClips).
  6. Project visibility: swap 'lecture-tags' → 'viewer-rules' in the wemeditate-app project.

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 label field.
    • rulesField config:
      rules: [
        { name: 'pathProgress', type: 'range' },
        { name: 'meditationsPerWeek', type: 'range' },
        { name: 'totalMeditationsViewed', type: 'range' },
        { name: 'totalLecturesViewed', type: 'range' },
      ]
    • Join fields: lectures (on audience), lectureClips (on audience), appCards (on audience).
  • Update barrel exports: src/collections/tags/index.ts, src/collections/index.ts.
  • Register in src/payload.config.ts (replace LectureTags import).

2. Update consuming collections

  • AppCards (src/collections/content/AppCards.ts): delete rulesField(...) 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 above weight.
  • Lectures (src/collections/resources/Lectures.ts, lines 76-84): rename tags → 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 hasRealization from querySchema.
    • Keep depth: 1 to populate audience; the populated object carries the virtual isEligibleForViewer flag because viewer data is threaded via req.context.
    • Change the filter from card.isEligibleForViewer === true to card.audience && typeof card.audience === 'object' && card.audience.isEligibleForViewer === true (null audience = hidden).
  • 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 audience is in set." Items with audience: null are excluded.
    • Drop the allTagsEligible helper; replace with a single-id check.
    • Rename tags field references to audience.

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.md rules 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:types after schema changes.
  • Update .claude/docs/architecture.md — "Tag Collections" entry for LectureTags; AppCards entry (remove rulesField mention, add audience).
  • Update .claude/docs/components/project-visibility.md — collection visibility matrix row lecture-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 hasRealization coverage.
    • Replace inline rules: {...} setup with createViewerRule(...) + audience: rule.id on the card.
    • Add: card with audience: null is hidden (behavior change).
    • Add: card with audience pointing to an ineligible rule is hidden.
    • Keep: card with audience pointing to an always-match rule (empty rules) is returned.
  • 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: null is excluded.
  • Update tests/utils/testData.ts factories: rename, replace hasRealization usages with pathProgress.

Critical Files

  • src/collections/tags/LectureTags.ts (rename target)
  • src/collections/content/AppCards.ts
  • src/collections/resources/Lectures.ts
  • src/collections/resources/LectureClips.ts
  • src/endpoints/appCardsForViewer.ts
  • src/endpoints/lecturesForViewer.ts
  • src/lib/access/config/projects.ts
  • src/payload.config.ts
  • New migration file under src/migrations/
  • Test files and tests/utils/testData.ts

Verification

  1. pnpm generate:types → no errors.
  2. pnpm lint → no errors.
  3. pnpm build → success.
  4. 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 >= 1 on a ViewerRule behaves as hasRealization used to.
  5. 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=10 excludes it and ?pathProgress=3&... includes it.
    • Same for /api/lectures/for-viewer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Hold Until

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions