Skip to main content
Version: 3.1

Notes

Technical reference for the NoteThread and NoteMessage models in Care EMR. For the plain-language view, see the Notes concept.

Source:

A discussion (NoteThread) belongs to one patient and, optionally, to one encounter of that patient. A message (NoteMessage) is one entry in a discussion.

The Django models are the storage layer. NoteThread stores the title, the thread type and the links to the patient and the encounter. NoteMessage stores the text and an opaque message_history JSON field.

The Pydantic resource specs define the read and write schemas, the shape of message_history, and the edit-history logic. The viewsets define the API actions, the list filters and the authorization checks.

Models​

ModelPurpose
NoteThreadA discussion: a titled group of messages about one patient. Linked to one encounter of that patient, or to none
NoteMessageOne message in a NoteThread. Keeps the earlier versions of its text after each edit

Both extend EMRBaseModel. It supplies external_id, created_date and modified_date, soft delete through deleted, created_by and updated_by, and the history and meta JSON fields.

NoteThread fields​

FieldTypeNotes
titleCharField(255), null=True, blank=TrueThe specs require it on create and on update: str, at most 255 characters, no minimum length. No uniqueness rule: two discussions of a patient can have the same title
thread_typeCharField(100), default "patient"One of NoteThreadType. Read-only on the API: create always sets it to patient, and PUT or PATCH cannot change it
patientFK → emr.Patient, required, on_delete=CASCADENot in any spec. Create sets it to the patient in the URL. Update cannot change it. The read response does not include it
encounterFK → emr.Encounter, null=True, blank=True, on_delete=CASCADEOptional UUID4 on create, validated as described under "Validation". Update cannot change it. The read response does not include it

A discussion without an encounter is a patient discussion. A discussion with an encounter is an encounter discussion, and it also belongs to the patient of that encounter.

The model has no facility or organization field. A TODO in the model plans organizations to restrict access further; it is not implemented.

on_delete=CASCADE applies only to a delete in the database. On these models, delete() is a soft delete that sets deleted=True, and the default manager hides such rows. The Notes API has no delete action.

Enum values​

NoteThreadType values​

ValueUse in the Notes API
patientThe default. Every discussion that the Notes API creates has this type
referralThe Notes API does not create this type. The Notes routes work with patient discussions only

NoteMessage​

One message in a discussion.

FieldTypeNotes
threadFK → NoteThread, on_delete=CASCADENot in any spec. Create takes the discussion from the URL, never from the body
messageTextField, not nullableThe specs require it on create and on update: str, no length limit
message_historyJSONField, default {}Not writable: the write specs have no message_history field. Care appends one entry on each edit, in the shape below. Returned on read

A message has no patient or encounter field. It reaches the patient and the encounter through its discussion.

message_history shape​

Each edit appends one entry to history. The entry holds the text as it was before that edit, so the oldest edit comes first.

message_history: { "history": [ HistoryEntry, ... ] } # {} until the first edit

HistoryEntry
{
"message": str, # the text that this edit replaced
"created_by": { "username": str, "external_id": str }, # the author of the message
"edited_at": str, # date and time of this edit
"created_at": str # modified_date of the replaced version
}
  • created_by is the author of the message. Only the author can edit a message, so the editor and the author are the same user.
  • created_at is the modified_date of the replaced version. For the first edit, that is the time the message was posted. For a later edit, it is the time of the edit before it.
  • Care writes both times as strings.

Resource specs (API schema)​

Every spec extends EMRResource. Its automatic field mapping skips foreign keys and the fields in __exclude__. So patient and thread come from the URL, and encounter goes through a create hook.

The discussion viewset uses the create spec to create, the update spec to update, and the read spec for reads. It sets no retrieve spec, so the list and a single discussion return the same fields.

Thread specs​

SpecRoleFieldsNotes
NoteThreadSpecshared baseid (UUID4 | None), title (str, required, at most 255 characters)__model__ = NoteThread; __exclude__ = ["patient", "encounter"]
NoteThreadCreateSpecwrite · createbase + encounter (UUID4 | None)Checks that the encounter exists. perform_extra_deserialization resolves the UUID to the encounter foreign key
NoteThreadUpdateSpecwrite · updatebase (title)Adds nothing. An update cannot move a discussion to another patient or encounter
NoteThreadReadSpecread · list and detailbase + thread_type (NoteThreadType), created_by, updated_by (dict | None), created_date, modified_date (datetime)perform_extra_serialization sets id from external_id and fills the audit users. Has no patient or encounter field

Message specs​

SpecRoleFieldsNotes
NoteMessageSpecshared baseid (UUID4 | None), message (str, required)__model__ = NoteMessage; __exclude__ = ["thread"]
NoteMessageCreateSpecwrite · createbase (message)Adds nothing
NoteMessageUpdateSpecwrite · updatebase (message)perform_extra_deserialization appends the replaced version to message_history["history"]
NoteMessageReadSpecreadbase + message_history (dict), created_by, updated_by (dict | None), created_date, modified_date (datetime)perform_extra_serialization sets id from external_id and fills the audit users

The audit users created_by and updated_by are user objects read from the cached UserSpec.

Validation​

  • title: required on create and on update, at most 255 characters. No minimum length is set.
  • message: required on create and on update. No length limit is set.
  • encounter on create: a UUID that matches no encounter fails with "Encounter not found". This spec check does not look at the patient.
  • encounter on create: an encounter of another patient fails later, in the viewset, with "Patient Mismatch".
  • On update, Care drops id, external_id, patient and encounter from the request body before validation.

Methods & save behaviour​

  • Create runs in this order: spec validation, the create authorization, de_serialize, then perform_create.
  • Discussion create: perform_create sets thread_type to patient and patient to the patient in the URL. Then it refuses an encounter of another patient with "Patient Mismatch".
  • Message create: perform_create takes the discussion from the URL.
  • Message create also reads an optional encounter from the request body. An unknown ID returns 404, and an encounter of another patient fails with "Patient Mismatch". NoteMessage has no column for it, so Care does not store it.
  • Create sets created_by and updated_by to the user who sends the request. Rename and edit set updated_by to that user.
  • Message edit: NoteMessageUpdateSpec appends the replaced version to message_history before the save. Outside tests and migrations, no other code writes message_history.

API integration notes​

Routes​

All routes are under /api/v1/ and are nested under the patient. No route is nested under the encounter: the discussion list takes an encounter filter instead.

ActionMethod and pathRequest body
List discussionsGET /api/v1/patient/{patient}/thread/None
Create a discussionPOST /api/v1/patient/{patient}/thread/title, and optionally encounter
Open a discussionGET /api/v1/patient/{patient}/thread/{thread}/None
Rename a discussionPUT /api/v1/patient/{patient}/thread/{thread}/title
List messagesGET /api/v1/patient/{patient}/thread/{thread}/note/None
Post a messagePOST /api/v1/patient/{patient}/thread/{thread}/note/message
Open a messageGET /api/v1/patient/{patient}/thread/{thread}/note/{note}/None
Edit a messagePUT /api/v1/patient/{patient}/thread/{thread}/note/{note}/message
Delete a discussion or a messageNoneNone
  • On message routes, Care finds {thread} by the discussion's external_id, which reads return as id.
  • The discussion routes reach only patient discussions of the patient in the URL.

List filters, ordering and pages​

Query parameterApplies toEffect
encounterDiscussion listEncounter UUID. Returns only the discussions of that encounter
encounter_isnullDiscussion listtrue returns only patient discussions. false returns only encounter discussions
limitBoth listsItems per page. Default 14, at most 200
offsetBoth listsItems to skip
  • Without a filter, the discussion list returns the patient discussions and the encounter discussions of the patient.
  • The discussion list is ordered by the created_date of the discussion, newest first.
  • The message list returns the messages of the discussion in the URL, ordered by created_date, newest first.
  • Both lists answer with count and results.
  • A discussion in a response does not say which encounter it belongs to. To find the discussions of an encounter, filter the list with encounter.
  • The Care frontend sends encounter_isnull=true for the patient's discussions and encounter=<id> for an encounter's discussions, so the two lists do not overlap.

Authorization​

The tables below use these terms:

  • "Closed": the encounter status is completed, cancelled, entered_in_error or discontinued. Any other status is open, discharged included.
  • "A role on the patient": a role that the user holds in one of these places.
    • A facility organization of an open encounter of the patient, or of that encounter's current location.
    • The patient's organization, or an organization above it, read from the patient's organization_cache.
    • The patient directly, through a role given on the patient.
  • "The read check": the check that the list and open routes run, and that rename and edit run first. It refuses with "Permission denied to user".
  • "In the encounter": in one of the encounter's facility organizations, or in an organization of its current location.
  • The facility organizations of an encounter are the organizations it is assigned to, their parents, and the facility's root organization "Administration".
CheckPermission (display name)Held whereCondition
can_view_clinical_data"Can view clinical data about patients"A role on the patientNone
can_write_patient_obj"Can Update a Patient's data"A role on the patientIf the setting PATIENT_GLOBAL_EDIT_ACCESS_ENABLED is on, any facility organization or organization is enough. The setting is off by default
can_view_encounter_clinical_data"Can Read encounter related clinical data"In the encounterAny encounter status
can_update_encounter_clinical_data"Update Encounter related clinical data"In the encounterAlways refused when the encounter is closed
RouteChecks, in orderRefusal message
List or open discussions, list or open messagesThe read check"Permission denied to user"
Create a patient discussion (no encounter in the body)can_write_patient_obj on the patient in the URL"You do not have permission for this action"
Create an encounter discussioncan_update_encounter_clinical_data on the encounter in the body. Then the encounter must belong to the patient in the URL"You do not have permission for this action", then "Patient Mismatch"
Rename a discussionThe read check. Then can_update_encounter_clinical_data on the encounter of the discussion, or can_write_patient_obj on its patient for a patient discussion"Permission denied to user", then "You do not have permission for this action"
Post a messagecan_update_encounter_clinical_data on the encounter of the discussion, or can_write_patient_obj on its patient for a patient discussion. No read check"You do not have permission for this action"
Edit a messageThe read check. Then the user must be the author of the message. Then the same write check as for posting"Permission denied to user", then "Cannot Update Message Created by Other User", then "You do not have permission for this action"
  • Every write to an encounter discussion is refused while its encounter is closed: create, rename, post and edit.
  • For who can do what in plain language, see the Notes concept.

Errors​

PermissionDenied answers HTTP 403, and ValidationError answers HTTP 400.

MessageRaised asWhen
"Permission denied to user"PermissionDenied (403)The read check fails
"You do not have permission for this action"PermissionDenied (403)A write check fails, including any write to a discussion of a closed encounter
"Cannot Update Message Created by Other User"PermissionDenied (403)A user edits a message that another user posted
"Patient Mismatch"ValidationError (400)The encounter of a new discussion, or of a new message, belongs to another patient
"Thread does not belong to the patient"ValidationError (400)A message route names a discussion of another patient
"Message does not belong to the thread"ValidationError (400)A request opens or edits a message under a discussion that does not hold it
"Encounter not found"Spec validation errorA new discussion names an encounter UUID that matches no encounter
No <Model> matches the given query.Http404 (404)An unknown or malformed patient, discussion, message or encounter ID in the URL or the query. Also a discussion that is not of type patient on a message route
  • A missing title or message, or a title longer than 255 characters, fails spec validation.