1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
|
"use server"
import { getKnoxConfig, createJsonHeaders, createFormHeaders } from '../common';
import { randomUUID } from 'crypto';
import { saveApprovalToDatabase, deleteApprovalFromDatabase, upsertApprovalStatus } from './service';
import { debugLog, debugError } from '@/lib/debug-utils'
// Knox API Approval 서버 액션들
// 가이드: lib/knox-api/approval/guide.html
// ========== 타입 정의 ==========
// 공통 응답 타입
export interface BaseResponse {
result: string;
}
// 결재 경로 타입
export interface ApprovalLine {
epId?: string;
userId?: string; // eVCP ID라서 사용하지 않음!
emailAddress?: string;
seq: string;
role: string; // 기안(0), 결재(1), 합의(2), 후결(3), 병렬합의(4), 병렬결재(7), 통보(9)
aplnStatsCode: string; // 미결(0), 결재(1), 반려(2), 전결(3), 자동결재(5)
arbPmtYn: string; // 전결권한여부
contentsMdfyPmtYn: string; // 본문수정권한여부
aplnMdfyPmtYn: string; // 경로변경권한여부
opinion?: string; // 상신의견 (상신자만)
}
// 결재 상신 요청 타입
export interface SubmitApprovalRequest {
contents: string; // 결재본문
contentsType: string; // 본문종류 (TEXT, HTML, MIME)
docSecuType: string; // 보안문서타입 (PERSONAL, CONFIDENTIAL, CONFIDENTIAL_STRICT)
notifyOption: string; // 통보옵션 (0-3)
urgYn: string; // 긴급여부 (Y/N)
sbmDt: string; // 상신일시 (YYYYMMDDHHMMSS)
timeZone: string; // 타임존 (GMT, GMT+9 등)
docMngSaveCode: string; // 문서관리저장코드 (0: 안함, 1: 저장)
subject: string; // 결재제목
sbmLang: string; // 상신언어 (ko, ja, zh, en)
apInfId: string; // 연계ID (32자리 고유값)
importantYn?: string; // 중요여부 (Y/N)
aplns: ApprovalLine[]; // 결재경로
attachments?: File[]; // 첨부파일
}
// 결재 상신 응답 타입
export interface SubmitApprovalResponse extends BaseResponse {
data: {
apInfId: string;
};
}
// 결재 상세 조회 응답 타입
export interface ApprovalDetailResponse extends BaseResponse {
data: {
contentsType: string;
sbmDt: string;
sbmLang: string;
apInfId: string;
systemId: string;
notifyOption: string;
urgYn: string;
docSecuType: string;
status: string; // 암호화실패(-3), 암호화중(-2), 예약상신(-1), 보류(0), 진행중(1), 완결(2), 반려(3), 상신취소(4), 전결(5), 후완결(6)
timeZone: string;
subject: string;
aplns: ApprovalLine[];
attachments?: File[];
};
}
// 결재 본문 조회 응답 타입
export interface ApprovalContentResponse extends BaseResponse {
data: {
contents: string;
contentsType: string;
apInfId: string;
};
}
// 결재 상황 조회 요청 타입
export interface ApprovalStatusRequest {
apinfids: { apinfid: string }[];
}
// 결재 상황 조회 응답 타입
export interface ApprovalStatusResponse extends BaseResponse {
data: {
apInfId: string;
docChgNum: string;
status: string;
}[];
}
// 상신 취소 응답 타입
export interface CancelApprovalResponse extends BaseResponse {
data: {
apInfId: string;
};
}
// 개인 결재경로 목록 조회 응답 타입
export interface OwnApprovalLineListResponse extends BaseResponse {
data: ApprovalLine[];
}
// 개인 결재경로 상세 조회 응답 타입
export interface OwnApprovalLineDetailResponse extends BaseResponse {
data: ApprovalLine;
}
// 상신함 리스트 조회 응답 타입
export interface SubmissionListResponse extends BaseResponse {
data: SubmitApprovalRequest[];
}
// 연계 이력 조회 응답 타입
export interface ApprovalHistoryResponse extends BaseResponse {
data: SubmitApprovalRequest[];
}
// 연계 ID 조회 응답 타입
export interface ApprovalIdsResponse extends BaseResponse {
data: string[];
}
// ========== 서버 액션 함수들 ==========
/**
* 결재 상신
* POST /approval/api/v2.0/approvals/submit
*/
export async function submitApproval(
request: SubmitApprovalRequest,
userInfo: { userId: string; epId: string; emailAddress: string }
): Promise<SubmitApprovalResponse> {
try {
const config = await getKnoxConfig();
const formData = new FormData();
// JSON 데이터 생성
const approvalData = {
contents: request.contents,
contentsType: request.contentsType,
docSecuType: request.docSecuType,
notifyOption: request.notifyOption,
urgYn: request.urgYn,
sbmDt: request.sbmDt,
timeZone: request.timeZone,
docMngSaveCode: request.docMngSaveCode,
subject: request.subject,
sbmLang: request.sbmLang || 'ko',
apInfId: request.apInfId, // 고정값, 환경변수로 설정해 common 에서 가져오기
importantYn: request.importantYn,
aplns: request.aplns
};
formData.append('approval', JSON.stringify(approvalData));
// 첨부파일 처리
if (request.attachments) {
request.attachments.forEach((file) => {
formData.append('attachments', file);
});
}
const response = await fetch(`${config.baseUrl}/approval/api/v2.0/approvals/submit`, {
method: 'POST',
headers: await createFormHeaders(),
body: formData,
});
if (!response.ok) {
const errorText = await response.text();
debugError('API Error Response', errorText);
throw new Error(`결재 상신 실패: ${response.status} - ${errorText}`);
}
// 응답의 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') {
try {
await saveApprovalToDatabase(
request.apInfId, // 개별 결재의 ID (결재연계ID)
userInfo.userId, // eVCP
userInfo.epId,
userInfo.emailAddress,
request.subject,
request.contents,
request.aplns,
{
contentsType: request.contentsType,
urgYn: request.urgYn,
importantYn: request.importantYn,
docSecuType: request.docSecuType,
notifyOption: request.notifyOption,
docMngSaveCode: request.docMngSaveCode,
sbmLang: request.sbmLang,
timeZone: request.timeZone,
sbmDt: request.sbmDt,
}
);
} catch (dbError) {
console.error('데이터베이스 저장 실패:', dbError);
// 데이터베이스 저장 실패는 Knox API 성공을 무효화하지 않음
// 필요시 별도 처리 로직 추가
}
}
return result;
} catch (error) {
debugError('결재 상신 오류', error);
throw error;
}
}
/**
* 보안 결재 상신
* POST /approval/api/v2.0/approvals/secu-submit
*/
export async function submitSecurityApproval(
request: SubmitApprovalRequest,
userInfo?: { userId: string; epId: string; emailAddress: string }
): Promise<SubmitApprovalResponse> {
try {
const config = await getKnoxConfig();
const formData = new FormData();
// JSON 데이터 생성
const approvalData = {
contents: request.contents,
contentsType: request.contentsType,
docSecuType: request.docSecuType, // CONFIDENTIAL 또는 CONFIDENTIAL_STRICT
notifyOption: request.notifyOption,
urgYn: request.urgYn,
sbmDt: request.sbmDt,
timeZone: request.timeZone,
docMngSaveCode: request.docMngSaveCode,
subject: request.subject,
sbmLang: request.sbmLang,
apInfId: request.apInfId,
importantYn: request.importantYn,
aplns: request.aplns
};
formData.append('approval', JSON.stringify(approvalData));
// 첨부파일 처리
if (request.attachments) {
request.attachments.forEach((file) => {
formData.append('attachments', file);
});
}
const response = await fetch(`${config.baseUrl}/approval/api/v2.0/approvals/secu-submit`, {
method: 'POST',
headers: await createFormHeaders(),
body: formData,
});
if (!response.ok) {
const errorText = await response.text();
debugError('Security API Error Response', errorText);
throw new Error(`보안 결재 상신 실패: ${response.status} - ${errorText}`);
}
// 응답의 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}`);
}
}
// Knox API 성공 시 데이터베이스에 저장 (사용자 정보가 있는 경우만)
if (result.result === 'success' && userInfo) {
try {
await saveApprovalToDatabase(
request.apInfId,
userInfo.userId,
userInfo.epId,
userInfo.emailAddress,
request.subject,
request.contents,
request.aplns,
{
contentsType: request.contentsType,
urgYn: request.urgYn,
importantYn: request.importantYn,
docSecuType: request.docSecuType,
notifyOption: request.notifyOption,
docMngSaveCode: request.docMngSaveCode,
sbmLang: request.sbmLang,
timeZone: request.timeZone,
sbmDt: request.sbmDt,
}
);
} catch (dbError) {
console.error('보안 결재 데이터베이스 저장 실패:', dbError);
// 데이터베이스 저장 실패는 Knox API 성공을 무효화하지 않음
// 필요시 별도 처리 로직 추가
}
}
return result;
} catch (error) {
debugError('보안 결재 상신 오류', error);
throw error;
}
}
/**
* 결재 상세 상황 조회
* GET /approval/api/v2.0/approvals/{apInfId}/detail
*/
export async function getApprovalDetail(
apInfId: string
): Promise<ApprovalDetailResponse> {
try {
const config = await getKnoxConfig();
const response = await fetch(`${config.baseUrl}/approval/api/v2.0/approvals/${apInfId}/detail`, {
method: 'GET',
headers: await createJsonHeaders(),
});
if (!response.ok) {
throw new Error(`결재 상세 조회 실패: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('결재 상세 조회 오류:', error);
throw error;
}
}
/**
* 결재 본문 조회
* GET /approval/api/v2.0/approvals/{apInfId}/content
*/
export async function getApprovalContent(
apInfId: string
): Promise<ApprovalContentResponse> {
try {
const config = await getKnoxConfig();
const response = await fetch(`${config.baseUrl}/approval/api/v2.0/approvals/${apInfId}/content`, {
method: 'GET',
headers: await createJsonHeaders(),
});
if (!response.ok) {
throw new Error(`결재 본문 조회 실패: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('결재 본문 조회 오류:', error);
throw error;
}
}
/**
* 결재 상황 조회
* POST /approval/api/v2.0/approvals/status
*/
export async function getApprovalStatus(
request: ApprovalStatusRequest
): Promise<ApprovalStatusResponse> {
try {
const config = await getKnoxConfig();
const response = await fetch(`${config.baseUrl}/approval/api/v2.0/approvals/status`, {
method: 'POST',
headers: await createJsonHeaders(),
body: JSON.stringify(request.apinfids),
});
if (!response.ok) {
throw new Error(`결재 상황 조회 실패: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('결재 상황 조회 오류:', error);
throw error;
}
}
/**
* 결재 연계 ID 조회
* GET /approval/api/v2.0/approvals/apinfids
*/
export async function getApprovalIds(
apIds?: string[]
): Promise<ApprovalIdsResponse> {
try {
const config = await getKnoxConfig();
let url = `${config.baseUrl}/approval/api/v2.0/approvals/apinfids`;
if (apIds && apIds.length > 0) {
const params = new URLSearchParams();
apIds.forEach(id => params.append('apId', id));
url += `?${params.toString()}`;
}
const response = await fetch(url, {
method: 'GET',
headers: await createJsonHeaders(),
});
if (!response.ok) {
throw new Error(`결재 연계 ID 조회 실패: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('결재 연계 ID 조회 오류:', error);
throw error;
}
}
// 상신함 리스트 조회 요청 타입
export interface SubmissionListRequest {
epId?: string;
userId?: string;
emailAddress?: string;
[key: string]: string | undefined; // 추가 파라미터 지원
}
/**
* 상신함 리스트 조회
* GET /approval/api/v2.0/approvals/submission
*
* epId, userId, emailAddress 중 최소 하나는 필수
* 우선순위: userId > epId > emailAddress
* 여기서의 userId는 knox email 주소 앞부분을 지칭함
*/
export async function getSubmissionList(
userParams: SubmissionListRequest,
additionalParams?: Record<string, string>
): Promise<SubmissionListResponse> {
try {
// 사용자 식별 파라미터 중 하나는 필수
if (!userParams.epId && !userParams.userId && !userParams.emailAddress) {
throw new Error('epId, userId, emailAddress 중 최소 하나는 필요합니다.');
}
const config = await getKnoxConfig();
const url = new URL(`${config.baseUrl}/approval/api/v2.0/approvals/submission`);
// 사용자 식별 파라미터 추가 (우선순위에 따라)
if (userParams.userId) {
url.searchParams.set('userId', userParams.userId);
} else if (userParams.epId) {
url.searchParams.set('epId', userParams.epId);
} else if (userParams.emailAddress) {
url.searchParams.set('emailAddress', userParams.emailAddress);
}
// 추가 파라미터가 있으면 추가
if (additionalParams) {
Object.entries(additionalParams).forEach(([key, value]) => {
if (value !== undefined) {
url.searchParams.set(key, value);
}
});
}
const response = await fetch(url.toString(), {
method: 'GET',
headers: await createJsonHeaders(),
});
if (!response.ok) {
throw new Error(`상신함 리스트 조회 실패: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('상신함 리스트 조회 오류:', error);
throw error;
}
}
/**
* 연계 이력 조회
* GET /approval/api/v2.0/approvals/apinfidinfos
*/
export async function getApprovalHistory(
params?: Record<string, string>
): Promise<ApprovalHistoryResponse> {
try {
const config = await getKnoxConfig();
let url = `${config.baseUrl}/approval/api/v2.0/approvals/apinfidinfos`;
if (params) {
const searchParams = new URLSearchParams(params);
url += `?${searchParams.toString()}`;
}
const response = await fetch(url, {
method: 'GET',
headers: await createJsonHeaders(),
});
if (!response.ok) {
throw new Error(`연계 이력 조회 실패: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('연계 이력 조회 오류:', error);
throw error;
}
}
/**
* 상신 취소
* POST /approval/api/v2.0/approvals/{apInfId}/cancel?opinion={opinion}
*/
export async function cancelApproval(
apInfId: string,
opinion: string
): Promise<CancelApprovalResponse> {
try {
const config = await getKnoxConfig();
const encodedOpinion = encodeURIComponent(opinion);
const response = await fetch(`${config.baseUrl}/approval/api/v2.0/approvals/${apInfId}/cancel?opinion=${encodedOpinion}`, {
method: 'POST',
headers: await createJsonHeaders(),
});
if (!response.ok) {
throw new Error(`상신 취소 실패: ${response.status}`);
}
const result = await response.json();
// Knox API 성공 시 데이터베이스에서 삭제
if (result.result === 'success') {
try {
await deleteApprovalFromDatabase(apInfId);
} catch (dbError) {
console.error('데이터베이스 삭제 실패:', dbError);
// 데이터베이스 삭제 실패는 Knox API 성공을 무효화하지 않음
// 필요시 별도 처리 로직 추가
}
}
return result;
} catch (error) {
console.error('상신 취소 오류:', error);
throw error;
}
}
/**
* 저장된 결재경로 목록 조회
* GET /approval/api/v2.0/approvals/ownaplnlist
*/
export async function getOwnApprovalLineList(
params?: Record<string, string>
): Promise<OwnApprovalLineListResponse> {
try {
const config = await getKnoxConfig();
let url = `${config.baseUrl}/approval/api/v2.0/approvals/ownaplnlist`;
if (params) {
const searchParams = new URLSearchParams(params);
url += `?${searchParams.toString()}`;
}
const response = await fetch(url, {
method: 'GET',
headers: await createJsonHeaders(),
});
if (!response.ok) {
throw new Error(`저장된 결재경로 목록 조회 실패: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('저장된 결재경로 목록 조회 오류:', error);
throw error;
}
}
/**
* 저장된 결재경로 상세 조회
* GET /approval/api/v2.0/approvals/{pslAplnId}/ownaplndetail
*/
export async function getOwnApprovalLineDetail(
pslAplnId: string
): Promise<OwnApprovalLineDetailResponse> {
try {
const config = await getKnoxConfig();
const response = await fetch(`${config.baseUrl}/approval/api/v2.0/approvals/${pslAplnId}/ownaplndetail`, {
method: 'GET',
headers: await createJsonHeaders(),
});
if (!response.ok) {
throw new Error(`저장된 결재경로 상세 조회 실패: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('저장된 결재경로 상세 조회 오류:', error);
throw error;
}
}
// ========== 유틸리티 함수들 ==========
/**
* 결재 상신 요청 데이터 생성 도우미
*/
export async function createSubmitApprovalRequest(
contents: string,
subject: string,
approvalLines: ApprovalLine[],
options: Partial<SubmitApprovalRequest> = {}
): Promise<SubmitApprovalRequest> {
// 요구하는 날짜 형식으로 변환 (YYYYMMDDHHMMSS) - UTC 기준 (타임존 정보를 GTC+9 로 제공하고 있음)
const now = new Date();
const formatter = new Intl.DateTimeFormat('en-CA', {
timeZone: 'UTC',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
});
const parts = formatter.formatToParts(now);
const sbmDt = parts
.filter((part) => part.type !== 'literal')
.map((part) => part.value)
.join('');
// EVCP 접두어 뒤에 28자리 무작위 문자열을 붙여 32byte 고유 ID 생성
const apInfId = `EVCP${randomUUID().replace(/-/g, '').slice(0, 28)}`;
const result = {
contents,
subject,
aplns: approvalLines,
contentsType: options.contentsType || 'TEXT',
docSecuType: options.docSecuType || 'PERSONAL',
notifyOption: options.notifyOption || '0',
urgYn: options.urgYn || 'N',
sbmDt,
timeZone: options.timeZone || 'GMT+9',
docMngSaveCode: options.docMngSaveCode || '0',
sbmLang: options.sbmLang || 'ko',
apInfId,
importantYn: options.importantYn || 'N',
...options
};
debugLog('Created Submit Request', result);
return result;
}
/**
* 결재 라인 생성 도우미
*/
export async function createApprovalLine(
userInfo: { epId?: string; userId?: string; emailAddress?: string },
role: string,
seq: string,
options: Partial<ApprovalLine> = {}
): Promise<ApprovalLine> {
return {
...userInfo,
seq,
role,
aplnStatsCode: '0',
arbPmtYn: 'Y',
contentsMdfyPmtYn: 'Y',
aplnMdfyPmtYn: 'Y',
...options
};
}
/**
* 결재 상태 문자열 변환
*/
export async function getApprovalStatusText(status: string): Promise<string> {
const statusMap: Record<string, string> = {
'-3': '암호화실패',
'-2': '암호화중',
'-1': '예약상신',
'0': '보류',
'1': '진행중',
'2': '완결',
'3': '반려',
'4': '상신취소',
'5': '전결',
'6': '후완결'
};
return statusMap[status] || '알 수 없음';
}
/**
* 결재 역할 문자열 변환
*/
export async function getApprovalRoleText(role: string): Promise<string> {
const roleMap: Record<string, string> = {
'0': '기안',
'1': '결재',
'2': '합의',
'3': '후결',
'4': '병렬합의',
'7': '병렬결재',
'9': '통보'
};
return roleMap[role] || '알 수 없음';
}
// ========== 서버 액션 함수들 ==========
/**
* 결재상황 일괄 조회 및 데이터베이스 업데이트 서버 액션
* 데이터베이스에 저장된 모든 결재건들의 상태를 Knox API로 조회하여 업데이트
*/
export async function syncApprovalStatusAction(): Promise<{
success: boolean;
message: string;
updated: number;
failed: string[];
}> {
"use server";
try {
const db = (await import('@/db/db')).default;
const { approvalLogs } = await import('@/db/schema/knox/approvals');
const { eq, and, inArray } = await import('drizzle-orm');
// 1. 진행중인 결재건들 조회 (암호화중부터 진행중까지)
const pendingApprovals = await db
.select({
apInfId: approvalLogs.apInfId,
status: approvalLogs.status,
})
.from(approvalLogs)
.where(
and(
eq(approvalLogs.isDeleted, false),
// 암호화중(-2), 예약상신(-1), 보류(0), 진행중(1) 상태만 조회
// 완결(2), 반려(3), 상신취소(4), 전결(5), 후완결(6)은 제외
inArray(approvalLogs.status, ['-2', '-1', '0', '1'])
)
);
if (pendingApprovals.length === 0) {
return {
success: true,
message: "업데이트할 결재건이 없습니다.",
updated: 0,
failed: [],
};
}
// 2. Knox API 호출을 위한 요청 데이터 구성
const apinfids = pendingApprovals.map(approval => ({
apinfid: approval.apInfId
}));
// 3. Knox API로 결재 상황 조회 (최대 1000건씩 처리)
const batchSize = 1000;
let updated = 0;
const failed: string[] = [];
for (let i = 0; i < apinfids.length; i += batchSize) {
const batch = apinfids.slice(i, i + batchSize);
try {
const statusResponse = await getApprovalStatus({
apinfids: batch
});
if (statusResponse.result === 'success' && statusResponse.data) {
// 4. 조회된 상태로 데이터베이스 업데이트
for (const statusData of statusResponse.data) {
try {
// 기존 상태 조회
const currentApproval = pendingApprovals.find(
approval => approval.apInfId === statusData.apInfId
);
if (currentApproval && currentApproval.status !== statusData.status) {
// upsert를 사용한 상태 업데이트
await upsertApprovalStatus(statusData.apInfId, statusData.status);
updated++;
}
} catch (updateError) {
console.error(`결재상태 업데이트 실패 (${statusData.apInfId}):`, updateError);
failed.push(statusData.apInfId);
}
}
} else {
console.error('Knox API 결재상황조회 실패:', statusResponse);
// 배치 전체를 실패로 처리
batch.forEach(item => failed.push(item.apinfid));
}
} catch (batchError) {
console.error('배치 처리 실패:', batchError);
// 배치 전체를 실패로 처리
batch.forEach(item => failed.push(item.apinfid));
}
}
const successMessage = `결재상황 동기화 완료: ${updated}건 업데이트${failed.length > 0 ? `, ${failed.length}건 실패` : ''}`;
console.log(successMessage, {
totalRequested: pendingApprovals.length,
updated,
failedCount: failed.length,
failedApinfIds: failed
});
return {
success: true,
message: successMessage,
updated,
failed,
};
} catch (error) {
console.error('결재상황 동기화 중 오류:', error);
return {
success: false,
message: `결재상황 동기화 실패: ${error instanceof Error ? error.message : '알 수 없는 오류'}`,
updated: 0,
failed: [],
};
}
}
/**
* 특정 결재건들의 상태만 조회 및 업데이트하는 서버 액션
*/
export async function syncSpecificApprovalStatusAction(
apInfIds: string[]
): Promise<{
success: boolean;
message: string;
updated: number;
failed: string[];
}> {
"use server";
try {
if (!apInfIds || apInfIds.length === 0) {
return {
success: false,
message: "조회할 결재 ID가 없습니다.",
updated: 0,
failed: [],
};
}
// Knox API 호출을 위한 요청 데이터 구성
const apinfids = apInfIds.map(apInfId => ({
apinfid: apInfId
}));
let updated = 0;
const failed: string[] = [];
// Knox API로 결재 상황 조회
try {
const statusResponse = await getApprovalStatus({
apinfids
});
if (statusResponse.result === 'success' && statusResponse.data) {
// 조회된 상태로 데이터베이스 업데이트
for (const statusData of statusResponse.data) {
try {
// upsert를 사용한 상태 업데이트
await upsertApprovalStatus(statusData.apInfId, statusData.status);
updated++;
} catch (updateError) {
console.error(`결재상태 업데이트 실패 (${statusData.apInfId}):`, updateError);
failed.push(statusData.apInfId);
}
}
} else {
console.error('Knox API 결재상황조회 실패:', statusResponse);
apInfIds.forEach(id => failed.push(id));
}
} catch (apiError) {
console.error('Knox API 호출 실패:', apiError);
apInfIds.forEach(id => failed.push(id));
}
const successMessage = `지정된 결재건 상태 동기화 완료: ${updated}건 업데이트${failed.length > 0 ? `, ${failed.length}건 실패` : ''}`;
console.log(successMessage, {
requestedIds: apInfIds,
updated,
failedCount: failed.length,
failedApinfIds: failed
});
return {
success: true,
message: successMessage,
updated,
failed,
};
} catch (error) {
console.error('특정 결재상황 동기화 중 오류:', error);
return {
success: false,
message: `결재상황 동기화 실패: ${error instanceof Error ? error.message : '알 수 없는 오류'}`,
updated: 0,
failed: apInfIds,
};
}
}
/**
* 데이터베이스에서 결재 로그 목록 조회 서버 액션
*/
/**
* 결재 상태가 완료/실패로 변경되었는지 확인
*/
export async function isApprovalStatusFinal(status: string): Promise<boolean> {
// 완결(2), 반려(3), 상신취소(4), 전결(5), 후완결(6)
return ['2', '3', '4', '5', '6'].includes(status);
}
/**
* 결재 상태가 성공인지 확인
*/
export async function isApprovalStatusSuccess(status: string): Promise<boolean> {
// 완결(2), 전결(5), 후완결(6)
return ['2', '5', '6'].includes(status);
}
/**
* 결재 상태가 실패인지 확인
*/
export async function isApprovalStatusFailure(status: string): Promise<boolean> {
// 반려(3), 상신취소(4)
return ['3', '4'].includes(status);
}
export async function getApprovalLogsAction(): Promise<{
success: boolean;
message: string;
data: Array<{
apInfId: string;
subject: string;
sbmDt: string;
status: string;
urgYn?: string;
docSecuType?: string;
userId?: string;
epId?: string;
emailAddress?: string;
}>;
}> {
"use server";
try {
const db = (await import('@/db/db')).default;
const { approvalLogs } = await import('@/db/schema/knox/approvals');
const { eq, desc } = await import('drizzle-orm');
// 1. 데이터베이스에서 결재 로그 조회 (삭제되지 않은 것만)
const logs = await db
.select({
apInfId: approvalLogs.apInfId,
userId: approvalLogs.userId,
epId: approvalLogs.epId,
emailAddress: approvalLogs.emailAddress,
subject: approvalLogs.subject,
content: approvalLogs.content,
contentsType: approvalLogs.contentsType,
status: approvalLogs.status,
urgYn: approvalLogs.urgYn,
importantYn: approvalLogs.importantYn,
docSecuType: approvalLogs.docSecuType,
notifyOption: approvalLogs.notifyOption,
docMngSaveCode: approvalLogs.docMngSaveCode,
sbmLang: approvalLogs.sbmLang,
timeZone: approvalLogs.timeZone,
sbmDt: approvalLogs.sbmDt,
createdAt: approvalLogs.createdAt,
updatedAt: approvalLogs.updatedAt,
})
.from(approvalLogs)
.where(eq(approvalLogs.isDeleted, false))
.orderBy(desc(approvalLogs.createdAt));
// 2. 상태가 변동될 수 있는 항목들 필터링 (암호화중(-2), 예약상신(-1), 보류(0), 진행중(1))
const pendingApprovals = logs.filter(log =>
['-2', '-1', '0', '1'].includes(log.status)
);
// 3. 상태 동기화가 필요한 경우 Knox API 호출 (1000건씩 배치 처리)
let statusSyncMessage = '';
if (pendingApprovals.length > 0) {
try {
const apinfids = pendingApprovals.map(approval => ({
apinfid: approval.apInfId
}));
let updatedCount = 0;
const batchSize = 1000;
// 1000건씩 배치 처리
for (let i = 0; i < apinfids.length; i += batchSize) {
const batch = apinfids.slice(i, i + batchSize);
try {
const statusResponse = await getApprovalStatus({
apinfids: batch
});
if (statusResponse.result === 'success' && statusResponse.data) {
// 4. 상태 변경된 항목들 업데이트
for (const statusData of statusResponse.data) {
const currentLog = logs.find(log => log.apInfId === statusData.apInfId);
if (currentLog && currentLog.status !== statusData.status) {
try {
await upsertApprovalStatus(statusData.apInfId, statusData.status);
// 메모리상의 데이터도 업데이트
currentLog.status = statusData.status;
updatedCount++;
} catch (updateError) {
console.error(`결재상태 업데이트 실패 (${statusData.apInfId}):`, updateError);
}
}
}
}
} catch (batchError) {
console.error(`배치 처리 실패 (${i}-${Math.min(i + batchSize, apinfids.length)}):`, batchError);
// 배치 실패는 전체 동기화를 중단하지 않고 다음 배치 계속 처리
}
}
statusSyncMessage = updatedCount > 0 ? ` (${updatedCount}건 상태 동기화)` : '';
} catch (syncError) {
console.error('결재 상태 동기화 중 오류:', syncError);
// 상태 동기화 실패는 전체 조회 실패로 처리하지 않음
}
}
// 5. ApprovalList 컴포넌트에서 기대하는 형식으로 데이터 변환
const formattedData = logs.map(log => ({
apInfId: log.apInfId,
subject: log.subject,
sbmDt: log.sbmDt || log.createdAt.toISOString().replace(/[-:T]/g, '').slice(0, 14), // YYYYMMDDHHMMSS 형식
status: log.status,
urgYn: log.urgYn || undefined,
docSecuType: log.docSecuType || undefined,
userId: log.userId || undefined,
epId: log.epId,
emailAddress: log.emailAddress,
// 추가 정보
contentsType: log.contentsType,
importantYn: log.importantYn || undefined,
notifyOption: log.notifyOption,
docMngSaveCode: log.docMngSaveCode,
sbmLang: log.sbmLang,
timeZone: log.timeZone,
}));
return {
success: true,
message: `${formattedData.length}건의 결재 로그를 조회했습니다.${statusSyncMessage}`,
data: formattedData,
};
} catch (error) {
console.error('결재 로그 조회 중 오류:', error);
return {
success: false,
message: `결재 로그 조회 실패: ${error instanceof Error ? error.message : '알 수 없는 오류'}`,
data: [],
};
}
}
|