What to fix (prioritised)
Ordered by severity. Each links to the note with the detail.
P0 — integrity & isolation (do first; they invalidate the product's promise)
- Move the workflow engine server-side. All transition guards, deadlines, gates, and
checklist logic become one transactional server command; RLS forbids direct
cases.statuswrites. → client-side integrity, non-atomic transitions. - Fix storage tenant isolation on
case-attachments. → storage isolation. - Close the RLS holes: public
companiesread, self-provision role escalation, unscoped evidence/gate queries. → RLS & tenancy gaps, self-provision. - Make the audit trail court-grade: server-authored, awaited, unconditional, paginated, append-only, tamper-evident, single stream. → audit not court-grade.
- Kill the hardcoded credentials:
invite-usertemp password,seed-test-dataopen endpoint, committed.env. Rotate keys. → hardcoded credentials. - Sanitise dossier/document generation (HTML injection). → dossier XSS.
P1 — correctness of the legal core
- One holiday-aware, timezone-correct working-day calculator; capture legal dates as explicit facts, not click-time. → naive deadline math, legal time vs click time.
- Statute-verify and test every legal rule (deadlines, caducidade, consultation, witness caps). → zero tests, domain.
- Reconcile the status enum: one canonical set + a legacy→canonical migration. → status generations.
- Reconstruct
case_phase_deliverablesand audit the migration set for other console-created objects. → phantom table. - Separate "document produced" from "obligation verified" (stop auto-approving checklists). → checklist auto-approve.
P2 — model & UX correctness
- Per-tenant, normalised RBAC; explicit active-tenant (URL); server-owned provisioning. → role global, company context, signup provisions nothing.
- Promote load-bearing JSONB fields to typed columns; unify the two deliverable subsystems. → JSONB state, deliverable subsystems.
- Single source for string vocabularies (events, statuses, entity types). → string vocabularies.
- Centralise routes; delete dead/stub surfaces (
CompanyDashboard,/consulta/:slug,Templates). → broken links. - Reimplement email off Lovable; wire the portal-notification email. → email queue.