Skip to content

About

Plug for CARE that sends SMS notifications to patients on booking

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

42 Commits

Folders and files

Repository files navigation

care_notifications_be

CARE plug that sends notifications to patients (SMS) and clinicians (in-app + web push).

Currently supports

  1. Token bookings — confirm, reminder, cancel, reschedule. (patient SMS + user in-app/web push)
  2. Service requests — raised, duplicate, repeated within 30 days. (in-app)
  3. Diagnostic reports — ready. (in-app)
  4. Encounters — IP admission with location assigned, bed change, watched tag added (e.g. emergency). (in-app)
  5. Medication stock — near expiry, low stock. (in-app)
  6. Stock deliveries — damaged, abandoned. (in-app)
  7. Referrals — raised. (in-app)
  8. Medication requests — duplicate. (in-app)
  9. Web push — every clinician (in-app) notification is also delivered to subscribed browsers/devices. (web push)

Install

Add to plug_config.py:

care_notifications = Plug(
    name="care_notifications",
    package_name="git+https://github.com/ohcnetwork/care_notifications_be.git",
    version="@main",
    configs={},
)
plugs = [care_notifications]

Then make build && make up.

Settings

Token bookings (SMS)

Key Default Description
TOKEN_BOOKING_NOTIFICATIONS_ENABLED True Master switch for all token booking notifications.
BOOKING_REMINDER_LEAD_MINUTES 60 Minutes before slot start to send the reminder.
BOOKING_REMINDER_SWEEP_MINUTES 1 How often the reminder sweep runs.
BOOKING_NOTIFY_CONFIRMATION True Send confirmation SMS to the patient.
BOOKING_NOTIFY_CONFIRMATION_USERS True Send an in-app / web-push alert to the users behind the booked resource — practitioner → the practitioner; healthcare_service → its managing-org members; location → the location's org members.
BOOKING_NOTIFY_REMINDER True Send reminder SMS.
BOOKING_NOTIFY_REMINDER_USERS True Send a reminder in-app / web-push alert to the resource's users (same audience rule as confirmation) when a booking's slot enters the lead window.
BOOKING_NOTIFY_CANCEL True Send cancellation SMS.
BOOKING_NOTIFY_CANCEL_USERS True Send a cancellation in-app / web-push alert to the resource's users (same audience rule as confirmation).
BOOKING_NOTIFY_RESCHEDULED True Send reschedule SMS.
BOOKING_NOTIFY_RESCHEDULED_USERS True Send a reschedule in-app / web-push alert to the resource's users (same audience rule as confirmation).
BOOKING_CONFIRMATION_SMS_TEXT Hi {patient_name}, your appointment is confirmed for {slot_start:%a, %d %b %Y %H:%M}. - Care Confirmation SMS body.
BOOKING_REMINDER_SMS_TEXT Reminder: {patient_name}, your appointment is at {slot_start:%a, %d %b %Y %H:%M}. - Care Reminder SMS body.
BOOKING_CANCEL_SMS_TEXT Hi {patient_name}, your appointment for {slot_start:%a, %d %b %Y %H:%M} has been cancelled. - Care Cancel SMS body.
BOOKING_RESCHEDULED_SMS_TEXT Hi {patient_name}, your previous appointment has been rescheduled. - Care Reschedule SMS body.
BOOKING_CONFIRMATION_USERS_TITLE New appointment: {patient_name} Confirmation alert title. Placeholders: {patient_name}, {slot_start}.
BOOKING_CONFIRMATION_USERS_BODY {slot_start:%a, %d %b %Y %H:%M} Confirmation alert body. Same placeholders.
BOOKING_CANCEL_USERS_TITLE Appointment cancelled: {patient_name} Cancellation alert title. Same placeholders.
BOOKING_CANCEL_USERS_BODY {slot_start:%a, %d %b %Y %H:%M} Cancellation alert body. Same placeholders.
BOOKING_RESCHEDULED_USERS_TITLE Appointment rescheduled: {patient_name} Reschedule alert title. Same placeholders.
BOOKING_RESCHEDULED_USERS_BODY {slot_start:%a, %d %b %Y %H:%M} Reschedule alert body. Same placeholders.
BOOKING_REMINDER_USERS_TITLE Upcoming appointment: {patient_name} Reminder alert title. Same placeholders.
BOOKING_REMINDER_USERS_BODY {slot_start:%a, %d %b %Y %H:%M} Reminder alert body. Same placeholders.

Service requests (in-app)

Key Default Description
SERVICE_REQUEST_NOTIFICATIONS_ENABLED True Master switch for all service request notifications.
SERVICE_REQUEST_NOTIFY_RAISED True Notify when an SR transitions to active. Sent to members of sr.healthcare_service.managing_organization.
SERVICE_REQUEST_RAISED_TITLE New {service_request_title} request Inbox title.
SERVICE_REQUEST_RAISED_BODY Patient: {patient_name} Inbox body.
SERVICE_REQUEST_NOTIFY_DUPLICATE True When an SR becomes active and the patient already has an active / on_hold SR for the same activity definition (or the same code when there is none).
SERVICE_REQUEST_DUPLICATE_TITLE Duplicate request: {service_request_title} Inbox title.
SERVICE_REQUEST_DUPLICATE_BODY Patient: {patient_name}. An active request was raised on {previous_date}. Inbox body.
SERVICE_REQUEST_NOTIFY_REPEAT True When an SR becomes active and, with no open duplicate, the patient has a completed SR for the same service raised within the window. Only one of duplicate / repeat fires per SR.
SERVICE_REQUEST_REPEAT_WINDOW_DAYS 30 Look-back window for repeat requests.
SERVICE_REQUEST_REPEAT_TITLE Repeat request: {service_request_title} Inbox title.
SERVICE_REQUEST_REPEAT_BODY Patient: {patient_name}. Already done on {previous_date}. Inbox body.

Duplicate and repeat alerts go to the requesters of the new and the earlier SR; if neither has a requester, to the encounter's care team.

Diagnostic reports (in-app)

Key Default Description
DIAGNOSTIC_REPORT_NOTIFICATIONS_ENABLED True Master switch for all diagnostic report notifications.
DIAGNOSTIC_REPORT_NOTIFY_READY True Notify when a DR transitions to final. Sent to the encounter's care team and the originating SR's requester.
DIAGNOSTIC_REPORT_READY_TITLE Diagnostic report ready for {patient_name} Inbox title.
DIAGNOSTIC_REPORT_READY_BODY `` Inbox body (empty by default; title carries enough).

Encounters (in-app)

Key Default Description
ENCOUNTER_NOTIFICATIONS_ENABLED True Master switch for all encounter notifications.
ENCOUNTER_NOTIFY_IP_CREATED True Notify when an encounter newly becomes IP (encounter_class="imp") with a current_location assigned. Sent to care team + members of every facility-org in the encounter's access cache.
ENCOUNTER_IP_CREATED_TITLE Bed {location_name} assigned to {patient_name} Inbox title.
ENCOUNTER_IP_CREATED_BODY {patient_name} is assigned to bed {location_name} Inbox body.
ENCOUNTER_NOTIFY_BED_TRANSFERRED True Notify when an encounter that already occupied a bed (active / completed; planned and reserved don't count) gets a new active bed. Sent to the care team, the encounter's orgs and the new location's orgs. A bed move does not re-send the IP admission alert.
ENCOUNTER_BED_TRANSFERRED_TITLE {patient_name} moved to {location_name} Inbox title.
ENCOUNTER_BED_TRANSFERRED_BODY Moved from {previous_location_name} to {location_name} Inbox body.
ENCOUNTER_NOTIFY_TAG_ADDED True Notify when a watched tag is added to an encounter. Sent to the encounter's care team; if it has none, to the encounter's orgs.
ENCOUNTER_TAG_NOTIFY_NAMES ["emergency"] Tag names to watch. Env: comma-separated, e.g. ENCOUNTER_TAG_NOTIFY_NAMES=emergency,code blue. Names are lowercased and trimmed, then matched exactly (no partial matches) against the tag's display name or the name of any of its parent tags, so listing a parent (emergency) also covers its child tags (Emergency 1, Emergency 2). Only tag configs with resource encounter count; patient tags never trigger. One alert per tag added; removing a tag sends nothing, re-adding alerts again. Empty disables it.
ENCOUNTER_TAG_ADDED_TITLE {tag_name}: {patient_name} Inbox title.
ENCOUNTER_TAG_ADDED_BODY {patient_name} was tagged {tag_name}. Inbox body.

Medication stock (in-app)

Key Default Description
MEDICATION_NOTIFICATIONS_ENABLED True Master switch for all medication stock notifications.
MEDICATION_NOTIFY_NEAR_EXPIRY True Notify when a medication InventoryItem's product expires within the lead window and has stock on hand.
MEDICATION_NOTIFY_LOW_STOCK True Notify when a medication InventoryItem's net_content drops below the threshold.
MEDICATION_STOCK_SWEEP_MINUTES 1440 How often the medication stock sweep runs.
MEDICATION_NEAR_EXPIRY_LEAD_DAYS 30 Days before expiry at which to fire near-expiry.
MEDICATION_LOW_STOCK_THRESHOLD 10 Threshold below which net_content triggers low-stock.
MEDICATION_NEAR_EXPIRY_TITLE Medication near expiry: {product_name} Inbox title.
MEDICATION_NEAR_EXPIRY_BODY Expires {expiration_date}. Location: {location_name}. Inbox body.
MEDICATION_LOW_STOCK_TITLE Low stock: {product_name} Inbox title.
MEDICATION_LOW_STOCK_BODY Remaining {net_content}. Location: {location_name}. Inbox body.

Stock deliveries (in-app)

Key Default Description
STOCK_NOTIFICATIONS_ENABLED True Master switch for all stock delivery notifications.
STOCK_NOTIFY_DAMAGED True Notify when a SupplyDelivery's supplied_item_condition becomes damaged (on create or update). Sent to members of the orgs attached to the delivery order's destination location.
STOCK_NOTIFY_ABANDONED True Notify when a DeliveryOrder becomes abandoned, or when a single SupplyDelivery is abandoned while its order is still open. An abandoned order sends one alert, not one per line. Same recipients.
STOCK_DAMAGED_TITLE Damaged stock: {product_name} Inbox title.
STOCK_DAMAGED_BODY {quantity} marked damaged in {order_name}. Location: {location_name}. Inbox body.
STOCK_DELIVERY_ABANDONED_TITLE Delivery abandoned: {product_name} Inbox title.
STOCK_DELIVERY_ABANDONED_BODY {quantity} from {order_name} was abandoned. Location: {location_name}. Inbox body.
STOCK_ORDER_ABANDONED_TITLE Delivery order abandoned: {order_name} Inbox title.
STOCK_ORDER_ABANDONED_BODY Location: {location_name}. Inbox body.

Referrals (in-app)

Key Default Description
REFERRAL_NOTIFICATIONS_ENABLED True Master switch for all referral notifications.
REFERRAL_NOTIFY_RAISED True Notify when a referral is created. Sent to the referral's care team (assignee_users, copied from the referral availability's care team).
REFERRAL_RAISED_TITLE New referral: {patient_name} Inbox title.
REFERRAL_RAISED_BODY {referral_title} ({referral_to}) Inbox body.

Medication requests (in-app)

Key Default Description
MEDICATION_REQUEST_NOTIFICATIONS_ENABLED True Master switch for all medication request notifications.
MEDICATION_REQUEST_NOTIFY_DUPLICATE True When an MR becomes active and the patient already has an active / on_hold MR for the same requested_product (or the same medication code when there is none). Sent to the requesters of both MRs; if neither has one, to the encounter's care team.
MEDICATION_REQUEST_DUPLICATE_TITLE Duplicate prescription: {medication_name} Inbox title.
MEDICATION_REQUEST_DUPLICATE_BODY Patient: {patient_name}. Already active since {previous_date}. Inbox body.

Web push (in-app companion)

Every clinician in-app notification is also pushed to the user's subscribed browsers/devices via the Web Push protocol (pywebpush + VAPID), reusing the same title/body. Requires a frontend service worker to receive and display. Generate a keypair with the py-vapid CLI (ships with pywebpush): vapid --gen.

Key Default Description
WEBPUSH_NOTIFICATIONS_ENABLED True Master switch. When True, the three VAPID settings below are required at startup — the app refuses to boot without them.
WEBPUSH_VAPID_PUBLIC_KEY "" VAPID public key (base64url), served to the frontend so it can subscribe.
WEBPUSH_VAPID_PRIVATE_KEY "" VAPID private key (base64url), used to sign sends. Set via env / PLUGIN_CONFIGS, never commit.
WEBPUSH_VAPID_ADMIN_EMAIL "" Operator contact; becomes the mailto: sub claim in the VAPID JWT.

Message bodies are Python str.format templates. Available placeholders per event:

Event Placeholders
Booking (all 4) {patient_name}, {slot_start} (timezone-aware datetime; supports format specs like {slot_start:%d %b %H:%M})
Service request raised {patient_name}, {service_request_title}
Service request duplicate / repeat {patient_name}, {service_request_title}, {previous_date} (when the earlier SR was raised)
Medication request duplicate {patient_name}, {medication_name}, {previous_date} (when the earlier MR was authored)
Diagnostic report ready {patient_name}
Encounter IP created {patient_name}, {location_name}
Encounter bed transferred {patient_name}, {location_name} (new bed), {previous_location_name}
Encounter tag added {patient_name}, {tag_name}, {location_name} (current bed, empty if none)
Medication near expiry {product_name}, {expiration_date}, {location_name}, {net_content}
Medication low stock {product_name}, {net_content}, {location_name}
Supply delivery damaged / abandoned {product_name}, {quantity}, {order_name}, {location_name}
Delivery order abandoned {order_name}, {location_name}
Referral raised {patient_name}, {referral_title}, {referral_to} (the referral availability's name)

API

All endpoints are mounted under /api/care_notifications/.

Visibility:

  • Everyone, admins included, sees only their own inbox and web push subscriptions. mark_read, mark_unread and unsubscribe only ever touch the caller's own rows.
  • Admin oversight is explicit: GET /in_app_notifications/?recipient=<user external_id> returns that user's notifications, limited to facilities where the caller holds a facility role with can_list_all_notifications (Administrator / Admin / Facility Admin by default). Superusers see every facility.
  • outbound_notifications (SMS audit) follows the same facility scope; users without such a role get 403.

GET /in_app_notifications/

Inbox feed. Ordered via ?ordering= on created_date or modified_date (prefix with - for desc).

Filter Type Possible values
event_type string (iexact) booking_confirmation, booking_reminder, booking_cancellation, booking_reschedule, service_request_raised, diagnostic_report_ready, encounter_ip_created, encounter_bed_transferred, encounter_tag_added, medication_stock_near_expiry, medication_stock_low, supply_delivery_damaged, supply_delivery_abandoned, delivery_order_abandoned, referral_raised, service_request_duplicate, service_request_repeat, medication_request_duplicate
resource_type string (iexact) booking, service_request, diagnostic_report, encounter, medication_stock, delivery_order, referral, medication_request
resource_id UUID external_id of the underlying resource
recipient UUID external_id of the recipient user. Another user's id needs an admin role in the notification's facility (see Visibility)
unread bool true → only unread (read_at IS NULL); false → only read
facility UUID external_id of the facility — for a facility-scoped inbox (e.g. /facility/:id/notifications)

GET /in_app_notifications/{id}/

Retrieve a single notification (caller's own).

POST /in_app_notifications/mark_read/

Marks the given notifications as read. Body: {"ids": ["<external_id>", ...]}. Returns {"updated": <count>}.

POST /in_app_notifications/mark_unread/

Inverse of mark_read — clears read_at back to null. Same body shape.

GET /outbound_notifications/

SMS audit log. Same filter params as in-app (event_type, resource_type, resource_id); ordering on created_date or sent_at. Possible event_type values currently emitted via SMS: booking_confirmation, booking_reminder, booking_cancellation, booking_reschedule. resource_type: booking.

GET /outbound_notifications/{id}/

Retrieve a single outbound (SMS) record.

GET /web_push_subscriptions/

List the caller's registered Web Push device subscriptions (id, endpoint, created_date).

POST /web_push_subscriptions/

Register (upsert by endpoint) the current browser's subscription. Body is the browser's PushSubscription.toJSON() shape:

{ "endpoint": "https://...", "keys": { "p256dh": "<base64url>", "auth": "<base64url>" } }

Returns {id, endpoint, created_date}. Re-registering the same endpoint updates the existing row (one row per device).

POST /web_push_subscriptions/unsubscribe/

Opt a device out. Body: {"endpoint": "<endpoint>"}. Deletes the caller's matching subscription. Returns {"deleted": <count>}.

GET /web_push_subscriptions/vapid_public_key/

Returns {"public_key": "<WEBPUSH_VAPID_PUBLIC_KEY>"} for the frontend service worker to call pushManager.subscribe().

Click-through routing (frontend)

Every in-app and web-push notification carries the identifiers needed to deep-link to the underlying resource. The backend stores the semantic target; the frontend owns the route mapping — implement one shared resource_type → path function and use it from both the inbox click handler and the service worker's notificationclick.

Fields delivered

Both the in-app row (inbox API) and the web-push event.data carry the same target:

Field Location Notes
resource_type top-level one of encounter, service_request, diagnostic_report, medication_stock, booking, delivery_order, referral, medication_request
resource_id top-level the resource's external_id
facility_id top-level facility external_id. For referral it is the receiving facility; for a referral to an external organization it falls back to the referring encounter's facility, then the patient's most recent encounter's facility
payload.patient_id payload present for encounter, diagnostic_report, booking, referral, medication_request, and the SR duplicate / repeat events
payload.encounter_id payload present for medication_request
payload.previous_id payload present for duplicate / repeat events: the earlier SR / MR
payload.location_id payload present for medication_stock (this is the route target — see note), delivery_order (the destination location) and encounter_bed_transferred (the new bed)
payload.order_kind payload present for delivery_order: internal (location to location) or external (from a supplier)
payload.supply_delivery_id payload present for supply_delivery_damaged / supply_delivery_abandoned (the line; resource_id is its order)

(The patient booking-confirmation SMS has no click target; the user booking-confirmation in-app / web-push alert does — see booking below.)

resource_type → route

resource_type route
encounter /facility/{facility_id}/patient/{patient_id}/encounter/{resource_id}
service_request /facility/{facility_id}/service_requests/{resource_id}
diagnostic_report /facility/{facility_id}/patient/{patient_id}/diagnostic_reports/{resource_id}
medication_stock /facility/{facility_id}/locations/{location_id}
delivery_order internal: /facility/{facility_id}/locations/{location_id}/inventory/internal/receive/deliveries/{resource_id}; external: /facility/{facility_id}/locations/{location_id}/inventory/external/deliveries/incoming/{resource_id}
booking /facility/{facility_id}/patient/{patient_id}/appointments/{resource_id}
referral /referrals/{resource_id}
medication_request /facility/{facility_id}/patient/{patient_id}/encounter/{encounter_id}/medicines

Reference resolver

// Shared by the inbox click handler AND the service worker's notificationclick.
type Notification = {
  resource_type: string;
  resource_id: string;
  facility_id: string;
  payload?: Record<string, string>;
};

function notificationPath(n: Notification): string | null {
  const { resource_type, resource_id, facility_id, payload = {} } = n;
  switch (resource_type) {
    case "encounter":
      return `/facility/${facility_id}/patient/${payload.patient_id}/encounter/${resource_id}`;
    case "service_request":
      return `/facility/${facility_id}/service_requests/${resource_id}`;
    case "diagnostic_report":
      return `/facility/${facility_id}/patient/${payload.patient_id}/diagnostic_reports/${resource_id}`;
    case "medication_stock":
      return `/facility/${facility_id}/locations/${payload.location_id}`; 
    case "medication_request":
      return `/facility/${facility_id}/patient/${payload.patient_id}/encounter/${payload.encounter_id}/medicines`;
    case "referral":
      return `/referrals/${resource_id}`;
    case "delivery_order":
      return payload.order_kind === "internal"
        ? `/facility/${facility_id}/locations/${payload.location_id}/inventory/internal/receive/deliveries/${resource_id}`
        : `/facility/${facility_id}/locations/${payload.location_id}/inventory/external/deliveries/incoming/${resource_id}`;
    default:
      return null; 
  }
}

About

Plug for CARE that sends SMS notifications to patients on booking

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages