181 lines
5.0 KiB
TypeScript
181 lines
5.0 KiB
TypeScript
import { apiClient } from "./client";
|
|
|
|
/**
|
|
* 사용자 관리 API 클라이언트
|
|
*/
|
|
|
|
interface ApiResponse<T> {
|
|
success: boolean;
|
|
data?: T;
|
|
message?: string;
|
|
errorCode?: string;
|
|
total?: number;
|
|
searchType?: "unified" | "single" | "advanced" | "none"; // 검색 타입 정보
|
|
pagination?: {
|
|
page: number;
|
|
limit: number;
|
|
totalPages: number;
|
|
};
|
|
// 백엔드 호환성을 위한 추가 필드
|
|
result?: boolean;
|
|
msg?: string;
|
|
}
|
|
|
|
/**
|
|
* 사용자 목록 조회
|
|
*/
|
|
export async function getUserList(params?: Record<string, any>) {
|
|
try {
|
|
console.log("📡 사용자 목록 API 호출:", params);
|
|
|
|
const response = await apiClient.get("/admin/users", {
|
|
params: params,
|
|
});
|
|
|
|
console.log("✅ 사용자 목록 API 응답:", response.data);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error("❌ 사용자 목록 API 오류:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 사용자 정보 단건 조회
|
|
*/
|
|
export async function getUserInfo(userId: string) {
|
|
try {
|
|
const response = await apiClient.get(`/admin/users/${userId}`);
|
|
|
|
if (response.data.success && response.data.data) {
|
|
return response.data.data;
|
|
}
|
|
|
|
throw new Error(response.data.message || "사용자 정보 조회에 실패했습니다.");
|
|
} catch (error) {
|
|
console.error("❌ 사용자 정보 조회 오류:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 사용자 등록
|
|
*/
|
|
export async function createUser(userData: any) {
|
|
try {
|
|
const response = await apiClient.post("/admin/users", userData);
|
|
|
|
// 백엔드에서 result 필드를 사용하므로 이에 맞춰 처리
|
|
if (response.data.result === true || response.data.success === true) {
|
|
return {
|
|
success: true,
|
|
message: response.data.msg || response.data.message || "사용자가 성공적으로 등록되었습니다.",
|
|
data: response.data,
|
|
};
|
|
}
|
|
|
|
throw new Error(response.data.msg || response.data.message || "사용자 등록에 실패했습니다.");
|
|
} catch (error) {
|
|
console.error("❌ 사용자 등록 오류:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// 사용자 수정 기능 제거됨
|
|
|
|
/**
|
|
* 사용자 상태 변경 (부분 수정)
|
|
*/
|
|
export async function updateUserStatus(userId: string, status: string) {
|
|
const response = await apiClient.patch(`/admin/users/${userId}/status`, { status });
|
|
|
|
return response.data;
|
|
}
|
|
|
|
// 사용자 삭제 기능 제거됨
|
|
|
|
// 사용자 변경이력 조회
|
|
export async function getUserHistory(userId: string, params?: Record<string, any>) {
|
|
const searchParams = new URLSearchParams();
|
|
|
|
// 기본 페이지네이션 파라미터
|
|
searchParams.append("page", String(params?.page || 1));
|
|
searchParams.append("countPerPage", String(params?.countPerPage || 10));
|
|
|
|
// 추가 파라미터가 있으면 추가
|
|
if (params) {
|
|
Object.entries(params).forEach(([key, value]) => {
|
|
if (key !== "page" && key !== "countPerPage" && value !== undefined && value !== null && value !== "") {
|
|
searchParams.append(key, String(value));
|
|
}
|
|
});
|
|
}
|
|
|
|
const queryString = searchParams.toString();
|
|
const endpoint = `/admin/users/${userId}/history${queryString ? `?${queryString}` : ""}`;
|
|
|
|
console.log("📡 사용자 변경이력 API 호출 URL:", endpoint);
|
|
const response = await apiClient.get(endpoint);
|
|
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* 사용자 비밀번호 초기화
|
|
*/
|
|
export async function resetUserPassword(resetData: { userId: string; newPassword: string }) {
|
|
const response = await apiClient.post("/admin/users/reset-password", resetData);
|
|
|
|
return response.data;
|
|
}
|
|
|
|
/**
|
|
* 회사 목록 조회 (기존 API 활용)
|
|
*/
|
|
export async function getCompanyList() {
|
|
const response = await apiClient.get("/admin/companies");
|
|
|
|
if (response.data.success && response.data.data) {
|
|
return response.data.data;
|
|
}
|
|
|
|
throw new Error(response.data.message || "회사 목록 조회에 실패했습니다.");
|
|
}
|
|
|
|
/**
|
|
* 부서 목록 조회
|
|
*/
|
|
export async function getDepartmentList(companyCode?: string) {
|
|
const params = companyCode ? `?companyCode=${encodeURIComponent(companyCode)}` : "";
|
|
const response = await apiClient.get(`/admin/departments${params}`);
|
|
|
|
if (response.data.success && response.data.data) {
|
|
// 백엔드 API 응답 구조: { data: { departments: [], flatList: [] } }
|
|
// departments 배열을 반환 (트리 구조)
|
|
return response.data.data.departments || [];
|
|
}
|
|
|
|
throw new Error(response.data.message || "부서 목록 조회에 실패했습니다.");
|
|
}
|
|
|
|
/**
|
|
* 사용자 ID 중복 체크
|
|
*/
|
|
export async function checkDuplicateUserId(userId: string) {
|
|
const response = await apiClient.post("/admin/users/check-duplicate", { userId });
|
|
return response.data;
|
|
}
|
|
|
|
// 사용자 API 객체로 export
|
|
export const userAPI = {
|
|
getList: getUserList,
|
|
getInfo: getUserInfo,
|
|
create: createUser,
|
|
updateStatus: updateUserStatus,
|
|
getHistory: getUserHistory,
|
|
resetPassword: resetUserPassword,
|
|
getCompanyList: getCompanyList,
|
|
getDepartmentList: getDepartmentList,
|
|
checkDuplicateId: checkDuplicateUserId,
|
|
};
|