diff options
| author | joonhoekim <26rote@gmail.com> | 2025-10-20 14:25:05 +0900 |
|---|---|---|
| committer | joonhoekim <26rote@gmail.com> | 2025-10-20 14:25:05 +0900 |
| commit | fdf72957d6ce5b5fec8e1b2999701d5f5a119cc3 (patch) | |
| tree | 7f1dd370c8f51fe15df33c9d0f8bd34d35be9dec /lib/soap/utils.ts | |
| parent | 28b9664c3a6aab2786e2429b2a8ae57f557856e2 (diff) | |
(김준회) PCR 인터페이스 수신시 비즈니스테이블로 매핑 처리 및 필요 공통함수들 작성
Diffstat (limited to 'lib/soap/utils.ts')
| -rw-r--r-- | lib/soap/utils.ts | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/lib/soap/utils.ts b/lib/soap/utils.ts index 6c09edbe..57e3b280 100644 --- a/lib/soap/utils.ts +++ b/lib/soap/utils.ts @@ -6,6 +6,7 @@ import { eq } from "drizzle-orm"; import db from "@/db/db"; import { soapLogs, type LogDirection, type SoapLogInsert } from "@/db/schema/SOAP/soap"; import { XMLBuilder } from 'fast-xml-parser'; // for object→XML 변환 +import { debugError } from "../debug-utils"; // XML 파싱용 타입 유틸리티: 스키마에서 XML 타입 생성 export type ToXMLFields<T> = { @@ -605,4 +606,50 @@ export function dateToSAPFormat(date: Date): { date: string; time: string } { date: formatDateForSAP(date.toISOString().slice(0, 10)), time: formatTimeForSAP(date.toTimeString().slice(0, 8)) }; -}
\ No newline at end of file +} + +/** + * SAP 날짜 문자열을 Date로 변환 (YYYYMMDD -> Date) + */ +export function parseSAPDate(dateStr: string | null | undefined): Date | null { + if (!dateStr || !dateStr.trim()) return null; + + const trimmed = dateStr.trim(); + if (trimmed.length !== 8) return null; + + try { + const year = parseInt(trimmed.substring(0, 4), 10); + const month = parseInt(trimmed.substring(4, 6), 10) - 1; // 월은 0부터 시작 + const day = parseInt(trimmed.substring(6, 8), 10); + + const date = new Date(year, month, day); + + // 유효한 날짜인지 확인 + if (isNaN(date.getTime())) return null; + + return date; + } catch (error) { + debugError('SAP 날짜 파싱 오류', { dateStr, error }); + return null; + } +} + + +/** + * 숫자 문자열을 string으로 변환 (numeric 타입용) + * drizzle-orm의 numeric 타입은 string으로 저장됨 + */ +export function parseNumericString(value: string | null | undefined): string | null { + if (!value || !value.trim()) return null; + + try { + const trimmed = value.trim(); + const parsed = parseFloat(trimmed); + // 유효한 숫자인지 확인 + if (isNaN(parsed)) return null; + // 원본 문자열 반환 (또는 정제된 숫자 문자열) + return trimmed; + } catch { + return null; + } +} |
