refactor(shipping-notice): 收料RPA通知并入排程通用出口与库存同门禁一次性发送
- run 终态归集后经 _notify_schedule_run_wecom 一次性发 textcard+全部收料文件,移除回调逐文件即时推送 - 门禁/审计/wecom_notified 记账与库存排程同链;WECOM_NOTIFY_EVENTS 须含 schedule_completed - 删除独立汇总函数 deliver_schedule_receipt_summary;deliver_receipt_file 仅保留管理员重发通道 - 同步 technical.md 通知描述与门禁说明;新增一次性发送与统一门禁测试
This commit is contained in:
1 parent
34fe0c4d78
commit
a06ff79d7c
6 files changed
+202
-65
No files matched your search
@@ -63,3 +63,109 @@ def test_receipt_notification_failure_is_audited(app, monkeypatch):
|
||||
# 审计结果遵循统一枚举;业务投递状态仍保留为 failed。
|
||||
assert audit.result == 'failure'
|
||||
assert audit.after_snapshot_json['error_code'] == 'WECOM_USER_UNBOUND'
|
||||
|
||||
|
||||
def _make_receipt_run_fixture(db, task_no: str, run_no: str, callback_id: str, *, awaiting: bool = True):
|
||||
"""构造一个收料排程任务+run+成功单元+归档文件的最小通知夹具。"""
|
||||
from app.models import (
|
||||
ScheduleRun, ScheduleTask, ShippingInventorySource, ShippingReceiptReportFile,
|
||||
ShippingReceiptRpaUnitRun,
|
||||
)
|
||||
|
||||
source = ShippingInventorySource.query.filter_by(source_code='DSV360_AWSDU').one()
|
||||
task = ScheduleTask(schedule_no=task_no, name=f'Receipt notify {task_no}', schedule_type='platform_job', platform_api_path='freight-receipt-rpa', owner_user_id=1, cron_expr='0 8 * * *')
|
||||
db.session.add(task); db.session.flush()
|
||||
run = ScheduleRun(run_no=run_no, schedule_task_id=task.id, status='running', response_summary_json={'awaiting_receipt_rpa': True} if awaiting else None)
|
||||
db.session.add(run); db.session.flush()
|
||||
unit = ShippingReceiptRpaUnitRun(schedule_run_id=run.id, inventory_source_id=source.id, source_code=source.source_code, connector_type='dbs', warehouse_id='AWSDU', callback_id=callback_id, status='success', config_snapshot_json={})
|
||||
db.session.add(unit); db.session.flush()
|
||||
report_file = ShippingReceiptReportFile(unit_run_id=unit.id, storage_provider='minio', storage_bucket='test', storage_key=f'{callback_id}.xlsx', original_filename='receipt.xlsx', file_size=12, sha256='c' * 64)
|
||||
db.session.add(report_file)
|
||||
db.session.commit()
|
||||
return task, run, report_file
|
||||
|
||||
|
||||
def _patch_unified_wecom(monkeypatch, *, enabled_events: bool):
|
||||
"""拦截通用排程出口的企微调用,返回可断言的捕获列表。"""
|
||||
textcards: list[tuple[str, str]] = []
|
||||
media_uploads: list[str] = []
|
||||
file_sends: list[str] = []
|
||||
|
||||
def _fake_send_textcard(_userid, _title, description, _jump_url):
|
||||
textcards.append((_title, description))
|
||||
return {'errcode': 0}
|
||||
|
||||
monkeypatch.setattr('app.services.wecom_notify.send_wecom_textcard_message', _fake_send_textcard)
|
||||
if enabled_events:
|
||||
monkeypatch.setattr('app.services.wecom_notify._get_enabled_events', lambda: {'schedule_completed'})
|
||||
monkeypatch.setattr('app.services.wecom_notify.upload_wecom_temp_media', lambda _data, filename: media_uploads.append(filename) or f'media-{len(media_uploads)}')
|
||||
monkeypatch.setattr('app.services.wecom_notify.send_wecom_file_message', lambda _userid, media_id: file_sends.append(media_id) or {'errcode': 0})
|
||||
monkeypatch.setattr('app.services.file_assets.storage.file_asset_storage.get_bytes', lambda _key: b'receipt-data')
|
||||
return textcards, media_uploads, file_sends
|
||||
|
||||
|
||||
def test_receipt_run_notification_is_one_shot_at_finalize(app, monkeypatch):
|
||||
"""收料 run 终态后经通用排程出口一次性发 textcard+全部收料文件;重复归集不重发。"""
|
||||
from app import db
|
||||
from app.models import User
|
||||
from app.services.schedule.freight_receipt_rpa_finalize import finalize_receipt_schedule_run_if_complete
|
||||
|
||||
with app.app_context():
|
||||
recipient = User(username='receipt-one-shot', email='one-shot@example.com', role='user', status='active', wecom_userid='one-shot-user')
|
||||
db.session.add(recipient); db.session.flush()
|
||||
task, run, _report_file = _make_receipt_run_fixture(db, 'SCH-RECEIPT-ONESHOT', 'RUN-RECEIPT-ONESHOT', 'receipt-oneshot-callback')
|
||||
task.notify_enabled = True
|
||||
task.notify_user_ids_json = [recipient.id]
|
||||
db.session.commit()
|
||||
|
||||
app.config['WECOM_NOTIFY_ENABLED'] = True
|
||||
app.config['WECOM_PUBLIC_BASE_URL'] = 'https://hxops.example.com'
|
||||
textcards, media_uploads, file_sends = _patch_unified_wecom(monkeypatch, enabled_events=True)
|
||||
|
||||
outcome = finalize_receipt_schedule_run_if_complete(run.id)
|
||||
|
||||
# [断言意图] 一次性出口:单条 textcard + 单文件消息,与库存排程同通道同门禁。
|
||||
assert outcome == {'finalized': True, 'status': 'success'}
|
||||
assert len(textcards) == 1
|
||||
assert '收料报表文件 1 份:已随本通知发送' in textcards[0][1]
|
||||
assert media_uploads == ['receipt.xlsx']
|
||||
assert file_sends == ['media-1']
|
||||
assert run.wecom_notified is True
|
||||
|
||||
# [断言意图] awaiting 标志清零后再次归集必须短路,绝不二次打扰接收人。
|
||||
outcome_again = finalize_receipt_schedule_run_if_complete(run.id)
|
||||
assert outcome_again == {'finalized': False, 'reason': 'not_awaiting_receipt_callback'}
|
||||
assert len(textcards) == 1 and len(file_sends) == 1
|
||||
|
||||
|
||||
def test_receipt_run_notification_follows_inventory_gates(app, monkeypatch):
|
||||
"""收料通知与库存共用门禁:事件未启用或接收人未绑定企微时不发送并记录原因。"""
|
||||
from app import db
|
||||
from app.models import User
|
||||
from app.services.schedule.scheduler_service import _notify_schedule_run_wecom
|
||||
|
||||
with app.app_context():
|
||||
app.config['WECOM_NOTIFY_ENABLED'] = True
|
||||
textcards, _media_uploads, file_sends = _patch_unified_wecom(monkeypatch, enabled_events=False)
|
||||
|
||||
bound_user = User(username='receipt-gate-bound', email='gate-bound@example.com', role='user', status='active', wecom_userid='gate-bound-user')
|
||||
unbound_user = User(username='receipt-gate-unbound', email='gate-unbound@example.com', role='user', status='active')
|
||||
db.session.add_all([bound_user, unbound_user]); db.session.flush()
|
||||
task_a, run_a, _f = _make_receipt_run_fixture(db, 'SCH-RECEIPT-GATE-A', 'RUN-RECEIPT-GATE-A', 'receipt-gate-a')
|
||||
task_a.notify_user_ids_json = [bound_user.id]
|
||||
task_b, run_b, _g = _make_receipt_run_fixture(db, 'SCH-RECEIPT-GATE-B', 'RUN-RECEIPT-GATE-B', 'receipt-gate-b')
|
||||
task_b.notify_user_ids_json = [unbound_user.id]
|
||||
db.session.commit()
|
||||
|
||||
# [断言意图] ①schedule_completed 不在 WECOM_NOTIFY_EVENTS 内 → 整体拦截并留因。
|
||||
app.config['WECOM_NOTIFY_EVENTS'] = ''
|
||||
_notify_schedule_run_wecom(task_a, run_a)
|
||||
assert textcards == [] and file_sends == []
|
||||
assert run_a.wecom_notify_error == 'event schedule_completed disabled'
|
||||
|
||||
# [断言意图] ②事件放行但接收人无 wecom_userid → 不发送并留因,与库存口径一致。
|
||||
app.config['WECOM_NOTIFY_EVENTS'] = 'schedule_completed'
|
||||
_notify_schedule_run_wecom(task_b, run_b)
|
||||
assert textcards == [] and file_sends == []
|
||||
assert run_b.wecom_notify_error == 'no wecom_userid bound users in notify list'
|
||||
|
||||
@@ -688,6 +688,8 @@ def test_build_schedule_run_wecom_description_includes_skipped_count():
|
||||
skipped_count=1,
|
||||
failed_count=0,
|
||||
response_summary_json=None,
|
||||
# 收料文件附加行读取 run.task 识别收料排程;非收料场景显式置 None。
|
||||
task=None,
|
||||
)
|
||||
text = _build_schedule_run_wecom_description(run)
|
||||
assert "跳过: 1" in text
|
||||
|
||||
Reference in new issue
Block a user