"""services/in_transit_transfer/capabilities.py 用途:在途调拨域的能力声明——本域经集成连接器暴露的能力契约(入参/出参 Schema、 幂等语义、读写标志),代码注册、禁止管理端动态定义。 依赖:integration_connector.capability(机制层:CapabilitySpec/注册表)。 逻辑:本模块属于领域包而非连接器框架——框架只提供注册/校验机制,域契约随域代码 版本化;应用组合根(app/__init__.py)在每次 create_app 时调用 register_transfer_capabilities()(幂等),保证管理端「能力清单」冷启动即可见, 测试 reset 注册表后也能重建。 """ from __future__ import annotations from app.services.integration_connector.capability import ( CapabilitySpec, list_capabilities, register_capability, ) # 在途调拨能力输入/输出 Schema(JSON Schema 最小子集,供入参/出参校验)。 # 查询契约:按组织批量查库存,目标为 (料号, 拨出库位, 拨出批号) 元组列表; # external_org 取自仓库映射(external_organization_code),由绑定参数映射翻译为接口的 organization。 _QUERY_INPUT_SCHEMA: dict = { "type": "object", "required": ["external_org", "targets"], "properties": { "external_org": {"type": "string"}, "targets": {"type": "array", "items": { "type": "object", "required": ["material_no", "from_warehouse", "lot_no"], "properties": { "material_no": {"type": "string"}, "from_warehouse": {"type": "string"}, "lot_no": {"type": "string"}, }, }}, }, } _QUERY_OUTPUT_SCHEMA: dict = { "type": "object", "required": ["rows"], "properties": { "rows": {"type": "array", "items": { "type": "object", "required": ["material_no", "from_warehouse", "lot_no", "quantity"], "properties": { "material_no": {"type": "string"}, "from_warehouse": {"type": "string"}, "lot_no": {"type": "string"}, "quantity": {"type": "number"}, }, }}, }, } _WRITE_OUTPUT_SCHEMA: dict = { "type": "object", "required": ["status"], "properties": { "status": { "type": "string", "enum": ["success", "business_failed", "technical_failed", "timeout", "result_unknown"], }, "document_no": {"type": ["string", "null"]}, "verified_posted": {"type": ["boolean", "null"]}, }, } def register_transfer_capabilities() -> None: """注册在途调拨域的三个能力(幂等:已注册的同名跳过)。 幂等是硬要求:组合根每次 create_app 都会调用;测试 reset 注册表后(模块缓存导致 import 副作用不再执行)也依赖本函数重建,故不能依赖「只注册一次」。 """ registered = {spec.name for spec in list_capabilities()} for spec in ( CapabilitySpec( name="in_transit_transfer.query_inventory", input_schema=_QUERY_INPUT_SCHEMA, output_schema=_QUERY_OUTPUT_SCHEMA, idempotency="none", write=False, ), CapabilitySpec( name="in_transit_transfer.create_transfer", input_schema=_QUERY_INPUT_SCHEMA, output_schema=_WRITE_OUTPUT_SCHEMA, idempotency="invoice", write=True, ), CapabilitySpec( name="in_transit_transfer.verify_duplicate", input_schema=_QUERY_INPUT_SCHEMA, output_schema=_WRITE_OUTPUT_SCHEMA, idempotency="invoice", write=False, ), ): if spec.name not in registered: register_capability(spec) register_transfer_capabilities()