From dbab2ca73b85a430ae7f981c1cd39ef8d7d8ebfd Mon Sep 17 00:00:00 2001 From: rash-27 Date: Fri, 12 Apr 2024 02:23:10 +0530 Subject: [PATCH 1/2] add notifications --- care/facility/api/serializers/file_upload.py | 21 ++++++++++ care/facility/api/viewsets/prescription.py | 17 ++++++++ .../0426_alter_notification_event.py | 40 +++++++++++++++++++ care/facility/models/notification.py | 2 + care/utils/notification_handler.py | 16 ++++++++ 5 files changed, 96 insertions(+) create mode 100644 care/facility/migrations/0426_alter_notification_event.py diff --git a/care/facility/api/serializers/file_upload.py b/care/facility/api/serializers/file_upload.py index e991cf045a..fd6ffa455a 100644 --- a/care/facility/api/serializers/file_upload.py +++ b/care/facility/api/serializers/file_upload.py @@ -6,11 +6,13 @@ from care.facility.api.serializers.shifting import has_facility_permission from care.facility.models.facility import Facility from care.facility.models.file_upload import FileUpload +from care.facility.models.notification import Notification from care.facility.models.patient import PatientRegistration from care.facility.models.patient_consultation import PatientConsultation from care.facility.models.patient_sample import PatientSample from care.users.api.serializers.user import UserBaseMinimumSerializer from care.users.models import User +from care.utils.notification_handler import NotificationGenerator from config.serializers import ChoiceField @@ -153,12 +155,31 @@ def create(self, validated_data): internal_id = check_permissions( validated_data["file_type"], validated_data["associating_id"], user ) + associating_id = validated_data["associating_id"] validated_data["associating_id"] = internal_id validated_data["uploaded_by"] = user validated_data["internal_name"] = validated_data["original_name"] del validated_data["original_name"] file_upload: FileUpload = super().create(validated_data) file_upload.signed_url = file_upload.signed_url(mime_type=mime_type) + if validated_data["file_type"] == FileUpload.FileType.CONSULTATION.value: + consultation = PatientConsultation.objects.get(external_id=associating_id) + NotificationGenerator( + event=Notification.Event.CONSULTATION_FILE_UPLOAD_CREATED, + caused_by=user, + caused_object=consultation, + facility=consultation.facility, + generate_for_facility=True, + ).generate() + if validated_data["file_type"] == FileUpload.FileType.PATIENT.value: + patient = PatientRegistration.objects.get(external_id=associating_id) + NotificationGenerator( + event=Notification.Event.PATIENT_FILE_UPLOAD_CREATED, + caused_by=user, + caused_object=patient, + facility=patient.facility, + generate_for_facility=True, + ).generate() return file_upload diff --git a/care/facility/api/viewsets/prescription.py b/care/facility/api/viewsets/prescription.py index 537faae98c..85bd0b3a64 100644 --- a/care/facility/api/viewsets/prescription.py +++ b/care/facility/api/viewsets/prescription.py @@ -21,9 +21,11 @@ PrescriptionType, generate_choices, ) +from care.facility.models.notification import Notification from care.facility.static_data.medibase import MedibaseMedicine 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.consultation import get_consultation_queryset from care.utils.static_data.helpers import query_builder, token_escaper @@ -119,6 +121,13 @@ def get_queryset(self): def perform_create(self, serializer): consultation_obj = self.get_consultation_obj() + NotificationGenerator( + event=Notification.Event.PATIENT_PRESCRIPTION_CREATED, + caused_by=self.request.user, + caused_object=consultation_obj, + facility=consultation_obj.facility, + generate_for_facility=True, + ).generate() serializer.save(prescribed_by=self.request.user, consultation=consultation_obj) @extend_schema(tags=["prescriptions"]) @@ -132,6 +141,14 @@ def discontinue(self, request, *args, **kwargs): prescription_obj.discontinued_reason = request.data.get( "discontinued_reason", None ) + consultation_obj = self.get_consultation_obj() + NotificationGenerator( + event=Notification.Event.PATIENT_PRESCRIPTION_UPDATED, + caused_by=self.request.user, + caused_object=consultation_obj, + facility=consultation_obj.facility, + generate_for_facility=True, + ).generate() prescription_obj.save() return Response({}, status=status.HTTP_200_OK) diff --git a/care/facility/migrations/0426_alter_notification_event.py b/care/facility/migrations/0426_alter_notification_event.py new file mode 100644 index 0000000000..bda4131a8f --- /dev/null +++ b/care/facility/migrations/0426_alter_notification_event.py @@ -0,0 +1,40 @@ +# Generated by Django 4.2.10 on 2024-04-11 19:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("facility", "0425_merge_20240403_2055"), + ] + + 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"), + (230, "PATIENT_PRESCRIPTION_CREATED"), + (240, "PATIENT_PRESCRIPTION_UPDATED"), + ], + default=0, + ), + ), + ] diff --git a/care/facility/models/notification.py b/care/facility/models/notification.py index e5bbdba8f3..420699d646 100644 --- a/care/facility/models/notification.py +++ b/care/facility/models/notification.py @@ -39,6 +39,8 @@ class Event(enum.Enum): SHIFTING_UPDATED = 200 PATIENT_NOTE_ADDED = 210 PUSH_MESSAGE = 220 + PATIENT_PRESCRIPTION_CREATED = 230 + PATIENT_PRESCRIPTION_UPDATED = 240 EventChoices = [(e.value, e.name) for e in Event] diff --git a/care/utils/notification_handler.py b/care/utils/notification_handler.py index bbb910fb7f..ea38baf668 100644 --- a/care/utils/notification_handler.py +++ b/care/utils/notification_handler.py @@ -177,6 +177,10 @@ def generate_system_message(self): message = "Patient {} was deleted by {}".format( self.caused_object.name, self.caused_by.get_full_name() ) + if self.event == Notification.Event.PATIENT_FILE_UPLOAD_CREATED.value: + message = "A file for patient {} was uploaded by {}".format( + self.caused_object.name, self.caused_by.get_full_name() + ) elif isinstance(self.caused_object, PatientConsultation): if self.event == Notification.Event.PATIENT_CONSULTATION_CREATED.value: message = "Consultation for Patient {} was created by {}".format( @@ -190,6 +194,18 @@ def generate_system_message(self): message = "Consultation for Patient {} was deleted by {}".format( self.caused_object.patient.name, self.caused_by.get_full_name() ) + if self.event == Notification.Event.CONSULTATION_FILE_UPLOAD_CREATED.value: + message = "Consultation file for Patient {} was uploaded by {}".format( + self.caused_object.patient.name, self.caused_by.get_full_name() + ) + if self.event == Notification.Event.PATIENT_PRESCRIPTION_CREATED.value: + message = "Prescription for Patient {} was created by {}".format( + self.caused_object.patient.name, self.caused_by.get_full_name() + ) + if self.event == Notification.Event.PATIENT_PRESCRIPTION_UPDATED.value: + message = "Prescription for Patient {} was updated by {}".format( + self.caused_object.patient.name, self.caused_by.get_full_name() + ) elif isinstance(self.caused_object, InvestigationSession): if self.event == Notification.Event.INVESTIGATION_SESSION_CREATED.value: message = ( From b7985006e223ea6ab1af5f7179b82ada8c87a30d Mon Sep 17 00:00:00 2001 From: Aakash Singh Date: Thu, 18 Jul 2024 15:49:45 +0530 Subject: [PATCH 2/2] update migrations --- ...otification_event.py => 0446_alter_notification_event.py} | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) rename care/facility/migrations/{0426_alter_notification_event.py => 0446_alter_notification_event.py} (93%) diff --git a/care/facility/migrations/0426_alter_notification_event.py b/care/facility/migrations/0446_alter_notification_event.py similarity index 93% rename from care/facility/migrations/0426_alter_notification_event.py rename to care/facility/migrations/0446_alter_notification_event.py index bda4131a8f..cc3bd00180 100644 --- a/care/facility/migrations/0426_alter_notification_event.py +++ b/care/facility/migrations/0446_alter_notification_event.py @@ -1,11 +1,12 @@ -# Generated by Django 4.2.10 on 2024-04-11 19:16 +# Generated by Django 4.2.10 on 2024-07-18 10:19 from django.db import migrations, models class Migration(migrations.Migration): + dependencies = [ - ("facility", "0425_merge_20240403_2055"), + ("facility", "0445_merge_20240715_0301"), ] operations = [