feat(fulfillment): 支持共享账号操作并完善时间线
This commit is contained in:
@@ -395,6 +395,9 @@ class TestEventsCategoryFilter(_Base):
|
||||
assert item["category"] == svc.CATEGORY_BUSINESS
|
||||
assert item["subject_key"] == "sku-1"
|
||||
assert item["actor_id"] == self.admin.id
|
||||
assert item["actor_name"] == "act-admin"
|
||||
assert item["actor_role"] == "super_admin"
|
||||
assert item["actor_role_label"] == "超级管理员"
|
||||
|
||||
|
||||
class TestBatchDeleteRecordsIds(_Base):
|
||||
|
||||
@@ -61,6 +61,7 @@ class TestConstructors(_Base):
|
||||
amount=100,
|
||||
channel="wechat_qr",
|
||||
douyu_uid="u1",
|
||||
actor_id=self.user.id,
|
||||
)
|
||||
is True
|
||||
)
|
||||
@@ -73,6 +74,8 @@ class TestConstructors(_Base):
|
||||
assert event.source_ref == "douyu_task:101"
|
||||
assert event.detail["channel"] == "wechat_qr"
|
||||
assert event.detail["confirmed"] is True
|
||||
assert event.actor_type == "user"
|
||||
assert event.actor_id == self.user.id
|
||||
|
||||
def test_recharge_without_amount_still_records(self):
|
||||
"""金额拿不到时宁可记一条金额为空的事件,也不要凭空编一个数字。"""
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
"""验证 DouyuTask 的 account_assigned_to 字段与任务查询优化。"""
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from tests.support.db import init_test_schema, make_test_session
|
||||
from web.backend.models import Account, DouyuTask, User
|
||||
from web.backend.routers.douyu import _visible_tasks_query
|
||||
from web.backend.models import Account, DouyuTask, PlatformFulfillmentTask, User
|
||||
from web.backend.routers.douyu import (
|
||||
_require_task_account_access,
|
||||
_visible_tasks_query,
|
||||
)
|
||||
from web.backend.services.douyu_service import create_douyu_planned_tasks
|
||||
|
||||
|
||||
@@ -67,13 +72,12 @@ def test_douyu_task_assigned_to_flow():
|
||||
assert task_map[acc_1.id].account_assigned_to == user_a.id
|
||||
assert task_map[acc_2.id].account_assigned_to == user_b.id
|
||||
|
||||
# 2. 验证 _visible_tasks_query 能够精准过滤所属客服
|
||||
# 2. 客服能看到自己创建的任务;被分配账号的客服也能看到。
|
||||
query_a = _visible_tasks_query(db, user_a).filter(
|
||||
DouyuTask.batch_id == batch_id
|
||||
)
|
||||
visible_a = query_a.all()
|
||||
assert len(visible_a) == 1
|
||||
assert visible_a[0].account_id == acc_1.id
|
||||
assert {row.account_id for row in visible_a} == {acc_1.id, acc_2.id}
|
||||
|
||||
query_b = _visible_tasks_query(db, user_b).filter(
|
||||
DouyuTask.batch_id == batch_id
|
||||
@@ -99,3 +103,92 @@ def test_douyu_task_assigned_to_flow():
|
||||
finally:
|
||||
db.rollback()
|
||||
db.close()
|
||||
|
||||
|
||||
def test_platform_task_context_allows_shared_account_operation():
|
||||
init_test_schema()
|
||||
db: Session = make_test_session()
|
||||
try:
|
||||
owner = User(
|
||||
username="ctx-owner",
|
||||
password_hash="h",
|
||||
role="support",
|
||||
is_active=True,
|
||||
)
|
||||
operator = User(
|
||||
username="ctx-operator",
|
||||
password_hash="h",
|
||||
role="support",
|
||||
custom_permissions=[
|
||||
"douyu:task",
|
||||
"account:view_assigned",
|
||||
"platform_fulfillment:use",
|
||||
],
|
||||
is_active=True,
|
||||
)
|
||||
db.add_all([owner, operator])
|
||||
db.flush()
|
||||
account = Account(
|
||||
username="ctx-shared-account",
|
||||
password="p",
|
||||
email="ctx@test.com",
|
||||
email_password="p",
|
||||
cookie="acf_uid=1; acf_auth=x",
|
||||
assigned_to=owner.id,
|
||||
)
|
||||
db.add(account)
|
||||
db.flush()
|
||||
task = PlatformFulfillmentTask(
|
||||
source_order_id="ctx-order",
|
||||
source_order_item_id="ctx-item",
|
||||
platform_order_id="ctx-platform-order",
|
||||
account_id=account.id,
|
||||
execution_status="allocated",
|
||||
)
|
||||
db.add(task)
|
||||
db.commit()
|
||||
|
||||
with pytest.raises(HTTPException) as exc_info:
|
||||
_require_task_account_access(db, operator, [account.id])
|
||||
assert exc_info.value.status_code == 403
|
||||
|
||||
_require_task_account_access(
|
||||
db,
|
||||
operator,
|
||||
[account.id],
|
||||
platform_task_id=task.id,
|
||||
)
|
||||
batch_id, count = create_douyu_planned_tasks(
|
||||
db,
|
||||
[account.id],
|
||||
"query_elite_status",
|
||||
"elite",
|
||||
operator.id,
|
||||
platform_task_id=task.id,
|
||||
)
|
||||
assert count == 1
|
||||
assert (
|
||||
db.query(DouyuTask).filter(DouyuTask.batch_id == batch_id).one().platform_task_id
|
||||
== task.id
|
||||
)
|
||||
|
||||
other = Account(
|
||||
username="ctx-other-account",
|
||||
password="p",
|
||||
email="ctx-other@test.com",
|
||||
email_password="p",
|
||||
cookie="acf_uid=2; acf_auth=x",
|
||||
assigned_to=owner.id,
|
||||
)
|
||||
db.add(other)
|
||||
db.commit()
|
||||
with pytest.raises(HTTPException):
|
||||
_require_task_account_access(
|
||||
db,
|
||||
operator,
|
||||
[other.id],
|
||||
platform_task_id=task.id,
|
||||
)
|
||||
finally:
|
||||
db.rollback()
|
||||
db.close()
|
||||
|
||||
@@ -40,7 +40,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261118_0095"
|
||||
HEAD_REVISION = "20261118_0096"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
|
||||
@@ -743,6 +743,62 @@ class TestFulfillmentCollabSync(_Base):
|
||||
}
|
||||
|
||||
|
||||
class TestDouyuOperationTimeline(_Base):
|
||||
def test_operation_start_and_result_are_written_once(self):
|
||||
task = self.task()
|
||||
account = self.account("operation-timeline-account")
|
||||
task.account_id = account.id
|
||||
self.session.commit()
|
||||
douyu_task = DouyuTask(
|
||||
batch_id="platform-operation",
|
||||
account_id=account.id,
|
||||
account_assigned_to=account.assigned_to,
|
||||
task_type="create_gold_qr",
|
||||
handbook_scope="elite",
|
||||
status="planned",
|
||||
created_by=self.admin_id,
|
||||
platform_task_id=task.id,
|
||||
)
|
||||
self.session.add(douyu_task)
|
||||
self.session.commit()
|
||||
|
||||
service.record_douyu_operation_started(
|
||||
self.session,
|
||||
task,
|
||||
current=self.admin,
|
||||
batch_id=douyu_task.batch_id,
|
||||
task_type=douyu_task.task_type,
|
||||
douyu_task_ids=[douyu_task.id],
|
||||
payload={"amount": 18},
|
||||
)
|
||||
service.record_douyu_operation_result(
|
||||
self.session,
|
||||
douyu_task,
|
||||
status="success",
|
||||
message="充值鱼翅成功",
|
||||
)
|
||||
service.record_douyu_operation_result(
|
||||
self.session,
|
||||
douyu_task,
|
||||
status="success",
|
||||
message="充值鱼翅成功",
|
||||
)
|
||||
self.session.commit()
|
||||
|
||||
detail = service.get_task_detail(self.session, task.id)
|
||||
operation_events = [
|
||||
event
|
||||
for event in detail["events"]
|
||||
if event["eventType"].startswith("douyu_operation_")
|
||||
]
|
||||
assert [event["eventType"] for event in operation_events] == [
|
||||
"douyu_operation_succeeded",
|
||||
"douyu_operation_started",
|
||||
]
|
||||
assert operation_events[0]["detail"]["douyuTaskId"] == douyu_task.id
|
||||
assert operation_events[1]["detail"]["batchId"] == douyu_task.batch_id
|
||||
|
||||
|
||||
class TestSnapshotSync(_Base):
|
||||
def snapshot(self, **overrides):
|
||||
payload = {
|
||||
|
||||
Reference in New Issue
Block a user