Commit eb181ce9 by imwangxiaozhou Committed by 胡悦

refactor(企业微信同步): 重构同步接口,统一使用list_contacts并支持contact_type参数

- 更新协议定义,新增带可选contact_type参数的list_contacts方法
- 优化HTTP客户端实现,添加contact_type参数处理逻辑
- 将业务层的list_customers调用替换为list_contacts,移除无用的起止时间参数
- 更新所有测试用例的FakeClient及mock签名以适配新接口
1 parent 6a67c1ff
...@@ -36,11 +36,11 @@ class HttpWeComSyncClient(WeComSyncClient): ...@@ -36,11 +36,11 @@ class HttpWeComSyncClient(WeComSyncClient):
def list_bots(self, *, page: int, page_size: int) -> dict[str, Any]: def list_bots(self, *, page: int, page_size: int) -> dict[str, Any]:
return self._get("/api/v1/business/bots", {"page": page, "page_size": page_size}) return self._get("/api/v1/business/bots", {"page": page, "page_size": page_size})
def list_contacts(self, *, bot_id: str, page: int, page_size: int) -> dict[str, Any]: def list_contacts(self, *, bot_id: str, page: int, page_size: int, contact_type: str | None = None) -> dict[str, Any]:
return self._get( params: dict[str, Any] = {"bot_id": bot_id, "page": page, "page_size": page_size}
"/api/v1/business/contacts", if contact_type:
{"bot_id": bot_id, "page": page, "page_size": page_size}, params["contact_type"] = contact_type
) return self._get("/api/v1/business/contacts", params)
def list_customers( def list_customers(
self, self,
......
...@@ -277,10 +277,8 @@ class WeComCustomerGroupOrchestrationService: ...@@ -277,10 +277,8 @@ class WeComCustomerGroupOrchestrationService:
remote_set: dict[str, dict] = {} remote_set: dict[str, dict] = {}
page = 1 page = 1
while True: while True:
payload = self.client.list_customers( payload = self.client.list_contacts(
bot_id=bot.bot_id, bot_id=bot.bot_id,
start_time=None,
end_time=None,
page=page, page=page,
page_size=self.page_size, page_size=self.page_size,
contact_type=self._customer_contact_type, contact_type=self._customer_contact_type,
...@@ -352,10 +350,8 @@ class WeComCustomerGroupOrchestrationService: ...@@ -352,10 +350,8 @@ class WeComCustomerGroupOrchestrationService:
remote_set: dict[str, dict] = {} remote_set: dict[str, dict] = {}
page = 1 page = 1
while True: while True:
payload = self.client.list_customers( payload = self.client.list_contacts(
bot_id=bot.bot_id, bot_id=bot.bot_id,
start_time=None,
end_time=None,
page=page, page=page,
page_size=self.page_size, page_size=self.page_size,
contact_type=self._customer_contact_type, contact_type=self._customer_contact_type,
......
...@@ -57,6 +57,9 @@ class WeComSyncClient(Protocol): ...@@ -57,6 +57,9 @@ class WeComSyncClient(Protocol):
def list_bots(self, *, page: int, page_size: int) -> dict[str, Any]: def list_bots(self, *, page: int, page_size: int) -> dict[str, Any]:
... ...
def list_contacts(self, *, bot_id: str, page: int, page_size: int, contact_type: str | None = None) -> dict[str, Any]:
...
def list_customers( def list_customers(
self, self,
*, *,
......
...@@ -56,7 +56,7 @@ class FakeClient: ...@@ -56,7 +56,7 @@ class FakeClient:
def list_bots(self, *, page: int, page_size: int): def list_bots(self, *, page: int, page_size: int):
return {'items': [], 'meta': {'total': 0, 'page': 1, 'page_size': page_size}} return {'items': [], 'meta': {'total': 0, 'page': 1, 'page_size': page_size}}
def list_contacts(self, *, bot_id: str, page: int, page_size: int): def list_contacts(self, *, bot_id: str, page: int, page_size: int, contact_type: str | None = None):
return {'items': [], 'meta': {'total': 0, 'page': 1, 'page_size': page_size}} return {'items': [], 'meta': {'total': 0, 'page': 1, 'page_size': page_size}}
def list_customers(self, *, bot_id: str, start_time, end_time, page: int, page_size: int, contact_type=None, keyword=None): def list_customers(self, *, bot_id: str, start_time, end_time, page: int, page_size: int, contact_type=None, keyword=None):
...@@ -262,7 +262,7 @@ def test_list_new_customers_returns_candidates_without_writing_groups(): ...@@ -262,7 +262,7 @@ def test_list_new_customers_returns_candidates_without_writing_groups():
_seed_group_context(db) _seed_group_context(db)
class CandidateClient(FakeClient): class CandidateClient(FakeClient):
def list_customers(self, *, bot_id: str, start_time, end_time, page: int, page_size: int, contact_type=None): def list_contacts(self, *, bot_id: str, page: int, page_size: int, contact_type: str | None = None):
return { return {
'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}], 'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}],
'meta': {'total': 1, 'page': 1, 'page_size': page_size}, 'meta': {'total': 1, 'page': 1, 'page_size': page_size},
...@@ -304,7 +304,7 @@ def test_list_new_customers_skips_customer_when_group_already_exists(): ...@@ -304,7 +304,7 @@ def test_list_new_customers_skips_customer_when_group_already_exists():
db.commit() db.commit()
class CandidateClient(FakeClient): class CandidateClient(FakeClient):
def list_customers(self, *, bot_id: str, start_time, end_time, page: int, page_size: int, contact_type=None): def list_contacts(self, *, bot_id: str, page: int, page_size: int, contact_type: str | None = None):
return { return {
'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}], 'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}],
'meta': {'total': 1, 'page': 1, 'page_size': page_size}, 'meta': {'total': 1, 'page': 1, 'page_size': page_size},
...@@ -340,7 +340,7 @@ def test_list_new_customers_skips_customer_when_group_status_is_not_invalid(): ...@@ -340,7 +340,7 @@ def test_list_new_customers_skips_customer_when_group_status_is_not_invalid():
db.commit() db.commit()
class CandidateClient(FakeClient): class CandidateClient(FakeClient):
def list_customers(self, *, bot_id: str, start_time, end_time, page: int, page_size: int, contact_type=None): def list_contacts(self, *, bot_id: str, page: int, page_size: int, contact_type: str | None = None):
return { return {
'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}], 'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}],
'meta': {'total': 1, 'page': 1, 'page_size': page_size}, 'meta': {'total': 1, 'page': 1, 'page_size': page_size},
...@@ -376,7 +376,7 @@ def test_list_new_customers_allows_customer_when_group_status_is_invalid(): ...@@ -376,7 +376,7 @@ def test_list_new_customers_allows_customer_when_group_status_is_invalid():
db.commit() db.commit()
class CandidateClient(FakeClient): class CandidateClient(FakeClient):
def list_customers(self, *, bot_id: str, start_time, end_time, page: int, page_size: int, contact_type=None): def list_contacts(self, *, bot_id: str, page: int, page_size: int, contact_type: str | None = None):
return { return {
'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}], 'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}],
'meta': {'total': 1, 'page': 1, 'page_size': page_size}, 'meta': {'total': 1, 'page': 1, 'page_size': page_size},
...@@ -648,7 +648,7 @@ def test_sync_customer_groups_skips_customer_when_group_exists(): ...@@ -648,7 +648,7 @@ def test_sync_customer_groups_skips_customer_when_group_exists():
db.commit() db.commit()
class CandidateClient(FakeClient): class CandidateClient(FakeClient):
def list_customers(self, *, bot_id: str, start_time, end_time, page: int, page_size: int, contact_type=None, keyword=None): def list_contacts(self, *, bot_id: str, page: int, page_size: int, contact_type: str | None = None):
return { return {
'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}], 'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}],
'meta': {'total': 1, 'page': 1, 'page_size': page_size}, 'meta': {'total': 1, 'page': 1, 'page_size': page_size},
...@@ -698,6 +698,12 @@ def test_sync_customer_groups_skips_customer_when_pending_request_exists(): ...@@ -698,6 +698,12 @@ def test_sync_customer_groups_skips_customer_when_pending_request_exists():
'meta': {'total': 1, 'page': 1, 'page_size': page_size}, 'meta': {'total': 1, 'page': 1, 'page_size': page_size},
} }
def list_contacts(self, *, bot_id: str, page: int, page_size: int, contact_type: str | None = None):
return {
'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}],
'meta': {'total': 1, 'page': 1, 'page_size': page_size},
}
client = CandidateClient() client = CandidateClient()
sync_service = WeComSyncService(db, client, NoopWeComMessageDispatcher()) sync_service = WeComSyncService(db, client, NoopWeComMessageDispatcher())
orchestration_service = WeComCustomerGroupOrchestrationService(db, client, sync_service) orchestration_service = WeComCustomerGroupOrchestrationService(db, client, sync_service)
...@@ -738,7 +744,7 @@ def test_sync_customer_groups_invalidates_timeout_pending_request_before_recreat ...@@ -738,7 +744,7 @@ def test_sync_customer_groups_invalidates_timeout_pending_request_before_recreat
db.commit() db.commit()
class CandidateClient(FakeClient): class CandidateClient(FakeClient):
def list_customers(self, *, bot_id: str, start_time, end_time, page: int, page_size: int, contact_type=None, keyword=None): def list_contacts(self, *, bot_id: str, page: int, page_size: int, contact_type: str | None = None):
return { return {
'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}], 'items': [{'contact_id': 'cust_1', 'contact_name': 'alice'}],
'meta': {'total': 1, 'page': 1, 'page_size': page_size}, 'meta': {'total': 1, 'page': 1, 'page_size': page_size},
......
...@@ -34,6 +34,9 @@ class FakeWeComSyncClient(WeComSyncClient): ...@@ -34,6 +34,9 @@ class FakeWeComSyncClient(WeComSyncClient):
def list_bots(self, *, page: int, page_size: int) -> dict[str, Any]: def list_bots(self, *, page: int, page_size: int) -> dict[str, Any]:
return _paginate(self.bots, page, page_size) return _paginate(self.bots, page, page_size)
def list_contacts(self, *, bot_id: str, page: int, page_size: int, contact_type: str | None = None) -> dict[str, Any]:
return {"items": [], "meta": {"total": 0, "page": page, "page_size": page_size}}
def list_messages( def list_messages(
self, self,
*, *,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!