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']] ids = [item['id'] for item in resp.data['results']]
self.assertNotIn(self.po_other_process.id, ids) 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): def test_without_state_id_returns_orders_without_any_logs(self):
resp = self.client.get( resp = self.client.get(
self.url, self.url,

View File

@@ -3,7 +3,7 @@ import datetime
from django.utils import timezone from django.utils import timezone
from django.db import transaction from django.db import transaction
from django.db import models as django_models 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 django.db.models.functions import Cast, Coalesce
from rest_framework import serializers, status, permissions from rest_framework import serializers, status, permissions
from rest_framework.pagination import LimitOffsetPagination 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' ordering = (qp.get('ordering') or '-created_at').strip() or '-created_at'
direction = '-' if ordering.startswith('-') else '' direction = '-' if ordering.startswith('-') else ''
field = ordering[1:] if ordering.startswith('-') else ordering field = ordering[1:] if ordering.startswith('-') else ordering

View File

@@ -26,7 +26,7 @@
| `offset` | 否 | int | `0` | LimitOffsetPagination 的 offset | | `offset` | 否 | int | `0` | LimitOffsetPagination 的 offset |
> `status` 说明: > `status` 说明:
> - `completed`:该节点存在未撤销的 `StateFlowRecord` > - `completed`:该节点是订单最近一次未撤销完成的节点(同一订单不会因历史记录出现在多个节点)
> - `not_started`:从未对该节点留下任何 `StateFlowRecord` > - `not_started`:从未对该节点留下任何 `StateFlowRecord`
> - `cancelled`:最近一次执行已被撤销(存在 `is_cancelled=True` 的记录,且无未撤销记录) > - `cancelled`:最近一次执行已被撤销(存在 `is_cancelled=True` 的记录,且无未撤销记录)
> - `in_progress`:为后续扩展保留(当前流程模型中不会命中) > - `in_progress`:为后续扩展保留(当前流程模型中不会命中)