fix(shipping-notice): 修复流程门禁审计与库存匹配同步
This commit is contained in:
1 parent
fbca0b485c
commit
9745bb78a7
14 files changed
+832
-154
No files matched your search
@@ -2,9 +2,10 @@
|
||||
backend/app/services/shipping_notice/fulfillment_audit.py
|
||||
|
||||
用途:履约关键节点与敏感访问写入统一审计事件的轻量封装。
|
||||
依赖:audit_service.append_shipping_audit_event、Flask g.request_id / request、AppError。
|
||||
依赖:audit_service.append_shipping_audit_event、Flask g.request_id / request、AppError、db.session。
|
||||
逻辑:由 candidate / decision / fulfillment_workflow / workspace 读取与导出 / Workflow 写路径调用;
|
||||
默认 strict=True,审计写入失败时抛错,禁止业务成功而审计缺失;访问审计逐次留痕;
|
||||
默认 strict=True;审计写入包在 SAVEPOINT 内,失败不污染外层业务事务;
|
||||
strict=False 时仅记日志(门禁路径用,禁止审计打断匹配等业务结果);
|
||||
自动附带 request_id 与客户端 IP/UA(仅哈希入库);透传 Workflow 软引用字段。
|
||||
"""
|
||||
|
||||
@@ -13,6 +14,7 @@ from __future__ import annotations
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from app import db
|
||||
from app.core.errors import ServiceUnavailableError
|
||||
from app.services.shipping_notice.audit_service import append_shipping_audit_event
|
||||
|
||||
@@ -93,7 +95,8 @@ def record_fulfillment_audit(
|
||||
"""
|
||||
追加履约审计。
|
||||
|
||||
核心逻辑:默认与业务同事务写入;strict=True 时失败抛错,保证必须留存;
|
||||
核心逻辑:审计写入包在 SAVEPOINT 内,与外层业务状态隔离;strict=True 时失败抛错,
|
||||
但已回滚审计子事务,不把会话留在失败态;strict=False 时仅记日志,业务继续。
|
||||
自动附带 request_id 与客户端 IP/UA 哈希;透传 Workflow 软引用。
|
||||
|
||||
Args:
|
||||
@@ -102,49 +105,55 @@ def record_fulfillment_audit(
|
||||
before/after: 必须已是白名单可投影的状态摘要。
|
||||
error_code: 拒绝/失败时的稳定业务错误码。
|
||||
sensitivity_level: 可选敏感分级(如 sensitive),透传审计表。
|
||||
strict: True 时审计失败阻断调用方;False 时仅记日志(仅限显式降级场景)。
|
||||
strict: True 时审计失败阻断调用方;False 时仅记日志(门禁等业务关键路径)。
|
||||
"""
|
||||
request_id = _request_id()
|
||||
client_ip, user_agent = _client_request_context()
|
||||
try:
|
||||
append_shipping_audit_event(
|
||||
event_type=event_type,
|
||||
aggregate_type=aggregate_type,
|
||||
aggregate_id=str(aggregate_id),
|
||||
action=action,
|
||||
actor_type=actor_type,
|
||||
actor_id=actor_id,
|
||||
request_id=request_id,
|
||||
operation_id=request_id,
|
||||
run_no=run_no,
|
||||
task_no=task_no,
|
||||
document_id=document_id,
|
||||
review_case_no=review_case_no,
|
||||
conversation_id=conversation_id,
|
||||
workflow_id=workflow_id,
|
||||
workflow_item_id=workflow_item_id,
|
||||
workflow_item_version_id=workflow_item_version_id,
|
||||
version_no=version_no,
|
||||
owner_user_id=owner_user_id,
|
||||
client_ip=client_ip,
|
||||
user_agent=user_agent,
|
||||
reason_code=reason_code,
|
||||
reason=reason,
|
||||
result=result,
|
||||
error_code=error_code,
|
||||
before=before,
|
||||
after=after,
|
||||
metadata=metadata or {},
|
||||
source=source,
|
||||
sensitivity_level=sensitivity_level,
|
||||
)
|
||||
# [STEP] SAVEPOINT:审计 INSERT 失败不得污染外层业务事务(否则匹配等结果会整单回滚)。
|
||||
with db.session.begin_nested():
|
||||
append_shipping_audit_event(
|
||||
event_type=event_type,
|
||||
aggregate_type=aggregate_type,
|
||||
aggregate_id=str(aggregate_id),
|
||||
action=action,
|
||||
actor_type=actor_type,
|
||||
actor_id=actor_id,
|
||||
request_id=request_id,
|
||||
operation_id=request_id,
|
||||
run_no=run_no,
|
||||
task_no=task_no,
|
||||
document_id=document_id,
|
||||
review_case_no=review_case_no,
|
||||
conversation_id=conversation_id,
|
||||
workflow_id=workflow_id,
|
||||
workflow_item_id=workflow_item_id,
|
||||
workflow_item_version_id=workflow_item_version_id,
|
||||
version_no=version_no,
|
||||
owner_user_id=owner_user_id,
|
||||
client_ip=client_ip,
|
||||
user_agent=user_agent,
|
||||
reason_code=reason_code,
|
||||
reason=reason,
|
||||
result=result,
|
||||
error_code=error_code,
|
||||
before=before,
|
||||
after=after,
|
||||
metadata=metadata or {},
|
||||
source=source,
|
||||
sensitivity_level=sensitivity_level,
|
||||
)
|
||||
except Exception as exc:
|
||||
_LOGGER.exception('fulfillment audit append failed event_type=%s aggregate_id=%s', event_type, aggregate_id)
|
||||
if strict:
|
||||
raise ServiceUnavailableError(
|
||||
'Shipping audit persistence failed',
|
||||
f'Shipping audit persistence failed: {exc}',
|
||||
code='SHIPPING_NOTICE.AUDIT_PERSIST_FAILED',
|
||||
details={'event_type': event_type, 'aggregate_id': str(aggregate_id)},
|
||||
details={
|
||||
'event_type': event_type,
|
||||
'aggregate_id': str(aggregate_id),
|
||||
'cause': str(exc)[:500],
|
||||
},
|
||||
) from exc
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user