Persist toggle state changes via Configuration Manager
epic-scenario-based-follow-up-prompts-scheduler-and-ui-task-013 — Implement the toggle change handler in ScenarioConfigurationScreen that calls the Scenario Configuration Manager to update the enabled/disabled state for a scenario template scoped to the coordinator's chapter. Show optimistic UI update with rollback on failure. Enforce coordinator role guard before allowing changes.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Manage optimistic state with a local StateProvider
Coordinate with the backend definition of the scenario configuration table's chapter-scoped enable/disable model — confirm whether it uses a JOIN table (template_id, chapter_id, is_enabled) or a flag on the template row itself, as this affects the updateTemplateState method signature. Do not implement any caching layer here — rely on the Realtime stream from task-012 as the source of truth.
Testing Requirements
Write unit tests and widget tests using flutter_test. Unit test 1: updateTemplateState called with correct templateId, chapterId from session, and new isEnabled value on toggle. Unit test 2: When server call fails, optimistic state rolls back to original value. Unit test 3: Debounce logic — rapid toggles within 300ms result in only one server call.
Widget test 1: Toggle shows new state immediately after tap (before server response). Widget test 2: Switch reverts to original state and SnackBar shown on server error. Widget test 3: Switch is non-interactive (onChanged null) for non-coordinator role. Widget test 4: Offline scenario — toggle reverts with 'No internet connection' SnackBar.
Integration test: Full round-trip — toggle ON → server confirms → Realtime stream emits → lastModified updates in list item. Mock the Configuration Manager and connectivity service. Aim for 85%+ branch coverage on the toggle handler.
If the scheduler runs concurrently (e.g., two overlapping cron invocations due to edge function retry), duplicate prompts could be dispatched before the first run's history records are committed, breaking the deduplication guarantee.
Mitigation & Contingency
Mitigation: Use a Postgres advisory lock or unique constraint on (user_id, scenario_id, activity_ref) in the prompt history table to make concurrent writes idempotent; design the scheduler to check history inside a transaction.
Contingency: If concurrency issues persist in production, add a distributed lock via Supabase Edge Function concurrency limit (max_instances=1) for the evaluation function as a hard guard.
Coordinators may find scenario configuration unclear if trigger conditions are expressed as raw JSON or technical terminology, leading to misconfiguration and irrelevant prompts being sent to peer mentors.
Mitigation & Contingency
Mitigation: Design the ScenarioConfigurationScreen to display human-readable descriptions of each template's trigger condition (e.g., 'Send 3 days after first contact if wellbeing concern was flagged') rather than raw rule properties; validate with an HLF coordinator in a design review before implementation.
Contingency: If coordinators still misconfigure rules after launch, add a preview mode that shows a simulated prompt based on a test activity before the rule is enabled.