2026-05-30 21:53:09 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-05-31 21:42:03 +08:00
|
|
|
from yuxi.channel.domain.exception.duplicate_binding import DuplicateBindingException
|
2026-05-30 21:53:09 +08:00
|
|
|
from yuxi.channel.domain.model.binding.channel_binding import ChannelBinding
|
|
|
|
|
from yuxi.channel.domain.repository.binding_repository import BindingRepositoryPort
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BindingService:
|
|
|
|
|
def __init__(self, binding_repo: BindingRepositoryPort) -> None:
|
|
|
|
|
self._repo = binding_repo
|
|
|
|
|
|
|
|
|
|
async def create(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
channel_type: str,
|
|
|
|
|
account_id: str,
|
|
|
|
|
group_id: str,
|
|
|
|
|
agent_config_id: int,
|
2026-05-31 21:42:03 +08:00
|
|
|
created_by: str | None = None,
|
2026-05-30 21:53:09 +08:00
|
|
|
) -> ChannelBinding:
|
2026-05-31 21:42:03 +08:00
|
|
|
existing = await self._repo.find_active_binding(channel_type=channel_type, account_id=account_id, group_id=group_id)
|
|
|
|
|
if existing:
|
|
|
|
|
raise DuplicateBindingException(channel_type, account_id, group_id)
|
2026-05-30 21:53:09 +08:00
|
|
|
return await self._repo.create_binding(
|
|
|
|
|
channel_type=channel_type,
|
|
|
|
|
account_id=account_id,
|
|
|
|
|
group_id=group_id,
|
|
|
|
|
agent_config_id=agent_config_id,
|
2026-05-31 21:42:03 +08:00
|
|
|
created_by=created_by,
|
2026-05-30 21:53:09 +08:00
|
|
|
)
|
|
|
|
|
|
2026-05-31 21:42:03 +08:00
|
|
|
async def update(
|
|
|
|
|
self,
|
|
|
|
|
binding_id: int,
|
|
|
|
|
*,
|
|
|
|
|
agent_config_id: int | None = None,
|
|
|
|
|
is_enabled: bool | None = None,
|
|
|
|
|
) -> ChannelBinding | None:
|
|
|
|
|
return await self._repo.update_binding(binding_id, agent_config_id=agent_config_id, is_enabled=is_enabled)
|
|
|
|
|
|
|
|
|
|
async def delete(self, binding_id: int, *, updated_by: str | None = None) -> ChannelBinding | None:
|
|
|
|
|
return await self._repo.delete_binding(binding_id, updated_by=updated_by)
|
2026-05-30 21:53:09 +08:00
|
|
|
|
|
|
|
|
async def get(self, binding_id: int) -> ChannelBinding | None:
|
|
|
|
|
return await self._repo.get_binding(binding_id)
|
|
|
|
|
|
|
|
|
|
async def list(
|
|
|
|
|
self, *, channel_type: str | None = None, offset: int = 0, limit: int = 50
|
|
|
|
|
) -> tuple[list[ChannelBinding], int]:
|
|
|
|
|
return await self._repo.list_bindings(channel_type=channel_type, offset=offset, limit=limit)
|