Skip to content

Commit

Permalink
Add notifications for doctor notes (#1688)
Browse files Browse the repository at this point in the history
* Add notifications for doctor notes

* Move type to PUSH_MESSAGE

* move push event to bottom
  • Loading branch information
Ashesh3 authored Nov 21, 2023
1 parent fa5ba42 commit fefa1a9
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
30 changes: 29 additions & 1 deletion care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@
)
from care.facility.models.base import covert_choice_dict
from care.facility.models.bed import AssetBed
from care.facility.models.notification import Notification
from care.facility.models.patient_base import DISEASE_STATUS_DICT
from care.users.models import User
from care.utils.cache.cache_allowed_facilities import get_accessible_facilities
from care.utils.filters.choicefilter import CareChoiceFilter
from care.utils.filters.multiselect import MultiSelectFilter
from care.utils.notification_handler import NotificationGenerator
from care.utils.queryset.patient import get_patient_notes_queryset
from config.authentication import (
CustomBasicAuthentication,
Expand Down Expand Up @@ -665,8 +667,34 @@ def perform_create(self, serializer):
raise ValidationError(
{"patient": "Only active patients data can be updated"}
)
return serializer.save(

instance = serializer.save(
facility=patient.facility,
patient=patient,
created_by=self.request.user,
)

message = {
"facility_id": str(patient.facility.external_id),
"patient_id": str(patient.external_id),
"from": "patient/doctor_notes/create",
}

NotificationGenerator(
event=Notification.Event.PUSH_MESSAGE,
caused_by=self.request.user,
caused_object=instance,
message=message,
facility=patient.facility,
generate_for_facility=True,
).generate()

NotificationGenerator(
event=Notification.Event.PATIENT_NOTE_ADDED,
caused_by=self.request.user,
caused_object=instance,
facility=patient.facility,
generate_for_facility=True,
).generate()

return instance
38 changes: 38 additions & 0 deletions care/facility/migrations/0393_alter_notification_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.2.6 on 2023-11-03 08:55

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("facility", "0392_alter_dailyround_consciousness_level"),
]

operations = [
migrations.AlterField(
model_name="notification",
name="event",
field=models.IntegerField(
choices=[
(0, "MESSAGE"),
(20, "PATIENT_CREATED"),
(30, "PATIENT_UPDATED"),
(40, "PATIENT_DELETED"),
(50, "PATIENT_CONSULTATION_CREATED"),
(60, "PATIENT_CONSULTATION_UPDATED"),
(70, "PATIENT_CONSULTATION_DELETED"),
(80, "INVESTIGATION_SESSION_CREATED"),
(90, "INVESTIGATION_UPDATED"),
(100, "PATIENT_FILE_UPLOAD_CREATED"),
(110, "CONSULTATION_FILE_UPLOAD_CREATED"),
(120, "PATIENT_CONSULTATION_UPDATE_CREATED"),
(130, "PATIENT_CONSULTATION_UPDATE_UPDATED"),
(140, "PATIENT_CONSULTATION_ASSIGNMENT"),
(200, "SHIFTING_UPDATED"),
(210, "PATIENT_NOTE_ADDED"),
(220, "PUSH_MESSAGE"),
],
default=0,
),
),
]
2 changes: 2 additions & 0 deletions care/facility/models/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Event(enum.Enum):
PATIENT_CONSULTATION_UPDATE_UPDATED = 130
PATIENT_CONSULTATION_ASSIGNMENT = 140
SHIFTING_UPDATED = 200
PATIENT_NOTE_ADDED = 210
PUSH_MESSAGE = 220

EventChoices = [(e.value, e.name) for e in Event]

Expand Down
20 changes: 18 additions & 2 deletions care/utils/notification_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from care.facility.models.daily_round import DailyRound
from care.facility.models.facility import Facility, FacilityUser
from care.facility.models.notification import Notification
from care.facility.models.patient import PatientRegistration
from care.facility.models.patient import PatientNotes, PatientRegistration
from care.facility.models.patient_consultation import PatientConsultation
from care.facility.models.patient_investigation import (
InvestigationSession,
Expand Down Expand Up @@ -230,6 +230,13 @@ def generate_system_message(self):
self.caused_object.patient.name,
self.caused_by.get_full_name(),
)
elif isinstance(self.caused_object, PatientNotes):
if self.event == Notification.Event.PATIENT_NOTE_ADDED.value:
message = "Notes for Patient {} was added by {}".format(
self.caused_object.patient.name,
self.caused_by.get_full_name(),
)

return message

def generate_sms_message(self):
Expand Down Expand Up @@ -309,6 +316,12 @@ def generate_cause_objects(self):
if isinstance(self.caused_object, ShiftingRequest):
self.caused_objects["shifting"] = str(self.caused_object.external_id)

if isinstance(self.caused_object, PatientNotes):
self.caused_objects["patient"] = str(self.caused_object.patient.external_id)
self.caused_objects["facility"] = str(
self.caused_object.facility.external_id
)

return True

def generate_system_users(self):
Expand Down Expand Up @@ -390,7 +403,10 @@ def generate(self):
json.dumps(
{
"external_id": str(notification_obj.external_id),
"title": self.message,
"message": self.message,
"type": Notification.Event(
notification_obj.event
).name,
}
),
)

0 comments on commit fefa1a9

Please sign in to comment.