Scenario Trigger Engine
Component Detail
Description
Core backend service that evaluates scenario conditions against user data and fires notification events when conditions are met. Runs as a Supabase Edge Function on both database event hooks and periodic cron schedules. Supports inactivity detection, milestone detection, and certification expiry detection.
scenario-trigger-engine
Summaries
The Scenario Trigger Engine is the intelligence core of the platform's proactive engagement system. It is the mechanism by which the application shifts from passive record-keeping to actively supporting peer mentors — detecting when someone has gone inactive, is approaching a significant milestone, or has a certification about to expire, and prompting them to take action before it is too late. This directly protects programme outcomes: fewer certifications lapse, more milestones are celebrated and shared, and disengaging mentors are re-activated before they churn. For organisations reporting on programme health, this engine is what converts raw activity data into measurable engagement improvements, providing a clear and defensible return on platform investment.
The Scenario Trigger Engine is the highest-complexity component in the notification system, running as a Supabase Edge Function on both database event hooks and periodic cron schedules. It has three upstream dependencies — notification-preferences-repository, scenario-notification-repository, and push-notification-dispatcher — all of which must be stable before end-to-end testing is possible. The two sub-components (Inactivity Detector and Milestone Detector) carry medium complexity each and should be scoped as separate development tasks within the same sprint. Key delivery risks include cron schedule reliability in the Supabase Edge Function environment, cooldown logic correctness to prevent notification spam, and opt-out preference lookups adding latency to the evaluation loop.
Load testing is required for the scheduled evaluation path to ensure it completes within the cron window for large user populations.
The Scenario Trigger Engine is a high-complexity Supabase Edge Function implementing three scenario evaluators against five data models: activity, certification, achievement, notification_preference, and push_notification. Core interfaces include: `evaluateInactivityScenario(userId, thresholdDays)` delegating to the Inactivity Detector sub-component; `evaluateMilestoneScenario(userId, milestoneType)` delegating to the Milestone Detector sub-component; `evaluateCertificationExpiryScenario(userId, leadTimeDays)` querying the certification model directly; `shouldNotify(userId, scenarioType)` checking opt-out preferences and cooldown expiry before any enqueue; `enqueueNotification(userId, scenarioType, contextData)` writing to scenario-notification-repository and calling push-notification-dispatcher; `recordLastTriggered(userId, scenarioType)` updating cooldown state; `runScheduledEvaluation()` iterating all active users on cron; and `handleDatabaseEvent(event)` responding to Supabase realtime database hooks. Cooldown state must be persisted in the database, not in-memory, to survive function cold starts. Each evaluator should be independently testable in isolation.
Responsibilities
- Evaluate inactivity windows per peer mentor (e.g. no activity in 21 days)
- Detect milestone thresholds (e.g. 50 sessions logged in current year)
- Identify certifications expiring within configurable lead time (e.g. 30 days)
- Respect per-user scenario opt-out preferences before enqueuing notifications
- Prevent duplicate notifications for the same scenario within a cooldown period
Interfaces
evaluateInactivityScenario(userId, thresholdDays)
evaluateMilestoneScenario(userId, milestoneType)
evaluateCertificationExpiryScenario(userId, leadTimeDays)
shouldNotify(userId, scenarioType)
enqueueNotification(userId, scenarioType, contextData)
recordLastTriggered(userId, scenarioType)
runScheduledEvaluation()
handleDatabaseEvent(event)
Relationships
Dependencies (3)
Components this component depends on
Dependents (1)
Components that depend on this component
Sub-Components (2)
Sub-service that queries activity logs per peer mentor and determines if a configurable inactivity threshold has been crossed since the last notification was sent.
- Query last activity date per user
- Compare against inactivity threshold
- Check cooldown to avoid repeat notifications
Sub-service that tracks cumulative counts (sessions, hours, contacts) and fires when a defined milestone boundary is crossed for the first time.
- Read current aggregate counts per user per period
- Detect first crossing of milestone thresholds
- Prevent re-triggering already-celebrated milestones
Related Data Entities (1)
Data entities managed by this component