Skip to main content

Document Service — Domain Model

Status: populated Owner: TBD Last updated: 2026-04-18 Companion: Service Template · 03 platform-services · 02 DDD


1. Aggregates​

1.1 DocumentTemplate​

The authoritative definition of a document structure. Versioned; published versions are immutable.

FieldTypeNotes
idTemplateId (prefix tmpl_)—
tenantIdTenantId—
namestringHuman-readable unique name per tenant
categoryDocumentCategorye.g. prescription, lab_requisition, report, letter, consent
facilityIdFacilityId | nullOptional facility scope
statusTemplateStatusdraft | published | retired
currentVersionIdTemplateVersionId | nullPoints to the active published version
originplatform | tenantplatform = owned by product; tenant = authored by tenant
platformFormKeystring | nullStable reference form key when origin=platform or forked
forkedFromPlatformFormKeystring | nullSet when tenant forks a platform reference template
createdByUserId—
updatedAtTimestamp—

State machine:

Invariants:

  • A published template version is immutable; changes require a new version.
  • Platform reference templates (origin=platform) cannot be edited or republished by tenant users; only forked.
  • effectiveTo in the past: generation requests against that version are rejected unless policy explicitly allows reprint.

1.2 DocumentTemplateVersion​

An immutable snapshot of a template's definition at a point in time.

FieldTypeNotes
idTemplateVersionId (prefix tv_)—
templateIdTemplateId—
tenantIdTenantId—
semverstringe.g. 1.2.0
definitionJSONBLayout, sections, field bindings
effectiveFromDate—
effectiveToDate | null—
publishedAtTimestamp | nullSet on publish transition
checksumstringSHA-256 of the definition JSON
platformPackageVersionstring | nullReference package semver when origin=platform

1.3 DocumentRenderJob​

Tracks an asynchronous PDF generation request.

FieldTypeNotes
idRenderJobId (prefix rjob_)—
tenantIdTenantId—
templateVersionIdTemplateVersionId—
patientIdPatientId—
encounterIdEncounterId | null—
contextJSONB{ resourceType, id } for FHIR context resource
statusRenderJobStatusqueued | running | completed | failed
inputSnapshotHashstringSHA-256 of resolved input data; enables regeneration audit
binaryIdstring | nullFHIR Binary logical ID when completed
documentReferenceIdstring | nullFHIR DocumentReference ID when completed
errorCodestring | nullStable error code on failure
clientMutationIdstring | nullIdempotency key for offline replay
createdAtTimestamp—
completedAtTimestamp | null—

State machine:


1.4 DocumentArtifact (logical)​

Maps to FHIR DocumentReference + Binary + optional Provenance. No separate Postgres table — the FHIR resources are the system of record.

Logical fieldFHIR mapping
Document IDDocumentReference.id
Binary contentBinary.data (stored in object storage; reference in DocumentReference.content.attachment.url)
PatientDocumentReference.subject
EncounterDocumentReference.context.encounter
Document typeDocumentReference.type (LOINC code)
CategoryDocumentReference.category
AuthorDocumentReference.author
StatusDocumentReference.status
Related (supersession)DocumentReference.relatesTo
ProvenanceProvenance.target → DocumentReference
OriginDocumentReference.extension[origin]

1.5 AccessGrant (logical)​

Represents a temporary access grant for a document (presigned URL or portal share). Not persisted as a separate table; generated on demand with short TTL and recorded in audit.


2. Value Objects​

Value ObjectType
TemplateIdBranded ULID, prefix tmpl_
TemplateVersionIdBranded ULID, prefix tv_
RenderJobIdBranded ULID, prefix rjob_
TemplateStatusdraft | published | retired
DocumentCategoryprescription | lab_requisition | report | letter | consent | discharge_summary | referral | scanned_record | other
RenderJobStatusqueued | running | completed | failed
DocumentOriginplatform | tenant
PlatformFormKeyStable string, e.g. platform.dms.general-lab.general-test-requisition

3. Domain Events​

Event subject (NATS)TriggerRetention
document.template.created.v1Template metadata created7 years
document.template.updated.v1Template metadata updated7 years
document.template.version_published.v1Template version published7 years
document.template.retired.v1Template retired7 years
document.render.started.v1Async render job started30 days
document.render.completed.v1Render job completed successfully30 days
document.render.failed.v1Render job failed30 days
document.artifact.registered.v1DocumentReference created (generated or uploaded)7 years
document.upload.quarantined.v1Upload failed virus scan7 years

4. Ubiquitous Language​

TermDefinition
TemplateA versioned document structure with layout and FHIR data bindings
Template VersionAn immutable snapshot of a template's definition at a specific semver
Published VersionAn immutable, active template version available for generation
Platform Reference FormA first-class template shipped with the platform; has a stable platformFormKey; immutable by tenants
Tenant ForkA tenant-owned copy of a platform reference template; has a new templateId but lineage to the source platformFormKey
Render JobAn asynchronous PDF generation task tracked by status
ArtifactThe generated or uploaded PDF document; represented as FHIR DocumentReference + Binary
SupersessionWhen a corrected artifact replaces an older one, preserving the original with a relatesTo link
Input Snapshot HashSHA-256 of resolved FHIR binding data; enables audit of what data produced which PDF
Signed URLA short-lived presigned URL granting temporary read access to a stored object
Virus QuarantineAn uploaded file flagged by the virus scanner; stored separately; access blocked pending review