diff options
Diffstat (limited to 'lib/knox-api/approval/approval.ts')
| -rw-r--r-- | lib/knox-api/approval/approval.ts | 72 |
1 files changed, 57 insertions, 15 deletions
diff --git a/lib/knox-api/approval/approval.ts b/lib/knox-api/approval/approval.ts index 5e62382d..080867cd 100644 --- a/lib/knox-api/approval/approval.ts +++ b/lib/knox-api/approval/approval.ts @@ -3,6 +3,7 @@ import { getKnoxConfig, createJsonHeaders, createFormHeaders } from '../common'; import { randomUUID } from 'crypto'; import { saveApprovalToDatabase, deleteApprovalFromDatabase } from './service'; +import { debugLog, debugError } from '@/lib/debug-utils' // Knox API Approval 서버 액션들 // 가이드: lib/knox-api/approval/guide.html @@ -174,10 +175,28 @@ export async function submitApproval( }); if (!response.ok) { - throw new Error(`결재 상신 실패: ${response.status}`); + const errorText = await response.text(); + debugError('API Error Response', errorText); + throw new Error(`결재 상신 실패: ${response.status} - ${errorText}`); } - const result = await response.json(); + // 응답의 Content-Type 확인 및 charset 처리 + const contentType = response.headers.get('content-type'); + let result; + + if (contentType && contentType.includes('application/json')) { + result = await response.json(); + } else { + // JSON이 아닌 경우 텍스트로 읽고 JSON 파싱 시도 + const text = await response.text(); + debugLog('Raw response text', text); + try { + result = JSON.parse(text); + } catch (parseError) { + console.error('JSON parse error:', parseError); + throw new Error(`응답 파싱 실패: ${text}`); + } + } // Knox API 성공 시 데이터베이스에 저장 if (result.result === 'SUCCESS') { @@ -200,7 +219,7 @@ export async function submitApproval( return result; } catch (error) { - console.error('결재 상신 오류:', error); + debugError('결재 상신 오류', error); throw error; } } @@ -249,12 +268,32 @@ export async function submitSecurityApproval( }); if (!response.ok) { - throw new Error(`보안 결재 상신 실패: ${response.status}`); + const errorText = await response.text(); + debugError('Security API Error Response', errorText); + throw new Error(`보안 결재 상신 실패: ${response.status} - ${errorText}`); } - return await response.json(); + // 응답의 Content-Type 확인 및 charset 처리 + const contentType = response.headers.get('content-type'); + let result; + + if (contentType && contentType.includes('application/json')) { + result = await response.json(); + } else { + // JSON이 아닌 경우 텍스트로 읽고 JSON 파싱 시도 + const text = await response.text(); + debugLog('Raw security response text', text); + try { + result = JSON.parse(text); + } catch (parseError) { + console.error('Security JSON parse error:', parseError); + throw new Error(`보안 응답 파싱 실패: ${text}`); + } + } + + return result; } catch (error) { - console.error('보안 결재 상신 오류:', error); + debugError('보안 결재 상신 오류', error); throw error; } } @@ -562,22 +601,25 @@ export async function createSubmitApprovalRequest( // EVCP 접두어 뒤에 28자리 무작위 문자열을 붙여 32byte 고유 ID 생성 const apInfId = `EVCP${randomUUID().replace(/-/g, '').slice(0, 28)}`; - return { + const result = { contents, subject, aplns: approvalLines, - contentsType: 'TEXT', - docSecuType: 'PERSONAL', - notifyOption: '0', - urgYn: 'N', + contentsType: options.contentsType || 'TEXT', + docSecuType: options.docSecuType || 'PERSONAL', + notifyOption: options.notifyOption || '0', + urgYn: options.urgYn || 'N', sbmDt, - timeZone: 'GMT+9', - docMngSaveCode: '0', - sbmLang: 'ko', + timeZone: options.timeZone || 'GMT+9', + docMngSaveCode: options.docMngSaveCode || '0', + sbmLang: options.sbmLang || 'ko', apInfId, - importantYn: 'N', + importantYn: options.importantYn || 'N', ...options }; + + debugLog('Created Submit Request', result); + return result; } /** |
