Document & dossier generation
Per-phase legal documents
Formal documents (Nota de Culpa, Relatório de Inquérito, Ata da Inquirição, Relatório
Final…) live in case_phase_deliverables (a table the migrations never create).
They are produced three ways in the UI drawers: auto-generate (template
string-concatenation from case data — local and instantaneous, not an AI call, per the
.lovable plan), company template upload, or manual upload. The generators emit
Portuguese legal prose with [placeholders]; report_version is hardcoded 1.
Lifecycle: draft → completed → approved/validated → closed. Only validated/closed
deliverables enter the dossier.
Known bug (documented in
.lovable/plan/…gerar-automaticamente…): after a successful auto-generate,DeliverableDrawerstays on the "choose method" step instead of advancing to review, so generation looks like it did nothing until you reopen the document.
The final dossier
The generate-dossier edge function assembles the exportable legal record as a ZIP
(not a PDF, despite the framing) into case-attachments at
dossiers/{case_id}/{export_id}.zip, tracked in case_dossier_exports
(pending→generating→ready→failed; the client polls every 3s while generating).
ZIP contents: dossier.html (Portuguese, self-contained, with a cover/capa, TOC, phase
timeline, checklist, evidence index with SHA-256 hashes, audit summary),
manifest.json (export metadata + evidence hashes = chain-of-custody), audit_log.csv,
plus evidence/ and deliverables/ folders. Authz = any company_memberships row for the
case's company (no role granularity). Footer asserts "snapshot imutável".
Gotchas / dangers
- HTML injection:
dossier.htmlis string-concatenated with incomplete escaping — cover fields (subject,employee_name, company name) are not escaped (dossier XSS). - Silent incompleteness: failed evidence downloads are skipped without warning; audit capped at 1000 events — the "immutable complete record" can quietly omit material.
- It's a ZIP; a real signed/immutable PDF would need a PDF renderer (none present).
- The "email dossier" action is a disabled "Em breve" stub.
Rebuild implication
Server-side, sanitised document assembly; real PDF with a digital signature/hash seal; fail loudly on missing evidence; define the deliverables table properly.