specs/cl/specs/CL-09-lab-diagnostic-orders-results.md
Overview
CL-09 provides lab and diagnostic order entry, result ingestion, abnormal flagging, review workflows, and trending. This document defines all integration points, event contracts, API contracts, and external integration architecture.Integration Matrix
Internal Integration Points
CL-01: Patient Chart Display
CL-09 orders and results are displayed in the patient chart under a “Labs & Orders” tab. Data Flow:- Chart queries
cl_ordersandcl_order_resultsfiltered bychart_idandorganization_id - Results display includes abnormal flagging with color-coded indicators
- Trending view uses
result_value_numericfor recharts time-series rendering
CL-05 → CL-08 → CL-09: Monitoring Protocol Flow
Ownership:- CL-05 owns medication data and
monitoring_protocoldefinitions - CL-08 evaluates monitoring gaps using CDS rules and generates reminder alerts
- CL-09 provides the order entry action (clinician responds to CL-08 alert by creating an order)
monitoring (new rule type in CL-08 evaluation engine)
CL-08: Abnormal Result Alerts
Whencl_lab_result_received event fires with abnormal_flag != ‘normal’:
- CL-08 CDS engine evaluates the result
- Creates
cl_cds_alertswith severity based on abnormal_flag:critical_low/critical_high→ severity:criticallow/high→ severity:moderateabnormal→ severity:low
PF-10: Notifications
PF-70: LOINC Code Library
cl_loinc_codes reference table is seeded with common BH codes. Future integration with PF-70 code library for comprehensive LOINC search.
Event Contracts
cl_lab_order_created
Channel:cl_pm_events
Publisher: CL-09
Subscribers: PF-10, FW automations
Trigger: Order status transitions from draft to ordered
cl_lab_result_received
Channel:cl_pm_events
Publisher: CL-09 (ingestion edge function)
Subscribers: CL-08 (abnormal alert evaluation), PF-10
Trigger: Result record inserted via ingestion or manual entry
cl_lab_result_reviewed
Channel:cl_pm_events
Publisher: CL-09
Subscribers: PF-10
Trigger: Clinician signs off on a result (reviewed_by/reviewed_at set)
External Integration: Lab Result Ingestion
Architecture
Edge Function: cl-lab-result-ingestion
Authentication: Custom API key per lab per organization. Stored in cl_reference_labs.connection_config.api_key (encrypted). verify_jwt: false.
Request Format:
- Custom segment parser in edge function (no external library dependency)
- Parse MSH, PID, OBR, OBX segments
- Map OBX-3 (LOINC) → result_code, OBX-5 → result_value/result_value_numeric, OBX-6 → result_units, OBX-7 → reference_range, OBX-8 → abnormal_flag
DiagnosticReport.result[]→ individualcl_order_resultsrowsObservation.code.coding[0].code→ result_code (LOINC)Observation.valueQuantity.value→ result_value_numericObservation.interpretation→ abnormal_flag
- 200 + ACK on success
- 400 + NAK on validation failure
- 401 on invalid API key
- 500 + error on server failure
Security Controls
- TLS 1.2+ required for all inbound connections
- API key validation against
cl_reference_labs.connection_config - Service-role client for database operations (bypasses RLS)
- Audit logging — all ingested results logged to
pf_audit_logswith source lab identifier - PHI handling — result values stored only in RLS-protected tables; no PHI in edge function logs
- Rate limiting — per-lab rate limiting via
pf_rate_limit_buckets
Security Patterns
- All CL-09 tables use composite FKs
(organization_id, entity_id)for tenant isolation - UPDATE RLS policies include
WITH CHECKto prevent organization_id modification - DELETE is blocked on
cl_ordersandcl_order_results(soft delete viadeleted_at) - Lab ingestion uses
SECURITY DEFINERfunctions when inserting via service-role to maintain audit trails
Related Documents
- Spec:
specs/cl/specs/CL-09-lab-diagnostic-orders-results.md - Task Breakdown:
specs/cl/tasks/CL-09-TASKS.md - CL-05 Integration:
docs/architecture/integrations/CL-05-medication-management-INTEGRATION.md - CL-08 Integration:
docs/architecture/integrations/CL-08-clinical-decision-support-INTEGRATION.md - Event Contracts:
docs/architecture/integrations/EVENT_CONTRACTS.md - CROSS_CORE_INTEGRATIONS:
docs/architecture/integrations/CROSS_CORE_INTEGRATIONS.md