40227-vm/frontend/docs/safety-quiz-integration.md

80 lines
4.0 KiB
Markdown

# Safety Quiz Frontend Integration
## Purpose
Safety/QBS quiz results follow the frontend three-layer architecture.
```text
View -> Business Logic -> API/Data Access -> Backend
```
## Files
View layer:
- `frontend/src/components/frameworks/QBSSafety.tsx`
- `frontend/src/components/safety-quiz/`
- `frontend/src/components/frameworks/DirectorDashboard.tsx`
- `frontend/src/components/director-dashboard/DirectorQuizResultsPanel.tsx`
- `frontend/src/components/director-dashboard/DirectorRiskList.tsx`
- `frontend/src/components/safety-quiz/SafetyQuizContentEditorPanel.tsx`
Business logic layer:
- `frontend/src/business/safety-quiz/hooks.ts`
- `frontend/src/business/safety-quiz/mappers.ts`
- `frontend/src/business/safety-quiz/selectors.ts`
- `frontend/src/business/safety-quiz/types.ts`
- `frontend/src/business/director-dashboard/hooks.ts`
- `frontend/src/business/director-dashboard/selectors.ts`
API/data access layer:
- `frontend/src/shared/api/safetyQuizResults.ts`
- `frontend/src/shared/types/safetyQuiz.ts`
Constants:
- `frontend/src/shared/constants/safetyQuiz.ts`
## Behavior
- Quiz submission uses `POST /api/safety_quiz_results`.
- Behavior Management is available at organization, school, campus, and class effective
tiers when the user has `READ_QBS`.
- Result-saving UI and mutation are enabled only in the user's own non-organization scope. A
parent user drilled into a child tenant can complete the quiz for immediate feedback, but no
"saved" badge is shown and no reportable child-scope result is created.
- The notification dropdown uses `GET /api/safety_quiz_results/me` for the current week. School,
campus, and class staff with `TAKE_QUIZ` see a reminder until their saved database result exists.
- Staff completion and leadership dashboard rows load from `GET /api/safety_quiz_results/completion`;
pending status is derived from missing saved rows in the backend response.
- QBS quiz content loads from `GET /api/content-catalog/read/safety-qbs-quiz`.
- Organization-scope content managers can add, update, and delete the QBS quiz and key reminders
through a form-based editor backed by the authenticated content catalog endpoints. The editor is
hidden outside the user's own organization scope, and the backend enforces the same rule.
- Editable QBS quiz payloads are typed and validated in the business layer before saving.
- Compliance views render completed and pending staff from the backend report instead of
substituting static staff rows.
- Result ownership is derived by the backend from the authenticated session.
- User profile renders the latest saved QBS result from `GET /api/safety_quiz_results/me`.
- `QBSSafety.tsx` is a thin wrapper that calls `useSafetyQuizPage` and renders focused safety quiz view components.
- Quiz score, progress, result feedback, and compliance summary are derived in business selectors.
- The "current week" key (`getCurrentSafetyQuizWeek`) uses the shared American (Sunday-start) week util (`shared/business/week.ts`) — consistent with the dashboard hero and F.R.A.M.E.
- The backend also normalizes submitted `week_of` values to the same Sunday-start key before saving,
so weekly status, notifications, completion, and dashboard metrics all read the current canonical
week.
- Weekly focus and key reminders are backend content payload fields, not frontend constants.
- QBS quiz content reads only the active `safety-qbs-quiz` catalog row. When organization content
managers update the quiz, the backend stores a new active row and keeps previous quiz versions
inactive for history.
- Safety quiz view components use shared `Button`, `StatePanel`, and `ModuleHeader` primitives.
- Leadership dashboards derive QBS completion metrics and risk rows from the backend completion
summary.
## Preset Content
The default `safety-qbs-quiz` payload is seeded from the backend content catalog defaults for new
organizations. School, campus, and classroom scopes read the organization-owned payload. The
frontend does not keep fallback quiz questions or reminder copy in constants.