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

150 行
3.4 KiB
TypeScript

import { apiRequestData } from "./http";
export type UserStatus = "active" | "archived";
export type UserSummary = {
id: string;
employeeNo: string;
name: string;
phone: string;
email: string;
status: UserStatus;
departmentId: string;
title: string;
roleCodes: string[];
createdAt: string | null;
updatedAt: string | null;
};
export type UserListResponse = {
page: number;
pageSize: number;
total: number;
items: UserSummary[];
};
export type UserImportRowError = {
field: string;
message: string;
};
export type UserImportPreviewRow = {
rowNumber: number;
employeeNo: string;
name: string;
normalized: {
employeeNo: string;
name: string;
phone: string;
email: string;
departmentName: string;
title: string;
directManagerEmployeeNo: string;
roles: string[];
hireDate: string;
};
errors: UserImportRowError[];
};
export type UserImportDryRunResult = {
sourceFilename: string;
summary: {
totalRows: number;
validRows: number;
invalidRows: number;
};
rows: UserImportPreviewRow[];
};
export type UserImportApplyResult = {
sourceFilename: string;
summary: {
totalRows: number;
validRows: number;
invalidRows: number;
};
imported: Array<{
employeeId: string;
employeeNo: string;
name: string;
email: string;
oneTimePassword: string;
}>;
};
export async function fetchUsers(params: {
page: number;
pageSize: number;
status?: UserStatus;
q?: string;
}) {
const search = new URLSearchParams();
search.set("page", String(params.page));
search.set("pageSize", String(params.pageSize));
if (params.status) search.set("status", params.status);
if (params.q) search.set("q", params.q);
return apiRequestData<UserListResponse>(`/api/users?${search.toString()}`);
}
export async function createUser(payload: {
name: string;
phone: string;
email: string;
departmentId?: string;
directManagerId?: string | null;
roles: string[];
}) {
return apiRequestData<UserSummary & { oneTimePassword: string }>("/api/users", {
method: "POST",
body: JSON.stringify(payload),
});
}
export async function disableUser(userId: string) {
return apiRequestData<{ id: string; status: UserStatus }>(`/api/users/${userId}/disable`, {
method: "POST",
});
}
export async function enableUser(userId: string) {
return apiRequestData<{ id: string; status: UserStatus }>(`/api/users/${userId}/enable`, {
method: "POST",
});
}
export async function resetUserPassword(userId: string) {
return apiRequestData<{ id: string; oneTimePassword: string }>(
`/api/users/${userId}/reset-password`,
{
method: "POST",
body: JSON.stringify({}),
},
);
}
export async function setUserRoles(userId: string, roles: string[]) {
return apiRequestData<{ id: string; roleCodes: string[] }>(`/api/users/${userId}/roles`, {
method: "POST",
body: JSON.stringify({ roles }),
});
}
export async function dryRunUserImport(file: File) {
const formData = new FormData();
formData.append("file", file);
return apiRequestData<UserImportDryRunResult>("/api/users/import/dry-run", {
method: "POST",
body: formData,
});
}
export async function applyUserImport(file: File) {
const formData = new FormData();
formData.append("file", file);
return apiRequestData<UserImportApplyResult>("/api/users/import/apply", {
method: "POST",
body: formData,
});
}