fix(fulfillment): 支持重开未过期支付二维码并展示具体待人工原因

This commit is contained in:
yml
2026-11-28 14:11:21 +08:00
parent 20314a2f1d
commit 2013a9bdeb
9 changed files with 302 additions and 64 deletions
+98 -1
View File
@@ -1,11 +1,15 @@
from datetime import UTC, datetime, timedelta
import pytest
from fastapi import HTTPException
from web.backend.models import Account, DouyuTask, User
from web.backend.routers.douyu import _require_batch_owner
from web.backend.routers.douyu import _prepare_task_batch, _require_batch_owner
from web.backend.schemas import DouyuTaskBatchRequest
from web.backend.services.douyu_service import (
DouyuActivePaymentError,
create_douyu_planned_tasks,
find_reusable_active_payment_task,
)
@@ -144,3 +148,96 @@ def test_operation_cannot_stop_another_users_batch(db):
assert exc_info.value.status_code == 403
_require_batch_owner(db, admin, batch_id)
def test_reuse_active_payment_reopens_unexpired_qr_and_skips_expired(db):
owner = _user(db, "payment-reopen-owner")
other = _user(db, "payment-reopen-other")
account = _account(db, "payment-reopen-acc")
db.commit()
first = _prepare_task_batch(
DouyuTaskBatchRequest(
account_ids=[account.id],
task_type="create_elite_qr",
handbook_scope="elite",
reuse_active_payment=True,
),
db,
owner,
)
assert first["success"] is True
assert first.get("reused") is not True
task = db.query(DouyuTask).filter(DouyuTask.batch_id == first["batch_id"]).one()
task.status = "running"
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=5)
task.result = {"pay_url": "https://pay.example.test/elite-qr"}
db.commit()
reopened = _prepare_task_batch(
DouyuTaskBatchRequest(
account_ids=[account.id],
task_type="create_elite_qr",
handbook_scope="elite",
reuse_active_payment=True,
),
db,
other,
)
assert reopened["batch_id"] == first["batch_id"]
assert reopened["reused"] is True
assert reopened["count"] == 1
assert db.query(DouyuTask).filter(DouyuTask.account_id == account.id).count() == 1
# 过期后的任务不再被复用
task.run_deadline_at = datetime.now(UTC) - timedelta(seconds=10)
db.commit()
assert (
find_reusable_active_payment_task(
db,
account.id,
task_type="create_elite_qr",
handbook_scope="elite",
)
is None
)
# 鱼翅二维码按金额严格区分:同金额复用,不同金额不复用
task.status = "stopped"
db.commit()
gold_first = _prepare_task_batch(
DouyuTaskBatchRequest(
account_ids=[account.id],
task_type="create_gold_qr",
handbook_scope="elite",
payload={"amount": 30},
reuse_active_payment=True,
),
db,
owner,
)
gold_same = _prepare_task_batch(
DouyuTaskBatchRequest(
account_ids=[account.id],
task_type="create_gold_qr",
handbook_scope="elite",
payload={"amount": 30},
reuse_active_payment=True,
),
db,
owner,
)
assert gold_same["batch_id"] == gold_first["batch_id"]
assert gold_same["reused"] is True
assert (
find_reusable_active_payment_task(
db,
account.id,
task_type="create_gold_qr",
handbook_scope="elite",
recharge_channel="wechat_qr",
recharge_amount=60,
)
is None
)