Spaces:
Paused

File size: 3,745 Bytes
383d839
 
 
 
 
 
 
 
 
16a78e2
383d839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16a78e2
383d839
 
 
 
 
 
 
 
 
 
 
 
 
16a78e2
 
 
 
 
383d839
 
 
 
 
 
 
 
 
 
 
 
16a78e2
383d839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
YAZ Care Platform — Document Models
=====================================
SQLModel entities for clinical document management.

Standards: HIPAA § 164.312(c)(1) — integrity controls
           ISO 13606 — EHR document exchange
           HL7 FHIR DocumentReference
"""

from __future__ import annotations

from datetime import datetime, timezone
from enum import Enum
from pathlib import Path
from typing import Optional
from uuid import UUID, uuid4

from sqlmodel import Field, SQLModel


# ---------------------------------------------------------------------------
# Enumerations
# ---------------------------------------------------------------------------


class DocumentType(str, Enum):
    CONSENT = "consent"
    PREOP_ASSESSMENT = "preop_assessment"
    IMAGING = "imaging"
    LAB_RESULT = "lab_result"
    OPERATIVE_NOTE = "operative_note"
    DISCHARGE_LETTER = "discharge_letter"
    FOLLOW_UP_REPORT = "follow_up_report"
    BILLING_ATTACHMENT = "billing_attachment"
    OTHER = "other"


class DocumentStatus(str, Enum):
    PENDING = "pending"  # uploaded, awaiting review
    ACTIVE = "active"  # validated and available
    SUPERSEDED = "superseded"  # replaced by a newer version
    ARCHIVED = "archived"  # retained but no longer primary
    DELETED = "deleted"  # soft-deleted (NEVER purged)


class StorageBackend(str, Enum):
    LOCAL = "local"
    S3 = "s3"
    OVH_OBJECT_STORAGE = "ovh"


# ---------------------------------------------------------------------------
# Database model
# ---------------------------------------------------------------------------


class ClinicalDocument(SQLModel, table=True):
    """
    Metadata record for a clinical document attached to a surgical case.

    The actual file bytes are stored in the :attr:`storage_path` location.
    The database record stores immutable metadata and provenance.

    Multi-tenancy: every query MUST filter by ``tenant_id``.
    Audit: ``created_by`` / ``updated_by`` are always set.
    Soft-delete: ``deleted_at`` is set instead of row removal.
    """

    __tablename__ = "clinical_documents"
    __table_args__ = {"schema": "surgio"}

    id: UUID = Field(default_factory=uuid4, primary_key=True)
    tenant_id: UUID = Field(index=True)
    case_id: Optional[UUID] = Field(default=None, index=True, foreign_key="surgio.cases.id")

    # Document identity
    document_type: DocumentType = Field(default=DocumentType.OTHER, index=True)
    status: DocumentStatus = Field(default=DocumentStatus.PENDING, index=True)

    # File metadata (filled at upload time)
    original_filename: str = Field(max_length=255)
    mime_type: str = Field(max_length=100)
    file_size_bytes: int = Field(ge=0)
    sha256_hash: str = Field(max_length=64)  # integrity check

    # Storage
    storage_backend: StorageBackend = Field(default=StorageBackend.LOCAL)
    storage_path: str = Field(max_length=1024)  # relative path or S3 key
    # Signed URL placeholder (populated on demand, never persisted)

    # Optional description
    title: Optional[str] = Field(default=None, max_length=255)
    description: Optional[str] = Field(default=None, max_length=1000)

    # Version tracking (for superseded documents)
    version: int = Field(default=1, ge=1)
    supersedes_id: Optional[UUID] = Field(default=None)

    # Provenance
    created_by: UUID
    updated_by: Optional[UUID] = Field(default=None)
    created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
    updated_at: Optional[datetime] = Field(default=None)

    # Soft-delete
    deleted_at: Optional[datetime] = Field(default=None)
    deleted_by: Optional[UUID] = Field(default=None)
    deletion_reason: Optional[str] = Field(default=None, max_length=500)