From fefa1a97d9c08ac50cfcdd4379aa10f3088905d4 Mon Sep 17 00:00:00 2001 From: Ashesh <3626859+Ashesh3@users.noreply.github.com> Date: Wed, 22 Nov 2023 00:27:11 +0530 Subject: [PATCH] Add notifications for doctor notes (#1688) * Add notifications for doctor notes * Move type to PUSH_MESSAGE * move push event to bottom --- care/facility/api/viewsets/patient.py | 30 ++++++++++++++- .../0393_alter_notification_event.py | 38 +++++++++++++++++++ care/facility/models/notification.py | 2 + care/utils/notification_handler.py | 20 +++++++++- 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 care/facility/migrations/0393_alter_notification_event.py diff --git a/care/facility/api/viewsets/patient.py b/care/facility/api/viewsets/patient.py index afa23f9904..2601d8af8c 100644 --- a/care/facility/api/viewsets/patient.py +++ b/care/facility/api/viewsets/patient.py @@ -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, @@ -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 diff --git a/care/facility/migrations/0393_alter_notification_event.py b/care/facility/migrations/0393_alter_notification_event.py new file mode 100644 index 0000000000..6bdf7f7d10 --- /dev/null +++ b/care/facility/migrations/0393_alter_notification_event.py @@ -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, + ), + ), + ] diff --git a/care/facility/models/notification.py b/care/facility/models/notification.py index de2b1d59d8..e5bbdba8f3 100644 --- a/care/facility/models/notification.py +++ b/care/facility/models/notification.py @@ -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] diff --git a/care/utils/notification_handler.py b/care/utils/notification_handler.py index 677eb132d4..bbe942c1ce 100644 --- a/care/utils/notification_handler.py +++ b/care/utils/notification_handler.py @@ -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, @@ -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): @@ -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): @@ -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, } ), )