# Alerts in DHIS2

Record each alert as a tracked entity in the DHIS2 alert program, enrolled and with its signal event.

This page follows [District notifications](https://zato.io/docs/dev/healthcare/dhis2/outbreak-alerts/notify.html) in the [Outbreak alerts](https://zato.io/docs/dev/healthcare/dhis2/outbreak-alerts/index.html) series. When alerts are managed in DHIS2, each alert becomes a tracked entity in the alert program, registered at the district. Its enrollment holds one event on the signal stage with the disease, the week and the number of cases, and the response teams work on it in the DHIS2 Capture app from there. `alerts.ini` names `dhis2.alerts.record-alert` as the service that records alerts.

## The signal event {#the-signal-event}

The event carries the values of the alert as data values, each one a string:

```python
# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class RecordAlert(Service):
    name = 'dhis2.alerts.record-alert'

    def build_event(self, alert:'dict') -> 'dict':

        program = self.config.alert_programs.alert
        cases = str(alert['cases'])

        data_values = [
            {'dataElement': program.element_disease, 'value': alert['disease']},
            {'dataElement': program.element_week, 'value': alert['week']},
            {'dataElement': program.element_cases, 'value': cases},
        ]

        event = {
            'program': program.id,
            'programStage': program.stage_signal,
            'orgUnit': alert['district'],
            'occurredAt': alert['detected'],
            'status': 'COMPLETED',
            'dataValues': data_values,
        }

        return event
```

## The tracked entity {#the-tracked-entity}

The tracker importer takes the tracked entity, its enrollment and the event in one nested request, so the alert is created in full or not at all:

```python
    def handle(self) -> 'None':

        alert = self.request.payload
        program = self.config.alert_programs.alert
        event = self.build_event(alert)

        # The enrollment in the alert program holds the event ..
        enrollment = {
            'program': program.id,
            'orgUnit': alert['district'],
            'enrolledAt': alert['detected'],
            'events': [event],
        }

        # .. and the tracked entity holds the enrollment.
        tracked_entity = {
            'trackedEntityType': program.tracked_entity_type,
            'orgUnit': alert['district'],
            'enrollments': [enrollment],
        }

        payload = {'trackedEntities': [tracked_entity]}
        params = {'async': 'false'}

        conn = self.rest['DHIS2 Tracker']
        response = conn.post(payload, params)

        status = response.data['status']
        self.logger.info('Alert for %s: %s', alert['district_name'], status)
```

DHIS2 rejects the whole request when any of its parts fails validation, which is what its default `atomicMode` of `ALL` means, and assigns the UIDs of the tracked entity, the enrollment and the event itself.

## Next {#next}

[7-1-7 register](https://zato.io/docs/dev/healthcare/dhis2/outbreak-alerts/register.html) keeps the detection and notification dates of the alert.

## See also {#see-also}

- [Setup](https://zato.io/docs/dev/healthcare/dhis2/outbreak-alerts/setup.html) - The DHIS2 Tracker connection and alert\_programs.ini
- [Write a lab result](https://zato.io/docs/dev/healthcare/dhis2/lab-results/write.html) - Events written to an existing enrollment

## Learn more {#learn-more}

- [Healthcare interface engine](https://zato.io/docs/dev/healthcare/) - Clinical messages, FHIR, conversion and operations
- [HL7 v2 parsing](https://zato.io/docs/dev/healthcare/hl7/v2/parsing/) - Messages as typed objects, with field access and validation
- [MLLP channels](https://zato.io/docs/dev/healthcare/hl7v2/mllp/) - Receiving and sending HL7 v2 over MLLP sockets
- [FHIR integrations](https://zato.io/docs/dev/healthcare/hl7/fhir/) - Read and write FHIR resources, with paths, bundles and extensions
- [HL7 v2 to FHIR](https://zato.io/docs/dev/healthcare/hl7/to-fhir/) - Converting v2 messages into FHIR bundles, codes and references included
- [EDIFACT](https://zato.io/docs/dev/healthcare/edifact/) - Parsing interchanges, dialects and the transports they arrive on
- [Transformation](https://zato.io/docs/dev/healthcare/transformation/) - One message in, another standard out, in ordinary Python
- [Audit log](https://zato.io/docs/dev/healthcare/audit-log.html) - Every message with its acknowledgment, searchable by patient identifier
- [AI in clinical interfaces](https://zato.io/docs/dev/healthcare/ai/) - Services calling LLMs and AI agents calling clinical services as tools
