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
|
/**
* 결재 워크플로우 관리 모듈
*
* 주요 기능:
* 1. 결재가 필요한 액션을 pending 상태로 저장
* 2. Knox 결재 시스템에 상신
* 3. 결재 완료 시 저장된 액션 실행
*
* 흐름:
* withApproval() → Knox 상신 → [폴링으로 상태 감지] → executeApprovedAction()
*/
import db from '@/db/db';
import { eq } from 'drizzle-orm';
import { pendingActions } from '@/db/schema/knox/pending-actions';
import type { ApprovalConfig } from './types';
import type { ApprovalLine } from '@/lib/knox-api/approval/approval';
/**
* 액션 핸들러 타입 정의
* payload를 받아서 실제 비즈니스 로직을 수행하는 함수
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ActionHandler = (payload: any) => Promise<any>;
/**
* 액션 타입별 핸들러 저장소
* registerActionHandler()로 등록된 핸들러들이 여기 저장됨
*/
const actionHandlers = new Map<string, ActionHandler>();
/**
* 특정 액션 타입에 대한 핸들러 등록
*
* @example
* registerActionHandler('vendor_investigation_request', async (payload) => {
* return await createInvestigation(payload);
* });
*/
export function registerActionHandler(actionType: string, handler: ActionHandler) {
actionHandlers.set(actionType, handler);
}
/**
* 등록된 핸들러 조회 (디버깅/테스트용)
*/
export function getRegisteredHandlers() {
return Array.from(actionHandlers.keys());
}
/**
* 결재가 필요한 액션을 래핑하는 공통 함수
*
* 사용법:
* ```typescript
* const result = await withApproval(
* 'vendor_investigation_request',
* { vendorId: 123, reason: '실사 필요' },
* {
* title: '실사 요청 결재',
* description: 'ABC 협력업체 실사 요청',
* templateName: '협력업체 실사 요청',
* variables: { '수신자이름': '결재자', ... },
* currentUser: { id: 1, epId: 'EP001' }
* }
* );
* ```
*
* @param actionType - 액션 타입 (핸들러 등록 시 사용한 키)
* @param actionPayload - 액션 실행에 필요한 데이터
* @param approvalConfig - 결재 상신 설정 (템플릿명, 변수 포함)
* @returns pendingActionId, approvalId, status
*/
export async function withApproval<T>(
actionType: string,
actionPayload: T,
approvalConfig: ApprovalConfig
) {
// 핸들러가 등록되어 있는지 확인
if (!actionHandlers.has(actionType)) {
throw new Error(`No handler registered for action type: ${actionType}`);
}
try {
// 1. 템플릿 조회 및 변수 치환
const { getApprovalTemplateByName, replaceTemplateVariables } = await import('./template-utils');
const template = await getApprovalTemplateByName(approvalConfig.templateName);
let content: string;
if (!template) {
console.warn(`[Approval Workflow] Template not found: ${approvalConfig.templateName}`);
// 템플릿이 없으면 기본 내용 사용
content = approvalConfig.description || '결재 요청';
} else {
// 템플릿 변수 치환
content = await replaceTemplateVariables(template.content, approvalConfig.variables);
}
// 2. Knox 결재 상신 (apInfId 생성, 치환된 content 사용)
const {
submitApproval,
createSubmitApprovalRequest,
createApprovalLine
} = await import('@/lib/knox-api/approval/approval');
// 결재선 생성
const aplns: ApprovalLine[] = [];
// 기안자 (현재 사용자)
if (approvalConfig.currentUser.epId) {
const drafterLine = await createApprovalLine(
{
epId: approvalConfig.currentUser.epId,
emailAddress: approvalConfig.currentUser.email,
},
'0', // 기안
'0' // seq
);
aplns.push(drafterLine);
}
// 결재자들
if (approvalConfig.approvers && approvalConfig.approvers.length > 0) {
for (let i = 0; i < approvalConfig.approvers.length; i++) {
const approverLine = await createApprovalLine(
{ epId: approvalConfig.approvers[i] },
'1', // 승인
String(i + 1)
);
aplns.push(approverLine);
}
}
// 결재 요청 생성
const submitRequest = await createSubmitApprovalRequest(
content, // 치환된 템플릿 content
approvalConfig.title,
aplns,
{
contentsType: 'HTML', // HTML 템플릿 사용
}
);
// Knox 결재 상신
await submitApproval(
submitRequest,
{
userId: String(approvalConfig.currentUser.id),
epId: approvalConfig.currentUser.epId || '',
emailAddress: approvalConfig.currentUser.email || '',
}
);
// 3. Pending Action 생성 (approvalLog의 apInfId로 연결)
// Knox에서 apInfId를 반환하지 않고, 요청 시 생성한 apInfId를 사용
const [pendingAction] = await db.insert(pendingActions).values({
apInfId: submitRequest.apInfId,
actionType,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
actionPayload: actionPayload as any,
status: 'pending',
createdBy: approvalConfig.currentUser.id,
}).returning();
return {
pendingActionId: pendingAction.id,
approvalId: submitRequest.apInfId,
status: 'pending_approval',
};
} catch (error) {
console.error('Failed to create approval workflow:', error);
throw error;
}
}
/**
* 결재가 승인된 액션을 실행
* 폴링 서비스에서 호출됨
*
* @param apInfId - Knox 결재 ID (approvalLogs의 primary key)
* @returns 액션 실행 결과
*/
export async function executeApprovedAction(apInfId: string) {
try {
// 1. apInfId로 pendingAction 조회
const pendingAction = await db.query.pendingActions.findFirst({
where: eq(pendingActions.apInfId, apInfId),
});
if (!pendingAction) {
console.log(`[Approval Workflow] No pending action found for approval: ${apInfId}`);
return null; // 결재만 있고 실행할 액션이 없는 경우
}
// 이미 실행되었거나 실패한 액션은 스킵
if (['executed', 'failed'].includes(pendingAction.status)) {
console.log(`[Approval Workflow] Pending action already processed: ${apInfId} (${pendingAction.status})`);
return null;
}
// 2. 등록된 핸들러 조회
const handler = actionHandlers.get(pendingAction.actionType);
if (!handler) {
throw new Error(`Handler not found for action type: ${pendingAction.actionType}`);
}
// 3. 실제 액션 실행
console.log(`[Approval Workflow] Executing action: ${pendingAction.actionType} (${apInfId})`);
const result = await handler(pendingAction.actionPayload);
// 4. 실행 완료 상태 업데이트
await db.update(pendingActions)
.set({
status: 'executed',
executedAt: new Date(),
executionResult: result,
})
.where(eq(pendingActions.apInfId, apInfId));
console.log(`[Approval Workflow] ✅ Successfully executed: ${pendingAction.actionType} (${apInfId})`);
return result;
} catch (error) {
console.error(`[Approval Workflow] ❌ Failed to execute action for ${apInfId}:`, error);
// 실패 상태 업데이트
await db.update(pendingActions)
.set({
status: 'failed',
errorMessage: error instanceof Error ? error.message : String(error),
executedAt: new Date(),
})
.where(eq(pendingActions.apInfId, apInfId));
throw error;
}
}
/**
* 결재 반려 시 처리
*
* @param apInfId - Knox 결재 ID (approvalLogs의 primary key)
* @param reason - 반려 사유
*/
export async function handleRejectedAction(apInfId: string, reason?: string) {
try {
const pendingAction = await db.query.pendingActions.findFirst({
where: eq(pendingActions.apInfId, apInfId),
});
if (!pendingAction) {
console.log(`[Approval Workflow] No pending action found for rejected approval: ${apInfId}`);
return; // 결재만 있고 실행할 액션이 없는 경우
}
await db.update(pendingActions)
.set({
status: 'rejected',
errorMessage: reason,
executedAt: new Date(),
})
.where(eq(pendingActions.apInfId, apInfId));
// TODO: 요청자에게 알림 발송 등 추가 처리
} catch (error) {
console.error(`[Approval Workflow] Failed to handle rejected action for approval ${apInfId}:`, error);
throw error;
}
}
|