feat(shipping-notice): 添加订单抽取功能,支持异步运行和管理,优化相关数据模型和处理逻辑

This commit is contained in:
hugo committed 2026-07-21 09:02:42 +08:00
1 parent 34d489a2d1
commit ce26d6d0c4
144 files changed
+12241 -4233

No files matched your search

@@ -7,7 +7,7 @@ import pytest
from app import db
from app.core.errors import NotFoundError, ValidationError
from app.models import Agent, User
from app.models import Agent, AgentRuntimeBinding, User
from app.services.admin import agent_service
@@ -85,6 +85,87 @@ def test_update_agent_clears_handler_config_when_explicit_null(app):
assert refreshed.handler_config is None
def test_create_agent_and_runtime_binding_are_atomic(app):
"""创建货代 Agent 时绑定校验失败必须回滚 Agent,不能留下半配置入口。"""
with app.app_context():
provider = Agent(
name='Invalid Chatflow Provider',
agent_code='generic',
runtime_provider='dify',
dify_app_type='chatflow',
exposure_mode='workspace',
dify_app_id='invalid-create-provider',
dify_api_key='sk-test',
api_base_url='http://dify.example/v1',
is_active=True,
)
db.session.add(provider)
db.session.commit()
with pytest.raises(ValidationError):
agent_service.create_agent({
'name': 'Atomic Shipping Create',
'agent_code': 'shipping_notice',
'runtime_provider': 'hybrid',
'conversation_mode': 'task_chat',
'dify_app_id': 'atomic-shipping-create',
'dify_api_key': 'sk-test',
'api_base_url': 'http://dify.example/v1',
'order_extraction_runtime_binding': {
'provider_agent_id': provider.id,
'enabled': True,
'config': {'timeout_seconds': 900, 'max_attempts': 3},
},
})
db.session.rollback()
assert Agent.query.filter_by(name='Atomic Shipping Create').count() == 0
assert AgentRuntimeBinding.query.count() == 0
def test_update_agent_and_runtime_binding_are_atomic(app):
"""更新 Agent 时绑定校验失败必须回滚 Agent 字段,避免配置只保存一半。"""
with app.app_context():
owner = Agent(
name='Atomic Shipping Update',
description='before',
agent_code='shipping_notice',
runtime_provider='hybrid',
conversation_mode='task_chat',
dify_app_id='atomic-shipping-update',
dify_api_key='sk-test',
api_base_url='http://dify.example/v1',
is_active=True,
)
provider = Agent(
name='Invalid Update Provider',
agent_code='generic',
runtime_provider='dify',
dify_app_type='chatflow',
exposure_mode='workspace',
dify_app_id='invalid-update-provider',
dify_api_key='sk-test',
api_base_url='http://dify.example/v1',
is_active=True,
)
db.session.add_all([owner, provider])
db.session.commit()
with pytest.raises(ValidationError):
agent_service.update_agent(owner.id, {
'description': 'after',
'order_extraction_runtime_binding': {
'provider_agent_id': provider.id,
'enabled': True,
'config': {'timeout_seconds': 900, 'max_attempts': 3},
},
})
db.session.rollback()
assert db.session.get(Agent, owner.id).description == 'before'
assert AgentRuntimeBinding.query.filter_by(owner_agent_id=owner.id).count() == 0
def test_update_agent_permissions(app):
with app.app_context():
agent = Agent(
@@ -27,6 +27,8 @@ from app.models import (
ShippingCustomerImportRow,
ShippingCustomerShadowMatch,
ShippingCustomerShadowResolution,
ShippingOrderField,
ShippingOrderSchema,
)
from app.services.admin.shipping_customer_lifecycle_service import (
delete_customer,
@@ -43,6 +45,17 @@ def _customer(code: str = 'WYC') -> ShippingCustomer:
return customer
def _context_field() -> ShippingOrderField:
"""创建身份定义非空外键所需的最小统一字段。"""
schema = ShippingOrderSchema(schema_code='SHIPPING_ORDER', display_name='Order')
db.session.add(schema)
db.session.flush()
field = ShippingOrderField(schema_id=schema.id, field_code='sender_email', scope='header', value_type='string')
db.session.add(field)
db.session.flush()
return field
def test_unused_customer_can_delete_and_identifiers_are_cascade_only(app):
"""仅有识别证据的误建客户可删除,证据不应成为业务阻断引用。"""
with app.app_context():
@@ -160,10 +173,9 @@ def test_delete_unused_customer_cascades_configurable_identity_evidence(app):
"""关键断言:新模型证据也是可级联配置,不能成为永久删除阻断或遗留外键。"""
with app.app_context():
customer = _customer('IDE')
field = _context_field()
definition = ShippingIdentityDefinition(
definition_code='delete_sender_email', target_type='customer', source_adapter_key='email_metadata',
source_selector_json={'attribute': 'email.from', 'projection': 'email'},
attribute_key='email', normalizer_key='email', comparator_key='normalized_exact',
definition_code='delete_sender_email', target_type='customer', context_field_id=field.id,
max_automation='conditional', status='active',
)
db.session.add(definition)
@@ -25,6 +25,8 @@ from app.models import (
ShippingCustomerLifecycleEvent,
ShippingCustomerMerge,
ShippingCustomerShadowResolution,
ShippingOrderField,
ShippingOrderSchema,
)
from app.services.admin.shipping_customer_lifecycle_service import get_customer_lifecycle_impact
from app.services.admin.shipping_customer_merge_service import get_customer_merge_impact, merge_customer
@@ -42,6 +44,17 @@ def _customer(name: str, code: str, *, status: str = 'active') -> ShippingCustom
return customer
def _context_field() -> ShippingOrderField:
"""创建身份定义非空外键所需的最小统一字段。"""
schema = ShippingOrderSchema(schema_code='SHIPPING_ORDER', display_name='Order')
db.session.add(schema)
db.session.flush()
field = ShippingOrderField(schema_id=schema.id, field_code='sender_email', scope='header', value_type='string')
db.session.add(field)
db.session.flush()
return field
def test_merge_moves_future_configuration_and_preserves_history(app):
"""归并迁移证据和来源键,但业务单、流水及其客户快照始终保留源客户。"""
with app.app_context():
@@ -112,10 +125,9 @@ def test_merge_moves_and_deduplicates_configurable_identity_evidences(app):
with app.app_context():
source = _customer('新证据源客户', 'IES')
target = _customer('新证据目标客户', 'IET')
field = _context_field()
definition = ShippingIdentityDefinition(
definition_code='merge_sender_email', target_type='customer', source_adapter_key='email_metadata',
source_selector_json={'attribute': 'email.from', 'projection': 'email'},
attribute_key='email', normalizer_key='email', comparator_key='normalized_exact',
definition_code='merge_sender_email', target_type='customer', context_field_id=field.id,
max_automation='conditional', status='active',
)
db.session.add(definition)