Skip to content

Commit

Permalink
Fix Bed Validation, End-Date; Add Bed to Admin (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
gigincg authored May 19, 2022
1 parent 8727d01 commit e4f295c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion care/facility/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from care.facility.models.ambulance import Ambulance, AmbulanceDriver
from care.facility.models.asset import Asset
from care.facility.models.bed import AssetBed
from care.facility.models.bed import AssetBed, Bed
from care.facility.models.patient_sample import PatientSample
from care.facility.models.patient_tele_consultation import PatientTeleConsultation

Expand Down Expand Up @@ -211,3 +211,4 @@ class FacilityUserAdmin(DjangoQLSearchMixin, admin.ModelAdmin, ExportCsvMixin):
admin.site.register(PatientInvestigationGroup, PatientTestGroupAdmin)
admin.site.register(AssetBed)
admin.site.register(Asset)
admin.site.register(Bed)
21 changes: 14 additions & 7 deletions care/facility/api/serializers/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ class ConsultationBedSerializer(ModelSerializer):

bed_object = BedSerializer(source="bed", read_only=True)

consultation = ExternalIdSerializerField(
queryset=PatientConsultation.objects.all(), write_only=True, required=True
)
consultation = ExternalIdSerializerField(queryset=PatientConsultation.objects.all(), write_only=True, required=True)
bed = ExternalIdSerializerField(queryset=Bed.objects.all(), write_only=True, required=True)

class Meta:
Expand All @@ -107,23 +105,32 @@ 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)
existing_qs = ConsultationBed.objects.filter(consultation=consultation)
# Conflict checking logic
if existing_qs.filter(start_date__gt=start_date, end_date__lt=start_date).exists():
if existing_qs.filter(start_date__gt=start_date).exists():
raise ValidationError({"start_date": "Cannot create conflicting entry"})
if end_date:
if existing_qs.filter(start_date__gt=end_date, end_date__lt=end_date).exists():
raise ValidationError({"end_date": "Cannot create conflicting entry"})
else:
raise ValidationError(
{"consultation": "Field is Required", "bed": "Field is Required", "start_date": "Field is Required",}
{
"consultation": "Field is Required",
"bed": "Field is Required",
"start_date": "Field is Required",
}
)
return super().validate(attrs)

def create(self, validated_data):
consultation = validated_data["consultation"]
bed = validated_data["bed"]
existing_beds = ConsultationBed.objects.filter(consultation=consultation, bed=bed, end_date__isnull=True)
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"])
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.
Expand Down

0 comments on commit e4f295c

Please sign in to comment.