Files
lingxi/apps/api/src/modules/identity/user.mapper.ts
T
Stev_Wang 4d9eb980af
ci / 质量门禁 (push) Canceled after 0s
feat(identity): 完成 A1 身份认证与用户管理全栈落地
背景:
- 开发计划 A1 批次要求登录/刷新/登出、用户生命周期、路由与按钮权限、
  权限三态用例;视觉已选定方案 B(主从分栏 + 分屏登录)。

改动:
- packages/shared:新增 identity 契约(User/Me/Login/Refresh/Department/Role)
  与 user:view 权限点;写接口 strict 校验。
- apps/api:新增 identity 模块与 Prisma expand 迁移(departments/users/roles/
  user_roles/role_permissions/refresh_tokens);Argon2id、JWT access+refresh
  旋转与 jti 吊销、登录失败锁定、首登强改、守卫链、/me、@Audit 上下文、幂等种子。
- apps/web:按方案 B 落地 /users、/users/new、/users/:id、分屏登录与 403;
  usePermission/<Can>、路由守卫、401 单飞刷新、停用会话兜底、Semi + token。
- e2e:新增 A1-QA-01 六类端到端用例(A1_E2E 门控)。
- openapi 与前端生成类型同步;.env.example 登记认证变量;实施报告入库。

影响面:
- 新增运行时依赖 @nestjs/jwt、argon2(计划 A1-BE-02 已批准,版本精确锁定)。
- 迁移为 expand,不改既有表;本地需 prisma migrate 后重新 seed。
- 平台导航「用户与组织」由 /platform/users 调整为 /users。

回滚方式:
- 代码整体回滚本提交;迁移为 expand,回滚代码不需删表;
- 本地如需彻底回退按实施报告 §6 处理。

验证证据:
- pnpm format:check / lint / typecheck 通过;
- pnpm test:shared 95 / api 60 / web 55;
- pnpm test:int:7 个(真实 PG/Redis);
- A1_E2E=1 playwright test e2e/a1-identity.spec.ts:6/6;
- 报告:docs/开发计划/灵犀_A1身份认证与用户管理_实施报告_V0.1.md
2026-11-03 00:03:33 +08:00

87 lines
2.5 KiB
TypeScript

import type { RoleSummary, UserDetail, UserSummary } from '@lingxi/shared';
import type { Prisma, Role, UserStatus as PrismaUserStatus } from '@prisma/client';
/** 用户列表 / 详情需要的关联数据(部门含父级、角色) */
export type UserWithRelations = Prisma.UserGetPayload<{
include: {
department: { include: { parent: true } };
roles: { include: { role: true } };
};
}>;
export function iso(value: Date): string {
return value.toISOString();
}
export function isoOrNull(value: Date | null): string | null {
return value === null ? null : value.toISOString();
}
/** 手机号脱敏:138****2046;全号只在具备 user:manage 时由详情接口附带 */
export function maskPhone(phone: string): string {
return `${phone.slice(0, 3)}****${phone.slice(7)}`;
}
export function toRoleSummary(role: Role): RoleSummary {
return {
id: role.id,
code: role.code,
name: role.name,
isSystem: role.isSystem,
...(role.description.length > 0 ? { description: role.description } : {}),
};
}
export function toApiStatus(status: PrismaUserStatus): UserSummary['status'] {
return status === 'ACTIVE' ? 'active' : 'disabled';
}
function departmentFields(
user: UserWithRelations,
): Pick<UserSummary, 'departmentId' | 'departmentName' | 'groupId' | 'groupName'> {
const leaf = user.department;
const parent = leaf.parent;
if (leaf.parentId !== null && parent !== null) {
return {
departmentId: parent.id,
departmentName: parent.name,
groupId: leaf.id,
groupName: leaf.name,
};
}
return {
departmentId: leaf.id,
departmentName: leaf.name,
groupId: null,
groupName: null,
};
}
export function toUserSummary(user: UserWithRelations): UserSummary {
return {
id: user.id,
name: user.name,
phoneMasked: maskPhone(user.phone),
account: user.account,
...departmentFields(user),
roles: user.roles
.filter((item) => item.role.status === 'ACTIVE')
.map((item) => toRoleSummary(item.role)),
status: toApiStatus(user.status),
lastLoginAt: isoOrNull(user.lastLoginAt),
createdAt: iso(user.createdAt),
};
}
/** 详情:phone 全号仅调用方具备 user:manage 时返回(契约铁律) */
export function toUserDetail(user: UserWithRelations, canSeePhone: boolean): UserDetail {
return {
...toUserSummary(user),
...(canSeePhone ? { phone: user.phone } : {}),
mustChangePassword: user.mustChangePassword,
updatedAt: iso(user.updatedAt),
lastLoginIp: user.lastLoginIp,
disabledAt: isoOrNull(user.disabledAt),
};
}