# SLA implementation tracking

This folder tracks **SRS §9.4 SLA Configuration & Escalation Management** against the codebase.

| Document | Purpose |
|----------|---------|
| [IMPLEMENTATION_STATUS.md](./IMPLEMENTATION_STATUS.md) | What is built vs SRS (by capability) |
| [ONBOARDING_SLA_RULES.md](./ONBOARDING_SLA_RULES.md) | Applicant steps vs internal SLA (Submitted, Questionnaire, etc.) |
| [STAGE_CONFIGURATION_MATRIX.md](./STAGE_CONFIGURATION_MATRIX.md) | Every approval stage vs SLA config row |
| [PENDING_WORK.md](./PENDING_WORK.md) | Ordered backlog to reach full compliance |

## Source of truth (code)

| Area | Location |
|------|----------|
| Activity catalog (all modules) | `backend/src/common/config/slaStageCatalog.ts` |
| Seed script | `backend/scripts/seed-sla-configs.ts` |
| Admin “Initialize defaults” API | `POST /api/master/sla-configs/initialize` |
| Runtime engine | `backend/src/services/SLAService.ts` |
| BullMQ worker | `backend/src/common/queues/sla.worker.ts` (`ENABLE_REDIS=true`) |
| Stage sync on transition | `backend/src/common/utils/slaWorkflowSync.ts` |

## Apply / refresh configuration

```bash
cd backend
npx tsx scripts/seed-sla-configs.ts
```

Or use **Master → SLA Configuration → Initialize defaults** in the UI.

## Environment

| Variable | Effect |
|----------|--------|
| `ENABLE_REDIS=true` | Runs SLA breach checker on schedule |
| `DEBUG_SLA_FAST_MODE=true` | 1 hour TAT ≈ 1 minute (dev only) |

## DB note

Schema for `sla_tracking` and `sla_notification_dispatches` is defined in the
Sequelize models (`src/database/models/compliance/`). On a fresh database
`npm run migrate` builds them automatically.

For environments that already hold data, run the versioned migration runner
which only applies the migrations not yet recorded in the `migrations` table:

```bash
npm run migrate:up
```

### SLA notification dispatch log (idempotency + audit)

Table `sla_notification_dispatches` records **one row per threshold per active track** (pre-breach reminder, breach, escalation L1–L3, repeat overdue window). Unique on `(trackingId, thresholdKey)` prevents duplicate emails even if the worker runs every minute. Created by the model definition + the migration `scripts/migrations/20260526000000_create_sla_notification_dispatches.ts`.

| `dispatchType` | `thresholdKey` example | Sends |
|----------------|------------------------|--------|
| `pre_breach_reminder` | `reminder:<uuid>` | Once per T−1d / T−4h config |
| `breach` | `breach` | Once when TAT exceeded |
| `escalation` | `escalation:L1` | Once per level |
| `repeat_overdue` | `repeat_overdue:<bucket>` | Daily (or fast-mode window) |

`sla_tracking.metadata` is still used for **pause/resume** timers only; legacy `reminder_sent_*` flags are honored until migrated.
