# Immunization recommendation

Technical reference for the `ImmunizationRecommendation` model in Care EMR. A recommendation is one planned vaccine dose in a patient's schedule, or a group that holds other recommendations. It maps to the FHIR `ImmunizationRecommendation` resource, with one Care row for each planned dose.

Recommendations usually come from an [immunization policy](https://docs.ohc.network/references/definitions/immunization-policy.md): the `apply_policy` action copies the policy template into a tree of recommendations for one patient. An [immunization](https://docs.ohc.network/references/clinical/immunization.md) record can point to the recommendation that it fulfils.

**Source:**

* Model: [`care/emr/models/immunisation.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/models/immunisation.py) (class `ImmunizationRecommendation`)
* Specs: [`care/emr/resources/immunisation/recommendation.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/resources/immunisation/recommendation.py)
* Viewset: [`care/emr/api/viewsets/immunization/recommendation.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/api/viewsets/immunization/recommendation.py)
* Permissions: [`care/security/permissions/immunization.py`](https://github.com/ohcnetwork/care/blob/develop/care/security/permissions/immunization.py) · [`care/security/authorization/immunization.py`](https://github.com/ohcnetwork/care/blob/develop/care/security/authorization/immunization.py)

## Models

| Model | Purpose |
| - | - |
| `ImmunizationRecommendation` | One planned dose (`is_group = false`) or a group of recommendations (`is_group = true`) for one patient |

It extends [`EMRBaseModel`](https://docs.ohc.network/references/foundation/base-model.md). The same model file also holds `Immunization` ([Immunization](https://docs.ohc.network/references/clinical/immunization.md)) and `ImmunizationPolicy` ([Immunization policy](https://docs.ohc.network/references/definitions/immunization-policy.md)).

## `ImmunizationRecommendation` fields

### Clinical details

These fields come from `BaseImmunizationRecommendationSpec`. The policy template uses the same base.

| Field | Type | Spec type | Notes |
| - | - | - | - |
| `codes` | `JSONField`, default `[]` | `list[Coding]`, each bound to `system-medication` | The vaccines. Required and not empty when `is_group` is `false`. Optional for a group |
| `diseases` | `JSONField`, nullable | `list[Coding]`, each bound to `system-condition-code` | Optional. Target diseases |
| `earliest_date` | `DateField`, nullable | `date` | Optional |
| `due_date` | `DateField`, nullable | `date` | Optional |
| `overdue_date` | `DateField`, nullable | `date` | Optional |
| `description` | `TextField`, nullable | `str` | Optional |
| `series` | `CharField(255)`, nullable | `str` | Optional. Free text |
| `dose_number` | `CharField(255)`, nullable | `str` | Optional. Free text, not a number |
| `series_number` | `CharField(255)`, nullable | `str` | Optional. Free text. care\_fe shows it as the number of doses in the series |

The recommendation specs do not check the order of the three dates. Only the policy template checks the order of its day offsets.

### Status and tree

| Field | Type | Spec type | Notes |
| - | - | - | - |
| `forecast_status` | `CharField(100)` | `ImmunizationRecommendationForecastStatus` | Required on create. Optional on update. See [forecast status values](#immunizationrecommendationforecaststatus-values) |
| `is_group` | `BooleanField` | `bool` | Required on create. Write once |
| `parent` | `FK → self`, `CASCADE`, nullable, `related_name="children"` | `UUID4` or `null` | Optional. Write once. Must be a group (`is_group = true`) of the same patient. A root recommendation has no parent |

### Links

| Field | Type | Spec type | Notes |
| - | - | - | - |
| `patient` | `FK → Patient`, `CASCADE` | `UUID4` | Required on create. Must match the patient in the URL. Write once |
| `encounter` | `FK → Encounter`, `CASCADE`, nullable | `UUID4` or `null` | Optional. Write once. Must belong to the patient. Decides which permission check applies (see below) |

Optional value fields accept a value or no key. They do not accept `null`. A key that an update leaves out keeps its stored value.

## Enum values

### `ImmunizationRecommendationForecastStatus` values

| Value | care\_fe label |
| - | - |
| `due` | Due |
| `immune` | Immune |
| `contraindicated` | Contraindicated |
| `complete` | Complete |

Care does not store "overdue" or "upcoming". care\_fe derives them from a `due` recommendation and its dates:

* **Overdue**: today is on or after `overdue_date`. Without `overdue_date`, today is after `due_date`.
* **Upcoming**: not overdue, and today is before `earliest_date`.
* **Due**: every other `due` recommendation.

## Nested JSON shapes

### `Coding` (each item of `codes` and `diseases`)

```
Coding {

  system:  str | null

  version: str | null

  code:    str            # required

  display: str | null

}
```

Care validates each code against the bound value set.

## Resource specs (API schema)

| Spec | Role | Fields |
| - | - | - |
| `BaseImmunizationRecommendationSpec` | Shared base, also used by the policy template | `id`, `codes`, `diseases`, `due_date`, `earliest_date`, `overdue_date`, `description`, `series`, `dose_number`, `series_number` |
| `ImmunizationRecommendationCreateSpec` | write · create | Base fields plus `parent`, `patient`, `encounter`, `is_group`, `forecast_status`. Rejects a non-group with no `codes` |
| `ImmunizationRecommendationUpdateSpec` | write · update | Base fields plus `forecast_status`. No `parent`, `patient`, `encounter` or `is_group`. Rejects an update that would leave a non-group with no `codes` |
| `ImmunizationRecommendationListSpec` | read · list | Base fields plus `forecast_status`, `is_group`, `parent` (UUID or `null`) |
| `ImmunizationRecommendationRetrieveSpec` | read · detail | List fields plus `patient`, `encounter` (UUID or `null`), `created_by`, `updated_by` |

## Applying a policy

`POST /api/v1/patient/{patient_external_id}/immunization/recommendation/apply_policy/` creates the recommendation tree of one policy for the patient.

Request (`ImmunizationPolicyApplySpec`):

```
{

  policy:         UUID4          # required

  reference_date: date           # required. Day offsets count from this date

  encounter:      UUID4 | null   # optional. Must belong to the patient

  parent:         UUID4 | null   # optional. An existing group of the patient

}
```

Behaviour:

* Care creates one recommendation for each node of the template, groups included, with the same tree structure. A `parent` in the request attaches the new root under that group.
* Each node copies its own clinical fields. A group's fields do not pass to its children.
* `earliest_date`, `due_date` and `overdue_date` are `reference_date` plus the node's day offset. A date outside the supported range returns a validation error.
* Every new recommendation gets `forecast_status = due`, the request's `encounter` and the URL's patient.
* Care creates the whole tree in one transaction. If one node fails, Care creates none.
* The response is `201` with a list of `ImmunizationRecommendationRetrieveSpec` objects, one for each created node.
* For a facility policy, the user needs `can_read_immunization_policy` in that facility. If `encounter` is set, it must be an encounter of the policy's facility.
* If `parent` belongs to an encounter, the request's `encounter` must be the same encounter.

care\_fe does not apply a policy that has more than 20 non-group recommendations. The backend has no such limit.

## API integration notes

* Endpoints are nested under the patient: `/api/v1/patient/{patient_external_id}/immunization/recommendation/`. The viewset allows create, list, retrieve, update and `apply_policy`. It has no delete.
* Filters: `encounter`, `parent` (UUIDs), `root` (`true` for recommendations with no parent), `is_group`, `forecast_status` (comma-separated, several values), `due_date_after`, `due_date_before`. Ordering: `created_date`, `modified_date`, `due_date`.
* Write (create, update and `apply_policy`): a recommendation with an encounter needs `can_write_immunization_recommendation` in that encounter, and Care refuses it when the encounter is closed (`completed`, `cancelled`, `entered_in_error` or `discontinued`). A recommendation without an encounter needs the permission on the patient.
* Read: `can_read_immunization_recommendation` on the patient, or in the recommendation's encounter. A list without the patient permission must pass an `encounter` filter for an encounter that the user can read.
* Default roles with `can_write_immunization_recommendation`: Facility Admin, Doctor, Nurse. Default roles with `can_read_immunization_recommendation`: Facility Admin, Administrator, Staff, Doctor, Nurse, Volunteer, Pharmacist.
* Recording an immunization against a recommendation does not change `forecast_status`. The client sets `complete` with an update.

## Related

* Reference: [Immunization](https://docs.ohc.network/references/clinical/immunization.md)
* Reference: [Immunization policy](https://docs.ohc.network/references/definitions/immunization-policy.md)
* Reference: [Encounter](https://docs.ohc.network/references/clinical/encounter.md)
* Reference: [Patient](https://docs.ohc.network/references/clinical/patient.md)
* Source (model): [immunisation.py on GitHub](https://github.com/ohcnetwork/care/blob/develop/care/emr/models/immunisation.py)
* Source (specs): [resources/immunisation on GitHub](https://github.com/ohcnetwork/care/tree/develop/care/emr/resources/immunisation)
