Files
vexplor/frontend/lib/api/user.ts

323 lines
8.6 KiB
TypeScript
Raw Normal View History

import { apiClient } from "./client";
2025-08-21 09:41:46 +09:00
/**
* API
*/
interface ApiResponse<T> {
success: boolean;
data?: T;
message?: string;
errorCode?: string;
total?: number;
2025-08-26 14:23:22 +09:00
searchType?: "unified" | "single" | "advanced" | "none"; // 검색 타입 정보
pagination?: {
page: number;
limit: number;
totalPages: number;
};
2025-08-21 09:41:46 +09:00
// 백엔드 호환성을 위한 추가 필드
result?: boolean;
msg?: string;
}
/**
*
2025-08-21 09:41:46 +09:00
*/
export async function getUserList(params?: Record<string, any>) {
2025-08-21 09:41:46 +09:00
try {
console.log("📡 사용자 목록 API 호출:", params);
2025-08-21 09:41:46 +09:00
const response = await apiClient.get("/admin/users", {
params: params,
});
2025-08-21 09:41:46 +09:00
console.log("✅ 사용자 목록 API 응답:", response.data);
return response.data;
2025-08-21 09:41:46 +09:00
} catch (error) {
console.error("❌ 사용자 목록 API 오류:", error);
2025-08-21 09:41:46 +09:00
throw error;
}
}
/**
*
*/
export async function getUserInfo(userId: string) {
try {
const response = await apiClient.get(`/admin/users/${userId}`);
2025-08-21 09:41:46 +09:00
if (response.data.success && response.data.data) {
return response.data.data;
}
2025-08-21 09:41:46 +09:00
throw new Error(response.data.message || "사용자 정보 조회에 실패했습니다.");
} catch (error) {
console.error("❌ 사용자 정보 조회 오류:", error);
throw error;
}
2025-08-21 09:41:46 +09:00
}
/**
*
*/
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,
};
}
2025-08-21 09:41:46 +09:00
throw new Error(response.data.msg || response.data.message || "사용자 등록에 실패했습니다.");
} catch (error) {
console.error("❌ 사용자 등록 오류:", error);
throw error;
}
2025-08-21 09:41:46 +09:00
}
// 사용자 수정 기능 제거됨
2025-10-27 16:40:59 +09:00
/**
*
*/
export async function updateUser(userData: {
userId: string;
userName?: string;
companyCode?: string;
deptCode?: string;
userType?: string;
status?: string;
[key: string]: any;
}) {
const response = await apiClient.put(`/admin/users/${userData.userId}`, userData);
return response.data;
}
2025-08-21 09:41:46 +09:00
/**
2025-08-26 09:56:45 +09:00
* ( )
2025-08-21 09:41:46 +09:00
*/
export async function updateUserStatus(userId: string, status: string) {
2025-08-26 09:56:45 +09:00
const response = await apiClient.patch(`/admin/users/${userId}/status`, { status });
2025-08-21 09:41:46 +09:00
return response.data;
2025-08-21 09:41:46 +09:00
}
// 사용자 삭제 기능 제거됨
// 사용자 변경이력 조회
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);
2025-08-21 09:41:46 +09:00
return response.data;
2025-08-21 09:41:46 +09:00
}
/**
*
*/
export async function resetUserPassword(resetData: { userId: string; newPassword: string }) {
const response = await apiClient.post("/admin/users/reset-password", resetData);
2025-08-21 09:41:46 +09:00
return response.data;
2025-08-21 09:41:46 +09:00
}
/**
* ( API )
*/
export async function getCompanyList() {
const response = await apiClient.get("/admin/companies");
2025-08-21 09:41:46 +09:00
if (response.data.success && response.data.data) {
return response.data.data;
2025-08-21 09:41:46 +09:00
}
throw new Error(response.data.message || "회사 목록 조회에 실패했습니다.");
2025-08-21 09:41:46 +09:00
}
/**
*
*/
export async function getDepartmentList(companyCode?: string) {
const params = companyCode ? `?companyCode=${encodeURIComponent(companyCode)}` : "";
const response = await apiClient.get(`/admin/departments${params}`);
2025-08-21 09:41:46 +09:00
if (response.data.success && response.data.data) {
2025-08-25 13:12:17 +09:00
// 백엔드 API 응답 구조: { data: { departments: [], flatList: [] } }
// departments 배열을 반환 (트리 구조)
return response.data.data.departments || [];
2025-08-21 09:41:46 +09:00
}
throw new Error(response.data.message || "부서 목록 조회에 실패했습니다.");
2025-08-21 09:41:46 +09:00
}
/**
* ID
*/
export async function checkDuplicateUserId(userId: string) {
const response = await apiClient.post("/admin/users/check-duplicate", { userId });
return response.data;
2025-08-21 09:41:46 +09:00
}
// ============================================================
// 사원 + 부서 통합 관리 API
// ============================================================
/**
* +
*/
export interface SaveUserWithDeptRequest {
userInfo: {
user_id: string;
user_name: string;
user_name_eng?: string;
user_password?: string;
email?: string;
tel?: string;
cell_phone?: string;
sabun?: string;
user_type?: string;
user_type_name?: string;
status?: string;
locale?: string;
dept_code?: string;
dept_name?: string;
position_code?: string;
position_name?: string;
};
mainDept?: {
dept_code: string;
dept_name?: string;
position_name?: string;
};
subDepts?: Array<{
dept_code: string;
dept_name?: string;
position_name?: string;
}>;
isUpdate?: boolean;
}
/**
* +
*/
export interface UserWithDeptResponse {
userInfo: Record<string, any>;
mainDept: {
dept_code: string;
dept_name?: string;
position_name?: string;
is_primary: boolean;
} | null;
subDepts: Array<{
dept_code: string;
dept_name?: string;
position_name?: string;
is_primary: boolean;
}>;
}
/**
* +
*
* user_info와 user_dept .
* -
* -
*
* @param data
* @returns
*/
export async function saveUserWithDept(data: SaveUserWithDeptRequest): Promise<ApiResponse<{ userId: string; isUpdate: boolean }>> {
try {
console.log("사원+부서 통합 저장 API 호출:", data);
const response = await apiClient.post("/admin/users/with-dept", data);
console.log("사원+부서 통합 저장 API 응답:", response.data);
return response.data;
} catch (error: any) {
console.error("사원+부서 통합 저장 API 오류:", error);
// Axios 에러 응답 처리
if (error.response?.data) {
return error.response.data;
}
return {
success: false,
message: error.message || "사원 저장 중 오류가 발생했습니다.",
};
}
}
/**
* + ( )
*
* user_info와 user_dept .
*
* @param userId ID
* @returns
*/
export async function getUserWithDept(userId: string): Promise<ApiResponse<UserWithDeptResponse>> {
try {
console.log("사원+부서 조회 API 호출:", userId);
const response = await apiClient.get(`/admin/users/${userId}/with-dept`);
console.log("사원+부서 조회 API 응답:", response.data);
return response.data;
} catch (error: any) {
console.error("사원+부서 조회 API 오류:", error);
if (error.response?.data) {
return error.response.data;
}
return {
success: false,
message: error.message || "사원 조회 중 오류가 발생했습니다.",
};
}
}
2025-08-21 09:41:46 +09:00
// 사용자 API 객체로 export
export const userAPI = {
getList: getUserList,
getInfo: getUserInfo,
create: createUser,
2025-10-27 16:40:59 +09:00
update: updateUser,
2025-08-21 09:41:46 +09:00
updateStatus: updateUserStatus,
getHistory: getUserHistory,
resetPassword: resetUserPassword,
getCompanyList: getCompanyList,
getDepartmentList: getDepartmentList,
checkDuplicateId: checkDuplicateUserId,
// 사원 + 부서 통합 관리
saveWithDept: saveUserWithDept,
getWithDept: getUserWithDept,
2025-08-21 09:41:46 +09:00
};