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

Send asset downtime status along with asset details #1794

Merged
merged 7 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion care/facility/api/serializers/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,15 @@ class AssetSerializer(ModelSerializer):
last_serviced_on = serializers.DateField(write_only=True, required=False)
note = serializers.CharField(write_only=True, required=False, allow_blank=True)
resolved_middleware = ResolvedMiddlewareField(read_only=True)
latest_status = serializers.CharField(read_only=True)

class Meta:
model = Asset
exclude = ("deleted", "external_id", "current_location")
read_only_fields = TIMESTAMP_FIELDS + ("resolved_middleware",)
read_only_fields = TIMESTAMP_FIELDS + (
"resolved_middleware",
"latest_status",
)

def validate_qr_code_id(self, value):
value = value or None # treat empty string as null
Expand Down
9 changes: 8 additions & 1 deletion care/facility/api/viewsets/asset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf import settings
from django.core.cache import cache
from django.db.models import Exists, OuterRef, Q
from django.db.models import Exists, OuterRef, Q, Subquery
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.http import Http404
Expand Down Expand Up @@ -252,6 +252,13 @@ def get_queryset(self):
queryset = queryset.filter(
current_location__facility__id__in=allowed_facilities
)
queryset = queryset.annotate(
latest_status=Subquery(
AssetAvailabilityRecord.objects.filter(asset=OuterRef("pk"))
.order_by("-created_date")
.values("status")[:1]
)
)
return queryset

def list(self, request, *args, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions care/facility/tests/test_asset_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def test_create_asset_with_warranty_past(self):
def test_retrieve_asset(self):
response = self.client.get(f"/api/v1/asset/{self.asset.external_id}/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIn("latest_status", response.data)

def test_update_asset(self):
sample_data = {
Expand Down