85 行
2.0 KiB
TypeScript
85 行
2.0 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 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: {
|
|
employeeNo: string;
|
|
name: string;
|
|
phone: string;
|
|
email: string;
|
|
password: string;
|
|
departmentId?: string;
|
|
directManagerId?: string | null;
|
|
roles: string[];
|
|
}) {
|
|
return apiRequestData<UserSummary>("/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 }),
|
|
});
|
|
}
|