Two UX gaps where patients had no signal that something happened:
- Document upload: admin POSTs to /api/documents → patient now gets
"New document shared: <title>" email linking to /documents
- Programme assignment: admin PATCHes programme → patient gets enrolment
email with programme name, phase label, and phase description; fires
only on first insert, not on subsequent updates
Both sends are fire-and-forget (.catch logged) so API response is never
blocked. Adds newDocumentEmail + programmeAssignedEmail templates with
escaping and 8 new tests (360 total, 0 failures).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Completes the domain-schema consolidation pass:
- lib/domain/programmes.ts: new — programmeUpdateSchema (fixes magic 10
→ PROGRAMME_START_DATE_MAX_LENGTH constant added to lib/config/portal.ts)
- lib/domain/bookings.ts: new — bookingCreateSchema
- lib/domain/messages.ts: add replySchema alongside existing message utils
API routes now import from domain; no more inline z.object() definitions
in app/api/**. All routes thin; all validation logic has a single home.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
All 20 unprotected findFirst/findMany calls across API routes now return
structured { success: false, error } JSON on DB failure instead of
bubbling an unhandled rejection (which produces a raw 500 HTML page that
breaks JSON.parse on the client).
Security highlight: forgot-password user lookup now returns OK on DB
failure, preserving the anti-enumeration guarantee even when Neon is
unreachable.
Fire-and-forget reads (notification enrichment in bookings, messages,
notes) use .catch(()=>null) to stay non-blocking without crashing the
request. The mark-as-read update in messages/[threadId] GET is also
made fire-and-forget — a read-receipt failure shouldn't prevent the
thread from loading.
Also wraps the bookings/[id] PATCH mutation (was missing try-catch).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
DRY violation: wellnessAvg() (stress-inverted 5-metric average) was defined
identically in lib/domain/signals.ts and app/(admin)/admin/patients/page.tsx.
Now exported from signals.ts — its natural home — and imported in the admin page.
Magic numbers: max(5000) for patient note body and max(1000) for check-in notes
were hardcoded in API routes. Both now reference named constants
PATIENT_NOTE_MAX_LENGTH and CHECKIN_NOTES_MAX_LENGTH in lib/config/portal.ts.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Add USER_ROLE named-constants object and UserRole type to lib/config/auth.ts —
the SSOT for user role values. Replace every raw "patient" / "admin" string
comparison in auth, middleware, cron routes, API routes, admin pages, and the
UserDropdown component with typed USER_ROLE constants.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
goalCreateSchema and goalUpdateSchema were duplicated across two API routes.
Now both routes import from a single SSOT in lib/domain/goals.ts.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Remove unused imports/variables across API routes and admin/portal pages
- Wrap async data-load functions in useCallback to satisfy exhaustive-deps
- Fix ref mutation during render in Assessment (move to useEffect)
- Fix raw anchors to Next.js Link for portal routes in Nav and checkin page
- Extract Date.now() to const in dashboard page
- Disable react-hooks/set-state-in-effect and react-hooks/purity (v7 rules
too aggressive for async useCallback+useEffect data-loading pattern)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>