openapi: 3.1.0
info:
  title: OHN-34 Data Quality and Stewardship API
  version: 1.0.1
  description: |
    Accountable human workflows for incomplete, contradictory, disputed and low-quality registry or clinical data.
    Every protected call needs a bearer token (a gateway-issued ES256 token: issuer ohn-platform-gateway, audience ohn-34; the purpose and organization_id are claims), a permitted purpose,
    the local default-deny role matrix and an OHN-16 policy permit. Every POST needs an Idempotency-Key
    (same key + same body replays the original response with `Idempotent-Replay: true`; same key + other body is 409).
    Tenancy (jurisdiction, organisation) always comes from the token, never from the request body.
servers:
  - url: https://opeshealthnetwork.com
security:
  - bearer: []
tags:
  - { name: rules, description: W003 quality rule deployment and F001 }
  - { name: evaluations, description: W001 data quality evaluation }
  - { name: cases, description: W002 stewardship case, F002, F003 }
  - { name: reports, description: R001-R003, metrics and exports }
paths:
  /health/ohn-34:
    get:
      security: []
      summary: Module health (database reachable, dependency adapter modes)
      responses:
        '200': { description: Healthy, content: { application/json: { schema: { $ref: '#/components/schemas/Health' } } } }
        '503': { description: Database unreachable, content: { application/json: { schema: { $ref: '#/components/schemas/Health' } } } }
  /api/ohn/v1/stewardship/vocabularies:
    get:
      tags: [rules]
      summary: Controlled lists, workflow state machines and effective policy
      responses: { '200': { description: Vocabularies }, '401': { $ref: '#/components/responses/Unauthenticated' }, '403': { $ref: '#/components/responses/Denied' } }
  /api/ohn/v1/stewardship/rules:
    get:
      tags: [rules]
      summary: List quality rules
      parameters:
        - { name: status, in: query, schema: { $ref: '#/components/schemas/RuleState' } }
        - { name: entity_type, in: query, schema: { $ref: '#/components/schemas/EntityType' } }
        - { name: in_force, in: query, schema: { type: string, enum: ['true'] } }
        - { $ref: '#/components/parameters/Page' }
        - { $ref: '#/components/parameters/PageSize' }
      responses: { '200': { description: Page of rules, content: { application/json: { schema: { $ref: '#/components/schemas/Page' } } } }, '422': { $ref: '#/components/responses/Validation' } }
    post:
      tags: [rules]
      summary: OHN-34-F001 Data quality rule configuration (creates a draft)
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/QualityRuleCommand' } } } }
      responses:
        '201': { description: Draft created, content: { application/json: { schema: { $ref: '#/components/schemas/QualityRuleView' } } } }
        '403': { $ref: '#/components/responses/Denied' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/stewardship/rules/{id}:
    get:
      tags: [rules]
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses: { '200': { description: Rule with history }, '404': { $ref: '#/components/responses/NotFound' } }
  /api/ohn/v1/stewardship/rules/{id}/transitions:
    post:
      tags: [rules]
      summary: W003 draft → sample_benchmarked → reviewed → approved → active
      description: |
        sample_benchmarked (data_steward, needs `sample`), reviewed (clinical_reviewer, not the author), approved (data_steward,
        not the author; a `quarantine` rule also needs OHN34_MAX_QUARANTINE_FAILURE_RATE configured and a benchmark failure
        rate at or below it), active (data_steward; retires the earlier active version of the same rule key).
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/RuleTransitionCommand' } } } }
      responses:
        '200': { description: Transitioned, content: { application/json: { schema: { $ref: '#/components/schemas/QualityRuleView' } } } }
        '403': { $ref: '#/components/responses/Denied' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
        '503': { $ref: '#/components/responses/Unavailable' }
  /api/ohn/v1/stewardship/evaluations:
    get:
      tags: [evaluations]
      parameters: [{ $ref: '#/components/parameters/Page' }, { $ref: '#/components/parameters/PageSize' }]
      responses: { '200': { description: Page of evaluations } }
    post:
      tags: [evaluations]
      summary: W001 submit a record for evaluation (contract ohn34.record-evaluation v1.0.0; field values are never stored)
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/EvaluationCommand' } } } }
      responses:
        '201': { description: Evaluated; disposition accepted, accepted_with_issues or quarantined }
        '422': { $ref: '#/components/responses/Validation' }
        '503': { $ref: '#/components/responses/Unavailable' }
  /api/ohn/v1/stewardship/evaluations/{id}:
    get: { tags: [evaluations], parameters: [{ $ref: '#/components/parameters/Id' }], responses: { '200': { description: Evaluation with history }, '404': { $ref: '#/components/responses/NotFound' } } }
  /api/ohn/v1/stewardship/records/{recordRef}/disposition:
    get:
      tags: [evaluations]
      summary: Whether a record is held (quarantined) and how many open issues it has; record owners must honour it
      parameters: [{ name: recordRef, in: path, required: true, schema: { type: string, format: uuid } }]
      responses: { '200': { description: Disposition }, '404': { $ref: '#/components/responses/NotFound' } }
  /api/ohn/v1/stewardship/issues:
    get:
      tags: [evaluations]
      parameters:
        - { name: status, in: query, schema: { type: string, enum: [open, resolved] } }
        - { name: severity, in: query, schema: { $ref: '#/components/schemas/Severity' } }
        - { name: without_case, in: query, schema: { type: string, enum: ['true'] } }
        - { $ref: '#/components/parameters/Page' }
        - { $ref: '#/components/parameters/PageSize' }
      responses: { '200': { description: Page of issues } }
  /api/ohn/v1/stewardship/issues/{id}:
    get: { tags: [evaluations], parameters: [{ $ref: '#/components/parameters/Id' }], responses: { '200': { description: Issue }, '404': { $ref: '#/components/responses/NotFound' } } }
  /api/ohn/v1/stewardship/cases:
    get:
      tags: [cases]
      operationId: ohn-34_get_api-ohn-v1-stewardship-cases
      summary: OHN-34-API-001 list stewardship cases (queues)
      parameters:
        - { name: status, in: query, schema: { $ref: '#/components/schemas/CaseState' } }
        - { name: priority, in: query, schema: { $ref: '#/components/schemas/Severity' } }
        - { name: open, in: query, schema: { type: string, enum: ['true'] } }
        - { name: overdue, in: query, schema: { type: string, enum: ['true'] } }
        - { name: assigned_to_me, in: query, schema: { type: string, enum: ['true'] } }
        - { name: awaiting_my_verification, in: query, schema: { type: string, enum: ['true'] } }
        - { $ref: '#/components/parameters/Page' }
        - { $ref: '#/components/parameters/PageSize' }
      responses:
        '200': { description: Page of cases, content: { application/json: { schema: { $ref: '#/components/schemas/Page' } } } }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Denied' }
        '422': { $ref: '#/components/responses/Validation' }
        '429': { $ref: '#/components/responses/RateLimited' }
        '503': { $ref: '#/components/responses/Unavailable' }
    post:
      tags: [cases]
      summary: OHN-34-F002 Stewardship case assignment (opens the case if needed; reassigns while `assigned`)
      parameters: [{ $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/CaseAssignmentCommand' } } } }
      responses:
        '200': { description: Assigned, content: { application/json: { schema: { $ref: '#/components/schemas/CaseView' } } } }
        '201': { description: Case opened and assigned }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/stewardship/cases/{id}:
    get:
      tags: [cases]
      summary: Case with issue, rule, quarantine, remediation receipts, acknowledgements and history
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses: { '200': { description: Case detail }, '404': { $ref: '#/components/responses/NotFound' } }
  /api/ohn/v1/stewardship/cases/{id}/transitions:
    post:
      tags: [cases]
      summary: W002 investigated (assignee) → verified (named clinical reviewer; approved or rejected → back to investigated) → closed (data steward)
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/CaseTransitionCommand' } } } }
      responses:
        '200': { description: Transitioned }
        '403': { $ref: '#/components/responses/Denied' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
  /api/ohn/v1/stewardship/cases/{id}/resolve:
    post:
      tags: [cases]
      operationId: ohn-34_post_api-ohn-v1-stewardship-cases-id-resolve
      summary: OHN-34-API-002 / F003 Quality resolution approval request (investigated → source_corrected)
      description: Only the assignee; the named clinical approver must be someone else. OHN-34 never overwrites the source record.
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { $ref: '#/components/schemas/ResolutionCommand' } } } }
      responses:
        '200': { description: Resolution recorded, content: { application/json: { schema: { $ref: '#/components/schemas/CaseView' } } } }
        '400': { $ref: '#/components/responses/Malformed' }
        '401': { $ref: '#/components/responses/Unauthenticated' }
        '403': { $ref: '#/components/responses/Denied' }
        '404': { $ref: '#/components/responses/NotFound' }
        '409': { $ref: '#/components/responses/Conflict' }
        '422': { $ref: '#/components/responses/Validation' }
        '429': { $ref: '#/components/responses/RateLimited' }
        '503': { $ref: '#/components/responses/Unavailable' }
  /api/ohn/v1/stewardship/cases/{id}/acknowledgements:
    post:
      tags: [cases]
      summary: A named affected consumer (OHN-03, OHN-08) acknowledges a closed case's outcome
      parameters: [{ $ref: '#/components/parameters/Id' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { type: object, additionalProperties: false, required: [consumer], properties: { consumer: { type: string, enum: [OHN-03, OHN-08] } } } } } }
      responses: { '201': { description: Acknowledged }, '200': { description: Already acknowledged }, '409': { $ref: '#/components/responses/Conflict' } }
  /api/ohn/v1/stewardship/metrics:
    get:
      tags: [reports]
      operationId: ohn-34_get_api-ohn-v1-stewardship-metrics
      summary: OHN-34-API-003 governance metrics (counts only, no record references)
      responses: { '200': { description: Metrics }, '401': { $ref: '#/components/responses/Unauthenticated' }, '403': { $ref: '#/components/responses/Denied' }, '503': { $ref: '#/components/responses/Unavailable' } }
  /api/ohn/v1/stewardship/signoffs:
    get:
      tags: [reports]
      summary: Sign-off history (rule reviews/approvals/activations; case resolutions, verifications, rejections, closures)
      parameters: [{ $ref: '#/components/parameters/Page' }, { $ref: '#/components/parameters/PageSize' }]
      responses: { '200': { description: Page of sign-offs } }
  /api/ohn/v1/stewardship/reports/{reportId}:
    get:
      tags: [reports]
      summary: Run R001 (filters facility, from, to), R002 (severity, source, min_age_days) or R003 (source, from, to)
      parameters:
        - { $ref: "#/components/parameters/ReportId" }
        - { name: facility, in: query, description: 'R001 only', schema: { type: string, format: uuid } }
        - { name: from, in: query, description: 'R001 and R003: ISO date or date-time', schema: { type: string } }
        - { name: to, in: query, description: 'R001 and R003: ISO date or date-time, exclusive', schema: { type: string } }
        - { name: severity, in: query, description: 'R002 only', schema: { $ref: "#/components/schemas/Severity" } }
        - { name: source, in: query, description: 'R002 and R003', schema: { type: string, format: uuid } }
        - { name: min_age_days, in: query, description: 'R002 only: whole days 0 to 3650', schema: { type: integer, minimum: 0, maximum: 3650 } }
      responses: { '200': { description: Tabular report with definition, summary and provenance }, '404': { $ref: '#/components/responses/NotFound' }, '422': { $ref: '#/components/responses/Validation' } }
  /api/ohn/v1/stewardship/reports/{reportId}/export:
    post:
      tags: [reports]
      summary: Audited CSV/PDF export signed with HMAC-SHA256 (503 not_configured without an export signing key of 32 or more characters); link valid 15 minutes
      parameters: [{ $ref: '#/components/parameters/ReportId' }, { $ref: '#/components/parameters/IdempotencyKey' }]
      requestBody: { required: true, content: { application/json: { schema: { type: object, additionalProperties: false, required: [format], properties: { format: { type: string, enum: [csv, pdf] }, filters: { type: object, additionalProperties: { type: [string, number] }, description: 'Only the filters of the report; any other key is 422' } } } } } }
      responses: { '201': { description: Export receipt with download_path }, '503': { $ref: '#/components/responses/Unavailable' } }
  /api/ohn/v1/stewardship/exports/{id}/download:
    get:
      tags: [reports]
      summary: Download an export; only its creator, in the same jurisdiction, before expiry (410 after)
      parameters: [{ $ref: '#/components/parameters/Id' }]
      responses: { '200': { description: File, headers: { X-OHN34-Signature: { schema: { type: string } } } }, '404': { $ref: '#/components/responses/NotFound' }, '410': { description: Expired } }
components:
  securitySchemes:
    bearer: { type: http, scheme: bearer, bearerFormat: JWT }
  parameters:
    Id: { name: id, in: path, required: true, schema: { type: string, format: uuid } }
    ReportId: { name: reportId, in: path, required: true, schema: { type: string, enum: [OHN-34-R001, OHN-34-R002, OHN-34-R003] } }
    IdempotencyKey: { name: Idempotency-Key, in: header, required: true, schema: { type: string, minLength: 1, maxLength: 255 } }
    Page: { name: page, in: query, schema: { type: integer, minimum: 1, default: 1 } }
    PageSize: { name: page_size, in: query, schema: { type: integer, minimum: 1, maximum: 100, default: 25 } }
  responses:
    Malformed: { description: Malformed request or missing Idempotency-Key, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Unauthenticated: { description: Missing or invalid token, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Denied: { description: Denied by role, purpose, scope, separation of duties or OHN-16 policy (audited), content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    NotFound: { description: Not found or non-disclosing refusal, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Conflict: { description: Stale version, invalid transition, duplicate or idempotency mismatch; nothing changed, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Validation: { description: Domain validation failed, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    RateLimited: { description: Rate limited (Retry-After), content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
    Unavailable: { description: Dependency unavailable or not configured; nothing accepted or fabricated, content: { application/json: { schema: { $ref: '#/components/schemas/Error' } } } }
  schemas:
    Error:
      type: object
      required: [error]
      properties:
        error: { type: string }
        message: { type: string }
        fields: { type: object, additionalProperties: { type: array, items: { type: string } } }
        current_state: { type: string }
        current_version: { type: integer }
        dependency: { type: string }
        next_state: { type: [string, 'null'], description: 'invalid_transition: the only state the aggregate can move to' }
        failure_rate: { type: [number, 'null'], description: benchmark_failure_rate_too_high }
        limit: { type: number, description: 'benchmark_failure_rate_too_high: the configured maximum' }
        case_id: { type: string, format: uuid, description: case_in_progress }
        details: { type: object, additionalProperties: { type: string }, description: 'A 422 from the strict body reader: the first failing check per top-level field as a zod issue code (invalid_type, too_small, too_big, invalid_string, invalid_enum_value, invalid_union), unknown keys under _ as unrecognized_keys; a failing report filter or query value under its name as its message.' }
        type: { type: string, description: 'Platform problem+json (RFC 9457) fields next to the reference ones' }
        title: { type: string }
        status: { type: integer }
        detail: { type: string }
        trace_id: { type: string }
    Health:
      type: object
      properties:
        status: { type: string, enum: [ok, unavailable] }
        module: { const: OHN-34 }
        version: { type: string }
        synthetic_gateways: { type: boolean, description: 'true only when the synthetic OHN-16 and OHN-23 doubles are switched on (never in production)' }
        dependencies: { type: object, description: 'OHN-16 and OHN-23: synthetic, unconfigured or live; OHN-03 and OHN-08: inbound-contract; OHN-18: local-audit' }
    Page: { type: object, required: [items, total, page, page_size], properties: { items: { type: array, items: {} }, total: { type: integer }, page: { type: integer }, page_size: { type: integer }, provenance: { $ref: '#/components/schemas/Provenance' } } }
    Provenance: { type: object, properties: { source: { type: string }, source_version: { type: string }, generated_at: { type: string, format: date-time }, dependency_adapters: { type: object } } }
    Receipt:
      type: object
      properties:
        receipt_id: { type: string, format: uuid }
        committed_at: { type: string, format: date-time }
        aggregate_id: { type: string }
        aggregate_type: { type: string }
        version: { type: integer }
        status: { const: committed }
        event_delivery: { type: string, enum: [pending, not_applicable] }
        event_id: { type: [string, 'null'] }
    EntityType: { type: string, enum: [patient_identity, clinical_statement, facility, health_worker] }
    Severity: { type: string, enum: [low, moderate, high, critical] }
    FailureAction: { type: string, enum: [flag, open_case, quarantine] }
    RuleState: { type: string, enum: [draft, sample_benchmarked, reviewed, approved, active] }
    CaseState: { type: string, enum: [issue_detected, assigned, investigated, source_corrected, verified, closed] }
    QualityRuleCommand:
      type: object
      additionalProperties: false
      required: [entity_type, field_or_rule, severity, jurisdiction_id, rule_version, action_on_failure]
      properties:
        entity_type: { $ref: '#/components/schemas/EntityType' }
        field_or_rule: { type: string, maxLength: 8192, description: 'A field name, or JSON {"kind":"required|one_of|not_future|code_valid","field":...}' }
        severity: { $ref: '#/components/schemas/Severity' }
        jurisdiction_id: { type: string, format: uuid, description: Must equal the token jurisdiction (403 otherwise) }
        rule_version: { type: string, maxLength: 512, pattern: '^[A-Za-z0-9][A-Za-z0-9._-]*$' }
        action_on_failure: { $ref: '#/components/schemas/FailureAction' }
    QualityRuleView:
      type: object
      properties:
        id: { type: string, format: uuid }
        rule_key: { type: string }
        definition: { type: object }
        status: { $ref: '#/components/schemas/RuleState' }
        next_state: { type: [string, 'null'] }
        benchmark: { type: [object, 'null'], properties: { sample_size: { type: integer }, failed: { type: integer }, failure_rate: { type: number } } }
        in_force: { type: boolean }
        version: { type: integer }
        receipt: { $ref: '#/components/schemas/Receipt' }
    RuleTransitionCommand:
      type: object
      additionalProperties: false
      required: [expected_version, to_state]
      properties:
        expected_version: { type: integer, minimum: 1 }
        to_state: { $ref: '#/components/schemas/RuleState' }
        reason: { type: string, minLength: 3, maxLength: 2048 }
        sample: { type: array, minItems: 1, maxItems: 500, items: { type: object, maxProperties: 100, additionalProperties: { type: [string, number, boolean, "null"], maxLength: 512 } } }
    EvaluationCommand:
      type: object
      additionalProperties: false
      required: [entity_type, record_ref, source_system_id, fields]
      properties:
        entity_type: { $ref: '#/components/schemas/EntityType' }
        record_ref: { type: string, format: uuid }
        source_system_id: { type: string, format: uuid }
        facility_id: { type: string, format: uuid }
        fields: { type: object, maxProperties: 100, additionalProperties: { type: [string, number, boolean, 'null'] } }
    CaseAssignmentCommand:
      type: object
      additionalProperties: false
      required: [issue_ref, assignee_id, priority, resolution_due_at, resolution_plan]
      properties:
        issue_ref: { type: string, format: uuid }
        assignee_id: { type: string, format: uuid }
        priority: { $ref: '#/components/schemas/Severity' }
        resolution_due_at: { type: string, format: date-time, description: Must be in the future }
        resolution_plan: { type: string, minLength: 3, maxLength: 8192 }
    CaseTransitionCommand:
      type: object
      additionalProperties: false
      required: [expected_version, to_state]
      properties:
        expected_version: { type: integer, minimum: 1 }
        to_state: { type: string, enum: [investigated, verified, closed] }
        findings: { type: string, minLength: 3, maxLength: 8192 }
        decision: { type: string, enum: [approved, rejected] }
        reason: { type: string, minLength: 3, maxLength: 2048 }
        affected_consumers: { type: array, items: { type: string, enum: [OHN-03, OHN-08] } }
    ResolutionCommand:
      type: object
      additionalProperties: false
      required: [case_id, source_evidence_ref, proposed_resolution, clinical_approver, decision, expected_version]
      properties:
        case_id: { type: string, format: uuid }
        source_evidence_ref: { type: string, format: uuid }
        proposed_resolution: { type: string, minLength: 3, maxLength: 8192 }
        clinical_approver: { type: string, format: uuid }
        decision: { type: string, enum: [corrected_at_source, disputed, no_change_required] }
        expected_version: { type: integer, minimum: 1 }
    CaseView:
      type: object
      properties:
        id: { type: string, format: uuid }
        status: { $ref: '#/components/schemas/CaseState' }
        next_state: { type: [string, 'null'] }
        assignee_id: { type: [string, 'null'] }
        priority: { type: [string, 'null'] }
        resolution_due_at: { type: [string, 'null'], format: date-time }
        overdue: { type: boolean }
        decision: { type: [string, 'null'] }
        clinical_approver: { type: [string, 'null'] }
        rejections: { type: integer }
        affected_consumers: { type: array, items: { type: string } }
        version: { type: integer }
        receipt: { $ref: '#/components/schemas/Receipt' }
