# Staff notifications (Prompt 4D)

`App\Services\Notifications\NotificationService` decides who gets what, on which
channel and in which language; `App\Notifications\Staff\StaffNotification` delivers it.

## Channels

| Channel | Delivery |
| --- | --- |
| `database` | in-app centre (`notifications` table) — always on, cannot be switched off |
| `mail` | Laravel mail |
| `sms` | `SmsGateway` (Sparrow / Aakash; `log` locally) |
| `push` | `PushGateway`: `fcm` (Firebase HTTP v1 — Android, iOS and **web** tokens) or `log`. Tokens live on `user_devices.push_token`; invalid tokens are cleared |
| `broadcast` | Reverb toast on private channel `users.{uid}` (auth: `POST /api/broadcasting/auth` with the Sanctum session or bearer token); P0 carries `sound: "flash-alert"` |

Queues: P0/P1 → `critical` with no delay (target: delivered within 30 s), all others
→ `notifications`.

## Routing

1. Event default (`config/staff_notifications.php → defaults`).
2. Role default replaces it for users holding that role (`role_defaults`).
3. For `story_submitted`, `sla_breach` and `escalated`, the priority level's
   `alert_channels` are added (P0/P1: mail, sms, push, broadcast).
4. The user's own rows (`notification_preferences`) switch channels on/off.
5. Quiet hours (`user_notification_settings`, Asia/Kathmandu, overnight windows
   wrap) keep only the in-app entry — **except for P0**.

## Events and recipients

| Event | Trigger | Recipients |
| --- | --- | --- |
| `story_submitted` | submit | reviewers (`workflow.review`/`workflow.publish` in scope); P0/P1: on-duty first |
| `story_assigned` | assign_editor, `POST …/assign` | assignee |
| `story_returned`, `story_rejected`, `story_published` | return / reject / any publish | story authors |
| `verification_requested` | send_for_verification, P0 fast-track | users with `workflow.verify` in scope |
| `changes_needed` | verify_changes_needed | assignee |
| `priority_changed` | priority change, anti-starvation bump | story authors |
| `sla_warning` / `sla_breach` | SLA sweep | reviewers |
| `escalated` | each escalation level | members of the escalation role who can see the story |
| `lock_handover` | lock override / P0 takeover | previous lock holder |
| `mention`, `security_alert` | reserved (`NotificationService::send`) for comments (Prompt 8) and account security | the mentioned / affected user |
| `admin_digest` | `notifications:admin-digest` 07:00 NPT | system-admin + super-admin (unless `daily_digest` is off) |

The actor never notifies themselves. Texts come from `notification_templates`
(`NotificationTemplatesSeeder`, ne + en; "short" for in-app/push/SMS/toast, "mail"
for e-mail) rendered in the recipient's `preferred_locale`; administrators' edits to
templates survive reseeding.

## Endpoints

| Method | Path | Notes |
| --- | --- | --- |
| GET | `me/notifications` | cursor pagination, `?unread=1`; `meta.unread` count |
| POST | `me/notifications/{id}/read` | |
| POST | `me/notifications/read-all` | |
| GET | `me/notification-preferences` | event × channel with `source` (always / user / default), quiet hours, digest |
| PUT | `me/notification-preferences` | `preferences[{event, channel, enabled}]`, `quiet_hours{start,end}`, `daily_digest`; audited |

## Settings (env)

`PUSH_DRIVER` (`log` | `fcm`; `log` refused in production), `FCM_PROJECT_ID`,
`FCM_CREDENTIALS` (path to the service-account JSON, never committed),
`BROADCAST_CONNECTION=reverb` + `REVERB_*` in staging/production.
