# Update patient

Canonical page: https://docs.ovok.com/api/slim/patient/update-patient

`PUT /v1/slim/patient/{id}`

Updates a resident (patient): names, gender, birth date, placement on devices, room and zone, and resident state (discharge or reactivation).

**Auth:** Bearer token. The caller must be a practitioner, an admin, a super admin, or hold the "System Owner" access policy. The access policy must grant `Patient:update`, plus `Device:update` when the request changes which devices the resident is on (an admin or super admin who holds no access policy skips this check).
**Scope:** The resident must belong to the caller's project (from the token). Devices, rooms and zones you reference must be in your project, its parent project, or one of its sub-projects.

## Behaviour

The body is a full replacement of the editable fields, designed so that the result of `GET /v1/slim/patient/{id}` can be sent back unchanged. Fields the update does not declare, such as `id`, `floor` or `admissionDate`, are ignored.

- `name` replaces all of the resident's names, each trimmed of leading and trailing spaces. It must contain a `nickname` entry with a non-blank first `given` value. If the nickname changes, it must be unique in the project.
- `gender` and `birthDate` are replaced. Omitting them, or sending `""`, clears them.
- **Placement depends on `locationId`:**
  - **`locationId` omitted or `""`: the resident is removed from all of their devices**, and `deviceId` and `organizationId` are ignored. Send the current `locationId` to keep the placement.
  - `locationId` with `deviceId`: the resident is placed on those devices, and the devices move to the room. The resident leaves any other device. A resident already on one of those devices is replaced.
  - `locationId` without `deviceId`: the resident is placed on the room's device. The room must have exactly one device.
  - `locationId` with `deviceId` `null`, `[]` or `""` is refused with 422 and changes nothing: a room is not a placement without a device.
  - A resident in a room is in the room's zone. An `organizationId` that differs from the zone of the room the resident stays in answers 422 with `path: ["organizationId"]` and changes nothing: move the room to that zone with `PUT /v1/slim/location/{id}`, or the resident to a room in it. A resident moving to another room takes that room's zone. When the room has no zone, `organizationId` re-zones the resident's devices.
  - A save that puts the resident on devices in more than one zone, when they were not in exactly those zones before, is refused with 422.
  - A discharged resident (`discharged` or `discharged-recent`) placed on a device they are not on is refused with 422 and nothing changes, with or without `deviceId`. Send `residentState: "active"` in the same save to reactivate and place them.
- `residentState`:
  - `discharged` on an active resident: the resident is tagged discharged, `dischargeDate` is stored (default: now), the `EpisodeOfCare` is finished, and the resident is removed from all devices. `locationId` must be omitted or `""`.
  - `discharged` on a resident already discharged: only a different `dischargeDate` has an effect; it corrects the stored date.
  - `active` on a discharged resident: the resident is reactivated, the latest `EpisodeOfCare` is reopened and `dischargeDate` is removed.
  - Other values, or the current state, change nothing.
- Any resident who loses a device in this request has their active out-of-bed notification revoked in the same write, and the device's live telemetry state is cleared afterwards.
- Archived residents cannot be updated.
- The write is atomic and takes a short write lock on the resident, the devices and, on a rename, the new Resident ID.
- Change-history entries are added for each changed field and assignment. A resident replaced on a device gets one naming the device, and the room and zone they leave with it.
- The response is the resident after the update, in the same shape as `GET /v1/slim/patient/{id}`.

## Example

```bash
curl -X PUT 'https://api.sandbox.ovok.com/v1/slim/patient/c3499c27-2973-0a7f-807e-fb8676a92dcb' \
  -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3NjcyNjg4MDAsImV4cCI6MTc2NzI3MjQwMH0.Ql3B_ztDvctlyZ2z0vp4sl3ExXRQGkzmxdmTAqxNzZk' \
  -H 'Content-Type: application/json' \
  -d '{
    "name": [{ "use": "nickname", "given": ["Maxi"] }],
    "gender": "male",
    "birthDate": "1940-05-01",
    "deviceId": "c3499c27-2973-0a7f-807e-fb8676a92dcb",
    "locationId": "c3499c27-2973-0a7f-807e-fb8676a92dcb",
    "organizationId": "87ea5dfc-8b8e-384d-8489-79496e706390",
    "residentState": "active"
  }'
```
