Implement Notification Payload Builder for Summaries
epic-periodic-summaries-core-logic-task-012 — Extend the push notification dispatcher with summary-specific payload construction logic. Must build personalised notification bodies referencing the peer mentor's name, the period label, key metric highlights, and deep-link URIs routing directly to the periodic summary card within the app.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 3 - 413 tasks
Can start after Tier 2 completes
Implementation Notes
Model the payload builder as a pure function `buildSummaryNotificationPayload(mentor: PeerMentor, summary: SummaryData, orgLabels: OrganisationLabels): NotificationPayload | null` — pure functions are easy to unit test. Define the deep-link URI scheme in the Flutter app's `AndroidManifest.xml` and `Info.plist` using the `app_links` or `go_router` deep-link configuration — the scheme `app://summary/{period_key}?org={org_id}` is a good starting point. Extract the two most impactful metric highlights using a priority list: (1) total activity count vs period average, (2) outlier classification if present, (3) year-over-year delta. Keep notification bodies under 100 characters for APNs visibility on lock screen.
The organisation labels system already exists in the app architecture — query it once per scheduler run and pass the resolved labels map into the payload builder to avoid per-mentor DB reads. For Norwegian: use `l10n` ARB files with `nb` locale as the default.
Testing Requirements
Unit tests: test payload construction with a mock peer mentor and summary object — verify title personalisation, metric highlight extraction, deep-link URI format, and language string resolution. Test mentor with notifications disabled produces no payload. Test mentor with missing push token logs warning and returns null. Test organisation label substitution replaces 'peer mentor' with org-specific term.
Integration test: send a test notification to a TestFlight device and verify it appears with correct title, body, and that tapping it deep-links to the correct summary card screen in the Flutter app. Test Norwegian Bokmål strings for correctness with a native speaker review checkpoint.
Supabase pg_cron or Edge Function retries could trigger multiple concurrent generation runs for the same period and organisation, producing duplicate summaries and sending multiple push notifications to users — a serious UX regression.
Mitigation & Contingency
Mitigation: Implement a database-level run-lock using an INSERT … ON CONFLICT DO NOTHING pattern keyed on (organisation_id, period_type, period_start). Only the first successful insert proceeds; subsequent attempts read the existing lock and exit early. Test with concurrent invocations in a Deno test suite.
Contingency: If duplicate summaries are detected post-deployment, add a deduplication cleanup job that removes all but the most recent summary per (user_id, period_type, period_start) and sends a corrective push notification.
FCM and APNs have different payload structures and size limits. An oversized or malformed payload could cause silent notification drops on iOS or delivery failures on Android, meaning mentors never learn their summary is ready.
Mitigation & Contingency
Mitigation: Build the PushNotificationDispatcher with separate FCM and APNs payload constructors, enforce a 256-byte body limit on the preview text, and run integration tests against the Firebase Emulator and a test APNs sandbox.
Contingency: Fall back to a generic 'Your periodic summary is ready' message if personalised preview text construction fails, ensuring delivery even when the personalisation pipeline encounters an error.
Outlier thresholds that are too tight will flag most mentors as outliers (alert fatigue for coordinators), while thresholds that are too loose will miss genuinely underactive mentors — directly undermining HLF's follow-up goal.
Mitigation & Contingency
Mitigation: Implement thresholds as configurable per-organisation database settings rather than hardcoded constants. Provide sensible defaults (underactive < 2 sessions/period, overloaded > 20 sessions/period) and document the tuning process for coordinators in the admin portal.
Contingency: If coordinators report threshold miscalibration after launch, expose a threshold configuration UI in the coordinator admin screen and allow real-time threshold adjustment without requiring a code deployment.
The app may not have 12 months of historical activity data for all organisations at launch, making year-over-year comparison impossible for most users and rendering the comparison widget empty, which could disappoint users expecting Wrapped-style insights.
Mitigation & Contingency
Mitigation: Design the generation service to gracefully handle missing prior-year data by setting the yoy_delta field to null rather than zero. The UI must treat null as 'no comparison available' with appropriate placeholder copy rather than showing a misleading 0% delta.
Contingency: If historical data import from legacy Excel/Word sources becomes feasible, add a one-time backfill Edge Function that populates prior-year activity records from imported spreadsheets. Until then, explicitly communicate the data-availability limitation in the first summary each user receives.