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

update patient bed switching logic #854

Merged
merged 3 commits into from
Jul 6, 2022
Merged
Changes from all 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
22 changes: 14 additions & 8 deletions care/facility/api/serializers/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,20 @@ def validate(self, attrs):
def create(self, validated_data):
consultation = validated_data["consultation"]
bed = validated_data["bed"]
current_bed = consultation.current_bed.bed if consultation.current_bed else None
existing_beds = ConsultationBed.objects.filter(
consultation=consultation,
bed=current_bed,
end_date__isnull=True,
)
existing_beds.update(end_date=validated_data["start_date"])

if not consultation.patient.is_active:
raise ValidationError(
{"patient:": ["Patient is already discharged from CARE"]})

occupied_beds = ConsultationBed.objects.filter(end_date__isnull=True)

if occupied_beds.filter(bed=bed).exists():
raise ValidationError({"bed:": ["Bed already in use by patient"]})

occupied_beds.filter(consultation=consultation).update(end_date=validated_data["start_date"])

# This needs better logic, when an update occurs and the latest bed is no longer the last bed consultation relation added.
obj = super().create(validated_data)
consultation.current_bed = obj # This needs better logic, when an update occurs and the latest bed is no longer the last bed consultation relation added.
consultation.current_bed = obj
consultation.save(update_fields=["current_bed"])
return obj