1
0
forked from erp-dev/erp

fix: exclude ran out state_id for api_v2.get_plate_orders_bet_by_state_status

This commit is contained in:
2025-12-25 19:11:06 +08:00
parent 6f6bcee761
commit 0ddf63f3c0
3 changed files with 32 additions and 2 deletions

View File

@@ -1165,6 +1165,21 @@ class PlateOrderByStateStatusV2APITest(TestCase):
ids = [item['id'] for item in resp.data['results']]
self.assertNotIn(self.po_other_process.id, ids)
def test_completed_status_only_returns_latest_state(self):
resp = self.client.get(
self.url,
{
'process_id': self.process.id,
'state_id': self.state_prepare.id,
'status': 'completed',
}
)
self.assertEqual(resp.status_code, 200)
ids = [item['id'] for item in resp.data['results']]
self.assertEqual(set(ids), {self.po_not_started.id, self.po_cancelled.id})
self.assertNotIn(self.po_completed_old.id, ids)
self.assertNotIn(self.po_completed_new.id, ids)
def test_without_state_id_returns_orders_without_any_logs(self):
resp = self.client.get(
self.url,

View File

@@ -3,7 +3,7 @@ import datetime
from django.utils import timezone
from django.db import transaction
from django.db import models as django_models
from django.db.models import Q, Count, CharField, Prefetch, Exists, OuterRef
from django.db.models import Q, Count, CharField, Prefetch, Exists, OuterRef, Subquery
from django.db.models.functions import Cast, Coalesce
from rest_framework import serializers, status, permissions
from rest_framework.pagination import LimitOffsetPagination
@@ -1092,6 +1092,21 @@ class PlateOrderByStateStatusView(APIView):
)
)
if status_value == 'completed':
latest_completed_state_subquery = (
stateflow_models.StateFlowRecord.objects
.filter(
business_object_id=OuterRef('business_object_id'),
is_cancelled=False,
)
.order_by('-completed_at', '-id')
)
queryset = queryset.annotate(
latest_completed_state_id=Subquery(
latest_completed_state_subquery.values('state_id')[:1]
)
).filter(latest_completed_state_id=state_id_int)
ordering = (qp.get('ordering') or '-created_at').strip() or '-created_at'
direction = '-' if ordering.startswith('-') else ''
field = ordering[1:] if ordering.startswith('-') else ordering