Skip to content

Implement Error Tracking for Missing DHIS2 Option Codes #184

@mtuchi

Description

@mtuchi

Background

Currently, when mapping OpenMRS encounter data to DHIS2 events, we have a fallback mechanism that attempts to use multiple sources for option values:

const matchingOption =
  opt?.["DHIS2 Option Code"] ??
  opt?.["DHIS2 Option name"] ?? 
  answer?.value?.display;

This fallback to answer.value.display masks metadata mapping issues and causes DHIS2 validation errors. The root cause is incomplete metadata configuration where DHIS2 Option Codes are not properly set in the metadata files by the MSF team.

Problem Statement

When DHIS2 Option Codes are missing from the metadata configuration, the system silently falls back to using answer.value.display, which often doesn't match DHIS2's expected option codes. This results in:

  1. DHIS2 API rejecting events with invalid option values
  2. Difficult-to-trace mapping errors that only surface during DHIS2 synchronization
  3. Getting blocked during development and testing

Proposed Solution

Implement a robust error tracking system that:

  1. Removes the fallback to answer.value.display and DHIS2 Option name
  2. Collects detailed information about missing option codes
  3. Logs errors to a centralized Google Sheet for MSF team review
  4. Provides clear visibility into metadata gaps without blocking testing

Acceptance Criteria

Code Changes

  • Remove the fallback logic: ?? opt?.["DHIS2 Option name"] ?? answer?.value?.display;
  • Initialize an error tracking array at the beginning of the mapping process
  • When !matchingOption, capture the following details:
    • OpenMRS Question (display name)
    • Concept External ID (answer.concept.uuid)
    • Answer Display Value (answer.value.display)
    • Answer Value UUID (answer.value.uuid)
    • DHIS2 Data Element UID (dataElement)
    • DHIS2 Option Set UID (matchingOptionSet)
    • Metadata file name (encounter.form.name or encounter.form.uuid)
    • Encounter UUID (encounter.uuid)
    • Patient UUID (encounter.patient.uuid)
    • Timestamp of error occurrence

Google Sheets Integration

  • Create a new workflow step that pushes error data to the tracking spreadsheet
  • Target Google Sheet: Missing OptionSets Mappings
  • Sheet columns should match:
  Timestamp | OpenMRS Question | Concept External ID | Answer Display | Answer UUID | DHIS2 DE UID | DHIS2 OptionSet UID | Metadata File | Encounter UUID | Patient UUID
  • Implement batch writing to avoid rate limits (group errors before writing)
  • Include error count summary and URL to the google sheet in workflow logs

Logging & Monitoring

  • Log the Google Sheet URL at the end of each workflow run
  • Include summary statistics:
    • Total encounters processed
    • Total missing option codes found
    • Number of unique option sets affected
    • Link to error tracking sheet
  • Console log format:
  ⚠️  Missing DHIS2 Option Codes Detected
  Total Errors: [count]
  Affected Option Sets: [count]
  View Details: [Google Sheet URL]

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions