Skip to content

Commit d085c2a

Browse files
rash-27sainak
andauthored
add notifications for file upload , prescription (#2070)
Co-authored-by: Aakash Singh <[email protected]>
1 parent ac7f696 commit d085c2a

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

care/facility/api/serializers/file_upload.py

+21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from care.facility.api.serializers.shifting import has_facility_permission
77
from care.facility.models.facility import Facility
88
from care.facility.models.file_upload import FileUpload
9+
from care.facility.models.notification import Notification
910
from care.facility.models.patient import PatientRegistration
1011
from care.facility.models.patient_consultation import (
1112
PatientConsent,
@@ -14,6 +15,7 @@
1415
from care.facility.models.patient_sample import PatientSample
1516
from care.users.api.serializers.user import UserBaseMinimumSerializer
1617
from care.users.models import User
18+
from care.utils.notification_handler import NotificationGenerator
1719
from config.serializers import ChoiceField
1820

1921

@@ -156,12 +158,31 @@ def create(self, validated_data):
156158
internal_id = check_permissions(
157159
validated_data["file_type"], validated_data["associating_id"], user
158160
)
161+
associating_id = validated_data["associating_id"]
159162
validated_data["associating_id"] = internal_id
160163
validated_data["uploaded_by"] = user
161164
validated_data["internal_name"] = validated_data["original_name"]
162165
del validated_data["original_name"]
163166
file_upload: FileUpload = super().create(validated_data)
164167
file_upload.signed_url = file_upload.signed_url(mime_type=mime_type)
168+
if validated_data["file_type"] == FileUpload.FileType.CONSULTATION.value:
169+
consultation = PatientConsultation.objects.get(external_id=associating_id)
170+
NotificationGenerator(
171+
event=Notification.Event.CONSULTATION_FILE_UPLOAD_CREATED,
172+
caused_by=user,
173+
caused_object=consultation,
174+
facility=consultation.facility,
175+
generate_for_facility=True,
176+
).generate()
177+
if validated_data["file_type"] == FileUpload.FileType.PATIENT.value:
178+
patient = PatientRegistration.objects.get(external_id=associating_id)
179+
NotificationGenerator(
180+
event=Notification.Event.PATIENT_FILE_UPLOAD_CREATED,
181+
caused_by=user,
182+
caused_object=patient,
183+
facility=patient.facility,
184+
generate_for_facility=True,
185+
).generate()
165186
return file_upload
166187

167188

care/facility/api/viewsets/prescription.py

+17
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
PrescriptionType,
2222
generate_choices,
2323
)
24+
from care.facility.models.notification import Notification
2425
from care.facility.static_data.medibase import MedibaseMedicine
2526
from care.utils.filters.choicefilter import CareChoiceFilter
2627
from care.utils.filters.multiselect import MultiSelectFilter
28+
from care.utils.notification_handler import NotificationGenerator
2729
from care.utils.queryset.consultation import get_consultation_queryset
2830
from care.utils.static_data.helpers import query_builder, token_escaper
2931

@@ -120,6 +122,13 @@ def get_queryset(self):
120122

121123
def perform_create(self, serializer):
122124
consultation_obj = self.get_consultation_obj()
125+
NotificationGenerator(
126+
event=Notification.Event.PATIENT_PRESCRIPTION_CREATED,
127+
caused_by=self.request.user,
128+
caused_object=consultation_obj,
129+
facility=consultation_obj.facility,
130+
generate_for_facility=True,
131+
).generate()
123132
serializer.save(prescribed_by=self.request.user, consultation=consultation_obj)
124133

125134
@extend_schema(tags=["prescriptions"])
@@ -133,6 +142,14 @@ def discontinue(self, request, *args, **kwargs):
133142
prescription_obj.discontinued_reason = request.data.get(
134143
"discontinued_reason", None
135144
)
145+
consultation_obj = self.get_consultation_obj()
146+
NotificationGenerator(
147+
event=Notification.Event.PATIENT_PRESCRIPTION_UPDATED,
148+
caused_by=self.request.user,
149+
caused_object=consultation_obj,
150+
facility=consultation_obj.facility,
151+
generate_for_facility=True,
152+
).generate()
136153
prescription_obj.save()
137154
return Response({}, status=status.HTTP_200_OK)
138155

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Generated by Django 4.2.10 on 2024-07-18 10:19
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("facility", "0445_merge_20240715_0301"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="notification",
15+
name="event",
16+
field=models.IntegerField(
17+
choices=[
18+
(0, "MESSAGE"),
19+
(20, "PATIENT_CREATED"),
20+
(30, "PATIENT_UPDATED"),
21+
(40, "PATIENT_DELETED"),
22+
(50, "PATIENT_CONSULTATION_CREATED"),
23+
(60, "PATIENT_CONSULTATION_UPDATED"),
24+
(70, "PATIENT_CONSULTATION_DELETED"),
25+
(80, "INVESTIGATION_SESSION_CREATED"),
26+
(90, "INVESTIGATION_UPDATED"),
27+
(100, "PATIENT_FILE_UPLOAD_CREATED"),
28+
(110, "CONSULTATION_FILE_UPLOAD_CREATED"),
29+
(120, "PATIENT_CONSULTATION_UPDATE_CREATED"),
30+
(130, "PATIENT_CONSULTATION_UPDATE_UPDATED"),
31+
(140, "PATIENT_CONSULTATION_ASSIGNMENT"),
32+
(200, "SHIFTING_UPDATED"),
33+
(210, "PATIENT_NOTE_ADDED"),
34+
(220, "PUSH_MESSAGE"),
35+
(230, "PATIENT_PRESCRIPTION_CREATED"),
36+
(240, "PATIENT_PRESCRIPTION_UPDATED"),
37+
],
38+
default=0,
39+
),
40+
),
41+
]

care/facility/models/notification.py

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class Event(enum.Enum):
3939
SHIFTING_UPDATED = 200
4040
PATIENT_NOTE_ADDED = 210
4141
PUSH_MESSAGE = 220
42+
PATIENT_PRESCRIPTION_CREATED = 230
43+
PATIENT_PRESCRIPTION_UPDATED = 240
4244

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

care/utils/notification_handler.py

+16
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ def generate_system_message(self):
177177
message = "Patient {} was deleted by {}".format(
178178
self.caused_object.name, self.caused_by.get_full_name()
179179
)
180+
if self.event == Notification.Event.PATIENT_FILE_UPLOAD_CREATED.value:
181+
message = "A file for patient {} was uploaded by {}".format(
182+
self.caused_object.name, self.caused_by.get_full_name()
183+
)
180184
elif isinstance(self.caused_object, PatientConsultation):
181185
if self.event == Notification.Event.PATIENT_CONSULTATION_CREATED.value:
182186
message = "Consultation for Patient {} was created by {}".format(
@@ -190,6 +194,18 @@ def generate_system_message(self):
190194
message = "Consultation for Patient {} was deleted by {}".format(
191195
self.caused_object.patient.name, self.caused_by.get_full_name()
192196
)
197+
if self.event == Notification.Event.CONSULTATION_FILE_UPLOAD_CREATED.value:
198+
message = "Consultation file for Patient {} was uploaded by {}".format(
199+
self.caused_object.patient.name, self.caused_by.get_full_name()
200+
)
201+
if self.event == Notification.Event.PATIENT_PRESCRIPTION_CREATED.value:
202+
message = "Prescription for Patient {} was created by {}".format(
203+
self.caused_object.patient.name, self.caused_by.get_full_name()
204+
)
205+
if self.event == Notification.Event.PATIENT_PRESCRIPTION_UPDATED.value:
206+
message = "Prescription for Patient {} was updated by {}".format(
207+
self.caused_object.patient.name, self.caused_by.get_full_name()
208+
)
193209
elif isinstance(self.caused_object, InvestigationSession):
194210
if self.event == Notification.Event.INVESTIGATION_SESSION_CREATED.value:
195211
message = (

0 commit comments

Comments
 (0)