test(shipping): 更新订单复核库存与通知流程测试

This commit is contained in:
hugo committed 2026-09-04 20:53:06 +08:00
1 parent 8ec028e6a3
commit 265a6e179a
168 files changed
+9420 -4613

No files matched your search

@@ -18,7 +18,7 @@ import uuid
from flask import current_app
from app import db
from app.models import ShippingNotice, ShippingTaskEvent
from app.models import ShippingNotice, ShippingOrderTask, ShippingTaskEvent
from app.services.shipping_notice.constants import AUTO_CHAIN_FALLBACK
@@ -221,8 +221,11 @@ def set_task_step_progress(
detail: str | None = None,
extra: dict[str, Any] | None = None,
) -> None:
"""写入任务当前步骤与步骤内细粒度进度,统一落到 current_step + step_payload_json。"""
payload = dict(task.step_payload_json or {})
"""写入任务当前步骤与步骤内细粒度进度,统一落到 OT 的流程字段。"""
owner = task
if isinstance(task, ShippingNotice) and getattr(task, 'order_task_id', None):
owner = db.session.get(ShippingOrderTask, int(task.order_task_id)) or task
payload = dict(getattr(owner, 'step_payload_json', None) or {})
progress: dict[str, Any] = {
"step_key": step_key,
"step_title": step_title,
@@ -240,15 +243,23 @@ def set_task_step_progress(
if extra:
progress["extra"] = extra
payload["current_step_progress"] = progress
task.current_step = step_key
task.step_payload_json = payload
if hasattr(owner, 'current_step'):
owner.current_step = step_key
if hasattr(owner, 'stage'):
owner.stage = step_key
if hasattr(owner, 'step_payload_json'):
owner.step_payload_json = payload
def clear_task_step_progress(task: ShippingNotice) -> None:
"""清空任务步骤内进度,保留其他 step_payload_json 字段。"""
payload = dict(task.step_payload_json or {})
owner = task
if isinstance(task, ShippingNotice) and getattr(task, 'order_task_id', None):
owner = db.session.get(ShippingOrderTask, int(task.order_task_id)) or task
payload = dict(getattr(owner, 'step_payload_json', None) or {})
payload.pop("current_step_progress", None)
task.step_payload_json = payload or None
if hasattr(owner, 'step_payload_json'):
owner.step_payload_json = payload or None
@@ -307,7 +318,7 @@ def build_task_action_event_detail(
def log_task_event(
task: ShippingNotice,
task: ShippingNotice | ShippingOrderTask,
event_type: str,
detail: dict[str, Any] | None = None,
*,
@@ -333,10 +344,14 @@ def log_task_event(
before: 操作前业务快照。
after: 操作后业务快照。
"""
if task.id is None:
event_task_id = task_event_task_id(task)
if event_task_id is None:
db.session.flush()
event_task_id = task_event_task_id(task)
if event_task_id is None:
raise ValueError('shipping task event requires an order task id')
db.session.add(ShippingTaskEvent(
task_id=task.id,
task_id=event_task_id,
event_type=event_type,
detail_json=detail or {},
actor_id=actor_id,
@@ -436,6 +451,14 @@ def log_task_event(
)
def task_event_task_id(task: ShippingNotice | ShippingOrderTask) -> int | None:
"""将 Notice/OT 输入统一解析为 ShippingTaskEvent 使用的 OT 主键。"""
if isinstance(task, ShippingOrderTask):
return int(task.id) if task.id is not None else None
order_task_id = getattr(task, 'order_task_id', None)
return int(order_task_id) if order_task_id is not None else None
def _audit_reason_code_for_task_event(event_type: str, detail: dict[str, Any]) -> str | None:
"""
将任务事件类型映射为可解释性稳定 reason_code,供审计中心筛选。
@@ -505,7 +528,7 @@ def task_lifecycle_audit_snapshot(task: ShippingNotice) -> dict[str, Any]:
snapshot = {
'phase': getattr(task, 'current_step', None) or getattr(task, 'review_status', None),
'phase': getattr(task, 'current_step', None) or getattr(task, 'review_status', None),
'reason_code': getattr(task, 'pending_human_reason', None),
'reason_code': __import__('app.services.shipping_notice.process_gate_projection', fromlist=['task_blocking_reason']).task_blocking_reason(task),
}
return {key: value for key, value in snapshot.items() if value is not None}