summaryrefslogtreecommitdiff
path: root/app/api/vendors/erp/route.ts
diff options
context:
space:
mode:
Diffstat (limited to 'app/api/vendors/erp/route.ts')
-rw-r--r--app/api/vendors/erp/route.ts144
1 files changed, 144 insertions, 0 deletions
diff --git a/app/api/vendors/erp/route.ts b/app/api/vendors/erp/route.ts
new file mode 100644
index 00000000..0724eeeb
--- /dev/null
+++ b/app/api/vendors/erp/route.ts
@@ -0,0 +1,144 @@
+import { NextRequest, NextResponse } from 'next/server';
+import { headers } from 'next/headers';
+import { getErrorMessage } from '@/lib/handle-error';
+
+/**
+ * 기간계 시스템에 벤더 정보를 전송하는 API 엔드포인트
+ * 서버 액션 내부에서 호출됨
+ */
+export async function POST(request: NextRequest) {
+ try {
+
+ // 요청 본문 파싱
+ const vendorData = await request.json();
+
+ // 기간계 시스템 API 설정
+ const erpApiUrl = process.env.ERP_API_URL;
+ const erpApiKey = process.env.ERP_API_KEY;
+
+ if (!erpApiUrl || !erpApiKey) {
+ return NextResponse.json(
+ { success: false, message: 'ERP API configuration is missing' },
+ { status: 500 }
+ );
+ }
+
+ // 기간계 시스템이 요구하는 형식으로 데이터 변환
+ const erpRequestData = {
+ vendor: {
+ name: vendorData.vendorName,
+ tax_id: vendorData.taxId,
+ address: vendorData.address || "",
+ country: vendorData.country || "",
+ phone: vendorData.phone || "",
+ email: vendorData.email || "",
+ website: vendorData.website || "",
+ external_id: vendorData.id.toString(),
+ },
+ contacts: vendorData.contacts.map((contact: any) => ({
+ name: contact.contactName,
+ position: contact.contactPosition || "",
+ email: contact.contactEmail,
+ phone: contact.contactPhone || "",
+ is_primary: contact.isPrimary ? 1 : 0,
+ })),
+ items: vendorData.possibleItems.map((item: any) => ({
+ item_code: item.itemCode,
+ description: item.description || "",
+ })),
+ attachments: vendorData.attachments.map((attach: any) => ({
+ file_name: attach.fileName,
+ file_path: attach.filePath,
+ })),
+ };
+
+ // 기간계 시스템 API 호출
+ const response = await fetch(erpApiUrl, {
+ method: 'POST',
+ headers: {
+ 'Authorization': `Bearer ${erpApiKey}`,
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify(erpRequestData),
+ // Next.js의 fetch는 기본 30초 타임아웃
+ });
+
+ // 응답 처리
+ if (!response.ok) {
+ const errorData = await response.json().catch(() => ({}));
+ return NextResponse.json(
+ {
+ success: false,
+ message: `ERP system error: ${response.status} ${response.statusText}`,
+ details: errorData
+ },
+ { status: 502 } // Bad Gateway (외부 서버 오류)
+ );
+ }
+
+ const result = await response.json();
+
+ // 벤더 코드 검증
+ if (!result.vendor_code) {
+ return NextResponse.json(
+ { success: false, message: 'Vendor code not provided in ERP response' },
+ { status: 502 }
+ );
+ }
+
+ // 성공 응답
+ return NextResponse.json({
+ success: true,
+ vendorCode: result.vendor_code,
+ message: 'Vendor successfully registered in ERP system',
+ ...result
+ });
+ } catch (error) {
+ console.error('Error in ERP API:', error);
+ return NextResponse.json(
+ {
+ success: false,
+ message: getErrorMessage(error)
+ },
+ { status: 500 }
+ );
+ }
+}
+
+/**
+ * 기간계 시스템 연결 상태 확인 (헬스 체크)
+ */
+export async function GET() {
+ try {
+ const healthCheckUrl = process.env.ERP_HEALTH_CHECK_URL;
+
+ if (!healthCheckUrl) {
+ return NextResponse.json(
+ { success: false, message: 'ERP health check URL not configured' },
+ { status: 500 }
+ );
+ }
+
+ const response = await fetch(healthCheckUrl, {
+ method: 'GET',
+ next: { revalidate: 60 } // 1분마다 재검증
+ });
+
+ const isAvailable = response.ok;
+
+ return NextResponse.json({
+ success: true,
+ available: isAvailable,
+ status: response.status,
+ timestamp: new Date().toISOString()
+ });
+ } catch (error) {
+ console.error('ERP health check error:', error);
+ return NextResponse.json({
+ success: false,
+ available: false,
+ error: getErrorMessage(error),
+ timestamp: new Date().toISOString()
+ });
+ }
+} \ No newline at end of file