文件
hyb-oa/client/src/api/departments.ts
T

59 行
1.7 KiB
TypeScript

import { apiRequestData } from "./http";
export type DepartmentItem = {
id: string;
name: string;
parentId: string | null;
archivedAt: string | null;
createdAt: string | null;
updatedAt: string | null;
};
export type DepartmentTreeNode = DepartmentItem & {
children: DepartmentTreeNode[];
};
export type DepartmentListResponse = {
includeArchived: boolean;
items: DepartmentItem[];
tree: DepartmentTreeNode[];
};
type DepartmentMutationResponse = {
department: DepartmentItem;
outcome?: "already_archived" | "archived";
};
export async function fetchDepartments(params: { includeArchived?: boolean } = {}) {
const search = new URLSearchParams();
if (params.includeArchived != null) {
search.set("includeArchived", params.includeArchived ? "1" : "0");
}
const qs = search.toString();
return apiRequestData<DepartmentListResponse>(`/api/departments${qs ? `?${qs}` : ""}`);
}
export async function createDepartment(payload: { name: string; parentId?: string | null }) {
const response = await apiRequestData<DepartmentMutationResponse>("/api/departments", {
method: "POST",
body: JSON.stringify(payload),
});
return response.department;
}
export async function updateDepartment(departmentId: string, payload: { name?: string; parentId?: string | null }) {
const response = await apiRequestData<DepartmentMutationResponse>(`/api/departments/${departmentId}`, {
method: "PATCH",
body: JSON.stringify(payload),
});
return response.department;
}
export async function archiveDepartment(departmentId: string) {
return apiRequestData<DepartmentMutationResponse>(`/api/departments/${departmentId}/archive`, {
method: "POST",
body: JSON.stringify({}),
});
}