Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added webpush on notes creation #1553

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion care/facility/api/viewsets/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@
)
from care.facility.models.base import covert_choice_dict
from care.facility.models.bed import AssetBed
from care.facility.models.facility import FacilityUser
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, send_webpush
from care.utils.queryset.patient import get_patient_notes_queryset
from config.authentication import (
CustomBasicAuthentication,
Expand Down Expand Up @@ -630,8 +633,31 @@ def perform_create(self, serializer):
raise ValidationError(
{"patient": "Only active patients data can be updated"}
)
return serializer.save(

message = {
"type": "MESSAGE",
"facility_id": str(patient.facility.external_id),
"patient_id": str(patient.external_id),
"status": "updated",
}

facility_users = FacilityUser.objects.filter(facility_id=patient.facility.id)
for facility_user in facility_users:
if facility_user.user.id != self.request.user.id:
send_webpush(username=facility_user.user, message=json.dumps(message))
Copy link
Member

@vigneshhari vigneshhari Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sainak is this call deterministic, if not can we move it to the notification generator, it sends the webpush from the worker.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is non-deterministic.

@Bhavik-ag can you move this to the notification generator?


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

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
37 changes: 37 additions & 0 deletions care/facility/migrations/0383_alter_notification_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 4.2.2 on 2023-09-06 08:56

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("facility", "0382_assetservice_remove_asset_last_serviced_on_and_more"),
]

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"),
],
default=0,
),
),
]
1 change: 1 addition & 0 deletions care/facility/models/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Event(enum.Enum):
PATIENT_CONSULTATION_UPDATE_UPDATED = 130
PATIENT_CONSULTATION_ASSIGNMENT = 140
SHIFTING_UPDATED = 200
PATIENT_NOTE_ADDED = 210

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

Expand Down
8 changes: 7 additions & 1 deletion 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 @@ -309,6 +309,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