openapi: 3.1.0
info:
  title: OHN-56 Public Health
  version: 1.0.0
  description: >
    Disease surveillance and outbreak alerts, civil registration links, pharmacovigilance, disease registries and national
    analytics. Every call except health needs a Bearer token (platform gateway, ES256, audience ohn-56) whose role and purpose match
    (each role has exactly one purpose). Every POST needs an Idempotency-Key header (1-255 characters, not blank). Request bodies are
    strict: unknown fields, wrong types or out-of-range values are 422 domain_validation_failed. Errors are
    {error, message, ...extra} (application/problem+json carrying the same fields plus type, title, status, detail, trace_id).
    Policy (small-cell k, ADR signal threshold, notifiable conditions and their thresholds) is configuration with no built-in
    defaults: unset policy refuses the dependent call (409 policy_not_configured). Synthetic data only.
servers:
  - url: https://opeshealthnetwork.com
security:
  - bearer: []
tags:
  - name: config
  - name: surveillance
  - name: civil-registration
  - name: pharmacovigilance
  - name: registries
  - name: analytics
paths:
  /health/ohn-56:
    get:
      security: []
      summary: Health and dependency modes
      responses:
        '200': { description: 'OK: status, module, dependencies (mode per OHN-03, 09, 10, 11, 12, 15, 50), writes_enabled, auth_configured', content: { application/json: { schema: { $ref: '#/components/schemas/Health' } } } }
        '503': { description: Database unavailable }
  /api/ohn/v1/public-health/context:
    get:
      summary: Caller role, purpose, dependency modes, settings and allowed actions (all roles)
      responses: { '200': { description: OK, content: { application/json: { schema: { type: object } } } }, '401': { $ref: '#/components/responses/Unauthenticated' } }
  /api/ohn/v1/public-health/settings:
    get:
      tags: [config]
      summary: Jurisdiction settings (admin, surveillance_officer, ministry_analyst, pv_reviewer)
      responses: { '200': { description: OK, content: { application/json: { schema: { $ref: '#/components/schemas/Settings' } } } } }
    post:
      tags: [config]
      summary: Set k and the ADR signal threshold (public_health_admin)
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/SettingsCommand' } } } }
      responses: { '200': { description: Saved }, '409': { $ref: '#/components/responses/Conflict' }, '422': { $ref: '#/components/responses/Invalid' } }
  /api/ohn/v1/public-health/surveillance/conditions:
    get:
      tags: [config]
      summary: Notifiable conditions (surveillance_officer, admin, ministry_analyst)
      responses: { '200': { description: OK, content: { application/json: { schema: { type: object, properties: { items: { type: array, items: { $ref: '#/components/schemas/Condition' } } } } } } } }
    post:
      tags: [config]
      summary: Create or replace a condition by code (public_health_admin)
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ConditionCommand' } } } }
      responses: { '200': { description: Saved }, '422': { $ref: '#/components/responses/Invalid' } }
  /api/ohn/v1/public-health/surveillance/areas:
    get:
      tags: [config]
      summary: Facility to reporting area map (surveillance_officer, admin)
      responses: { '200': { description: OK } }
    post:
      tags: [config]
      summary: Place a facility in an area (public_health_admin)
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { type: object, additionalProperties: false, required: [facility_id, area_code, area_name], properties: { facility_id: { type: string, format: uuid }, area_code: { $ref: '#/components/schemas/Code' }, area_name: { type: string, minLength: 1, maxLength: 120 } } } } } }
      responses: { '200': { description: Saved }, '422': { $ref: '#/components/responses/Invalid' } }
  /api/ohn/v1/public-health/surveillance/sync:
    post:
      tags: [surveillance]
      summary: Count cases from OHN-11 results and OHN-09/OHN-10 diagnoses; raise or update alerts (surveillance_officer, admin)
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { type: object, additionalProperties: false, properties: { since: { type: string, format: date, description: 'Default: the last OHN56 sync look-back (configuration, 56 days).' } } } } } }
      responses:
        '200': { description: Completed run, content: { application/json: { schema: { $ref: '#/components/schemas/SyncResult' } } } }
        '409': { description: No active condition configured (policy_not_configured) }
        '422': { $ref: '#/components/responses/Invalid' }
        '503': { $ref: '#/components/responses/Dependency' }
  /api/ohn/v1/public-health/surveillance/sync-runs:
    get: { tags: [surveillance], summary: Last 50 counting runs, responses: { '200': { description: OK } } }
  /api/ohn/v1/public-health/surveillance/case-counts:
    get:
      tags: [surveillance]
      summary: Distinct-patient case counts by condition, area and week (surveillance_officer)
      parameters:
        - { name: weeks, in: query, description: 'Weeks 1-52 (default 8; 0 means the default).', schema: { type: string, pattern: '^\d{1,2}$' } }
        - { name: condition, in: query, schema: { type: string, maxLength: 64 } }
      responses: { '200': { description: OK }, '404': { description: Unknown condition } }
  /api/ohn/v1/public-health/surveillance/alerts:
    get:
      tags: [surveillance]
      summary: Outbreak alerts (surveillance_officer)
      parameters: [{ name: state, in: query, description: 'Exact state; any string up to 32 characters is accepted (an unknown state matches nothing).', schema: { type: string, maxLength: 32 } }]
      responses: { '200': { description: OK, content: { application/json: { schema: { type: object, properties: { items: { type: array, items: { $ref: '#/components/schemas/Alert' } } } } } } } }
  /api/ohn/v1/public-health/surveillance/alerts/{id}:
    get:
      tags: [surveillance]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses: { '200': { description: Alert with history }, '404': { description: Not found } }
  /api/ohn/v1/public-health/surveillance/alerts/{id}/transitions:
    post:
      tags: [surveillance]
      summary: Triage (open->investigating|dismissed, investigating->confirmed_outbreak|dismissed, confirmed_outbreak->closed)
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { type: object, additionalProperties: false, required: [to_state, note, expected_version], properties: { to_state: { $ref: '#/components/schemas/AlertState' }, note: { type: string, minLength: 1, maxLength: 1000 }, expected_version: { type: integer, minimum: 1 } } } } } }
      responses: { '200': { description: Moved }, '404': { description: Not found }, '409': { $ref: '#/components/responses/Conflict' }, '422': { $ref: '#/components/responses/Invalid' } }
  /api/ohn/v1/public-health/civil-registration/notices:
    get:
      tags: [civil-registration]
      summary: Notices (reporter_clinician own organisation; surveillance_officer all)
      parameters: [{ name: kind, in: query, schema: { type: string, maxLength: 32 } }, { name: state, in: query, schema: { type: string, maxLength: 32 } }]
      responses: { '200': { description: OK } }
    post:
      tags: [civil-registration]
      summary: Record a birth or death notification (reporter_clinician). Subject and mother checked in OHN-03; birth event in OHN-12.
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/CivilNoticeCommand' } } } }
      responses: { '201': { description: Recorded }, '409': { description: A live notice exists }, '422': { $ref: '#/components/responses/Invalid' }, '503': { $ref: '#/components/responses/Dependency' } }
  /api/ohn/v1/public-health/civil-registration/notices/{id}:
    get: { tags: [civil-registration], parameters: [{ $ref: '#/components/parameters/Id' }], responses: { '200': { description: Notice with history }, '404': { description: Not found } } }
  /api/ohn/v1/public-health/civil-registration/notices/{id}/outcome:
    post:
      tags: [civil-registration]
      summary: Record the civil registration answer
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              oneOf:
                - { type: object, additionalProperties: false, required: [outcome, civil_registration_ref, expected_version], properties: { outcome: { const: registered }, civil_registration_ref: { type: string, minLength: 1, maxLength: 80 }, expected_version: { type: integer, minimum: 1 } } }
                - { type: object, additionalProperties: false, required: [outcome, reason, expected_version], properties: { outcome: { const: rejected }, reason: { type: string, minLength: 1, maxLength: 500 }, expected_version: { type: integer, minimum: 1 } } }
      responses: { '200': { description: Recorded }, '404': { description: Not found }, '409': { $ref: '#/components/responses/Conflict' }, '422': { $ref: '#/components/responses/Invalid' } }
  /api/ohn/v1/public-health/pharmacovigilance/reports:
    get:
      tags: [pharmacovigilance]
      summary: ADR reports (patient own; clinician own organisation; pv_reviewer all)
      parameters: [{ name: state, in: query, schema: { type: string, maxLength: 32 } }]
      responses: { '200': { description: OK } }
    post:
      tags: [pharmacovigilance]
      summary: Report an adverse drug reaction (reporter_clinician, patient). Optional OHN-50 prescription link must match patient and medicine.
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/AdrCommand' } } } }
      responses: { '201': { description: Recorded }, '403': { description: Patient reporting for someone else }, '422': { $ref: '#/components/responses/Invalid' }, '503': { $ref: '#/components/responses/Dependency' } }
  /api/ohn/v1/public-health/pharmacovigilance/reports/{id}:
    get: { tags: [pharmacovigilance], parameters: [{ $ref: '#/components/parameters/Id' }], responses: { '200': { description: Report with history }, '404': { description: Not found } } }
  /api/ohn/v1/public-health/pharmacovigilance/reports/{id}/review:
    post:
      tags: [pharmacovigilance]
      summary: Review (pv_reviewer, not the reporter). assessed requires causality.
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { type: object, additionalProperties: false, required: [to_state, note, expected_version], properties: { to_state: { enum: [under_review, assessed, rejected] }, causality: { enum: [certain, probable, possible, unlikely, unclassifiable], description: required when to_state is assessed }, note: { type: string, minLength: 1, maxLength: 2000 }, expected_version: { type: integer, minimum: 1 } } } } } }
      responses: { '200': { description: Reviewed }, '403': { description: Own report }, '409': { $ref: '#/components/responses/Conflict' }, '422': { $ref: '#/components/responses/Invalid' } }
  /api/ohn/v1/public-health/pharmacovigilance/signals:
    get: { tags: [pharmacovigilance], summary: Signal counts per medicine and reaction (pv_reviewer), responses: { '200': { description: OK } } }
  /api/ohn/v1/public-health/registries:
    get: { tags: [registries], summary: Registry catalog with counts (sensitive counts hidden from surveillance_officer), responses: { '200': { description: OK } } }
  /api/ohn/v1/public-health/registries/{registry}/entries:
    get:
      tags: [registries]
      summary: Entries. HIV/TB - 403 segmented_registry for surveillance_officer; consent re-checked per entry; withheld count returned.
      parameters: [{ $ref: '#/components/parameters/Registry' }, { name: status, in: query, schema: { type: string, maxLength: 32 } }]
      responses: { '200': { description: OK }, '403': { description: Segmented }, '503': { $ref: '#/components/responses/Dependency' } }
    post:
      tags: [registries]
      summary: Enrol (registry_steward, reporter_clinician). HIV/TB need an OHN-15 permit.
      parameters: [{ $ref: '#/components/parameters/Registry' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/EnrolCommand' } } } }
      responses: { '201': { description: Enrolled }, '403': { description: consent_denied }, '409': { description: Already enrolled }, '422': { $ref: '#/components/responses/Invalid' } }
  /api/ohn/v1/public-health/registries/{registry}/entries/{id}:
    get: { tags: [registries], parameters: [{ $ref: '#/components/parameters/Registry' }, { $ref: '#/components/parameters/Id' }], responses: { '200': { description: Entry with history }, '403': { description: Segmented or consent denied }, '404': { description: Not found } } }
  /api/ohn/v1/public-health/registries/{registry}/entries/{id}/status:
    post:
      tags: [registries]
      summary: Change status (deceased and closed are final)
      parameters: [{ $ref: '#/components/parameters/Registry' }, { $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { type: object, additionalProperties: false, required: [status, note, expected_version], properties: { status: { enum: [active, in_remission, transferred_out, lost_to_follow_up, deceased, closed] }, note: { type: string, minLength: 1, maxLength: 1000 }, expected_version: { type: integer, minimum: 1 } } } } } }
      responses: { '200': { description: Changed }, '403': { description: Segmented or consent denied }, '404': { description: Not found }, '409': { $ref: '#/components/responses/Conflict' }, '422': { $ref: '#/components/responses/Invalid' } }
  /api/ohn/v1/public-health/analytics/dashboard:
    get:
      tags: [analytics]
      summary: Aggregates with small-cell suppression (ministry_analyst, admin). 409 when k is not configured.
      parameters: [{ name: weeks, in: query, description: 'Weeks 1-52 (default 12).', schema: { type: string, pattern: '^\d{1,2}$' } }]
      responses: { '200': { description: OK, content: { application/json: { schema: { $ref: '#/components/schemas/Dashboard' } } } }, '409': { description: policy_not_configured } }
  /api/ohn/v1/public-health/analytics/{dataset}/export:
    get:
      tags: [analytics]
      summary: CSV export of one dataset (ministry_analyst)
      parameters: [{ name: dataset, in: path, required: true, schema: { enum: [cases, alerts, registries, adr, civil] } }, { name: weeks, in: query, description: 'Weeks 1-52 (default 12).', schema: { type: string, pattern: '^\d{1,2}$' } }]
      responses: { '200': { description: CSV, content: { text/csv: { schema: { type: string } } } }, '409': { description: policy_not_configured } }
  /api/ohn/v1/public-health/audit:
    get: { summary: Last 200 audit records (public_health_admin), responses: { '200': { description: OK } } }
components:
  securitySchemes:
    bearer: { type: http, scheme: bearer, bearerFormat: JWT }
  parameters:
    Id: { name: id, in: path, required: true, schema: { type: string, format: uuid } }
    Registry: { name: registry, in: path, required: true, schema: { enum: [sickle_cell, hiv, tb, cancer, maternal] } }
    IdempotencyKey: { name: Idempotency-Key, in: header, required: true, description: 'Not blank, 1-255 characters. The same key with the same body replays the first answer (Idempotent-Replay: true); with another body it is 409 idempotency_conflict.', schema: { type: string, minLength: 1, maxLength: 255 } }
  responses:
    Unauthenticated: { description: Missing or invalid token }
    Invalid: { description: 'domain_validation_failed (details: first failing check per top-level field as a zod issue code, unknown fields under _) or a named refusal (patient_not_found, birth_event_not_linked, mother_mismatch, prescription_not_found, prescription_mismatch, medicine_mismatch, ...)' }
    Conflict: { description: version_conflict, invalid_transition or idempotency_conflict }
    Dependency: { description: 'dependency_unavailable or not_configured (503; names the dependency; nothing stored). Also 503 not_configured with setting OHN56_WRITES_DISABLED while the operator kill switch is on.' }
  schemas:
    Code: { type: string, description: 'Trimmed before validation.', pattern: '^[A-Za-z0-9._:-]+$', minLength: 1, maxLength: 64 }
    Health: { type: object, properties: { status: { type: string }, module: { const: OHN-56 }, dependencies: { type: object, additionalProperties: { enum: [live, unconfigured, test_fake, test_failing] } } } }
    Settings: { type: object, description: 'Unset values are null and version 0; configured is true once small_cell_k is set.', properties: { small_cell_k: { type: [integer, 'null'] }, adr_signal_threshold: { type: [integer, 'null'] }, version: { type: integer }, updated_at: { type: [string, 'null'], format: date-time }, configured: { type: boolean } } }
    SettingsCommand: { type: object, additionalProperties: false, required: [small_cell_k, adr_signal_threshold, expected_version], properties: { small_cell_k: { type: [integer, 'null'], minimum: 1, maximum: 100 }, adr_signal_threshold: { type: [integer, 'null'], minimum: 1, maximum: 10000 }, expected_version: { type: integer, minimum: 0 } } }
    ConditionCommand:
      type: object
      additionalProperties: false
      description: At least one diagnosis code or result report type is required (422 on diagnosis_codes otherwise).
      required: [code, name, diagnosis_codes, result_report_types, positive_interpretations, threshold_count, active]
      properties:
        code: { $ref: '#/components/schemas/Code' }
        name: { type: string, minLength: 1, maxLength: 120, description: trimmed }
        diagnosis_codes: { type: array, items: { $ref: '#/components/schemas/Code' }, maxItems: 50 }
        result_report_types: { type: array, items: { $ref: '#/components/schemas/Code' }, maxItems: 50 }
        positive_interpretations: { type: array, items: { type: string, minLength: 1, maxLength: 40 }, maxItems: 20, description: 'Stored lower-case; a result counts when its source_interpretation (lower-cased) is listed, or always when the list is empty.' }
        threshold_count: { type: [integer, 'null'], minimum: 1, maximum: 100000, description: 'Distinct patients per area and ISO week that raise an alert; null means cases are counted but never alert.' }
        active: { type: boolean }
    Condition: { allOf: [{ $ref: '#/components/schemas/ConditionCommand' }, { type: object, properties: { id: { type: string, format: uuid }, version: { type: integer } } }] }
    SyncResult: { type: object, properties: { sync_run_id: { type: string, format: uuid }, state: { const: completed }, since: { type: string, format: date }, counts: { type: object, additionalProperties: { type: integer } } } }
    AlertState: { enum: [open, investigating, confirmed_outbreak, dismissed, closed] }
    Alert:
      type: object
      properties:
        id: { type: string, format: uuid }
        condition_code: { type: string }
        area_code: { type: string }
        week_start: { type: string, format: date }
        case_count: { type: integer }
        threshold: { type: integer }
        state: { $ref: '#/components/schemas/AlertState' }
        version: { type: integer }
        allowed_transitions: { type: array, items: { type: string } }
    CivilNoticeCommand:
      type: object
      additionalProperties: false
      description: 'A death needs cause_of_death_code and takes no mother or birth event; a birth takes no cause. event_date cannot be in the future (422).'
      required: [kind, subject_patient_id, event_date, place_facility_id, notification_channel, notification_ref]
      properties:
        kind: { enum: [birth, death] }
        subject_patient_id: { type: string, format: uuid }
        mother_patient_id: { type: string, format: uuid, description: births only }
        ohn12_birth_event_id: { type: string, format: uuid, description: births only }
        event_date: { type: string, format: date }
        place_facility_id: { type: string, format: uuid }
        cause_of_death_code: { $ref: '#/components/schemas/Code', description: required for deaths }
        notification_channel: { enum: [electronic, paper_form, courier] }
        notification_ref: { type: string, minLength: 1, maxLength: 80 }
    AdrCommand:
      type: object
      additionalProperties: false
      required: [medicine_code, medicine_display, reaction_code, reaction_description, onset_date, seriousness, outcome]
      properties:
        patient_id: { type: string, format: uuid, description: required for clinicians; a patient may only name themselves }
        prescription_id: { type: string, format: uuid }
        medicine_code: { $ref: '#/components/schemas/Code' }
        medicine_display: { type: string, minLength: 1, maxLength: 200 }
        reaction_code: { $ref: '#/components/schemas/Code' }
        reaction_description: { type: string, minLength: 1, maxLength: 2000 }
        onset_date: { type: string, format: date, description: cannot be in the future }
        seriousness: { enum: [non_serious, serious] }
        outcome: { enum: [recovered, recovering, not_recovered, fatal, unknown] }
    EnrolCommand:
      type: object
      additionalProperties: false
      required: [patient_id, diagnosis_date, facility_id, details]
      properties:
        patient_id: { type: string, format: uuid }
        diagnosis_date: { type: string, format: date, description: cannot be in the future }
        facility_id: { type: string, format: uuid }
        details: { type: object, maxProperties: 20, propertyNames: { pattern: '^[a-z_]{1,40}$' }, additionalProperties: { type: string, maxLength: 200 } }
    Dashboard:
      type: object
      properties:
        k: { type: integer }
        weeks: { type: integer }
        aggregates_only: { const: true }
        datasets:
          type: object
          additionalProperties:
            type: object
            properties:
              columns: { type: array, items: { type: string } }
              suppressed_cells: { type: integer }
              rows: { type: array, items: { type: object, properties: { count: { type: [integer, 'null'] }, suppressed: { type: boolean } } } }
