feat(identity): 补齐部门管理(两级组织树、停用启用)
ci / 质量门禁 (push) Canceled after 0s

背景:
- A1 只实现了部门树读取与新建 / 改名接口,没有部门管理入口,
  负责人、排序、停用状态都无法维护;
- 用户必须关联部门,部门无法维护会让 A2 的数据范围缺少组织基础。

改动:
- shared:DepartmentNode 增加 status / sortOrder / directMemberCount;
  新增 createDepartmentInput / updateDepartmentInput / departmentTreeQuery 契约;
- api:GET /departments/tree?include_disabled;POST/PATCH /departments
  (名称 / 负责人 / 排序);POST /departments/{id}/disable|enable;
  只停用不删除,有在职成员 / 启用子部门 / 下级在职成员时禁止停用;
  新建 / 编辑用户校验部门必须启用;@Audit 与 OpenAPI 同步;
- web:管理中心新增「部门管理」/system/departments,左两级组织树 +
  右详情 / 内联编辑 + 新建部门/小组 + 停用启用二次确认;显示已停用、
  ?id= 选中可恢复、无 user:manage 只读;用户页部门选择仍只取启用部门;
- 测试:shared 契约、api 单测 + int、web 树/表单/接口单测同步。

影响面:
- 新增部门写接口与页面;GET tree 默认仍只返回启用部门,对用户页无破坏;
- 权限复用 user:view / user:manage,不新增权限点;
- 无数据库迁移(status / leader_user_id / sort_order 列已存在)。

回滚方式:
- 回滚本提交即移除部门管理页与写接口,保留原只读树;数据库无需回滚。

验证证据:
- pnpm format:check / lint / typecheck 全绿;
- pnpm test:shared 121 / api 90 / web 120;test:int 4 files / 33 tests;
- pnpm openapi:diff:无破坏性变更;
- 真实 API + 浏览器(jiang.lan):新建一级部门 / 小组、改名、停用空小组、
  重名 40903、树/详情/停用确认均正常;
  截图 design/a1-ui-v1/shots/impl/70-departments.png。
This commit is contained in:
Stev_Wang committed 2026-11-03 15:01:07 +08:00
1 parent f4da1f3c02
commit 6661c42e84
33 files changed
+3237 -125

No files matched your search

+32 -1
View File
@@ -4,6 +4,7 @@ import { ulid } from '../ulid.js';
import {
accountSchema,
changePasswordInputSchema,
createDepartmentInputSchema,
createUserInputSchema,
DATA_SCOPE_TYPES,
dataScopeSchema,
@@ -22,6 +23,7 @@ import {
roleSummarySchema,
tokenPairSchema,
transferCustomersInputSchema,
updateDepartmentInputSchema,
updateUserInputSchema,
USER_LIST_SORTS,
USER_LIST_STATUSES,
@@ -183,7 +185,10 @@ describe('departmentNodeSchema', () => {
name: '运营中心',
parentId: null,
leader: null,
status: 'active',
sortOrder: 0,
memberCount: 0,
directMemberCount: 0,
createdAt: ISO,
});
expect(parsed.parentId).toBeNull();
@@ -196,19 +201,25 @@ describe('departmentNodeSchema', () => {
name: '投放一组',
parentId: ulid(),
leader: { id: ulid(), name: '王五' },
status: 'active',
sortOrder: 1,
memberCount: 12,
directMemberCount: 12,
createdAt: ISO,
});
expect(parsed.leader?.name).toBe('王五');
});
it('拒绝负数 / 非整数人数、非法 parentId、负责人缺姓名', () => {
it('拒绝负数 / 非整数人数、非法 parentId、负责人缺姓名、非法状态', () => {
const base = {
id: ulid(),
name: '运营中心',
parentId: null,
leader: null,
status: 'active',
sortOrder: 0,
memberCount: 3,
directMemberCount: 3,
createdAt: ISO,
};
expect(() => departmentNodeSchema.parse({ ...base, memberCount: -1 })).toThrow();
@@ -217,6 +228,26 @@ describe('departmentNodeSchema', () => {
expect(() =>
departmentNodeSchema.parse({ ...base, leader: { id: ulid(), name: '' } }),
).toThrow();
expect(() => departmentNodeSchema.parse({ ...base, status: 'offline' })).toThrow();
});
});
describe('部门写接口契约', () => {
it('新建部门:名称必填、parentId 可空、排序与负责人可选', () => {
const parsed = createDepartmentInputSchema.parse({ name: '运营中心' });
expect(parsed.parentId).toBeNull();
expect(() => createDepartmentInputSchema.parse({ name: '' })).toThrow();
expect(() =>
createDepartmentInputSchema.parse({ name: '运营中心', parentId: 'bad' }),
).toThrow();
expect(() => createDepartmentInputSchema.parse({ name: '运营中心', unknown: true })).toThrow();
});
it('更新部门:只允许名称 / 负责人 / 排序,未知字段拒绝', () => {
const parsed = updateDepartmentInputSchema.parse({ name: '运营中心', sortOrder: 2 });
expect(parsed.sortOrder).toBe(2);
expect(() => updateDepartmentInputSchema.parse({ parentId: ulid() })).toThrow();
expect(() => updateDepartmentInputSchema.parse({ sortOrder: -1 })).toThrow();
});
});
+56 -1
View File
@@ -82,7 +82,26 @@ export type RoleSummary = z.infer<typeof roleSummarySchema>;
// 部门
// ---------------------------------------------------------------------------
/** 部门 / 小组节点;parentId 为 null 表示一级部门(最多两级,见 MAX_DEPARTMENT_DEPTH) */
/** 部门状态:停用后不出现在新建 / 编辑用户的部门选择中,历史数据保留 */
export const DEPARTMENT_STATUSES = ['active', 'disabled'] as const;
export const departmentStatusSchema = z.enum(DEPARTMENT_STATUSES, {
errorMap: () => ({ message: '部门状态只能是 active 或 disabled' }),
});
export type DepartmentStatus = z.infer<typeof departmentStatusSchema>;
/** 部门名称:1–20 个字符(同级唯一由服务端校验) */
export const departmentNameSchema = z
.string()
.trim()
.min(1, '请输入部门名称')
.max(20, '部门名称最多 20 个字符');
/**
* 部门 / 小组节点;parentId 为 null 表示一级部门(最多两级,见 MAX_DEPARTMENT_DEPTH)。
* memberCount 含下级成员,directMemberCount 仅本部门直属成员。
*/
export const departmentNodeSchema = z.object({
id: ulidSchema,
name: z.string().min(1, '部门名称不能为空'),
@@ -93,12 +112,48 @@ export const departmentNodeSchema = z.object({
name: z.string().min(1, '负责人姓名不能为空'),
})
.nullable(),
status: departmentStatusSchema,
sortOrder: z.number().int('排序必须是整数').min(0, '排序不能为负数'),
memberCount: z.number().int('部门人数必须是整数').nonnegative('部门人数不能为负数'),
directMemberCount: z.number().int('直属人数必须是整数').nonnegative('直属人数不能为负数'),
createdAt: isoDateTimeSchema,
});
export type DepartmentNode = z.infer<typeof departmentNodeSchema>;
/** 新建部门 / 小组:parentId 为 null 建一级部门,否则在该部门下建小组 */
export const createDepartmentInputSchema = z
.object({
name: departmentNameSchema,
parentId: ulidSchema.nullable().default(null),
leaderUserId: ulidSchema.nullable().optional(),
sortOrder: z.number().int().min(0).max(9999).optional(),
})
.strict();
export type CreateDepartmentInput = z.infer<typeof createDepartmentInputSchema>;
/** 更新部门:只允许改名称 / 负责人 / 排序;上级关系创建后不可移动 */
export const updateDepartmentInputSchema = z
.object({
name: departmentNameSchema.optional(),
leaderUserId: ulidSchema.nullable().optional(),
sortOrder: z.number().int().min(0).max(9999).optional(),
})
.strict();
export type UpdateDepartmentInput = z.infer<typeof updateDepartmentInputSchema>;
/** 部门树查询:管理页需要包含已停用部门,普通选择器默认只看启用 */
export const departmentTreeQuerySchema = z.object({
include_disabled: z
.enum(['true', 'false'])
.default('false')
.transform((value) => value === 'true'),
});
export type DepartmentTreeQuery = z.infer<typeof departmentTreeQuerySchema>;
// ---------------------------------------------------------------------------
// 用户
// ---------------------------------------------------------------------------