refactor(fulfillment): 拆分履约服务并修正换绑冷却分类

This commit is contained in:
yml
2026-11-23 13:56:32 +08:00
parent 04e9b1d4c3
commit 05a7a56c4e
19 changed files with 4760 additions and 3951 deletions
+144 -21
View File
@@ -171,7 +171,7 @@ class TestSyncAndMapping(_Base):
self, monkeypatch
):
monkeypatch.setattr(
service,
service.order_sync,
"list_pending_manual_fulfillments",
lambda **_kwargs: (_ for _ in ()).throw(
AssertionError("没有映射时不应查询 order_site")
@@ -221,7 +221,7 @@ class TestSyncAndMapping(_Base):
]
monkeypatch.setattr(
service,
service.order_sync,
"list_pending_manual_fulfillments",
_rows,
)
@@ -418,7 +418,7 @@ class TestSyncAndMapping(_Base):
current=self.admin,
)
monkeypatch.setattr(
service,
service.order_sync,
"lookup_pending_manual_fulfillment",
lambda *_args, **_kwargs: None,
)
@@ -905,6 +905,51 @@ class TestDouyuOperationTimeline(_Base):
assert operation_events[0]["detail"]["douyuTaskId"] == douyu_task.id
assert operation_events[1]["detail"]["batchId"] == douyu_task.batch_id
def test_binding_failure_event_uses_raw_confirm_error_classification(self):
task = self.task()
account = self.account("operation-customer-cooldown")
task.account_id = account.id
self.session.commit()
douyu_task = DouyuTask(
batch_id="platform-customer-cooldown",
account_id=account.id,
account_assigned_to=account.assigned_to,
task_type="confirm_bind",
handbook_scope="elite",
status="failed",
message="斗鱼账号当前绑定角色(旧角色)未到可换绑时间",
result={
"issue_code": "account_role_cooldown",
"bind_phase": "confirm_failed",
"role_name": "客户角色",
"confirm_error": "该账号未到可换绑时间,请使用其他账号",
"confirm_error_code": -200001,
},
created_by=self.admin_id,
platform_task_id=task.id,
)
self.session.add(douyu_task)
self.session.commit()
service.record_douyu_operation_result(
self.session,
douyu_task,
status="failed",
message=douyu_task.message,
)
self.session.commit()
event = (
self.session.query(PlatformFulfillmentEvent)
.filter(
PlatformFulfillmentEvent.task_id == task.id,
PlatformFulfillmentEvent.event_type == "douyu_operation_failed",
)
.one()
)
assert event.detail_json["issueCode"] == "customer_role_cooldown"
assert "待绑定游戏账号(客户角色)" in event.detail_json["message"]
class TestSnapshotSync(_Base):
def snapshot(self, **overrides):
@@ -938,7 +983,7 @@ class TestSnapshotSync(_Base):
):
self.mapping()
monkeypatch.setattr(
service,
service.order_sync,
"lookup_pending_manual_fulfillment",
lambda *_args, **_kwargs: (_ for _ in ()).throw(
AssertionError("新快照契约不应回查 order_site")
@@ -1011,7 +1056,11 @@ class TestSnapshotSync(_Base):
requested.append(item_id)
return self.snapshot()
monkeypatch.setattr(service, "lookup_pending_manual_fulfillment", lookup)
monkeypatch.setattr(
service.order_sync,
"lookup_pending_manual_fulfillment",
lookup,
)
result = service.sync_order_item_ids(self.session, ["101"])
@@ -1646,7 +1695,11 @@ class TestBindingAndCompletion(_Base):
task.issue_code = "binding_failed"
task.last_error = "绑定失败,请刷新二维码后重试"
self.session.commit()
monkeypatch.setattr(service, "start_douyu_batch", lambda **_kwargs: None)
monkeypatch.setattr(
service.binding,
"start_douyu_batch",
lambda **_kwargs: None,
)
asyncio.run(
service.start_binding(
@@ -1709,7 +1762,11 @@ class TestBindingAndCompletion(_Base):
def test_customer_start_creates_one_binding_task_and_links_platform(self, monkeypatch):
task = self._allocated()
monkeypatch.setattr(service, "start_douyu_batch", lambda **_kwargs: None)
monkeypatch.setattr(
service.binding,
"start_douyu_batch",
lambda **_kwargs: None,
)
first = asyncio.run(service.customer_start_binding(self.session, task_id=task.id))
second = asyncio.run(service.customer_start_binding(self.session, task_id=task.id))
@@ -1732,7 +1789,7 @@ class TestBindingAndCompletion(_Base):
task = self._allocated()
captured: dict[str, object] = {}
monkeypatch.setattr(
service,
service.binding,
"start_douyu_batch",
lambda **kwargs: captured.update(kwargs),
)
@@ -1744,7 +1801,11 @@ class TestBindingAndCompletion(_Base):
def test_concurrent_start_binding_creates_one_task(self, monkeypatch):
task = self._allocated()
monkeypatch.setattr(service, "start_douyu_batch", lambda **_kwargs: None)
monkeypatch.setattr(
service.binding,
"start_douyu_batch",
lambda **_kwargs: None,
)
barrier = threading.Barrier(2)
errors: list[BaseException] = []
other_session = make_test_session(self.engine)
@@ -1815,7 +1876,11 @@ class TestBindingAndCompletion(_Base):
task.binding_status = "role_ready"
task.execution_status = "role_ready"
self.session.commit()
monkeypatch.setattr(service, "start_douyu_batch", lambda **_kwargs: None)
monkeypatch.setattr(
service.binding,
"start_douyu_batch",
lambda **_kwargs: None,
)
asyncio.run(
service.confirm_binding(
@@ -1985,7 +2050,7 @@ class TestBindingAndCompletion(_Base):
self.session.add(collab_order)
self.session.commit()
monkeypatch.setattr(
service,
service.lifecycle,
"lookup_pending_manual_fulfillment",
lambda _item_id: {
"order_status": "paid",
@@ -2054,7 +2119,7 @@ class TestBindingAndCompletion(_Base):
)
self.session.commit()
monkeypatch.setattr(
service,
service.lifecycle,
"lookup_pending_manual_fulfillment",
lambda _item_id: {
"order_status": "paid",
@@ -2083,7 +2148,7 @@ class TestBindingAndCompletion(_Base):
reason="客服误点取消",
)
monkeypatch.setattr(
service,
service.lifecycle,
"lookup_pending_manual_fulfillment",
lambda _item_id: {
"order_status": "refunded",
@@ -2128,7 +2193,7 @@ class TestBindingAndCompletion(_Base):
lease_id = task.lease_id
stopped: list[str] = []
monkeypatch.setattr(
service,
service.lifecycle,
"stop_douyu_batches",
lambda batch_ids: stopped.extend(batch_ids) or len(batch_ids),
)
@@ -2230,7 +2295,7 @@ class TestBindingAndCompletion(_Base):
def test_refresh_binding_maps_role_ready(self, monkeypatch):
notified: list[int] = []
monkeypatch.setattr(
service,
service.binding,
"_emit_order_site_binding_state",
lambda _db, task_id: notified.append(task_id) or True,
)
@@ -2332,7 +2397,11 @@ class TestBindingAndCompletion(_Base):
task.binding_status = "qr_pending"
task.execution_status = "waiting_bind"
self.session.commit()
monkeypatch.setattr(service, "start_douyu_batch", lambda **_kwargs: None)
monkeypatch.setattr(
service.binding,
"start_douyu_batch",
lambda **_kwargs: None,
)
asyncio.run(
service.start_binding(
@@ -2509,6 +2578,7 @@ class TestBindingAndCompletion(_Base):
message="待绑定游戏账号(测试角色)未到换绑时间,请重新换账号扫码绑定",
result={
"bind_phase": "confirm_failed",
"issue_code": "account_role_cooldown",
"role_name": "测试角色",
"bind_ready_for_confirm": True,
"bind_confirmed": False,
@@ -2520,10 +2590,59 @@ class TestBindingAndCompletion(_Base):
self.session.add(bind_task)
self.session.flush()
task.bind_task_id = bind_task.id
task.binding_status = "role_ready"
task.execution_status = "role_ready"
task.binding_status = "failed"
task.execution_status = "manual_required"
task.issue_code = "account_role_cooldown"
task.last_error = (
"斗鱼账号当前绑定角色(测试角色)未到可换绑时间,"
"请更换斗鱼账号或稍后重试"
)
self.session.add_all(
[
PlatformFulfillmentEvent(
task_id=task.id,
event_type="douyu_operation_failed",
actor_type="system",
detail_json={
"douyuTaskId": bind_task.id,
"message": task.last_error,
},
),
PlatformFulfillmentEvent(
task_id=task.id,
event_type="binding_failed",
actor_type="system",
detail_json={
"bindTaskId": bind_task.id,
"issueCode": "account_role_cooldown",
"rawError": task.last_error,
},
),
]
)
self.session.commit()
historical = service.get_task_detail(self.session, task_id=task.id)
assert historical["task"]["issueCode"] == "customer_role_cooldown"
assert "游戏角色" in historical["task"]["lastError"]
operation_event = next(
event
for event in historical["events"]
if event["eventType"] == "douyu_operation_failed"
)
binding_event = next(
event
for event in historical["events"]
if event["eventType"] == "binding_failed"
)
assert "待绑定游戏账号" in operation_event["detail"]["message"]
assert operation_event["detail"]["issueCode"] == "customer_role_cooldown"
assert "待绑定游戏账号" in binding_event["detail"]["rawError"]
assert binding_event["detail"]["issueCode"] == "customer_role_cooldown"
historical_live = service.customer_binding_out(self.session, task.id)
assert historical_live["issueCode"] == "customer_role_cooldown"
assert "游戏角色" in historical_live["message"]
detail = service.refresh_binding(self.session, task_id=task.id)
assert detail["task"]["issueCode"] == "customer_role_cooldown"
@@ -2531,7 +2650,11 @@ class TestBindingAndCompletion(_Base):
assert detail["task"]["executionStatus"] == "manual_required"
assert "截图" in detail["task"]["lastError"]
assert "联系客服" in detail["task"]["lastError"]
self.session.refresh(task)
assert task.issue_code == "customer_role_cooldown"
live = service.customer_binding_out(self.session, task.id)
assert live["issueCode"] == "customer_role_cooldown"
assert "游戏角色" in live["message"]
assert live["bindingStatus"] == "failed"
assert live["executionStatus"] == "manual_required"
assert live["bindReadyForConfirm"] is False
@@ -2696,7 +2819,7 @@ class TestBindingAndCompletion(_Base):
task.execution_status = "bound"
self.session.commit()
monkeypatch.setattr(
service,
service.lifecycle,
"probe_account",
lambda *_args, **_kwargs: {
"probe_id": "points-short",
@@ -2740,7 +2863,7 @@ class TestBindingAndCompletion(_Base):
start_at = NOW + timedelta(days=4)
end_at = NOW + timedelta(days=10)
monkeypatch.setattr(
service,
service.lifecycle,
"probe_account",
lambda *_args, **_kwargs: {
"probe_id": "presale-waiting",
@@ -2792,7 +2915,7 @@ class TestBindingAndCompletion(_Base):
task.execution_status = "bound"
self.session.commit()
monkeypatch.setattr(
service,
service.lifecycle,
"probe_account",
lambda *_args, **_kwargs: {
"probe_id": "expired-elite",