File size: 10,992 Bytes
8eb630b 091ca1d 8eb630b 2a99443 8eb630b 091ca1d 8eb630b 2a99443 8eb630b 091ca1d 8eb630b 091ca1d 8eb630b 091ca1d 8eb630b 091ca1d 8eb630b 2a99443 8eb630b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | """
Document Tool - Data Models
==============================
Domain-agnostic document/evidence lifecycle management with versioning,
multi-signer workflows, and storage abstraction.
Entities:
- DocumentDefinition: Reusable document template (e.g., "Operative Report")
- Document: Concrete document instance with content and metadata
- DocumentVersion: Immutable version record (content-addressed)
- DocumentSignature: Electronic signature record (21 CFR Part 11)
- DocumentEvent: Immutable audit trail
Domains:
- Surgical: Operative reports, surgical checklists, procedure logs
- Clinical: Progress notes, discharge summaries, H&P
- Laboratory: Lab certificates, QC reports, specimen chain-of-custody
- Insurance: Claim documents, policy certificates, authorization letters
- Logistics: Shipping manifests, inspection certificates, customs docs
- Hospitality: Inspection reports, maintenance logs, guest agreements
- Education: Training certificates, competency records, transcripts
Standards: HIPAA, HL7 CDA, FDA 21 CFR Part 11, ISO 15489 (Records Mgmt)
Performance: <100ms p95 for metadata ops, storage-dependent for content
"""
from __future__ import annotations
from datetime import datetime, timezone
from enum import Enum
from typing import Any, Dict, List, Optional
from uuid import UUID, uuid4
from pydantic import BaseModel, ConfigDict, Field as PydanticField
from sqlmodel import Column, Field, JSON, SQLModel
# ============================================================================
# ENUMS
# ============================================================================
class DocumentStatus(str, Enum):
"""Document lifecycle states."""
DRAFT = "draft"
PENDING_REVIEW = "pending_review"
PENDING_SIGNATURE = "pending_signature"
SIGNED = "signed"
FINAL = "final"
AMENDED = "amended"
SUPERSEDED = "superseded"
ARCHIVED = "archived"
DELETED = "deleted"
class DocumentCategory(str, Enum):
"""Document categories."""
CLINICAL_NOTE = "clinical_note"
OPERATIVE_REPORT = "operative_report"
DISCHARGE_SUMMARY = "discharge_summary"
CONSENT_FORM = "consent_form"
LAB_REPORT = "lab_report"
RADIOLOGY_REPORT = "radiology_report"
CERTIFICATE = "certificate"
POLICY = "policy"
MANIFEST = "manifest"
INSPECTION = "inspection"
AGREEMENT = "agreement"
INVOICE = "invoice"
TRAINING_RECORD = "training_record"
FORM = "form"
ATTACHMENT = "attachment"
OTHER = "other"
class ContentFormat(str, Enum):
"""Document content formats."""
MARKDOWN = "markdown"
HTML = "html"
PLAIN_TEXT = "plain_text"
JSON = "json"
PDF = "pdf"
IMAGE = "image"
BINARY = "binary"
class AccessLevel(str, Enum):
"""Document access levels."""
PUBLIC = "public"
INTERNAL = "internal"
RESTRICTED = "restricted"
CONFIDENTIAL = "confidential"
class SignatureType(str, Enum):
"""Electronic signature types (FDA 21 CFR Part 11)."""
AUTHOR = "author"
REVIEWER = "reviewer"
APPROVER = "approver"
WITNESS = "witness"
CO_SIGNER = "co_signer"
AUTHENTICATOR = "authenticator"
class DocumentEventType(str, Enum):
"""Audit event types."""
DOCUMENT_CREATED = "document.created"
DOCUMENT_UPDATED = "document.updated"
VERSION_CREATED = "document.version_created"
SUBMITTED_FOR_REVIEW = "document.submitted_for_review"
SIGNATURE_ADDED = "document.signature_added"
DOCUMENT_FINALIZED = "document.finalized"
DOCUMENT_AMENDED = "document.amended"
DOCUMENT_ARCHIVED = "document.archived"
DOCUMENT_ACCESSED = "document.accessed"
DOCUMENT_EXPORTED = "document.exported"
DOCUMENT_DELETED = "document.deleted"
# ============================================================================
# CORE ENTITIES
# ============================================================================
class DocumentDefinition(SQLModel, table=False):
"""
Reusable document template.
Examples:
- "Operative Report" (Surgio)
- "Discharge Summary" (Clinica)
- "Lab Certificate of Analysis" (Labo)
- "Insurance Claim Document" (Insura)
"""
id: UUID = Field(default_factory=uuid4, primary_key=True)
tenant_id: UUID = Field(index=True)
key: str = Field(max_length=120, index=True)
title: str = Field(max_length=250)
description: Optional[str] = Field(default=None, max_length=1000)
version: str = Field(default="1.0")
domain: str = Field(default="general", max_length=60)
category: DocumentCategory = Field(default=DocumentCategory.OTHER)
content_format: ContentFormat = Field(default=ContentFormat.MARKDOWN)
access_level: AccessLevel = Field(default=AccessLevel.INTERNAL)
# Template content (body template with {{placeholders}})
template_body: Optional[str] = Field(default=None)
# Sections schema (JSON list of section defs)
sections: Optional[List[Dict[str, Any]]] = Field(
default=None, sa_column=Column(JSON),
)
# Signature requirements
required_signatures: Optional[List[Dict[str, Any]]] = Field(
default=None, sa_column=Column(JSON),
)
# Retention policy
retention_days: Optional[int] = Field(default=None, description="Days to retain (HIPAA: 2190=6yr)")
meta_data: Optional[Dict[str, Any]] = Field(default=None, sa_column=Column(JSON))
is_active: bool = Field(default=True)
is_system: bool = Field(default=False)
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
class Document(SQLModel, table=False):
"""
A concrete document instance with content and metadata.
"""
id: UUID = Field(default_factory=uuid4, primary_key=True)
tenant_id: UUID = Field(index=True)
definition_id: Optional[UUID] = Field(default=None, index=True)
case_id: Optional[UUID] = Field(default=None, index=True)
subject_id: Optional[UUID] = Field(default=None, index=True)
title: str = Field(max_length=500)
category: DocumentCategory = Field(default=DocumentCategory.OTHER)
content_format: ContentFormat = Field(default=ContentFormat.MARKDOWN)
access_level: AccessLevel = Field(default=AccessLevel.INTERNAL)
status: DocumentStatus = Field(default=DocumentStatus.DRAFT)
# Content
content: Optional[str] = Field(default=None)
content_hash: Optional[str] = Field(default=None) # SHA-256 of content
file_size_bytes: Optional[int] = None
mime_type: Optional[str] = Field(default=None, max_length=100)
# Versioning
current_version: int = Field(default=1)
# Author
author_id: Optional[UUID] = None
author_name: Optional[str] = Field(default=None, max_length=200)
# Tags / classification
tags: Optional[List[str]] = Field(default=None, sa_column=Column(JSON))
meta_data: Optional[Dict[str, Any]] = Field(default=None, sa_column=Column(JSON))
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
updated_at: Optional[datetime] = None
finalized_at: Optional[datetime] = None
archived_at: Optional[datetime] = None
class DocumentVersion(SQLModel, table=False):
"""Immutable version record (content-addressed)."""
id: UUID = Field(default_factory=uuid4, primary_key=True)
document_id: UUID = Field(index=True)
version_number: int = Field(ge=1)
content: Optional[str] = None
content_hash: str = Field(default="")
change_summary: Optional[str] = Field(default=None, max_length=500)
created_by: Optional[UUID] = None
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
class DocumentSignature(SQLModel, table=False):
"""Electronic signature record (FDA 21 CFR Part 11 compliant)."""
id: UUID = Field(default_factory=uuid4, primary_key=True)
document_id: UUID = Field(index=True)
signer_id: UUID
signer_name: str = Field(max_length=200)
signer_role: Optional[str] = Field(default=None, max_length=100)
signature_type: SignatureType = Field(default=SignatureType.AUTHOR)
reason: Optional[str] = Field(default=None, max_length=500)
signature_hash: str = Field(default="")
signed_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
class DocumentEvent(SQLModel, table=False):
"""Immutable audit event."""
id: UUID = Field(default_factory=uuid4, primary_key=True)
tenant_id: UUID = Field(index=True)
document_id: Optional[UUID] = None
event_type: DocumentEventType = Field(index=True)
actor_id: Optional[UUID] = None
data: Optional[Dict[str, Any]] = Field(default=None, sa_column=Column(JSON))
prev_hash: str = Field(default="genesis")
event_hash: str = Field(default="")
occurred_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
# ============================================================================
# API SCHEMAS
# ============================================================================
class DocumentDefinitionResponse(BaseModel):
id: UUID
key: str
title: str
description: Optional[str] = None
version: str
domain: str
category: str
content_format: str
access_level: str
required_signatures: Optional[List[Dict[str, Any]]] = None
retention_days: Optional[int] = None
is_active: bool
is_system: bool
model_config = ConfigDict(from_attributes=True)
class DocumentCreateRequest(BaseModel):
definition_id: Optional[UUID] = None
case_id: Optional[UUID] = None
subject_id: Optional[UUID] = None
title: str
category: DocumentCategory = DocumentCategory.OTHER
content_format: ContentFormat = ContentFormat.MARKDOWN
access_level: AccessLevel = AccessLevel.INTERNAL
content: Optional[str] = None
tags: Optional[List[str]] = None
meta_data: Optional[Dict[str, Any]] = None
class DocumentUpdateRequest(BaseModel):
content: str
change_summary: Optional[str] = None
class DocumentSignRequest(BaseModel):
signer_name: str
signer_role: Optional[str] = None
signature_type: SignatureType = SignatureType.AUTHOR
reason: Optional[str] = None
class DocumentResponse(BaseModel):
id: UUID
definition_id: Optional[UUID] = None
case_id: Optional[UUID] = None
subject_id: Optional[UUID] = None
title: str
category: str
content_format: str
access_level: str
status: str
content_hash: Optional[str] = None
current_version: int
author_id: Optional[UUID] = None
author_name: Optional[str] = None
tags: Optional[List[str]] = None
signature_count: int = 0
created_at: datetime
updated_at: Optional[datetime] = None
finalized_at: Optional[datetime] = None
model_config = ConfigDict(from_attributes=True)
class DocumentDetailResponse(DocumentResponse):
content: Optional[str] = None
versions: List[Dict[str, Any]] = []
signatures: List[Dict[str, Any]] = []
|