Skip to content

Commit

Permalink
Validations based of the latest entry (#731)
Browse files Browse the repository at this point in the history
* Validations based of the latest entry [not tested]

* add validation based on latest bed
  • Loading branch information
arpancodes authored Jul 7, 2022
1 parent 0247b81 commit f4914b8
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions care/facility/api/serializers/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ def validate(self, attrs):
raise ValidationError({"consultation": "Should be in the same facility as the bed"})
start_date = attrs["start_date"]
end_date = attrs.get("end_date", None)
existing_qs = ConsultationBed.objects.filter(consultation=consultation, bed=bed)
latest_qs = ConsultationBed.objects.filter(consultation=consultation).latest("id")
# Validations based of the latest entry
if latest_qs.exists():
if latest_qs.bed == bed:
raise ValidationError({"bed": "Bed is already in use"})
if start_date < latest_qs.start_date:
raise ValidationError({"start_date": "Start date cannot be before the latest start date"})
if end_date and end_date < latest_qs.start_date:
raise ValidationError({"end_date": "End date cannot be before the latest start date"})
existing_qs = ConsultationBed.objects.filter(consultation=consultation)
# Conflict checking logic
if existing_qs.filter(start_date__gt=start_date).exists():
Expand Down

0 comments on commit f4914b8

Please sign in to comment.