feat(履约绑定): 支持工作台修改客户资料、扫码不匹配时间线及确认后斗鱼复核
This commit is contained in:
@@ -2030,3 +2030,114 @@ def test_query_esports_game_name_uses_valid_bind_status_enums(db, monkeypatch):
|
||||
)
|
||||
db.refresh(account)
|
||||
assert account.esports_bind_status == "unbound"
|
||||
|
||||
|
||||
def test_poll_bind_role_once_records_mismatched_cjm_and_ignores_bound_action_role(
|
||||
db, monkeypatch
|
||||
):
|
||||
"""轮询遇到 cjm 扫出不匹配角色时记录 last_mismatched_role 并同步履约,但忽略 QYOOB 历史已绑定角色。"""
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
task.task_type = "get_bind_qr"
|
||||
task.platform_task_id = 123
|
||||
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=10)
|
||||
task.result = {
|
||||
"url": "https://livelink.example/qr",
|
||||
"expected_role_name": "妎䏓",
|
||||
"expected_role_required": True,
|
||||
"baseline_role_name": "愛love恨",
|
||||
"baseline_is_bound_act": True,
|
||||
"action_act_aliases": ["20260120QYOOB"],
|
||||
"query_act_aliases": ["cjm", "20260120QYOOB"],
|
||||
"bind_poll_initial_legacy_snapshot": {
|
||||
"role_name": "",
|
||||
"area_name": "",
|
||||
"plat_name": "",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": False,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": True,
|
||||
},
|
||||
"bind_poll_legacy_snapshot": {
|
||||
"role_name": "",
|
||||
"area_name": "",
|
||||
"plat_name": "",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": False,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": True,
|
||||
},
|
||||
}
|
||||
db.commit()
|
||||
|
||||
runner = DouyuBatchRunner(db, task.batch_id, "get_bind_qr", close_db=False)
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: object())
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_fetch_bind_info_candidates",
|
||||
lambda _client, aliases: [
|
||||
item
|
||||
for item in (
|
||||
{
|
||||
"act_alias": "cjm",
|
||||
"role_name": "妎朊",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "苹果(iOS)",
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": True,
|
||||
},
|
||||
{
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": "愛love恨",
|
||||
"area_name": "微信",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
},
|
||||
)
|
||||
if item["act_alias"] in aliases
|
||||
],
|
||||
)
|
||||
sync_calls: list[bool] = []
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_sync_platform_fulfillment",
|
||||
lambda _task_id, *, enabled: sync_calls.append(enabled),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_sync_platform_fulfillment_operation",
|
||||
lambda *_a, **_kw: None,
|
||||
)
|
||||
|
||||
cfg = {
|
||||
"bind_act_alias": "20260120QYOOB",
|
||||
"confirm_act_alias": "20260120QYOOB",
|
||||
"legacy_act_alias": "cjm",
|
||||
}
|
||||
# 第 1 次出现新不匹配角色:立即触发一次履约同步写入时间线
|
||||
runner.poll_bind_role_once(db, task, account, cfg)
|
||||
db.refresh(task)
|
||||
assert task.status == "running"
|
||||
assert sync_calls == [True]
|
||||
assert task.result["last_mismatched_role"]["role_name"] == "妎朊"
|
||||
assert [m["role_name"] for m in task.result["mismatched_roles"]] == ["妎朊"]
|
||||
|
||||
# 同一不匹配角色重复轮询不重复同步
|
||||
runner.poll_bind_role_once(db, task, account, cfg)
|
||||
db.refresh(task)
|
||||
assert sync_calls == [True]
|
||||
|
||||
# 超时后落失败文案明确指出扫到的不匹配角色
|
||||
task.run_deadline_at = datetime.now(UTC) - timedelta(seconds=1)
|
||||
db.commit()
|
||||
runner.poll_bind_role_once(db, task, account, cfg)
|
||||
db.refresh(task)
|
||||
assert task.status == "failed"
|
||||
assert "妎朊" in task.message
|
||||
assert "妎䏓" in task.message
|
||||
assert task.result["last_mismatched_role"]["role_name"] == "妎朊"
|
||||
assert [m["role_name"] for m in task.result["mismatched_roles"]] == ["妎朊"]
|
||||
|
||||
@@ -5896,3 +5896,267 @@ class TestBindingAndCompletion(_Base):
|
||||
.count()
|
||||
== 1
|
||||
)
|
||||
|
||||
def test_operator_update_customer_profile_reopens_failed_and_syncs_pending(self):
|
||||
"""工作台修改客户资料:可修正名字/区服/系统并重置失败态,也会同步当前绑定任务与 Outbox。"""
|
||||
task = self._allocated()
|
||||
self.complete_customer_profile(task)
|
||||
task.customer_role_name = "妎䏓"
|
||||
task.customer_platform = "qq"
|
||||
task.customer_system = "ios"
|
||||
qr_task = DouyuTask(
|
||||
batch_id="edit-profile-failed",
|
||||
account_id=task.account_id,
|
||||
account_assigned_to=self.admin_id,
|
||||
task_type="get_bind_qr",
|
||||
handbook_scope="elite",
|
||||
status="failed",
|
||||
platform_task_id=task.id,
|
||||
message="绑定二维码已过期:期间扫到「妎朊」,与填写角色「妎䏓」不一致",
|
||||
result={"expected_role_name": "妎䏓", "expected_role_required": True},
|
||||
created_by=self.admin_id,
|
||||
)
|
||||
self.session.add(qr_task)
|
||||
self.session.flush()
|
||||
task.bind_task_id = qr_task.id
|
||||
task.binding_status = "failed"
|
||||
task.execution_status = "manual_required"
|
||||
task.issue_code = "binding_failed"
|
||||
task.last_error = "已扫到角色「妎朊」,与下单填写的「妎䏓」不一致"
|
||||
self.session.commit()
|
||||
|
||||
detail = service.update_customer_profile_by_operator(
|
||||
self.session,
|
||||
task_id=task.id,
|
||||
current=self.admin,
|
||||
customer_role_name="妎朊",
|
||||
platform="qq",
|
||||
system="ios",
|
||||
reopen_binding=True,
|
||||
)
|
||||
|
||||
self.session.refresh(task)
|
||||
assert task.customer_role_name == "妎朊"
|
||||
assert task.binding_status == "not_started"
|
||||
assert task.execution_status == "allocated"
|
||||
assert task.bind_task_id is None
|
||||
assert task.issue_code == ""
|
||||
assert task.last_error == ""
|
||||
assert detail["task"]["customerProfile"]["roleName"] == "妎朊"
|
||||
updated_events = (
|
||||
self.session.query(PlatformFulfillmentEvent)
|
||||
.filter_by(task_id=task.id, event_type="customer_profile_updated")
|
||||
.all()
|
||||
)
|
||||
assert len(updated_events) == 1
|
||||
assert updated_events[0].detail_json["previousRoleName"] == "妎䏓"
|
||||
assert updated_events[0].detail_json["customerRoleName"] == "妎朊"
|
||||
assert updated_events[0].detail_json["reopenedBinding"] is True
|
||||
|
||||
# 已绑定订单不允许直接修改资料
|
||||
task.binding_status = "bound"
|
||||
task.execution_status = "bound"
|
||||
self.session.commit()
|
||||
with pytest.raises(HTTPException) as exc:
|
||||
service.update_customer_profile_by_operator(
|
||||
self.session,
|
||||
task_id=task.id,
|
||||
current=self.admin,
|
||||
customer_role_name="新角色",
|
||||
platform="wechat",
|
||||
system="android",
|
||||
)
|
||||
assert exc.value.status_code == 409
|
||||
|
||||
def test_mismatched_scanned_role_is_recorded_in_timeline_and_evidence(self):
|
||||
"""扫码扫到与下单填写不一致的角色时,应写入时间线并在失败提示/证据中透出。"""
|
||||
task = self._allocated()
|
||||
self.complete_customer_profile(task)
|
||||
task.customer_role_name = "妎䏓"
|
||||
qr_task = DouyuTask(
|
||||
batch_id="mismatch-qr",
|
||||
account_id=task.account_id,
|
||||
account_assigned_to=self.admin_id,
|
||||
task_type="get_bind_qr",
|
||||
handbook_scope="elite",
|
||||
status="running",
|
||||
platform_task_id=task.id,
|
||||
result={
|
||||
"url": "https://livelink.example/qr",
|
||||
"expected_role_name": "妎䏓",
|
||||
"expected_role_required": True,
|
||||
"last_mismatched_role": {
|
||||
"act_alias": "cjm",
|
||||
"role_name": "妎朊",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "苹果(iOS)",
|
||||
"expected_role_name": "妎䏓",
|
||||
"observed_at": NOW.isoformat(),
|
||||
},
|
||||
"mismatched_roles": [
|
||||
{
|
||||
"act_alias": "cjm",
|
||||
"role_name": "妎朊",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "苹果(iOS)",
|
||||
"expected_role_name": "妎䏓",
|
||||
"observed_at": NOW.isoformat(),
|
||||
}
|
||||
],
|
||||
},
|
||||
created_by=self.admin_id,
|
||||
)
|
||||
self.session.add(qr_task)
|
||||
self.session.flush()
|
||||
task.bind_task_id = qr_task.id
|
||||
task.binding_status = "qr_pending"
|
||||
task.execution_status = "waiting_bind"
|
||||
self.session.commit()
|
||||
|
||||
# 轮询期间记录一次不匹配事件(重复同步应去重)
|
||||
service.sync_task_from_binding_runner(self.session, douyu_task_id=qr_task.id)
|
||||
service.sync_task_from_binding_runner(self.session, douyu_task_id=qr_task.id)
|
||||
mismatch_events = (
|
||||
self.session.query(PlatformFulfillmentEvent)
|
||||
.filter_by(task_id=task.id, event_type="binding_scanned_role_mismatch")
|
||||
.all()
|
||||
)
|
||||
assert len(mismatch_events) == 1
|
||||
assert mismatch_events[0].detail_json["scannedRoleName"] == "妎朊"
|
||||
assert mismatch_events[0].detail_json["expectedRoleName"] == "妎䏓"
|
||||
|
||||
detail = service.get_task_detail(self.session, task.id)
|
||||
assert detail["task"]["bindingEvidence"]["scannedRole"] == ""
|
||||
assert detail["task"]["bindingEvidence"]["mismatchedRole"]["roleName"] == "妎朊"
|
||||
|
||||
# 超时失败时明确提示曾扫到的不匹配角色
|
||||
qr_task.status = "failed"
|
||||
qr_task.message = "绑定二维码已过期:期间扫到「妎朊」,与填写角色「妎䏓」不一致"
|
||||
self.session.commit()
|
||||
service.sync_task_from_binding_runner(self.session, douyu_task_id=qr_task.id)
|
||||
self.session.refresh(task)
|
||||
assert task.binding_status == "failed"
|
||||
assert "妎朊" in task.last_error
|
||||
assert "妎䏓" in task.last_error
|
||||
|
||||
def test_verify_task_binding_records_success_and_reverts_mismatch(self, monkeypatch):
|
||||
"""确认绑定后复核斗鱼:一致时记复核通过;斗鱼未绑定或角色不一致时回退转人工。"""
|
||||
from web.backend.services.platform_fulfillment import binding as pf_binding
|
||||
|
||||
task = self._allocated()
|
||||
self.complete_customer_profile(task)
|
||||
task.customer_role_name = "越伤痛"
|
||||
confirm_task = DouyuTask(
|
||||
batch_id="verify-confirm",
|
||||
account_id=task.account_id,
|
||||
account_assigned_to=self.admin_id,
|
||||
task_type="confirm_bind",
|
||||
handbook_scope="elite",
|
||||
status="success",
|
||||
platform_task_id=task.id,
|
||||
result={"role_name": "越伤痛", "bind_confirmed": True},
|
||||
created_by=self.admin_id,
|
||||
)
|
||||
self.session.add(confirm_task)
|
||||
self.session.flush()
|
||||
task.bind_task_id = confirm_task.id
|
||||
task.binding_status = "bound"
|
||||
task.execution_status = "bound"
|
||||
self.session.commit()
|
||||
|
||||
monkeypatch.setattr(pf_binding, "activity_client", lambda *_a, **_kw: object())
|
||||
monkeypatch.setattr(
|
||||
pf_binding,
|
||||
"resolve_elite_role",
|
||||
lambda _client, _cfg: {
|
||||
"status": "bound",
|
||||
"role_name": "越伤痛",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "安卓(android)",
|
||||
"game_channel": "手Q / 安卓(android)",
|
||||
"source_alias": "20260120QYOOB",
|
||||
"bind_info": {
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": "越伤痛",
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
},
|
||||
"bind_infos": [
|
||||
{
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": "越伤痛",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
],
|
||||
"errors": [],
|
||||
},
|
||||
)
|
||||
|
||||
detail = service.verify_task_binding_sync(
|
||||
self.session,
|
||||
task_id=task.id,
|
||||
current=self.admin,
|
||||
automatic=True,
|
||||
)
|
||||
self.session.refresh(task)
|
||||
assert task.binding_status == "bound"
|
||||
assert detail["events"][0]["eventType"] == "binding_verified"
|
||||
assert (
|
||||
self.session.query(DouyuRoleApiLog)
|
||||
.filter_by(account_id=task.account_id, stage="fulfillment.verify_binding")
|
||||
.count()
|
||||
== 1
|
||||
)
|
||||
|
||||
# 若复核发现斗鱼当前其实绑在别的旧角色上,应回退为人工处理
|
||||
monkeypatch.setattr(
|
||||
pf_binding,
|
||||
"resolve_elite_role",
|
||||
lambda _client, _cfg: {
|
||||
"status": "bound",
|
||||
"role_name": "清耆白清万入识",
|
||||
"area_name": "手Q",
|
||||
"plat_name": "安卓(android)",
|
||||
"game_channel": "手Q / 安卓(android)",
|
||||
"source_alias": "20260120QYOOB",
|
||||
"bind_info": {
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": "清耆白清万入识",
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
},
|
||||
"bind_infos": [
|
||||
{
|
||||
"act_alias": "20260120QYOOB",
|
||||
"role_name": "清耆白清万入识",
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
],
|
||||
"errors": [],
|
||||
},
|
||||
)
|
||||
service.verify_task_binding_sync(
|
||||
self.session,
|
||||
task_id=task.id,
|
||||
current=self.admin,
|
||||
automatic=False,
|
||||
)
|
||||
self.session.refresh(task)
|
||||
assert task.binding_status == "failed"
|
||||
assert task.execution_status == "manual_required"
|
||||
assert task.issue_code == "role_changed"
|
||||
assert "清耆白清万入识" in task.last_error
|
||||
assert (
|
||||
self.session.query(PlatformFulfillmentEvent)
|
||||
.filter_by(task_id=task.id, event_type="binding_verify_failed")
|
||||
.count()
|
||||
== 1
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user