fix(fulfillment): reconcile stale pending roles from newer observations
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import asdict, dataclass
|
||||
from dataclasses import asdict, dataclass, replace
|
||||
from typing import Any
|
||||
|
||||
from ..models import Account, DouyuTask
|
||||
@@ -28,6 +28,17 @@ BINDING_VIEW_PENDING = "pending"
|
||||
BINDING_VIEW_CONFIRMED = "confirmed"
|
||||
BINDING_VIEW_FAILED = "failed"
|
||||
|
||||
BINDING_OBSERVATION_TASK_TYPES = frozenset(
|
||||
{
|
||||
"query_game_name",
|
||||
"query_change_bind_time",
|
||||
}
|
||||
)
|
||||
|
||||
BINDING_RECONCILE_NONE = "none"
|
||||
BINDING_RECONCILE_CONFIRMED = "confirmed"
|
||||
BINDING_RECONCILE_STALE_PENDING = "stale_pending"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DouyuBindingView:
|
||||
@@ -64,6 +75,13 @@ class DouyuBindingView:
|
||||
return asdict(self)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DouyuBindingReconciliation:
|
||||
action: str
|
||||
pending_role_name: str
|
||||
observed_role_name: str
|
||||
|
||||
|
||||
def resolve_douyu_binding(
|
||||
task: DouyuTask | None,
|
||||
*,
|
||||
@@ -103,8 +121,16 @@ def resolve_douyu_binding(
|
||||
|
||||
account_bind_status = get_bind_status(account, "elite") if account else ""
|
||||
current_role = str(account.game_name or "").strip() if account else ""
|
||||
current_area = str(result.get("current_area_name") or "").strip()
|
||||
current_plat = str(result.get("current_plat_name") or "").strip()
|
||||
current_area = str(
|
||||
result.get("current_area_name")
|
||||
or (result.get("area_name") if bind_confirmed else "")
|
||||
or ""
|
||||
).strip()
|
||||
current_plat = str(
|
||||
result.get("current_plat_name")
|
||||
or (result.get("plat_name") if bind_confirmed else "")
|
||||
or ""
|
||||
).strip()
|
||||
current_channel = str(account.game_channel or "").strip() if account else ""
|
||||
if not current_role:
|
||||
current_role = str(
|
||||
@@ -163,3 +189,76 @@ def resolve_douyu_binding(
|
||||
bind_ready_for_confirm=status == BINDING_VIEW_PENDING,
|
||||
account_bind_status=account_bind_status,
|
||||
)
|
||||
|
||||
|
||||
def resolve_fulfillment_binding(
|
||||
bind_task: DouyuTask | None,
|
||||
*,
|
||||
account: Account | None,
|
||||
binding_status: str,
|
||||
) -> DouyuBindingView:
|
||||
"""解析履约展示;查询任务只是观测,不能直接宣告订单完成。"""
|
||||
binding = resolve_douyu_binding(bind_task, account=account)
|
||||
if (
|
||||
bind_task is None
|
||||
or bind_task.task_type not in BINDING_OBSERVATION_TASK_TYPES
|
||||
):
|
||||
return binding
|
||||
if binding_status == "bound" and binding.status == BINDING_VIEW_CONFIRMED:
|
||||
return binding
|
||||
return replace(
|
||||
binding,
|
||||
status=BINDING_VIEW_FAILED
|
||||
if binding.status == BINDING_VIEW_FAILED
|
||||
else BINDING_VIEW_NONE,
|
||||
bind_phase="observed_current" if binding.current_role_name else "",
|
||||
pending_role_name="",
|
||||
pending_area_name="",
|
||||
pending_plat_name="",
|
||||
pending_channel="",
|
||||
confirmed_role_name="",
|
||||
confirmed_area_name="",
|
||||
confirmed_plat_name="",
|
||||
confirmed_channel="",
|
||||
effective_role_name=binding.current_role_name,
|
||||
effective_area_name=binding.current_area_name,
|
||||
effective_plat_name=binding.current_plat_name,
|
||||
effective_channel=binding.current_channel,
|
||||
qr_url="",
|
||||
reported_ready_for_confirm=False,
|
||||
bind_confirmed=False,
|
||||
bind_ready_for_confirm=False,
|
||||
)
|
||||
|
||||
|
||||
def reconcile_douyu_binding(
|
||||
current: DouyuBindingView,
|
||||
observation: DouyuBindingView,
|
||||
) -> DouyuBindingReconciliation:
|
||||
"""用较新的账号观测收敛旧的待确认角色。"""
|
||||
if (
|
||||
current.status != BINDING_VIEW_PENDING
|
||||
or observation.status != BINDING_VIEW_CONFIRMED
|
||||
or current.source_task_id is None
|
||||
or observation.source_task_id is None
|
||||
or observation.source_task_id <= current.source_task_id
|
||||
or not current.pending_role_name
|
||||
):
|
||||
return DouyuBindingReconciliation(
|
||||
action=BINDING_RECONCILE_NONE,
|
||||
pending_role_name=current.pending_role_name,
|
||||
observed_role_name=observation.confirmed_role_name,
|
||||
)
|
||||
same_role = (
|
||||
current.pending_role_name.casefold()
|
||||
== observation.confirmed_role_name.casefold()
|
||||
)
|
||||
return DouyuBindingReconciliation(
|
||||
action=(
|
||||
BINDING_RECONCILE_CONFIRMED
|
||||
if same_role
|
||||
else BINDING_RECONCILE_STALE_PENDING
|
||||
),
|
||||
pending_role_name=current.pending_role_name,
|
||||
observed_role_name=observation.confirmed_role_name,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user