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
|
/**
* PQ 실사 관련 결재 서버 액션
*
* ✅ 베스트 프랙티스:
* - 'use server' 지시어 포함 (서버 액션)
* - UI에서 호출하는 진입점 함수들
* - withApproval()을 사용하여 결재 프로세스 시작
* - 템플릿 변수 준비 및 입력 검증
* - 핸들러(Internal)에는 최소 데이터만 전달
*/
'use server';
import { withApproval } from '@/lib/approval/approval-workflow';
import { mapPQInvestigationToTemplateVariables } from './handlers';
import { debugLog, debugError, debugSuccess } from '@/lib/debug-utils';
/**
* 결재를 거쳐 PQ 실사 의뢰를 생성하는 서버 액션
*
* ✅ 사용법 (클라이언트 컴포넌트에서):
* ```typescript
* const result = await requestPQInvestigationWithApproval({
* pqSubmissionIds: [1, 2, 3],
* vendorNames: "협력사A, 협력사B",
* qmManagerId: 123,
* qmManagerName: "홍길동",
* qmManagerEmail: "hong@example.com",
* forecastedAt: new Date('2025-11-01'),
* investigationAddress: "경기도 ...",
* investigationNotes: "...",
* currentUser: { id: 1, epId: 'EP001', email: 'user@example.com' },
* approvers: ['EP002', 'EP003']
* });
*
* if (result.status === 'pending_approval') {
* toast.success(`결재가 상신되었습니다. (ID: ${result.approvalId})`);
* }
* ```
*/
export async function requestPQInvestigationWithApproval(data: {
pqSubmissionIds: number[];
vendorNames: string; // 여러 업체명 (쉼표로 구분)
qmManagerId: number;
qmManagerName: string;
qmManagerEmail?: string;
forecastedAt: Date;
investigationAddress: string;
investigationNotes?: string;
currentUser: { id: number; epId: string | null; email?: string };
approvers?: string[]; // Knox EP ID 배열 (결재선)
}) {
debugLog('[PQInvestigationApproval] 실사 의뢰 결재 서버 액션 시작', {
pqCount: data.pqSubmissionIds.length,
vendorNames: data.vendorNames,
qmManagerId: data.qmManagerId,
qmManagerName: data.qmManagerName,
hasQmEmail: !!data.qmManagerEmail,
userId: data.currentUser.id,
hasEpId: !!data.currentUser.epId,
});
// 1. 입력 검증
if (!data.currentUser.epId) {
debugError('[PQInvestigationApproval] Knox EP ID 없음');
throw new Error('Knox EP ID가 필요합니다');
}
if (data.pqSubmissionIds.length === 0) {
debugError('[PQInvestigationApproval] PQ 제출 ID 없음');
throw new Error('실사 의뢰할 PQ를 선택해주세요');
}
// 2. 템플릿 변수 매핑
debugLog('[PQInvestigationApproval] 템플릿 변수 매핑 시작');
const requestedAt = new Date();
const variables = await mapPQInvestigationToTemplateVariables({
vendorNames: data.vendorNames,
qmManagerName: data.qmManagerName,
qmManagerEmail: data.qmManagerEmail,
forecastedAt: data.forecastedAt,
investigationAddress: data.investigationAddress,
investigationNotes: data.investigationNotes,
requestedAt,
});
debugLog('[PQInvestigationApproval] 템플릿 변수 매핑 완료', {
variableKeys: Object.keys(variables),
});
// 3. 결재 워크플로우 시작
debugLog('[PQInvestigationApproval] withApproval 호출');
const result = await withApproval(
// actionType: 핸들러를 찾을 때 사용할 키
'pq_investigation_request',
// actionPayload: 결재 승인 후 핸들러에 전달될 데이터 (최소 데이터만)
{
pqSubmissionIds: data.pqSubmissionIds,
qmManagerId: data.qmManagerId,
forecastedAt: data.forecastedAt,
investigationAddress: data.investigationAddress,
investigationNotes: data.investigationNotes,
},
// approvalConfig: 결재 상신 정보 (템플릿 포함)
{
title: `Vendor 실사의뢰 - ${data.vendorNames}`,
description: `${data.vendorNames}에 대한 실사 의뢰`,
templateName: 'Vendor 실사의뢰', // 한국어 템플릿명
variables, // 치환할 변수들
approvers: data.approvers,
currentUser: data.currentUser,
}
);
debugSuccess('[PQInvestigationApproval] 결재 워크플로우 완료', {
approvalId: result.approvalId,
pendingActionId: result.pendingActionId,
status: result.status,
});
return result;
}
/**
* 결재를 거쳐 PQ 실사 재의뢰를 처리하는 서버 액션
*
* ✅ 사용법 (클라이언트 컴포넌트에서):
* ```typescript
* const result = await reRequestPQInvestigationWithApproval({
* investigationIds: [1, 2, 3],
* vendorNames: "협력사A, 협력사B",
* currentUser: { id: 1, epId: 'EP001', email: 'user@example.com' },
* approvers: ['EP002', 'EP003'],
* reason: '재의뢰 사유...'
* });
*
* if (result.status === 'pending_approval') {
* toast.success(`재의뢰 결재가 상신되었습니다. (ID: ${result.approvalId})`);
* }
* ```
*/
export async function reRequestPQInvestigationWithApproval(data: {
investigationIds: number[];
vendorNames: string; // 여러 업체명 (쉼표로 구분)
currentUser: { id: number; epId: string | null; email?: string };
approvers?: string[]; // Knox EP ID 배열 (결재선)
reason?: string; // 재의뢰 사유
}) {
debugLog('[PQReRequestApproval] 실사 재의뢰 결재 서버 액션 시작', {
investigationCount: data.investigationIds.length,
vendorNames: data.vendorNames,
userId: data.currentUser.id,
hasEpId: !!data.currentUser.epId,
hasReason: !!data.reason,
});
// 1. 입력 검증
if (!data.currentUser.epId) {
debugError('[PQReRequestApproval] Knox EP ID 없음');
throw new Error('Knox EP ID가 필요합니다');
}
if (data.investigationIds.length === 0) {
debugError('[PQReRequestApproval] 실사 ID 없음');
throw new Error('재의뢰할 실사를 선택해주세요');
}
// 2. 기존 실사 정보 조회 (첫 번째 실사 기준 - 템플릿 변수용)
debugLog('[PQReRequestApproval] 기존 실사 정보 조회');
const { default: db } = await import('@/db/db');
const { vendorInvestigations, users } = await import('@/db/schema');
const { eq } = await import('drizzle-orm');
const results = await db
.select({
id: vendorInvestigations.id,
forecastedAt: vendorInvestigations.forecastedAt,
investigationAddress: vendorInvestigations.investigationAddress,
qmManagerId: vendorInvestigations.qmManagerId,
qmManagerName: users.name,
qmManagerEmail: users.email,
})
.from(vendorInvestigations)
.leftJoin(users, eq(vendorInvestigations.qmManagerId, users.id))
.where(eq(vendorInvestigations.id, data.investigationIds[0]))
.limit(1);
const existingInvestigation = results[0];
if (!existingInvestigation) {
debugError('[PQReRequestApproval] 기존 실사 정보를 찾을 수 없음');
throw new Error('기존 실사 정보를 찾을 수 없습니다.');
}
debugLog('[PQReRequestApproval] 기존 실사 정보 조회 완료', {
investigationId: existingInvestigation.id,
qmManagerName: existingInvestigation.qmManagerName,
forecastedAt: existingInvestigation.forecastedAt,
});
// 3. 템플릿 변수 매핑
debugLog('[PQReRequestApproval] 템플릿 변수 매핑 시작');
const reRequestedAt = new Date();
const { mapPQReRequestToTemplateVariables } = await import('./handlers');
const variables = await mapPQReRequestToTemplateVariables({
vendorNames: data.vendorNames,
investigationCount: data.investigationIds.length,
reRequestedAt,
reason: data.reason,
// 기존 실사 정보 전달 (템플릿 표시용)
forecastedAt: existingInvestigation.forecastedAt || undefined,
investigationAddress: existingInvestigation.investigationAddress || undefined,
qmManagerName: existingInvestigation.qmManagerName || undefined,
qmManagerEmail: existingInvestigation.qmManagerEmail || undefined,
});
debugLog('[PQReRequestApproval] 템플릿 변수 매핑 완료', {
variableKeys: Object.keys(variables),
});
// 4. 결재 워크플로우 시작
debugLog('[PQReRequestApproval] withApproval 호출');
const result = await withApproval(
// actionType: 핸들러를 찾을 때 사용할 키
'pq_investigation_rerequest',
// actionPayload: 결재 승인 후 핸들러에 전달될 데이터 (최소 데이터만)
{
investigationIds: data.investigationIds,
},
// approvalConfig: 결재 상신 정보 (템플릿 포함)
{
title: `Vendor 실사 재의뢰 - ${data.vendorNames}`,
description: `${data.vendorNames}에 대한 실사 재의뢰`,
templateName: 'Vendor 실사 재의뢰', // 한국어 템플릿명
variables, // 치환할 변수들
approvers: data.approvers,
currentUser: data.currentUser,
}
);
debugSuccess('[PQReRequestApproval] 재의뢰 결재 워크플로우 완료', {
approvalId: result.approvalId,
pendingActionId: result.pendingActionId,
status: result.status,
});
return result;
}
|