Implement NotificationPreferencesRepository
epic-push-notification-delivery-foundation-task-004 — Build the NotificationPreferencesRepository Dart class with methods: getPreferences(userId), updatePreference(userId, category, enabled), initializeDefaultPreferences(userId, orgId), and a stream for real-time preference changes via Supabase Realtime. Default all categories to enabled on first login. Use Riverpod for provider exposure.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Place the repository in lib/features/notifications/data/notification_preferences_repository.dart following the project's feature-based directory structure. Define NotificationPreference as a freezed data class for immutability and copyWith support. The Realtime stream should use supabase.from('notification_preferences').stream(primaryKey: ['id']).eq('user_id', userId) which returns a Stream>> — map each event to a full getPreferences() call or parse inline. Prefer the .stream() approach over .on() for type safety.
For the Riverpod provider, use a family provider pattern: final notificationPreferencesProvider = StreamProvider.family, String>((ref, userId) => ref.watch(notificationPreferencesRepositoryProvider).watchPreferences(userId)). Dispose the Realtime channel in the ref.onDispose callback to prevent socket leaks.
Testing Requirements
Unit tests using flutter_test with a mocked SupabaseClient (use mockito or mocktail). Test cases: (1) getPreferences returns parsed List
iOS only allows one system permission prompt per app install. If the rationale dialog timing or content is wrong the user may permanently deny permissions during onboarding, permanently blocking push delivery for that device with no recovery path short of manual system settings navigation.
Mitigation & Contingency
Mitigation: Design and user-test the rationale dialog content and trigger point (after onboarding value-demonstration step, not at first launch). Implement the settings-deep-link fallback in NotificationPermissionManager so the permission state screen always offers a path to system settings if denied.
Contingency: If denial rates are high in TestFlight testing, revise the rationale copy and trigger timing before production release. Ensure the in-app notification centre provides full value without push so denied users are not blocked from the feature.
FCM token rotation callbacks can fire at any time, including during app termination or network outage. If the token rotation is not persisted reliably the backend trigger service will dispatch to a stale token, resulting in silent notification failures that are hard to diagnose.
Mitigation & Contingency
Mitigation: Persist token rotation updates with a local queue that retries on next app foreground if network is unavailable. Use Supabase upsert by (user_id, device_id) to prevent duplicate token rows and ensure the latest token always wins.
Contingency: If token staleness is observed in production, add a token validity check on each app foreground and force a re-registration if the stored token does not match the FCM-reported current token.
Incorrect RLS policies on notification_preferences or fcm_tokens could expose one user's preferences or device tokens to another user, or could block the backend Edge Function service role from reading token lists needed for dispatch, silently dropping all notifications.
Mitigation & Contingency
Mitigation: Write explicit RLS policy tests using the Supabase test harness covering user-scoped read/write, service-role read for dispatch, and cross-user access denial. Review policies during code review with a security checklist.
Contingency: Maintain a rollback migration that reverts the RLS changes, and add an integration test in CI that asserts the service role can query all tokens and that a normal user JWT cannot access another user's token rows.