Skip to content

Commit

Permalink
adds parent_note field to PatientNotes model
Browse files Browse the repository at this point in the history
  • Loading branch information
UdaySagar-Git committed Oct 24, 2024
1 parent 9582da0 commit 53f18cb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
17 changes: 6 additions & 11 deletions care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,19 +501,11 @@ class PatientNotesSerializer(serializers.ModelSerializer):
reply_to_object = ReplyToPatientNoteSerializer(source="reply_to", read_only=True)
files = serializers.SerializerMethodField()
replies = ReplyToPatientNoteSerializer(many=True, read_only=True)
parent_note_object = serializers.SerializerMethodField()
parent_note_object = ReplyToPatientNoteSerializer(
source="parent_note", read_only=True
)
mentioned_users = UserBaseMinimumSerializer(many=True, read_only=True)

def get_parent_note_object(self, obj):
parent_note = obj
while parent_note.reply_to is not None:
parent_note = parent_note.reply_to
return (
ReplyToPatientNoteSerializer(parent_note).data
if parent_note != obj
else None
)

def get_files(self, obj):
from care.facility.api.serializers.file_upload import FileUploadListSerializer

Expand Down Expand Up @@ -560,6 +552,9 @@ def create(self, validated_data):
msg = "Reply to note should be in the same consultation"
raise serializers.ValidationError(msg)

# Set the parent_note to the parent of the reply_to_note if it exists else set it to the reply_to_note
validated_data["parent_note"] = reply_to_note.parent_note or reply_to_note

user = self.context["request"].user
note = validated_data.get("note")
with transaction.atomic():
Expand Down
19 changes: 19 additions & 0 deletions care/facility/migrations/0467_patientnotes_parent_note.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.1.1 on 2024-10-24 20:56

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('facility', '0466_alter_fileupload_file_type_alter_notification_event'),
]

operations = [
migrations.AddField(
model_name='patientnotes',
name='parent_note',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='facility.patientnotes'),
),
]
6 changes: 6 additions & 0 deletions care/facility/models/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,12 @@ class PatientNotes(FacilityBaseModel, ConsultationRelatedPermissionMixin):
related_name="replies",
)
note = models.TextField(default="", blank=True)
parent_note = models.ForeignKey(
"self",
on_delete=models.SET_NULL,
null=True,
blank=True,
)

def get_related_consultation(self):
# This is a temporary hack! this model does not have `assigned_to` field
Expand Down

0 comments on commit 53f18cb

Please sign in to comment.