diff --git a/care/facility/api/serializers/file_upload.py b/care/facility/api/serializers/file_upload.py index 399d7862ce..e1f2c77752 100644 --- a/care/facility/api/serializers/file_upload.py +++ b/care/facility/api/serializers/file_upload.py @@ -6,6 +6,7 @@ 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 ( PatientConsent, @@ -14,6 +15,7 @@ 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 @@ -156,12 +158,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 e8322aebee..c949e350e5 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 @@ -120,6 +122,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"]) @@ -133,6 +142,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/0446_alter_notification_event.py b/care/facility/migrations/0446_alter_notification_event.py new file mode 100644 index 0000000000..cc3bd00180 --- /dev/null +++ b/care/facility/migrations/0446_alter_notification_event.py @@ -0,0 +1,41 @@ +# Generated by Django 4.2.10 on 2024-07-18 10:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("facility", "0445_merge_20240715_0301"), + ] + + 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 = (