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

Validations based of the latest entry #731

Merged
merged 3 commits into from
Jul 7, 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
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")
Copy link
Member

Choose a reason for hiding this comment

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

This logic will crash if there is no latest bed. you need to add a block that only runs if the latest bed exists.

# 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