本次提交包含多项代码优化与规范修正: 1. 文档与注释优化:修正注释术语、补充注解与FR编号 2. 代码格式调整:统一空格、换行与缩进规范 3. 类型与接口完善:补充__all__导出、修正返回类型注解 4. 错误处理增强:新增领域错误类与校验逻辑 5. 依赖与导入调整:修复路径引用、统一时区导入 6. 协议与契约更新:完善接口文档与一致性注解
79 lines
2.9 KiB
Python
79 lines
2.9 KiB
Python
"""服务账号 DTO。
|
||
|
||
定义服务账号及其创建命令的跨层共享不可变值对象。服务账号是渠道账户绑定的
|
||
系统 User(user_type=service),作为 Agent 执行的鉴权主体,满足会话归属
|
||
与用户存在性校验。所有 DTO 均为 ``dataclass(frozen=True)``,仅依赖标准库
|
||
与契约层内部类型。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from dataclasses import dataclass
|
||
|
||
from yuxi.channels.contract.dtos.channel import ChannelType
|
||
from yuxi.channels.contract.errors import ValidationError
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class ServiceAccount:
|
||
"""服务账号 DTO(契约层共享值对象)。
|
||
|
||
服务账号是渠道账户绑定的系统 User(user_type=service),作为 Agent
|
||
执行的鉴权主体,满足会话归属与用户存在性校验。
|
||
|
||
字段:
|
||
uid: 系统用户 UID(User 表主键标识)。
|
||
username: 用户名(系统登录名)。
|
||
channel_type: 渠道类型,标识该服务账号所属渠道。
|
||
account_id: 渠道账户 ID,关联 ChannelAccount。
|
||
department_id: 所属部门 ID(可选)。
|
||
"""
|
||
|
||
uid: str
|
||
username: str
|
||
channel_type: ChannelType
|
||
account_id: str
|
||
department_id: int | None = None
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class CreateServiceAccountCmd:
|
||
"""创建服务账号命令 DTO。
|
||
|
||
在 ``__post_init__`` 中校验必填字段与业务规则,在构造时即拦截非法值,
|
||
避免空值传播到仓储层后才暴露(INV-8)。
|
||
|
||
字段:
|
||
channel_type: 渠道类型,标识服务账号所属渠道。
|
||
account_id: 渠道账户 ID,关联 ChannelAccount。
|
||
department_id: 所属部门 ID(可选)。
|
||
uid: 服务账号唯一标识,格式 ``svc:channel:{channel_type}:{account_id}``。
|
||
由应用层调 ``ServiceAccount.create()`` 聚合根工厂生成后传入,
|
||
适配器直接消费不再 import core.model(§4.6 / INV-8)。
|
||
username: 服务账号用户名,格式
|
||
``svc_channel_{channel_type}_{account_id_short}``,
|
||
account_id_short 为 account_id 的 sha256 前 8 位。
|
||
"""
|
||
|
||
channel_type: ChannelType
|
||
account_id: str
|
||
uid: str
|
||
username: str
|
||
department_id: int | None = None
|
||
|
||
def __post_init__(self) -> None:
|
||
"""校验必填字段非空。
|
||
|
||
``channel_type`` 不可为 ``None``,``account_id`` / ``uid`` /
|
||
``username`` 必须为非空字符串;违规抛 ``ValidationError``,在构造时
|
||
即拦截(INV-8)。
|
||
"""
|
||
if self.channel_type is None:
|
||
raise ValidationError("channel_type", "must not be None")
|
||
if not self.account_id:
|
||
raise ValidationError("account_id", "must not be empty")
|
||
if not self.uid:
|
||
raise ValidationError("uid", "must not be empty")
|
||
if not self.username:
|
||
raise ValidationError("username", "must not be empty")
|