Skip to content

reports: consolidate user demographics handling into appendUserDemographics - #10399

Draft
Mutugiii wants to merge 1 commit into
masterfrom
claude/report-export-demographics-6t266x
Draft

reports: consolidate user demographics handling into appendUserDemographics#10399
Mutugiii wants to merge 1 commit into
masterfrom
claude/report-export-demographics-6t266x

Conversation

@Mutugiii

@Mutugiii Mutugiii commented Sep 4, 2026

Copy link
Copy Markdown
Member

Summary

Refactors user demographics handling in the reports service by consolidating appendAge and appendGender into a single appendUserDemographics method. Extracts gender localization logic into a reusable utility function and improves age calculation to handle edge cases.

Key Changes

  • Consolidated demographics method: Merged appendAge() and appendGender() into appendUserDemographics() to reduce duplication and provide a single entry point for adding user age and gender to activity records
  • Extracted gender localization: Created localizedGender() utility in reports.utils.ts and reused it in submissions.service.ts to eliminate duplicate gender formatting logic
  • Improved user lookup: Added userOfActivity() helper method that correctly handles nested user documents (from child planets stored in doc property)
  • Enhanced age calculation:
    • Updated ageFromBirthDate() to accept Date objects in addition to timestamps
    • Added null/invalid date handling, returning null instead of NaN
    • Fixed date comparison logic to use getDate() instead of getDay() for accurate birthday calculations
    • Records with their own demographics (e.g., health examinations) now preserve their values instead of being overwritten
  • Updated call sites: Modified reports-detail.component.ts to use the new consolidated method and export gender/age columns for login and AI chat activities
  • Added comprehensive tests: Created reports.service.spec.ts with test coverage for the new appendUserDemographics method and added tests for ageFromBirthDate edge cases in utils.spec.ts

Implementation Details

  • The appendUserDemographics method uses nullish coalescing (??) to preserve existing demographics on records while filling in missing values from user profiles
  • Gender values are normalized to proper case via the new localizedGender() utility, which handles 'male'/'female' with localization and falls back to proper-cased input for other values
  • Age calculation now correctly distinguishes between "birthday hasn't occurred yet this year" (same month but later date) and "birthday already passed"

https://claude.ai/code/session_012RPeuWgSojRNqiUXXz4mxB

Decision makers need the age and gender of members alongside the activity
rows they export, so every per-member CSV in the manager reports now carries
both columns:

- Member Visits, Resource Views, Course Views and Courses Progress look the
  demographics up from the member's profile.
- Chat Usage gains Gender and Age (years) columns.
- Community Health keeps the age and gender stored on each anonymous
  examination instead of overwriting them, which previously exported the age
  of every health record as NaN.

Along the way:

- appendAge is replaced by appendUserDemographics, which matches members of
  child planets too (their docs are nested in a doc property, so the old
  lookup by name silently missed them and left the gender columns of the
  nation summary export empty).
- ageFromBirthDate returns null rather than NaN when there is no usable birth
  date, and compares the day of the month rather than the day of the week, so
  a birthday later in the current month no longer counts as reached.
- The gender labels of the survey export move to reports.utils so both
  exports share one source of truth.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012RPeuWgSojRNqiUXXz4mxB
@github-actions github-actions Bot added the large 101-200 reviewable lines label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

large 101-200 reviewable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants