Skip to main content
Version: 3.1

Care team

Technical reference for the encounter care team in Care EMR. See the care team concept for the plain-language view.

Source:

The care team has no model of its own. It is stored on the Encounter model as a JSONField, and its real shape lives in the Pydantic specs. The write path is a dedicated action on the encounter viewset, not the encounter create or update schema.

Models​

ModelPurpose
EncounterOwns the care team through the care_team and care_team_users fields

Encounter extends EMRBaseModel.

Encounter care team fields​

FieldTypeNotes
care_teamJSONFieldDefault {}. After a save through the care team endpoint, a list of { "user_id": int, "role": Coding } entries in team order. In the __exclude__ list of the encounter specs, so the encounter create and update schemas cannot write it
care_team_usersArrayField(IntegerField)Default []. Cache of the internal user ids in care_team, rebuilt on every save while care_team is a list. Platform-maintained

The stored user_id is the internal integer id of User. The API takes the user's external_id (UUID) on write, and returns it as the member's id on read.

Stored shape​

Keys of role that are left at their default (None) are not stored.

care_team: [
{
"user_id": int, # internal User id
"role": Coding { system?, version?, code, display? } # defaults left out
}
]

The care team links each entry to a User by internal id, in JSON, with no foreign key. Three other models store lists with the same user_id and role keys.

Encounter.care_team[].user_id ──> User.id (internal id, no foreign key)
Schedule.care_team list of { user_id, role } default care team of a schedule
ReferralAvailability.care_team list of { user_id, role } care team of a referral destination
Referral.assignees list of { user_id, role } copied from ReferralAvailability.care_team

A schedule's care team reaches an encounter through the frontend. When the frontend creates an encounter from an appointment on that schedule, the create form then sends the schedule's members to set_care_team_members.

A referral destination's care team is copied to each new referral's assignees. It does not change the encounter's care team.

Resource specs (API schema)​

SpecRole
EncounterCareTeamMemberSpecwrite · one member, defined in care_team.py: user_id: UUID4, role: ValueSetBoundCoding[system-practitioner-role-code]
EncounterCareTeamMemberWriteSpecwrite · the full replacement list: members: list[EncounterCareTeamMemberSpec]
EncounterListSpecread · list. Serializes care_team as [{ "member": UserSpec, "role": Coding }]
EncounterRetrieveSpecread · detail. Same care_team serialization as the list spec

On read, each member is expanded from the cached UserSpec for the stored user_id. The list order of care_team is preserved, and the frontend shows the first entry as the primary member.

PRACTITIONER_ROLE_VALUESET​

role is bound to the system value set "Practitioner Role", slug system-practitioner-role-code. It composes two SNOMED CT is-a filters:

ConceptName
223366009Healthcare professional
224930009Services

The code holds only the concept ids. The names are the SNOMED CT International Edition names, as returned by Care's default terminology server.

Validation runs when the body is parsed. It takes the first value set with the slug (ValuesetDatabaseModel.objects.filter(slug=slug).first()) and looks the code up in it.

A code that is not in that value set fails with "Code does not exist in the valueset". If no value set has the slug, validation fails with "Valueset does not exist in care, Resync valuesets".

The lookup filters on the slug only. A slug can exist more than once: instance-wide, and again per facility, per department or per user.

The role picker in the frontend asks for the slug with the facility. The backend then returns the user's saved preference for that slug in that facility, if one exists. Otherwise it returns a facility value set or the instance-wide one.

Other specs that use EncounterCareTeamMemberSpec​

SpecFieldHolds
ScheduleWriteSpec (base of ScheduleCreateSpec and ScheduleUpdateSpec)care_teamThe default care team of a schedule
ReferralAvailabilityCreateSpec, ReferralAvailabilityUpdateSpeccare_teamThe care team of a referral destination
ReferralUpdateSpec · ReferralAssigneesWriteSpecassignees · membersThe assignees of a referral

These paths accept only active users, and refuse other users with "Care team user not found or inactive". Schedule members must also belong to the schedule's facility ("Care team members must belong to the schedule facility").

The encounter endpoint makes neither check. It accepts any existing user id.

Methods & save behaviour​

  • Encounter.save() calls sync_care_team_users_cache() on every save. It rebuilds care_team_users from the user_id of each entry, and uses -1 for an entry without one.
  • sync_care_team_users_cache() rebuilds the cache only when care_team is a list. The model default is a dict, so an encounter with no saved care team keeps an empty cache.
  • set_care_team_members sets updated_by to the signed-in user, and saves with update_fields=["care_team", "care_team_users", "updated_by", "modified_date"].

API integration notes​

Endpoint​

  • Write endpoint: POST /api/v1/encounter/{external_id}/set_care_team_members/, a detail action on EncounterViewSet. The body is EncounterCareTeamMemberWriteSpec.
  • The response is HTTP 200 with an empty body {}. Only the API schema declares EncounterRetrieveSpec as the response. Read the encounter again to get the new team.
  • The write is a full replacement. Send the complete member list on every call.
  • To remove a member, send the list without that member. To change the primary member, send the list with that member first. An empty list clears the team.
  • The endpoint has no version or concurrency check. Each call replaces what is stored, including changes that another client saved after you read the encounter.

Order of checks and errors​

Before step 1, the default permission class IsAuthenticated refuses a request with no signed-in user. The encounter viewset adds no other permission class.

The endpoint runs these checks in order and stops at the first failure. Steps 4 to 6 run once for each member, in list order.

StepCheckOn failure
1The body parses as EncounterCareTeamMemberWriteSpec: members is present, each user_id is a UUID4, each role is a Coding (extra keys refused) whose code is in the value set400 with {"errors": [...]}, for example "Code does not exist in the valueset"
2The encounter exists. The lookup by external_id applies no access filter for this action404
3authorize_update → can_update_encounter_obj for the signed-in user403 "You do not have permission to update encounter"
4The member's user_id matches a User by external_id404
5The member's user is not already in this body400, from ValidationError({"user": "repeats are not allowed"})
6can_view_encounter_obj for the signed-in user (request.user), not for the member403 "Treating doctor does not have permission on encounter"
7Save200 with {}

Authorization​

  • authorize_update calls can_update_encounter_obj on the EncounterAccess handler. It returns False when the encounter status is in COMPLETED_CHOICES: completed, cancelled, entered_in_error or discontinued.
  • Otherwise can_update_encounter_obj requires can_write_encounter, shown as "Update Encounter non clinical".
  • Inside the member loop, the endpoint calls can_view_encounter_obj with request.user, once for each member. The arguments do not depend on the member, so the check fails on the first member or not at all. This handler requires can_read_encounter, shown as "Can Read encounter", with no status condition.
  • Both handlers check the permission in the encounter's departments, their parent departments, the facility's root department (Administration) and the current location's departments.
  • By default, the Doctor, Nurse and Facility Admin roles hold "Update Encounter non clinical" and "Can Read encounter".
  • The permission definitions also list ADMIN_ROLE. After the role sync, the only role named "Admin" has the role-organization context alone, so it cannot be given in a facility.
  • The members themselves are not checked. A user who holds no role at all is accepted, and the backend tests cover this case.
  • An empty members list skips the member loop, so only can_update_encounter_obj applies.
  • Reading the team needs read access to the encounter. authorize_retrieve passes with "Can list patients" through a role on the patient (can_view_patient_obj) or with "Can Read encounter" (can_view_encounter_obj). Otherwise it gives 403 "You do not have permission to view this patient".
  • Being on the care team gives no access. No authorization handler reads care_team or care_team_users. Roles on an encounter come only from the user's roles in the encounter's departments and its location's departments.

Filtering and reports​

  • The encounter list accepts care_team_user=<username>. It resolves the username to a user id and returns the encounters whose care_team_users contains that id. An unknown username gives 404.
  • Report templates read the care team as encounter.care_team, shown as "Care Team". Each entry has user ("User", a user context) and role ("Role", the role's display, or an empty string).