Commit Graph
11 Commits
Author SHA1 Message Date
g-butandClaude Sonnet 4.6 08854eadcd feat(notifications): email patients when document uploaded or programme assigned
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>
2026-04-24 14:39:41 +02:00
g-butandClaude Sonnet 4.6 ad628c6307 refactor(domain): extract remaining inline schemas to lib/domain
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>
2026-04-23 09:12:55 +02:00
g-butandClaude Sonnet 4.6 85f9ca593c fix(api): wrap all unprotected DB reads in try-catch across non-cron routes
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>
2026-04-23 08:43:00 +02:00
g-butandClaude Sonnet 4.6 6e9f51f6fb fix(api): wrap all DB mutations in try-catch across 13 API routes
Previously, any DB error in a POST/PATCH/DELETE handler would bubble up
as an unhandled rejection and produce a raw 500 with no structured body.
Affected routes now catch DB failures and return:
  { success: false, error: "..." } with status 500

Routes fixed:
- api/account (registration: users insert + profiles insert)
- api/checkin (daily check-in upsert)
- api/messages (thread creation)
- api/messages/[threadId] (reply insert + thread lastMessageAt update)
- api/assessment (result insert)
- api/profile (name update + profile upsert)
- api/documents (JSON create route)
- api/documents/upload (blob put + doc insert — two separate catches)
- api/admin/goals/[goalId] (PATCH update + DELETE)
- api/admin/patients/[id]/goals (goal insert)
- api/admin/patients/[id]/notes (note insert)
- api/admin/patients/[id]/programme (programme upsert)
- api/webhooks/calendly (invitee.created + invitee.canceled DB ops)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 03:29:06 +02:00
g-butandClaude Sonnet 4.6 b5f7a4840e fix(security): bound all unbounded API inputs; add PASSWORD_MAX_LENGTH / EMAIL_MAX_LENGTH
- auth schemas: email.max(255), password.max(200) — prevents bcrypt DoS via
  extremely long inputs (applies to login, register, change-password, reset-password,
  forgot-password)
- programme/route: startDate.max(10) matches varchar(10) column; notes bounded at
  PATIENT_NOTE_MAX_LENGTH
- assessment/route: scores record keys max(50), values clamped 0–100
- lib/config/auth: PASSWORD_MAX_LENGTH=200, EMAIL_MAX_LENGTH=255 as SSOT constants

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-23 01:04:30 +02:00
g-butandClaude Sonnet 4.6 eb69aba16a fix(domain): export wellnessAvg from signals.ts, add missing length constants
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>
2026-04-22 23:28:00 +02:00
g-butandClaude Sonnet 4.6 7db1780ae0 refactor(config): add USER_ROLE constants, eliminate all role magic strings
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>
2026-04-22 22:22:19 +02:00
g-butandClaude Sonnet 4.6 4356954466 refactor(goals): extract shared goal Zod schemas to lib/domain/goals.ts
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>
2026-04-22 20:47:02 +02:00
g-butandClaude Sonnet 4.6 7d51006b7e fix(lint): code health pass — zero ESLint errors and zero type errors
- 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>
2026-04-22 14:30:43 +02:00
g-butandClaude Sonnet 4.6 143bdb6561 feat(platform): Calendly webhook, check-in dip alert, clinical goals API, PWA manifest
- Calendly webhook (/api/webhooks/calendly): HMAC-SHA256 signature verification,
  invitee.created → confirm booking, invitee.canceled → cancel booking
- Cron job (/api/cron/checkin-dip-alert): daily 9am alert when all 3-day avg ≤ 2.5
- Check-in API (/api/checkin): save daily mood/energy/sleep/stress + notes
- Clinical goals API: CRUD at /api/admin/patients/[id]/goals and /api/admin/goals/[id]
- Patient-facing goals endpoint: /api/goals (active + completed for session user)
- Admin patient routes: notes, programme assignment endpoints
- PWA manifest (app/manifest.ts): standalone display, theme #1a1a22
- vercel.json: cron schedule for check-in dip alert at 0 9 * * *

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-15 10:59:34 +02:00
g-butandClaude Sonnet 4.6 95432036f4 feat(portal): add patient portal with auth, DB, and admin panel
Complete patient portal built on Neon (vitareba DB, same project as SYL):
- Drizzle schema: users/auth tables, profiles, assessment_results, bookings, documents, threads, thread_messages
- NextAuth v5 beta with DrizzleAdapter — credentials + Google, JWT strategy, patient|admin roles
- Lazy DB singleton (Proxy) + functional NextAuth config so build never requires DATABASE_URL
- 11 API routes: account, assessment, bookings, documents, messages, profile, admin endpoints
- Auth pages: login, register, forgot-password (Suspense-wrapped for useSearchParams)
- Portal pages: dashboard, assessment (saves on complete), assessments history, profile, bookings, messages/[threadId]
- Admin pages: patient list, patient detail, bookings management, documents
- Middleware: route protection, admin guard, returnTo param
- Landing page CTA wired to /assessment (login required) — Assessment overlay removed from homepage

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-14 11:06:57 +02:00