Implement cache invalidation and staleness detection
epic-contact-search-data-layer-task-007 — Add cache invalidation logic to the ContactCacheSyncRepository: track last_synced_at in a preferences store (shared_preferences or Drift settings table), expose isCacheStale(maxAgeMinutes) helper, and provide a clearCache() method. Define a configurable staleness threshold (default 30 minutes) so the service layer can decide when to trigger a background refresh.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 4 - 323 tasks
Can start after Tier 3 completes
Implementation Notes
Compute staleness using `DateTime.now().toUtc().difference(lastSyncedAt).inMinutes > maxAgeMinutes`. Store last_synced_at as a Unix timestamp (milliseconds since epoch) in SharedPreferences for reliable cross-timezone comparison. Namespace the key as `contact_cache_last_synced_
Expose the staleness threshold as an optional named parameter with a default of 30 rather than a constructor constant — this makes it trivial for callers to override without subclassing. Document the logout integration requirement in a code comment so future developers do not accidentally remove the clearCache() call from the auth flow.
Testing Requirements
Unit tests using an in-memory Drift database and a mocked SharedPreferences instance. Cover: (1) isCacheStale returns true when no last_synced_at is stored, (2) isCacheStale returns true when stored timestamp is older than maxAgeMinutes, (3) isCacheStale returns false when stored timestamp is within maxAgeMinutes, (4) isCacheStale respects custom maxAgeMinutes parameter, (5) clearCache deletes all rows from contacts and notes tables, (6) clearCache resets last_synced_at so subsequent isCacheStale call returns true, (7) keys are correctly namespaced per organisationId — clearing org A cache does not affect org B timestamp.
Supabase RLS policies may not correctly scope ilike search results to the authenticated user's organisation and chapter, causing data leakage across organisations or empty result sets for valid queries.
Mitigation & Contingency
Mitigation: Reuse and extend existing RLS query builder patterns from the contact-list-management feature. Write integration tests against a seeded multi-organisation test database to verify cross-org isolation before merging.
Contingency: If RLS scoping is insufficient, add an explicit organisation_id filter in the Dart query builder layer as a defence-in-depth measure while the Supabase policy is corrected.
Adding new Drift tables for the contact cache may conflict with existing migrations or schema versions in the contact-list-management feature if both features cache the same contacts table, causing migration failures on user devices.
Mitigation & Contingency
Mitigation: Audit existing Drift schema versions from contact-list-management before writing new migrations. Reuse existing cache tables if the schema already covers required fields; only add missing fields via ALTER or new version.
Contingency: If schema conflict occurs, consolidate into a single shared cache table owned by contact-list-management and expose a DAO interface to the search feature, avoiding duplicated schema ownership.
The offline cache may surface significantly stale contact data if sync has not run recently, leading coordinators to act on outdated information (wrong phone numbers, changed assignments).
Mitigation & Contingency
Mitigation: Store and surface the last-sync timestamp prominently in the UI layer. Trigger a background cache refresh on app foreground when connectivity is detected.
Contingency: If staleness becomes a reported UX issue, implement a maximum-age threshold that shows a warning banner when the cache is older than a configurable limit (e.g. 24 hours).