147 lines
3.6 KiB
TypeScript
147 lines
3.6 KiB
TypeScript
|
|
/**
|
||
|
|
* 회사 관리 API 호출 함수들
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { Company, CompanyFormData } from "@/types/company";
|
||
|
|
|
||
|
|
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "/api";
|
||
|
|
|
||
|
|
// API 응답 타입 정의
|
||
|
|
interface ApiResponse<T = any> {
|
||
|
|
success: boolean;
|
||
|
|
message?: string;
|
||
|
|
data?: T;
|
||
|
|
total?: number;
|
||
|
|
errorCode?: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
// API 호출 헬퍼 함수
|
||
|
|
async function apiCall<T = any>(endpoint: string, options: RequestInit = {}): Promise<ApiResponse<T>> {
|
||
|
|
try {
|
||
|
|
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
|
||
|
|
credentials: "include", // 세션 쿠키를 포함
|
||
|
|
headers: {
|
||
|
|
"Content-Type": "application/json",
|
||
|
|
...options.headers,
|
||
|
|
},
|
||
|
|
...options,
|
||
|
|
});
|
||
|
|
|
||
|
|
const data = await response.json();
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
throw new Error(data.message || `HTTP ${response.status}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
return data;
|
||
|
|
} catch (error) {
|
||
|
|
console.error(`API Error [${endpoint}]:`, error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 회사 목록 조회
|
||
|
|
*/
|
||
|
|
export async function getCompanyList(params?: { company_name?: string; status?: string }): Promise<Company[]> {
|
||
|
|
const searchParams = new URLSearchParams();
|
||
|
|
|
||
|
|
if (params?.company_name) {
|
||
|
|
searchParams.append("company_name", params.company_name);
|
||
|
|
}
|
||
|
|
if (params?.status && params.status !== "all") {
|
||
|
|
searchParams.append("status", params.status);
|
||
|
|
}
|
||
|
|
|
||
|
|
const queryString = searchParams.toString();
|
||
|
|
const endpoint = `/admin/companies${queryString ? `?${queryString}` : ""}`;
|
||
|
|
|
||
|
|
const response = await apiCall<Company[]>(endpoint);
|
||
|
|
|
||
|
|
if (response.success && response.data) {
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new Error(response.message || "회사 목록 조회에 실패했습니다.");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 회사 단건 조회
|
||
|
|
*/
|
||
|
|
export async function getCompanyInfo(companyCode: string): Promise<Company> {
|
||
|
|
const response = await apiCall<Company>(`/admin/companies/${companyCode}`);
|
||
|
|
|
||
|
|
if (response.success && response.data) {
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new Error(response.message || "회사 정보 조회에 실패했습니다.");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 회사 등록
|
||
|
|
*/
|
||
|
|
export async function createCompany(formData: CompanyFormData): Promise<Company> {
|
||
|
|
console.log("회사 등록 요청:", formData);
|
||
|
|
|
||
|
|
const response = await apiCall<Company>("/admin/companies", {
|
||
|
|
method: "POST",
|
||
|
|
body: JSON.stringify(formData),
|
||
|
|
});
|
||
|
|
|
||
|
|
if (response.success && response.data) {
|
||
|
|
console.log("회사 등록 완료:", {
|
||
|
|
code: response.data.company_code,
|
||
|
|
name: response.data.company_name,
|
||
|
|
writer: response.data.writer,
|
||
|
|
});
|
||
|
|
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new Error(response.message || "회사 등록에 실패했습니다.");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 회사 정보 수정
|
||
|
|
*/
|
||
|
|
export async function updateCompany(
|
||
|
|
companyCode: string,
|
||
|
|
formData: Partial<CompanyFormData> & { status?: string },
|
||
|
|
): Promise<Company> {
|
||
|
|
const response = await apiCall<Company>(`/admin/companies/${companyCode}`, {
|
||
|
|
method: "PUT",
|
||
|
|
body: JSON.stringify(formData),
|
||
|
|
});
|
||
|
|
|
||
|
|
if (response.success && response.data) {
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new Error(response.message || "회사 정보 수정에 실패했습니다.");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 회사 삭제
|
||
|
|
*/
|
||
|
|
export async function deleteCompany(companyCode: string): Promise<void> {
|
||
|
|
const response = await apiCall(`/admin/companies/${companyCode}`, {
|
||
|
|
method: "DELETE",
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!response.success) {
|
||
|
|
throw new Error(response.message || "회사 삭제에 실패했습니다.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 회사 관리 API 객체 (통합)
|
||
|
|
*/
|
||
|
|
export const companyAPI = {
|
||
|
|
getList: getCompanyList,
|
||
|
|
getInfo: getCompanyInfo,
|
||
|
|
create: createCompany,
|
||
|
|
update: updateCompany,
|
||
|
|
delete: deleteCompany,
|
||
|
|
};
|