Files
lingxi/apps/web/src/hooks/queries/use-directory.ts
T
Stev_Wang 6661c42e84
ci / 质量门禁 (push) Canceled after 0s
feat(identity): 补齐部门管理(两级组织树、停用启用)
背景:
- 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。
2026-11-03 15:01:07 +08:00

101 lines
2.7 KiB
TypeScript

import type {
CreateDepartmentInput,
DepartmentNode,
RoleSummary,
UpdateDepartmentInput,
} from '@lingxi/shared';
import {
useMutation,
type UseMutationResult,
useQuery,
useQueryClient,
type UseQueryResult,
} from '@tanstack/react-query';
import {
createDepartment,
disableDepartment,
enableDepartment,
fetchDepartments,
fetchRoles,
updateDepartment,
} from '@/api/identity';
import { queryKeys } from '@/api/query-keys';
/** 部门树:默认只取启用部门(用户页选择器);部门管理页传 includeDisabled=true */
export function useDepartmentsQuery(includeDisabled = false): UseQueryResult<DepartmentNode[]> {
return useQuery({
queryKey: queryKeys.departments(includeDisabled),
queryFn: () => fetchDepartments(includeDisabled),
staleTime: 5 * 60_000,
});
}
export function useRolesQuery(): UseQueryResult<RoleSummary[]> {
return useQuery({
queryKey: queryKeys.roles,
queryFn: fetchRoles,
staleTime: 5 * 60_000,
});
}
/** 部门写操作:失效部门树与用户缓存(用户列表/详情的部门名称与人数会变) */
function useInvalidateDirectory(): () => Promise<void> {
const queryClient = useQueryClient();
return async () => {
await Promise.all([
queryClient.invalidateQueries({ queryKey: ['departments'] }),
queryClient.invalidateQueries({ queryKey: queryKeys.users }),
]);
};
}
export function useCreateDepartmentMutation(): UseMutationResult<
string,
Error,
CreateDepartmentInput
> {
const invalidate = useInvalidateDirectory();
return useMutation({
mutationFn: (input: CreateDepartmentInput) => createDepartment(input),
onSuccess: async () => {
await invalidate();
},
});
}
export function useUpdateDepartmentMutation(): UseMutationResult<
void,
Error,
{ departmentId: string; input: UpdateDepartmentInput }
> {
const invalidate = useInvalidateDirectory();
return useMutation({
mutationFn: ({ departmentId, input }: { departmentId: string; input: UpdateDepartmentInput }) =>
updateDepartment(departmentId, input),
onSuccess: async () => {
await invalidate();
},
});
}
export function useDisableDepartmentMutation(): UseMutationResult<void, Error, string> {
const invalidate = useInvalidateDirectory();
return useMutation({
mutationFn: (departmentId: string) => disableDepartment(departmentId),
onSuccess: async () => {
await invalidate();
},
});
}
export function useEnableDepartmentMutation(): UseMutationResult<void, Error, string> {
const invalidate = useInvalidateDirectory();
return useMutation({
mutationFn: (departmentId: string) => enableDepartment(departmentId),
onSuccess: async () => {
await invalidate();
},
});
}