refactor(WEB): 전체 actions.ts에 공통 API 유틸 적용
- buildApiUrl / executePaginatedAction 패턴으로 전환 (40+ actions 파일) - 직접 URLSearchParams 조립 → buildApiUrl 유틸 사용 - 수동 페이지네이션 메타 변환 → executePaginatedAction 자동 처리 - HandoverReportDocumentModal, OrderDocumentModal 개선 - 급여관리 SalaryManagement 코드 개선 - CLAUDE.md Server Action 공통 유틸 규칙 정리 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,13 +2,12 @@
|
||||
|
||||
|
||||
import { executeServerAction, type ActionResult } from '@/lib/api/execute-server-action';
|
||||
import { buildApiUrl } from '@/lib/api/query-params';
|
||||
import type { PaginatedApiResponse } from '@/lib/api/types';
|
||||
import { isNextRedirectError } from '@/lib/utils/redirect-error';
|
||||
import type { Account, AccountFormData, AccountStatus } from './types';
|
||||
import { BANK_LABELS } from './types';
|
||||
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL;
|
||||
|
||||
// ===== API 응답 타입 =====
|
||||
interface BankAccountApiData {
|
||||
id: number;
|
||||
@@ -61,14 +60,12 @@ export async function getBankAccounts(params?: {
|
||||
success: boolean; data?: Account[]; meta?: { currentPage: number; lastPage: number; perPage: number; total: number };
|
||||
error?: string; __authError?: boolean;
|
||||
}> {
|
||||
const searchParams = new URLSearchParams();
|
||||
if (params?.page) searchParams.set('page', params.page.toString());
|
||||
if (params?.perPage) searchParams.set('per_page', params.perPage.toString());
|
||||
if (params?.search) searchParams.set('search', params.search);
|
||||
const queryString = searchParams.toString();
|
||||
|
||||
const result = await executeServerAction({
|
||||
url: `${API_URL}/api/v1/bank-accounts${queryString ? `?${queryString}` : ''}`,
|
||||
url: buildApiUrl('/api/v1/bank-accounts', {
|
||||
page: params?.page,
|
||||
per_page: params?.perPage,
|
||||
search: params?.search,
|
||||
}),
|
||||
transform: (data: BankAccountPaginatedResponse) => ({
|
||||
accounts: (data?.data || []).map(transformApiToFrontend),
|
||||
meta: { currentPage: data?.current_page || 1, lastPage: data?.last_page || 1, perPage: data?.per_page || 20, total: data?.total || 0 },
|
||||
@@ -81,7 +78,7 @@ export async function getBankAccounts(params?: {
|
||||
// ===== 계좌 상세 조회 =====
|
||||
export async function getBankAccount(id: number): Promise<ActionResult<Account>> {
|
||||
return executeServerAction({
|
||||
url: `${API_URL}/api/v1/bank-accounts/${id}`,
|
||||
url: buildApiUrl(`/api/v1/bank-accounts/${id}`),
|
||||
transform: (data: BankAccountApiData) => transformApiToFrontend(data),
|
||||
errorMessage: '계좌 조회에 실패했습니다.',
|
||||
});
|
||||
@@ -90,7 +87,7 @@ export async function getBankAccount(id: number): Promise<ActionResult<Account>>
|
||||
// ===== 계좌 생성 =====
|
||||
export async function createBankAccount(data: AccountFormData): Promise<ActionResult<Account>> {
|
||||
return executeServerAction({
|
||||
url: `${API_URL}/api/v1/bank-accounts`,
|
||||
url: buildApiUrl('/api/v1/bank-accounts'),
|
||||
method: 'POST',
|
||||
body: transformFrontendToApi(data),
|
||||
transform: (d: BankAccountApiData) => transformApiToFrontend(d),
|
||||
@@ -101,7 +98,7 @@ export async function createBankAccount(data: AccountFormData): Promise<ActionRe
|
||||
// ===== 계좌 수정 =====
|
||||
export async function updateBankAccount(id: number, data: Partial<AccountFormData>): Promise<ActionResult<Account>> {
|
||||
return executeServerAction({
|
||||
url: `${API_URL}/api/v1/bank-accounts/${id}`,
|
||||
url: buildApiUrl(`/api/v1/bank-accounts/${id}`),
|
||||
method: 'PUT',
|
||||
body: transformFrontendToApi(data),
|
||||
transform: (d: BankAccountApiData) => transformApiToFrontend(d),
|
||||
@@ -112,7 +109,7 @@ export async function updateBankAccount(id: number, data: Partial<AccountFormDat
|
||||
// ===== 계좌 삭제 =====
|
||||
export async function deleteBankAccount(id: number): Promise<ActionResult> {
|
||||
return executeServerAction({
|
||||
url: `${API_URL}/api/v1/bank-accounts/${id}`,
|
||||
url: buildApiUrl(`/api/v1/bank-accounts/${id}`),
|
||||
method: 'DELETE',
|
||||
errorMessage: '계좌 삭제에 실패했습니다.',
|
||||
});
|
||||
@@ -121,7 +118,7 @@ export async function deleteBankAccount(id: number): Promise<ActionResult> {
|
||||
// ===== 계좌 상태 토글 =====
|
||||
export async function toggleBankAccountStatus(id: number): Promise<ActionResult<Account>> {
|
||||
return executeServerAction({
|
||||
url: `${API_URL}/api/v1/bank-accounts/${id}/toggle`,
|
||||
url: buildApiUrl(`/api/v1/bank-accounts/${id}/toggle`),
|
||||
method: 'PATCH',
|
||||
transform: (data: BankAccountApiData) => transformApiToFrontend(data),
|
||||
errorMessage: '상태 변경에 실패했습니다.',
|
||||
@@ -131,7 +128,7 @@ export async function toggleBankAccountStatus(id: number): Promise<ActionResult<
|
||||
// ===== 대표 계좌 설정 =====
|
||||
export async function setPrimaryBankAccount(id: number): Promise<ActionResult<Account>> {
|
||||
return executeServerAction({
|
||||
url: `${API_URL}/api/v1/bank-accounts/${id}/set-primary`,
|
||||
url: buildApiUrl(`/api/v1/bank-accounts/${id}/set-primary`),
|
||||
method: 'PATCH',
|
||||
transform: (data: BankAccountApiData) => transformApiToFrontend(data),
|
||||
errorMessage: '대표 계좌 설정에 실패했습니다.',
|
||||
@@ -158,4 +155,4 @@ export async function deleteBankAccounts(ids: number[]): Promise<{
|
||||
if (isNextRedirectError(error)) throw error;
|
||||
return { success: false, error: '서버 오류가 발생했습니다.' };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user