Commit 3ecf63f4 by imwangxiaozhou Committed by 胡悦

fix(wecom客户群服务): 修复企业微信群消息的代理商获取逻辑并更新测试用例

- 新增`member_bot_ids`字段到ResolvedCustomerGroupBinding绑定类,存储客户群关联的机器人ID列表
- 替换旧的`_build_position_agents`方法为`_build_agents_from_member_bots`,通过成员机器人ID获取正确的代理商信息
- 移除接口返回中废弃的`bot_code`、`position`等旧字段
- 更新测试用例以匹配新的接口返回格式
1 parent 96689555
...@@ -158,6 +158,7 @@ class ResolvedCustomerGroupBinding: ...@@ -158,6 +158,7 @@ class ResolvedCustomerGroupBinding:
position_id: int position_id: int
position_code: str position_code: str
position_name: str position_name: str
member_bot_ids: list[str] | None = None
class WeComCustomerGroupOrchestrationService: class WeComCustomerGroupOrchestrationService:
...@@ -643,7 +644,7 @@ class WeComCustomerGroupOrchestrationService: ...@@ -643,7 +644,7 @@ class WeComCustomerGroupOrchestrationService:
"group_contact_id": binding.group_contact_id, "group_contact_id": binding.group_contact_id,
"group_name": binding.group_name, "group_name": binding.group_name,
}, },
"agents": self._build_position_agents(binding.position_id), "agents": self._build_agents_from_member_bots(binding.member_bot_ids or []),
"messages": [ "messages": [
{ {
"msg_id": item.get("msg_id"), "msg_id": item.get("msg_id"),
...@@ -1185,6 +1186,7 @@ class WeComCustomerGroupOrchestrationService: ...@@ -1185,6 +1186,7 @@ class WeComCustomerGroupOrchestrationService:
position_id=position.id, position_id=position.id,
position_code=position.code, position_code=position.code,
position_name=position.name, position_name=position.name,
member_bot_ids=customer_group.member_bot_ids or [],
) )
return None return None
...@@ -1376,14 +1378,8 @@ class WeComCustomerGroupOrchestrationService: ...@@ -1376,14 +1378,8 @@ class WeComCustomerGroupOrchestrationService:
"group": { "group": {
"group_contact_id": binding.group_contact_id, "group_contact_id": binding.group_contact_id,
"group_name": binding.group_name, "group_name": binding.group_name,
"bot_code": binding.bot_code,
}, },
"position": { "agents": self._build_agents_from_member_bots(binding.member_bot_ids or []),
"position_id": binding.position_id,
"position_code": binding.position_code,
"position_name": binding.position_name,
},
"agents": self._build_position_agents(binding.position_id),
"messages": [ "messages": [
{ {
"msg_id": message.msg_id, "msg_id": message.msg_id,
...@@ -1398,15 +1394,29 @@ class WeComCustomerGroupOrchestrationService: ...@@ -1398,15 +1394,29 @@ class WeComCustomerGroupOrchestrationService:
], ],
} }
def _build_position_agents(self, position_id: int) -> list[dict[str, Any]]: def _build_agents_from_member_bots(self, member_bot_ids: list[str]) -> list[dict[str, Any]]:
"""从群的 member_bot_ids 查询关联的 Agent 信息。
流程:member_bot_ids → WeComBot → Agent
member_bot_ids 存的是 WeComBot.bot_id,通过它查到 WeComBot.id,
再通过 Agent.managed_user_id 关联到 Agent。
"""
cleaned_ids = [item.strip() for item in member_bot_ids if str(item).strip()]
if not cleaned_ids:
return []
bot_rows = self.db.query(WeComBot).filter(WeComBot.bot_id.in_(cleaned_ids)).all()
bot_row_ids = [row.id for row in bot_rows if row.id is not None]
if not bot_row_ids:
return []
bot_id_map: dict[int, str] = {row.id: row.bot_id for row in bot_rows}
agent_rows = ( agent_rows = (
self.db.query(Agent, WeComBot) self.db.query(Agent)
.join(AgentPosition, AgentPosition.agent_id == Agent.id)
.join(WeComBot, WeComBot.id == Agent.managed_user_id)
.filter( .filter(
AgentPosition.position_id == position_id, Agent.managed_user_id.in_(bot_row_ids),
Agent.is_deleted == False, Agent.is_deleted == False,
Agent.status == AgentStatus.enable,
Agent.managed_user_id.isnot(None), Agent.managed_user_id.isnot(None),
) )
.all() .all()
...@@ -1414,11 +1424,12 @@ class WeComCustomerGroupOrchestrationService: ...@@ -1414,11 +1424,12 @@ class WeComCustomerGroupOrchestrationService:
return [ return [
{ {
"agent_id": agent.id, "agent_id": agent.id,
"agent_name": agent.name, "bot_id": bot_id_map.get(agent.managed_user_id, ""),
"bot_id": bot.bot_id,
"is_steward": bool(agent.is_steward), "is_steward": bool(agent.is_steward),
"status": agent.status.value if agent.status else "enable",
} }
for agent, bot in agent_rows for agent in agent_rows
if agent.managed_user_id in bot_id_map
] ]
def _resolve_message_group_binding(self, message: WeComMessage) -> ResolvedCustomerGroupBinding | None: def _resolve_message_group_binding(self, message: WeComMessage) -> ResolvedCustomerGroupBinding | None:
...@@ -1446,6 +1457,7 @@ class WeComCustomerGroupOrchestrationService: ...@@ -1446,6 +1457,7 @@ class WeComCustomerGroupOrchestrationService:
position_id=position.id, position_id=position.id,
position_code=position.code, position_code=position.code,
position_name=position.name, position_name=position.name,
member_bot_ids=customer_group.member_bot_ids or [],
) )
bot = self.db.query(WeComBot).filter(WeComBot.bot_id == message.bot_id).first() bot = self.db.query(WeComBot).filter(WeComBot.bot_id == message.bot_id).first()
......
...@@ -234,7 +234,7 @@ def test_dispatch_customer_group_messages_prefers_customer_group_id_and_marks_me ...@@ -234,7 +234,7 @@ def test_dispatch_customer_group_messages_prefers_customer_group_id_and_marks_me
'group_name': 'alice-service', 'group_name': 'alice-service',
}, },
'agents': [ 'agents': [
{'agent_id': 10, 'bot_id': 'managed_1', 'is_steward': False, 'status': 'enable', 'is_deleted': False}, {'agent_id': 10, 'bot_id': 'managed_1', 'is_steward': False, 'status': 'enable'},
], ],
'messages': [ 'messages': [
{ {
...@@ -993,7 +993,7 @@ def test_pull_group_messages_dispatches_customer_group_payload(): ...@@ -993,7 +993,7 @@ def test_pull_group_messages_dispatches_customer_group_payload():
'group_name': '张三专属服务群', 'group_name': '张三专属服务群',
}, },
'agents': [ 'agents': [
{'agent_id': 10, 'bot_id': 'managed_1', 'is_steward': False, 'status': 'enable', 'is_deleted': False}, {'agent_id': 10, 'bot_id': 'managed_1', 'is_steward': False, 'status': 'enable'},
], ],
'messages': [ 'messages': [
{ {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!