feat(shipping-notice): 新增临时库存管理功能,支持创建会话、获取分析结果及字段映射建议

This commit is contained in:
hugo committed 2026-07-18 11:41:02 +08:00
1 parent 46eb63f5bb
commit e9bdd81f1a
320 files changed
+28361 -10487

No files matched your search

+212 -13
View File
@@ -13,6 +13,7 @@ from app.models import (
FileAsset,
ShippingCase,
ShippingCustomer,
ShippingCustomerIdentifier,
ShippingCustomerShadowResolution,
ShippingNoticeCandidate,
ShippingNoticeDocument,
@@ -80,29 +81,227 @@ def test_shipping_customer_admin_routes_reject_non_admin(app, client):
assert parse_api_error(response.get_json())['code'] == 'AUTH.FORBIDDEN'
def test_shipping_customer_historical_backfill_api_does_not_number(app, client):
"""管理员可为历史任务回填客户,且响应与业务单均不生成历史业务号。"""
def test_shipping_customer_lifecycle_impact_api_is_admin_only(app, client):
"""生命周期影响预览返回统一结构,普通用户不能读取删除资格。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
_create_users()
customer = ShippingCustomer(standard_name='历史客户', short_code='HIS', status='active')
task = ShippingTask(task_no='SN-20200101-HISTAPI', source_type='manual', process_status='closed')
db.session.add_all([customer, task])
customer = ShippingCustomer(standard_name='生命周期客户', short_code='LIF', status='active')
db.session.add(customer)
db.session.commit()
customer_id = customer.id
response = client.post(
'/api/admin/shipping-customers/historical-task-backfill',
json={'task_no': 'SN-20200101-HISTAPI', 'customer_id': customer_id},
forbidden = client.get(
f'/api/admin/shipping-customers/{customer_id}/lifecycle-impact',
headers=_headers('shipping-user', 'user'),
)
assert forbidden.status_code == 403
response = client.get(
f'/api/admin/shipping-customers/{customer_id}/lifecycle-impact',
headers=_headers('shipping-admin', 'admin'),
)
assert response.status_code == 200
payload = parse_api_data(response.get_json())
assert payload['business_no'] is None
impact = parse_api_data(response.get_json())
assert impact['can_delete'] is True
assert impact['cascade']['identifiers'] == 0
def test_shipping_customer_timeline_and_governance_quality_apis_are_admin_only(app, client):
"""治理时间线和质量汇总必须复用管理员权限,并返回自动确认门禁状态。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
persisted = ShippingTask.query.filter_by(task_no='SN-20200101-HISTAPI').one()
assert persisted.shipping_case.business_no is None
assert persisted.shipping_case.numbering_status == 'historical_unnumbered'
_create_users()
customer = ShippingCustomer(standard_name='时间线客户', short_code='TIM', status='active')
db.session.add(customer)
db.session.flush()
from app.models import ShippingCustomerLifecycleEvent
db.session.add(ShippingCustomerLifecycleEvent(
event_type='created', customer_id_snapshot=customer.id,
customer_name_snapshot=customer.standard_name, customer_short_code_snapshot=customer.short_code,
detail_json={},
))
db.session.commit()
customer_id = customer.id
admin_headers = _headers('shipping-admin', 'admin')
for path in (
f'/api/admin/shipping-customers/{customer_id}/lifecycle-timeline',
'/api/admin/shipping-customers/governance-quality',
):
forbidden = client.get(path, headers=_headers('shipping-user', 'user'))
assert forbidden.status_code == 403
timeline = client.get(
f'/api/admin/shipping-customers/{customer_id}/lifecycle-timeline', headers=admin_headers,
)
assert timeline.status_code == 200
assert parse_api_data(timeline.get_json())['items'][0]['event_type'] == 'created'
quality = client.get('/api/admin/shipping-customers/governance-quality', headers=admin_headers)
assert quality.status_code == 200
payload = parse_api_data(quality.get_json())
assert payload['shadow_quality']['total'] == 0
assert payload['shadow_quality']['auto_resolution']['quality_gate']['status'] == 'insufficient_labeled_samples'
assert payload['lifecycle_event_counts']['created'] == 1
def test_shipping_customer_delete_api_is_admin_only_and_audited(app, client):
"""删除 API 仅管理员可用,并在成功后保留客户快照审计。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
_create_users()
customer = ShippingCustomer(standard_name='可删除客户', short_code='DEL', status='active')
db.session.add(customer)
db.session.flush()
db.session.add(ShippingCustomerIdentifier(
customer_id=customer.id, identifier_type='name_alias', raw_value='Delete Customer',
normalized_value='DELETECUSTOMER', status='approved', source='manual',
))
db.session.commit()
customer_id = customer.id
payload = {'confirmation_short_code': 'DEL', 'reason': '误建'}
forbidden = client.delete(
f'/api/admin/shipping-customers/{customer_id}', json=payload,
headers=_headers('shipping-user', 'user'),
)
assert forbidden.status_code == 403
response = client.delete(
f'/api/admin/shipping-customers/{customer_id}', json=payload,
headers=_headers('shipping-admin', 'admin'),
)
assert response.status_code == 200
deleted = parse_api_data(response.get_json())
assert deleted['deleted_customer']['id'] == customer_id
assert deleted['cascade']['identifiers'] == 1
missing = client.get(f'/api/admin/shipping-customers/{customer_id}', headers=_headers('shipping-admin', 'admin'))
assert missing.status_code == 404
def test_shipping_customer_delete_api_rejects_invalid_confirmation_and_blockers(app, client):
"""API 必须校验二次确认,并在删除提交时返回完整阻断明细。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
_create_users()
customer = ShippingCustomer(standard_name='有业务客户', short_code='BLK', status='active')
db.session.add(customer)
db.session.flush()
db.session.add(ShippingCase(case_no='CASE-DELETE-API', customer_id=customer.id))
db.session.commit()
customer_id = customer.id
headers = _headers('shipping-admin', 'admin')
invalid = client.delete(
f'/api/admin/shipping-customers/{customer_id}',
json={'confirmation_short_code': 'BAD', 'reason': '误建'}, headers=headers,
)
assert invalid.status_code == 400
assert parse_api_error(invalid.get_json())['code'] == 'SHIPPING.CUSTOMER_DELETE_CONFIRMATION_INVALID'
blocked = client.delete(
f'/api/admin/shipping-customers/{customer_id}',
json={'confirmation_short_code': 'BLK', 'reason': '误建'}, headers=headers,
)
assert blocked.status_code == 409
error = parse_api_error(blocked.get_json())
assert error['code'] == 'SHIPPING.CUSTOMER_DELETE_BLOCKED'
assert error['details']['blockers']['business_cases'] == 1
def test_shipping_customer_merge_api_is_admin_only_and_preserves_source_history(app, client):
"""管理员归并客户时仅迁移未来证据,源客户历史业务仍保持原关联。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
_create_users()
source = ShippingCustomer(standard_name='重复客户', short_code='SRC', status='active')
target = ShippingCustomer(standard_name='标准客户', short_code='TGT', status='active')
db.session.add_all([source, target])
db.session.flush()
db.session.add_all([
ShippingCustomerIdentifier(
customer_id=source.id, identifier_type='name_alias', raw_value='Source Alias',
normalized_value='SOURCEALIAS', status='approved', source='manual',
),
ShippingCase(case_no='CASE-MERGE-API', customer_id=source.id, business_no='SRC26071601'),
])
db.session.commit()
source_id = source.id
target_id = target.id
admin_headers = _headers('shipping-admin', 'admin')
forbidden = client.post(
f'/api/admin/shipping-customers/{source_id}/merge',
json={'target_customer_id': target_id, 'confirmation_short_code': 'SRC', 'reason': '重复建档'},
headers=_headers('shipping-user', 'user'),
)
assert forbidden.status_code == 403
preview = client.get(
f'/api/admin/shipping-customers/{source_id}/merge-impact?target_customer_id={target_id}',
headers=admin_headers,
)
assert preview.status_code == 200
assert parse_api_data(preview.get_json())['historical_references']['business_cases'] == 1
response = client.post(
f'/api/admin/shipping-customers/{source_id}/merge',
json={'target_customer_id': target_id, 'confirmation_short_code': 'SRC', 'reason': '重复建档'},
headers=admin_headers,
)
assert response.status_code == 200
result = parse_api_data(response.get_json())
assert result['source']['id'] == source_id
assert result['target']['id'] == target_id
with app.app_context():
assert db.session.get(ShippingCustomer, source_id).status == 'merged'
assert db.session.query(ShippingCustomerIdentifier).filter_by(customer_id=target_id).count() == 1
assert db.session.query(ShippingCase).filter_by(case_no='CASE-MERGE-API').one().customer_id == source_id
def test_shipping_customer_merge_api_validates_target_confirmation_and_reason(app, client):
"""合并 API 由服务端校验目标状态、源简码与原因,不能依赖前端限制。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
_create_users()
source = ShippingCustomer(standard_name='源客户', short_code='SRC', status='active')
target = ShippingCustomer(standard_name='停用目标', short_code='TGT', status='inactive')
db.session.add_all([source, target])
db.session.commit()
source_id = source.id
target_id = target.id
headers = _headers('shipping-admin', 'admin')
invalid_target = client.post(
f'/api/admin/shipping-customers/{source_id}/merge',
json={'target_customer_id': target_id, 'confirmation_short_code': 'SRC', 'reason': '重复'}, headers=headers,
)
assert invalid_target.status_code == 400
assert parse_api_error(invalid_target.get_json())['code'] == 'SHIPPING.CUSTOMER_MERGE_TARGET_INVALID'
empty_reason = client.post(
f'/api/admin/shipping-customers/{source_id}/merge',
json={'target_customer_id': source_id, 'confirmation_short_code': 'SRC', 'reason': ''}, headers=headers,
)
assert empty_reason.status_code == 400
assert parse_api_error(empty_reason.get_json())['code'] == 'SHIPPING.CUSTOMER_MERGE_REASON_REQUIRED'
def test_shipping_customer_historical_backfill_api_is_not_registered(app, client):
"""新契约不保留历史客户回填入口,管理员也不能调用。"""
app.config['AUTH_JWT_REQUIRED'] = False
with app.app_context():
_create_users()
response = client.post(
'/api/admin/shipping-customers/historical-task-backfill',
json={'task_no': 'SN-20200101-HISTAPI', 'customer_id': 1},
headers=_headers('shipping-admin', 'admin'),
)
assert response.status_code == 404
def test_shipping_customer_shadow_report_and_label_api(app, client):