diff options
Diffstat (limited to 'components/common/selectors/currency/currency-service.ts')
| -rw-r--r-- | components/common/selectors/currency/currency-service.ts | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/components/common/selectors/currency/currency-service.ts b/components/common/selectors/currency/currency-service.ts new file mode 100644 index 00000000..c911d6b1 --- /dev/null +++ b/components/common/selectors/currency/currency-service.ts @@ -0,0 +1,98 @@ +"use server" + +import { oracleKnex } from '@/lib/oracle-db/db' + +// 통화 타입 정의 +export interface Currency { + CURRENCY_CODE: string // 통화 코드 (예: KRW, USD, JPY) + CURRENCY_NAME: string // 통화 이름/설명 + DECIMAL_PLACES: number // 입력가능 소수점 자리수 +} + +// 테스트 환경용 폴백 데이터 +const FALLBACK_TEST_DATA: Currency[] = [ + { CURRENCY_CODE: 'KRW', CURRENCY_NAME: '원화(테스트데이터 - 오라클 페칭 실패시)', DECIMAL_PLACES: 0 }, + { CURRENCY_CODE: 'USD', CURRENCY_NAME: '미국 달러(테스트데이터 - 오라클 페칭 실패시)', DECIMAL_PLACES: 2 }, + { CURRENCY_CODE: 'EUR', CURRENCY_NAME: '유로(테스트데이터 - 오라클 페칭 실패시)', DECIMAL_PLACES: 2 }, + { CURRENCY_CODE: 'JPY', CURRENCY_NAME: '일본 엔화(테스트데이터 - 오라클 페칭 실패시)', DECIMAL_PLACES: 0 }, + { CURRENCY_CODE: 'CNY', CURRENCY_NAME: '중국 위안(테스트데이터 - 오라클 페칭 실패시)', DECIMAL_PLACES: 2 }, + { CURRENCY_CODE: 'GBP', CURRENCY_NAME: '영국 파운드(테스트데이터 - 오라클 페칭 실패시)', DECIMAL_PLACES: 2 }, +] + +/** + * 통화 코드별 소수점 자리수 반환 + * - 원화(KRW): 0 + * - 엔화(JPY): 0 + * - 기타: 2 + */ +export function getDecimalPlaces(currencyCode: string): number { + const code = currencyCode.toUpperCase() + if (code === 'KRW' || code === 'JPY') { + return 0 + } + return 2 +} + +/** + * 모든 통화 목록 조회 (Oracle에서 전체 조회, 실패 시 폴백 데이터 사용) + * CMCTB_CD, CMCTB_CDNM 테이블에서 CD_CLF = 'SPB032' 조건으로 조회 + * 클라이언트에서 검색/필터링 수행 + */ +export async function getCurrencies(): Promise<{ + success: boolean + data: Currency[] + error?: string + isUsingFallback?: boolean +}> { + try { + console.log('📋 [getCurrencies] Oracle 쿼리 시작...') + + const result = await oracleKnex.raw(` + SELECT CD.CD AS CURRENCY_CODE, NM.GRP_DSC AS CURRENCY_NAME + FROM CMCTB_CD CD, CMCTB_CDNM NM + WHERE CD.CD_CLF = NM.CD_CLF + AND CD.CD = NM.CD + AND CD.CD2 = NM.CD2 + AND CD.CD3 = NM.CD3 + AND CD.CD_CLF = 'SPB032' + ORDER BY CD.USR_DF_CHAR_8 ASC, CD.CD ASC + `) + + // Oracle raw query의 결과는 rows 배열에 들어있음 + const rows = (result.rows || result) as Array<Record<string, unknown>> + + console.log(`✅ [getCurrencies] Oracle 쿼리 성공 - ${rows.length}건 조회`) + + // null 값 필터링 및 소수점 자리수 추가 + const cleanedResult = rows + .filter((item) => + item.CURRENCY_CODE && + item.CURRENCY_NAME + ) + .map((item) => { + const currencyCode = String(item.CURRENCY_CODE) + return { + CURRENCY_CODE: currencyCode, + CURRENCY_NAME: String(item.CURRENCY_NAME), + DECIMAL_PLACES: getDecimalPlaces(currencyCode) + } + }) + + console.log(`✅ [getCurrencies] 필터링 후 ${cleanedResult.length}건`) + + return { + success: true, + data: cleanedResult, + isUsingFallback: false + } + } catch (error) { + console.error('❌ [getCurrencies] Oracle 오류:', error) + console.log('🔄 [getCurrencies] 폴백 테스트 데이터 사용 (' + FALLBACK_TEST_DATA.length + '건)') + return { + success: true, + data: FALLBACK_TEST_DATA, + isUsingFallback: true + } + } +} + |
