fix(training): 出国规章企微卡片改跳通用入口聊天页
This commit is contained in:
1 parent
80d78130f8
commit
89a033f0f9
3 files changed
+72
-2
No files matched your search
@@ -90,6 +90,30 @@ def _is_overseas_regulations_training(training_type: str) -> bool:
|
||||
return "出国规章培训" in str((published or {}).get("name") or "")
|
||||
|
||||
|
||||
def _resolve_overseas_jump_agent_id() -> Optional[int]:
|
||||
"""
|
||||
解析出国规章企微卡片落地聊天页的「通用入口」Agent ID。
|
||||
|
||||
用途:企微 textcard 点击后应进入 /chat/<通用入口> 聊天页(学习之窗入口所在页),
|
||||
而 /workspace 是应用管理工作台页,不符合培训通知落地预期。
|
||||
核心逻辑:各环境 Agent ID 不同,禁止硬编码;在启用 Agent 中优先按
|
||||
handler_config.content_profile='generic_entrance' 匹配,兜底按名称精确匹配「通用入口」。
|
||||
Returns:
|
||||
通用入口 Agent ID;解析失败返回 None,调用方回退 /workspace。
|
||||
"""
|
||||
agents = db.session.execute(
|
||||
select(Agent).where(Agent.is_active.is_(True)).order_by(Agent.sort_order, Agent.id)
|
||||
).scalars().all()
|
||||
for agent in agents:
|
||||
handler_config = agent.handler_config if isinstance(agent.handler_config, dict) else {}
|
||||
if str(handler_config.get("content_profile") or "").strip() == "generic_entrance":
|
||||
return int(agent.id)
|
||||
for agent in agents:
|
||||
if str(agent.name or "").strip() == "通用入口":
|
||||
return int(agent.id)
|
||||
return None
|
||||
|
||||
|
||||
def _normalize_training_version(value: Any, training_type: str) -> str:
|
||||
"""
|
||||
规范培训版本号,未传时按培训类型补默认值。
|
||||
@@ -612,12 +636,14 @@ def upsert_user_training_record(user: User, payload: dict[str, Any]) -> Training
|
||||
db.session.commit()
|
||||
if stage == "result" and _is_overseas_regulations_training(training_type):
|
||||
try:
|
||||
# [STEP] 企微卡片落地页:优先跳「通用入口」聊天页(/chat/<id>);解析失败回退 /workspace 保持历史行为
|
||||
jump_agent_id = _resolve_overseas_jump_agent_id()
|
||||
send_wecom_event_message(
|
||||
event_type="training_overseas_thailand_guidelines",
|
||||
user=user,
|
||||
title="中国员工出差泰国的行为准则",
|
||||
content="中国员工出差泰国的行为准则",
|
||||
jump_path="/workspace",
|
||||
jump_path=f"/chat/{jump_agent_id}" if jump_agent_id else "/workspace",
|
||||
)
|
||||
db.session.commit()
|
||||
except Exception:
|
||||
|
||||
@@ -305,6 +305,50 @@ def test_overseas_regulations_video_completion_sends_wecom_notice_each_time(clie
|
||||
assert all(message["content"] == "中国员工出差泰国的行为准则" for message in sent_messages)
|
||||
|
||||
|
||||
def test_overseas_regulations_wecom_notice_jumps_to_generic_entrance_chat(client, app, monkeypatch):
|
||||
"""关键断言:出国规章完成的企微通知必须跳转「通用入口」聊天页(/chat/<id>),不得落入 /workspace 应用管理页。"""
|
||||
sent_messages = []
|
||||
monkeypatch.setattr(
|
||||
training_service,
|
||||
"send_wecom_event_message",
|
||||
lambda **kwargs: sent_messages.append(kwargs) or {"ok": True, "trace_id": "training-notice-trace"},
|
||||
)
|
||||
with app.app_context():
|
||||
user = _create_user(username="overseas_wecom_jump")
|
||||
entrance_agent = Agent(
|
||||
name="通用入口",
|
||||
type="api",
|
||||
agent_code="generic",
|
||||
runtime_provider="dify",
|
||||
conversation_mode="chat",
|
||||
dify_app_type="chatflow",
|
||||
handler_config={"content_profile": "generic_entrance"},
|
||||
)
|
||||
db.session.add(entrance_agent)
|
||||
db.session.add(TrainingConfig(
|
||||
training_type="overseas_jump_v1",
|
||||
name="出国规章培训",
|
||||
version="v1",
|
||||
status="published",
|
||||
))
|
||||
db.session.commit()
|
||||
entrance_agent_id = entrance_agent.id
|
||||
|
||||
response = client.put("/api/user/training-record/progress", headers=_auth_headers(user), json={
|
||||
"type": "overseas_jump_v1",
|
||||
"stage": "result",
|
||||
"score": 100,
|
||||
"passed": True,
|
||||
"attempt_count": 0,
|
||||
"answer_details": [],
|
||||
})
|
||||
|
||||
assert response.status_code == 200
|
||||
assert len(sent_messages) == 1
|
||||
# [ASSERT] 卡片跳转路径必须指向通用入口聊天页,员工点击后进入图二页面而非应用管理工作台
|
||||
assert sent_messages[0]["jump_path"] == f"/chat/{entrance_agent_id}"
|
||||
|
||||
|
||||
def test_safety_training_progress_round_trip(client, app):
|
||||
"""安全培训应持久化培训版本、考试模式、三级状态和级别进度。"""
|
||||
with app.app_context():
|
||||
|
||||
Reference in new issue
Block a user