# Immunization policy

Technical reference for the `ImmunizationPolicy` model in Care EMR. A policy is a reusable template of vaccine recommendations. A policy with a facility belongs to that facility. A policy without a facility is an instance policy, and every facility can use it.

A policy does not change a patient's record. The `apply_policy` action on [immunization recommendations](https://docs.ohc.network/references/clinical/immunization-recommendation.md) copies the template into recommendations for one patient.

**Source:**

* Model: [`care/emr/models/immunisation.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/models/immunisation.py) (class `ImmunizationPolicy`)
* Specs: [`care/emr/resources/immunisation/policy.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/resources/immunisation/policy.py) · [`recommendation.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/resources/immunisation/recommendation.py) (shared base)
* Viewset: [`care/emr/api/viewsets/immunization/policy.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/api/viewsets/immunization/policy.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 |
| - | - |
| `ImmunizationPolicy` | A named template of recommendations, for one facility or for the instance |

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 `ImmunizationRecommendation` ([Immunization recommendation](https://docs.ohc.network/references/clinical/immunization-recommendation.md)).

## `ImmunizationPolicy` fields

| Field | Type | Spec type | Notes |
| - | - | - | - |
| `name` | `CharField(255)` | `str` | Required |
| `description` | `TextField`, nullable | `str` | Optional |
| `policy_template` | `JSONField`, default `{}` | `ImmunizationPolicyTemplateSpec` | Required. The root node of the template tree. See [Template shape](#template-shape) |
| `facility` | `FK → Facility`, `PROTECT`, nullable | `UUID4` or `null` | Create only. `null` makes an instance policy. The update spec has no `facility`, so a policy cannot change scope. The read specs do not return it |

## Template shape

`policy_template` holds one `ImmunizationPolicyTemplateSpec` node. A group node holds child nodes, to any depth.

```
ImmunizationPolicyTemplateSpec {

  is_group:       bool                                   # required

  children:       list[ImmunizationPolicyTemplateSpec]   # default []

  codes:          list[Coding]   # bound to system-medication

  diseases:       list[Coding]   # bound to system-condition-code

  earliest_date:  int            # days from the reference date

  due_date:       int            # days from the reference date

  overdue_date:   int            # days from the reference date

  description:    str

  series:         str

  dose_number:    str

  series_number:  str

}
```

The clinical fields come from `BaseImmunizationRecommendationSpec`, the same base that recommendations use. In the template, the three date fields are whole numbers of days instead of dates. All fields except `is_group` and `children` are optional.

### Template validation

Each node must pass these checks:

| Check | Error |
| - | - |
| Only a group can have `children` | `Only groups can contain children` |
| A node that is not a group has at least one `codes` entry | `Codes are required for non-group recommendations` |
| The offsets that are set increase strictly, in the order `earliest_date`, `due_date`, `overdue_date`. Two offsets cannot be equal | `<left> must be before <right>`, for example `earliest_date must be before due_date` |

The specs do not restrict the sign of an offset.

## Resource specs (API schema)

| Spec | Role | Fields |
| - | - | - |
| `ImmunizationPolicySpec` | Shared base | `id`, `policy_template`, `name`, `description` |
| `ImmunizationPolicyCreateSpec` | write · create | Base fields plus `facility` (UUID or `null`) |
| `ImmunizationPolicyUpdateSpec` | write · update | Base fields. `policy_template` and `name` are required, so an update sends the whole template |
| `ImmunizationPolicyListSpec` | read · list | Base fields |
| `ImmunizationPolicyRetrieveSpec` | read · detail | Base fields |
| `ImmunizationPolicyTemplateSpec` | Nested | One template node (see above) |
| `ImmunizationPolicyApplySpec` | write · `apply_policy` request | `policy`, `reference_date`, `encounter`, `parent`. See [Applying a policy](https://docs.ohc.network/references/clinical/immunization-recommendation.md#applying-a-policy) |
| `ImmunizationPolicyApplyResponseSpec` | read · `apply_policy` response | A list of `ImmunizationRecommendationRetrieveSpec` |

## API integration notes

* Endpoint: `/api/v1/immunization/policy/`. The viewset allows create, list, retrieve and update. It has no delete.
* List without a `facility` query parameter returns instance policies only. List with `facility={facility_external_id}` returns that facility's policies and the instance policies, and needs `can_read_immunization_policy` in the facility.
* Filters: `name` (contains, case-insensitive) and `facility`. Ordering: `created_date`, `modified_date`.
* The viewset adds `FavoritesFilter` as a filter backend, but it has no `FAVORITE_RESOURCE` and no favorites endpoints. Do not send `favorite_list`: the filter reads the missing attribute and the request fails.
* Retrieve: a facility policy needs `can_read_immunization_policy` in its facility. Any signed-in user can retrieve an instance policy.
* Create and update: a facility policy needs `can_write_immunization_policy` held in the facility's root organization. Only a superuser can create or update an instance policy.
* Default roles with `can_write_immunization_policy`: Facility Admin. Default roles with `can_read_immunization_policy`: Facility Admin, Administrator, Staff, Doctor, Nurse, Volunteer, Pharmacist.
* The read specs do not return `facility`. To tell instance policies from facility policies in a facility list, care\_fe also fetches the list without `facility` (instance policies only) and compares the IDs.
* `apply_policy` lives on the recommendation endpoint, not here. See [Immunization recommendation](https://docs.ohc.network/references/clinical/immunization-recommendation.md#applying-a-policy).

## Related

* Reference: [Immunization recommendation](https://docs.ohc.network/references/clinical/immunization-recommendation.md)
* Reference: [Immunization](https://docs.ohc.network/references/clinical/immunization.md)
* Reference: [Facility](https://docs.ohc.network/references/facility.md)
* Reference: [Value set](https://docs.ohc.network/references/definitions/valueset.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)
