feat(in-transit-transfer): 外部调用切换集成连接器并新增调拨类型与在途仓编码

- 退役 erp_adapter/gateway_adapter:外部查询/写经 capability_client 走集成连接器,端点凭据与并发超时由客户端实例管理
- 域能力代码注册 capabilities:入参出参 Schema、幂等语义与读写标志随域代码版本化,组合根 create_app 幂等注册
- 新增调拨业务类型 transfer_type(在途仓至HUB/HUB维修出/HUB间)冻结枚举,迁移落 CheckConstraint 数据库兜底
- 外部仓库映射新增 in_transit_warehouse_code 拨出库位来源,管理端库存映射页同步维护
- 单据命名外部化:erp_* 字段与索引改称 external_*,单据号格式交由连接器映射,测试数据同步换牌
- 收料归档文件可用性判定收敛 receipt_file_gate 单一事实来源(当天抓取/保留期/清理态)
- 同步 chat 调拨卡片、useChatTransfer、e2e、全套服务/API/Job 测试与域文档
This commit is contained in:
hugo committed 2026-09-23 00:49:38 +08:00
1 parent 31591e663e
commit 9959b06acd
97 files changed
+3525 -1489

No files matched your search

@@ -29,8 +29,8 @@ def _jwt_headers(user: User) -> dict:
def transfer_deps(monkeypatch, dbs_ready_file):
"""注入 Fake 预检依赖,并预置可执行库存。"""
deps, erp, _ = make_transfer_deps(dbs_ready_file)
erp.seed_inventory("INV-001", "1200")
erp.seed_inventory("INV-002", "1200")
erp.seed_stock("MAT-001", "DBS-TRANSIT", "LOT-10", "1200")
erp.seed_stock("MAT-002", "DBS-TRANSIT", "LOT-20", "1200")
monkeypatch.setattr("app.services.in_transit_transfer.deps._override", deps)
return deps
@@ -99,7 +99,7 @@ def test_create_precheck_returns_envelope_with_precheck_id(app, client, admin_he
data = body["data"]
assert data["precheck_id"].startswith("TPC-")
assert data["precheck_hash"].startswith("sha256:")
assert data["estimated_erp_write_count"] == 2
assert data["estimated_external_write_count"] == 2
assert body.get("request_id")
@@ -136,6 +136,25 @@ def test_create_precheck_validates_source_codes(app, client, admin_headers, tran
assert response.get_json()["success"] is False
def test_create_precheck_accepts_transfer_type_and_rejects_placeholder(app, client, admin_headers, transfer_deps):
"""transfer_type 合法值放行;占位类型(类型2/3)与非法值 → 400 fail-closed。"""
created = client.post(f"{API_PREFIX}/prechecks", headers=admin_headers, json={
"source_codes": ["DBS-AWSBI"],
"business_date": "2026-08-06",
"transfer_type": "in_transit_to_hub",
})
assert created.status_code == 201
for bad_type in ("hub_repair_out", "hub_to_hub", "bogus_type"):
response = client.post(f"{API_PREFIX}/prechecks", headers=admin_headers, json={
"source_codes": ["DBS-AWSBI"],
"business_date": "2026-08-06",
"transfer_type": bad_type,
})
assert response.status_code == 400, bad_type
assert response.get_json()["success"] is False
def test_restore_precheck_progress(app, client, admin_headers, transfer_deps):
"""GET /prechecks/{id} 恢复预检进度(Hash/明细一致)。"""
created = client.post(f"{API_PREFIX}/prechecks", headers=admin_headers, json={
@@ -146,7 +165,7 @@ def test_restore_precheck_progress(app, client, admin_headers, transfer_deps):
assert restored.status_code == 200
body = restored.get_json()["data"]
assert body["summary"]["precheck_hash"] == created["precheck_hash"]
assert {item["invoice_no"] for item in body["items"]} == {"INV-001", "INV-002"}
assert {item["invoice_no"] for item in body["items"]} == {"HOM-001", "HOX-002"}
def test_restore_unknown_precheck_404(app, client, admin_headers, transfer_deps):
@@ -156,14 +175,14 @@ def test_restore_unknown_precheck_404(app, client, admin_headers, transfer_deps)
def test_execute_precheck_creates_run_and_projects_documents(app, client, admin_headers, transfer_deps):
"""执行预检 → 201 run_no;GET run 返回 ERP 单号聚合结果。"""
"""执行预检 → 201 run_no;GET run 返回 外部系统 单号聚合结果。"""
created = client.post(f"{API_PREFIX}/prechecks", headers=admin_headers, json={
"source_codes": ["DBS-AWSBI"],
"business_date": "2026-08-06",
}).get_json()["data"]
executed = client.post(
f"{API_PREFIX}/prechecks/{created['precheck_id']}/execute",
headers=admin_headers, json={"invoice_nos": ["INV-001", "INV-002"]},
headers=admin_headers, json={"invoice_nos": ["HOM-001", "HOX-002"]},
)
assert executed.status_code == 201
run_no = executed.get_json()["data"]["run_no"]
@@ -172,7 +191,7 @@ def test_execute_precheck_creates_run_and_projects_documents(app, client, admin_
run = client.get(f"{API_PREFIX}/runs/{run_no}", headers=admin_headers)
assert run.status_code == 200
result = run.get_json()["data"]["result"]
assert len(result["erp_documents"]) == 2
assert len(result["external_documents"]) == 2
assert result["exceptions"] == []
@@ -190,7 +209,7 @@ def test_execute_unconfirmed_precheck_rejected(app, client, admin_headers, trans
db.session.commit()
response = client.post(
f"{API_PREFIX}/prechecks/{created['precheck_id']}/execute",
headers=admin_headers, json={"invoice_nos": ["INV-001"]},
headers=admin_headers, json={"invoice_nos": ["HOM-001"]},
)
assert response.status_code == 400
assert response.get_json()["error"]["code"] == "TRANSFER_CONFIRMATION_REQUIRED"
@@ -219,7 +238,7 @@ def test_cancel_run_marks_remaining_cancelled(app, client, admin_headers, transf
}).get_json()["data"]
executed = client.post(
f"{API_PREFIX}/prechecks/{created['precheck_id']}/execute",
headers=admin_headers, json={"invoice_nos": ["INV-001", "INV-002"]},
headers=admin_headers, json={"invoice_nos": ["HOM-001", "HOX-002"]},
)
run_no = executed.get_json()["data"]["run_no"]
# 模拟执行中状态:Run 回退 executing,INV-002 尚未开始
@@ -228,9 +247,9 @@ def test_cancel_run_marks_remaining_cancelled(app, client, admin_headers, transf
run = TransferRun.query.filter_by(run_no=run_no).first()
run.status = "executing"
item = TransferInvoiceItem.query.filter_by(run_id=run.id, invoice_no="INV-002").first()
item = TransferInvoiceItem.query.filter_by(run_id=run.id, invoice_no="HOX-002").first()
item.status = "ready_to_execute"
item.erp_document_no = None
item.external_document_no = None
db.session.commit()
response = client.post(f"{API_PREFIX}/runs/{run_no}/cancel", headers=admin_headers, json={
"reason": "货源调整,停止剩余",
@@ -248,7 +267,7 @@ def test_manual_close_item_requires_reason(app, client, admin_headers, transfer_
with app.app_context():
from app.models import TransferInvoiceItem
item = TransferInvoiceItem.query.filter_by(invoice_no="INV-001").first()
item = TransferInvoiceItem.query.filter_by(invoice_no="HOM-001").first()
item.status = "blocked_recoverable"
item.result_code = "blocked_quantity_mismatch"
db.session.commit()
@@ -266,7 +285,7 @@ def test_manual_close_item_success(app, client, admin_headers, transfer_deps):
with app.app_context():
from app.models import TransferInvoiceItem
item = TransferInvoiceItem.query.filter_by(invoice_no="INV-001").first()
item = TransferInvoiceItem.query.filter_by(invoice_no="HOM-001").first()
item.status = "blocked_recoverable"
item.result_code = "blocked_quantity_mismatch"
db.session.commit()
@@ -286,7 +305,7 @@ def test_exceptions_excel_download(app, transfer_db, client, admin_headers, tran
}).get_json()["data"]
executed = client.post(
f"{API_PREFIX}/prechecks/{created['precheck_id']}/execute",
headers=admin_headers, json={"invoice_nos": ["INV-001", "INV-002"]},
headers=admin_headers, json={"invoice_nos": ["HOM-001", "HOX-002"]},
)
run_no = executed.get_json()["data"]["run_no"]
response = client.get(f"{API_PREFIX}/runs/{run_no}/exceptions.xlsx", headers=admin_headers)
@@ -308,7 +327,7 @@ def test_receipt_files_download_records_access_audit(app, transfer_db, client, a
}).get_json()["data"]
executed = client.post(
f"{API_PREFIX}/prechecks/{created['precheck_id']}/execute",
headers=admin_headers, json={"invoice_nos": ["INV-001", "INV-002"]},
headers=admin_headers, json={"invoice_nos": ["HOM-001", "HOX-002"]},
)
run_no = executed.get_json()["data"]["run_no"]
response = client.get(f"{API_PREFIX}/runs/{run_no}/receipt-files", headers=admin_headers)
@@ -322,24 +341,24 @@ def test_receipt_files_download_records_access_audit(app, transfer_db, client, a
def test_erp_inventory_download_records_access_audit(app, transfer_db, client, admin_headers, transfer_deps):
"""下载预检冻结的 ERP 库存表 → 200(csv),记录访问审计。"""
"""下载预检冻结的 外部系统 库存表 → 200(csv),记录访问审计。"""
created = client.post(f"{API_PREFIX}/prechecks", headers=admin_headers, json={
"source_codes": ["DBS-AWSBI"],
"business_date": "2026-08-06",
}).get_json()["data"]
executed = client.post(
f"{API_PREFIX}/prechecks/{created['precheck_id']}/execute",
headers=admin_headers, json={"invoice_nos": ["INV-001", "INV-002"]},
headers=admin_headers, json={"invoice_nos": ["HOM-001", "HOX-002"]},
)
run_no = executed.get_json()["data"]["run_no"]
response = client.get(f"{API_PREFIX}/runs/{run_no}/erp-inventory", headers=admin_headers)
response = client.get(f"{API_PREFIX}/runs/{run_no}/external-inventory", headers=admin_headers)
assert response.status_code == 200
assert "csv" in response.content_type
assert b"INV-001" in response.data
assert b"HOM-001" in response.data
with app.app_context():
from app.models import TransferAuditEvent
audit = TransferAuditEvent.query.filter_by(event_type="TRANSFER_ERP_INVENTORY_DOWNLOADED").first()
audit = TransferAuditEvent.query.filter_by(event_type="TRANSFER_EXTERNAL_INVENTORY_DOWNLOADED").first()
assert audit is not None
@@ -355,7 +374,7 @@ def test_downloads_forbidden_without_permission(app, transfer_db, client, admin_
)
run_no = executed.get_json()["data"]["run_no"]
for path in (f"{API_PREFIX}/runs/{run_no}/receipt-files",
f"{API_PREFIX}/runs/{run_no}/erp-inventory",
f"{API_PREFIX}/runs/{run_no}/external-inventory",
f"{API_PREFIX}/runs/{run_no}/exceptions.xlsx"):
response = client.get(path, headers=unauthorized_headers)
assert response.status_code == 403, path
@@ -370,7 +389,7 @@ def test_execute_ignores_forged_warehouse_and_quantity(app, transfer_db, client,
executed = client.post(
f"{API_PREFIX}/prechecks/{created['precheck_id']}/execute",
headers=admin_headers, json={
"invoice_nos": ["INV-001"],
"invoice_nos": ["HOM-001"],
"from_warehouse": "FAKE-TRANSIT",
"to_warehouse": "FAKE-HUB",
"quantity": "1",
@@ -379,7 +398,7 @@ def test_execute_ignores_forged_warehouse_and_quantity(app, transfer_db, client,
assert executed.status_code == 201
run_no = executed.get_json()["data"]["run_no"]
run = client.get(f"{API_PREFIX}/runs/{run_no}", headers=admin_headers).get_json()["data"]
documents = run["result"]["erp_documents"]
documents = run["result"]["external_documents"]
assert documents[0]["from_warehouse"] == "DBS-TRANSIT" # 冻结值,非伪造值
assert documents[0]["to_warehouse"] == "HUB-CN01"
assert documents[0]["total_quantity"] == "1200"
@@ -395,17 +414,17 @@ def test_backlog_list_and_summary(app, transfer_db, client, admin_headers, trans
with app.app_context():
from app.models import TransferInvoiceItem
item = TransferInvoiceItem.query.filter_by(invoice_no="INV-001").first()
item = TransferInvoiceItem.query.filter_by(invoice_no="HOM-001").first()
item.status = "blocked_recoverable"
item.result_code = "blocked_quantity_mismatch"
item.result_message = "收料 1200 / ERP 1000"
item.result_message = "收料 1200 / 外部系统 1000"
db.session.commit()
listed = client.get(f"{API_PREFIX}/backlog", headers=admin_headers)
assert listed.status_code == 200
invoice_set = {entry["invoice_no"] for entry in listed.get_json()["data"]}
assert "INV-001" in invoice_set
assert "INV-002" not in invoice_set # 可执行项不在待处理池
assert "HOM-001" in invoice_set
assert "HOX-002" not in invoice_set # 可执行项不在待处理池
assert listed.get_json()["data"][0]["aging_level"] in {"normal", "warn_3d", "escalate_5d", "critical_7d"}
summary = client.get(f"{API_PREFIX}/backlog/summary", headers=admin_headers)
@@ -456,8 +475,8 @@ def test_requery_items_returns_envelope(app, client, admin_headers, monkeypatch,
用户上下文(actor_id)随请求正确传递。
"""
deps, erp, _ = make_transfer_deps(dbs_ready_file)
erp.seed_inventory("INV-001", "1000") # 与文件 1200 不符 → blocked_quantity_mismatch
erp.seed_inventory("INV-002", "1200")
erp.seed_stock("MAT-001", "DBS-TRANSIT", "LOT-10", "1000") # 与文件 1200 不符 → blocked_quantity_mismatch
erp.seed_stock("MAT-002", "DBS-TRANSIT", "LOT-20", "1200")
monkeypatch.setattr("app.services.in_transit_transfer.deps._override", deps)
created = client.post(f"{API_PREFIX}/prechecks", headers=admin_headers, json={
@@ -469,7 +488,7 @@ def test_requery_items_returns_envelope(app, client, admin_headers, monkeypatch,
response = client.post(
f"{API_PREFIX}/prechecks/{precheck_no}/items/requery",
headers=admin_headers,
json={"invoice_nos": ["INV-001"]},
json={"invoice_nos": ["HOM-001"]},
)
assert response.status_code == 200
body = response.get_json()
@@ -478,5 +497,5 @@ def test_requery_items_returns_envelope(app, client, admin_headers, monkeypatch,
assert data["precheck_no"] == precheck_no
assert data["total"] == 1
result = data["results"][0]
assert result["invoice_no"] == "INV-001"
assert result["invoice_no"] == "HOM-001"
assert result["ok"] is True