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
|
"use server";
import { revalidateTag } from "next/cache";
import db from "@/db/db";
import { eq, and, desc, inArray, sql, isNotNull, ne } from "drizzle-orm";
import { agreementComments, basicContract, vendors, users } from "@/db/schema";
import { saveFile, deleteFile } from "@/lib/file-stroage";
import { sendEmail } from "@/lib/mail/sendEmail";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
export type AgreementCommentAuthorType = 'SHI' | 'Vendor';
export interface AgreementCommentAttachment {
id: string;
fileName: string;
filePath: string;
fileSize: number;
uploadedAt: Date;
}
export interface AgreementCommentData {
id: number;
basicContractId: number;
authorType: AgreementCommentAuthorType;
authorUserId: number | null;
authorVendorId: number | null;
authorName: string | null;
comment: string;
attachments: AgreementCommentAttachment[];
createdAt: Date;
updatedAt: Date;
}
export interface AgreementCommentSummary {
hasComments: boolean;
commentCount: number;
}
function getContractDocumentLabel(templateName?: string | null) {
if (!templateName) return "기본계약서";
const normalized = templateName.toLowerCase();
if (normalized.includes('준법')) {
return "준법서약";
}
if (normalized.includes('gtc')) {
return "GTC 기본계약서";
}
return templateName;
}
/**
* 특정 기본계약서의 모든 코멘트 조회
*/
export async function getAgreementComments(
basicContractId: number
): Promise<AgreementCommentData[]> {
try {
const comments = await db
.select()
.from(agreementComments)
.where(
and(
eq(agreementComments.basicContractId, basicContractId),
eq(agreementComments.isDeleted, false),
isNotNull(agreementComments.comment),
ne(agreementComments.comment, '')
)
)
.orderBy(desc(agreementComments.createdAt));
const mappedComments: AgreementCommentData[] = comments.map((comment) => {
// attachments 안전하게 파싱
let attachments: AgreementCommentAttachment[] = [];
if (comment.attachments) {
try {
const attachmentData = comment.attachments;
// 문자열인 경우 파싱 시도
if (typeof attachmentData === 'string') {
const trimmed = String(attachmentData).trim();
if (trimmed && trimmed !== '') {
attachments = JSON.parse(trimmed);
}
}
// 이미 배열인 경우 그대로 사용
else if (Array.isArray(attachmentData)) {
attachments = attachmentData as AgreementCommentAttachment[];
}
} catch (parseError) {
console.warn(`⚠️ [getAgreementComments] 코멘트 ${comment.id}의 attachments 파싱 실패:`, parseError);
console.warn(` attachments 값:`, comment.attachments);
attachments = []; // 파싱 실패 시 빈 배열로 설정
}
}
return {
...comment,
authorType: comment.authorType as AgreementCommentAuthorType,
attachments,
} as AgreementCommentData;
});
return mappedComments;
} catch (error) {
console.error("코멘트 조회 실패:", error);
throw new Error("코멘트 조회 중 오류가 발생했습니다.");
}
}
/**
* 코멘트 추가 (파일 첨부 포함)
*/
export async function addAgreementComment(data: {
basicContractId: number;
comment: string;
authorName?: string;
files?: File[];
shouldSendEmail?: boolean; // 이메일 발송 여부 (제출할 때만 true)
}): Promise<{ success: boolean; data?: AgreementCommentData; error?: string }> {
try {
const session = await getServerSession(authOptions);
if (!session?.user) {
return { success: false, error: "인증이 필요합니다." };
}
// 사용자 정보로부터 authorType 결정
// companyId가 있으면 Vendor, 없으면 SHI
const user = session.user as any;
const isVendor = !!user.companyId;
const authorType: AgreementCommentAuthorType = isVendor ? 'Vendor' : 'SHI';
// 기본계약서 정보 조회 (이메일 발송을 위해)
const [contract] = await db
.select()
.from(basicContract)
.where(eq(basicContract.id, data.basicContractId))
.limit(1);
if (!contract) {
return { success: false, error: "계약서를 찾을 수 없습니다." };
}
// 벤더 정보 조회 (이메일 발송용)
let vendor: any = null;
if (contract.vendorId) {
[vendor] = await db
.select()
.from(vendors)
.where(eq(vendors.id, contract.vendorId))
.limit(1);
}
// 요청자 정보 조회 (이메일 발송용)
let requester: any = null;
if (contract.requestedBy) {
[requester] = await db
.select()
.from(users)
.where(eq(users.id, contract.requestedBy))
.limit(1);
}
// 템플릿 이름 조회
const { basicContractTemplates } = await import("@/db/schema");
let templateName: string | null = null;
if (contract.templateId) {
const [template] = await db
.select()
.from(basicContractTemplates)
.where(eq(basicContractTemplates.id, contract.templateId))
.limit(1);
templateName = template?.templateName || null;
}
// 파일 업로드 처리 (있을 경우)
const uploadedAttachments: AgreementCommentAttachment[] = [];
if (data.files && data.files.length > 0) {
for (const file of data.files) {
try {
const saveResult = await saveFile({
file,
directory: "agreement-comments",
originalName: file.name,
userId: user.id?.toString(),
});
if (saveResult.success && saveResult.publicPath) {
uploadedAttachments.push({
id: crypto.randomUUID(),
fileName: file.name,
filePath: saveResult.publicPath,
fileSize: file.size,
uploadedAt: new Date(),
});
}
} catch (fileError) {
console.error(`파일 업로드 실패 (${file.name}):`, fileError);
// 개별 파일 실패는 무시하고 계속 진행
}
}
}
// 코멘트 저장
const [newComment] = await db
.insert(agreementComments)
.values({
basicContractId: data.basicContractId,
authorType,
authorUserId: isVendor ? null : parseInt(user.id),
authorVendorId: isVendor ? user.companyId : null,
authorName: data.authorName || user.name,
comment: data.comment,
attachments: uploadedAttachments.length > 0 ? JSON.stringify(uploadedAttachments) : JSON.stringify([]),
} as any)
.returning();
// 이메일 알림 발송 (shouldSendEmail이 true일 때만)
if (data.shouldSendEmail) {
try {
await sendCommentNotificationEmail({
comment: newComment,
contract,
vendor,
requester,
templateName,
authorType,
authorName: data.authorName || user.name,
attachmentCount: uploadedAttachments.length,
});
} catch (emailError) {
console.error("이메일 발송 실패:", emailError);
// 이메일 실패는 코멘트 저장 성공에 영향을 주지 않음
}
}
// 계약서 상태 업데이트 (협의중으로 변경)
await updateContractNegotiationStatus(data.basicContractId);
// 캐시 무효화: 코멘트 목록 + 기본계약서 목록
revalidateTag(`agreement-comments-${data.basicContractId}`);
revalidateTag(`basic-contracts`); // 기본계약서 목록 새로고침
return {
success: true,
data: {
...newComment,
authorType: newComment.authorType as AgreementCommentAuthorType,
attachments: uploadedAttachments,
} as AgreementCommentData,
};
} catch (error) {
console.error("코멘트 추가 실패:", error);
return {
success: false,
error: "코멘트 추가 중 오류가 발생했습니다.",
};
}
}
/**
* 코멘트 삭제
*/
export async function deleteAgreementComment(
commentId: number
): Promise<{ success: boolean; error?: string }> {
try {
const session = await getServerSession(authOptions);
if (!session?.user) {
return { success: false, error: "인증이 필요합니다." };
}
// 코멘트 조회
const [comment] = await db
.select()
.from(agreementComments)
.where(eq(agreementComments.id, commentId));
if (!comment) {
return { success: false, error: "코멘트를 찾을 수 없습니다." };
}
// 권한 확인 (작성자만 삭제 가능)
const user = session.user as any;
const isVendor = !!user.companyId;
const canDelete =
(isVendor && comment.authorVendorId === user.companyId) ||
(!isVendor && comment.authorUserId === parseInt(user.id));
if (!canDelete) {
return { success: false, error: "삭제 권한이 없습니다." };
}
// Soft delete
await db
.update(agreementComments)
.set({
isDeleted: true,
deletedAt: new Date(),
updatedAt: new Date(),
} as any)
.where(eq(agreementComments.id, commentId));
// 첨부파일이 있으면 파일 시스템에서도 삭제
if (comment.attachments) {
try {
const attachmentsStr = typeof comment.attachments === 'string'
? comment.attachments
: JSON.stringify(comment.attachments);
const attachments: AgreementCommentAttachment[] = JSON.parse(attachmentsStr);
for (const attachment of attachments) {
try {
await deleteFile(attachment.filePath);
} catch (fileError) {
console.error("파일 삭제 실패:", fileError);
}
}
} catch (parseError) {
console.error("첨부파일 파싱 실패:", parseError);
}
}
// 캐시 무효화: 코멘트 목록 + 기본계약서 목록
revalidateTag(`agreement-comments-${comment.basicContractId}`);
revalidateTag(`basic-contracts`); // 기본계약서 목록 새로고침
return { success: true };
} catch (error) {
console.error("코멘트 삭제 실패:", error);
return {
success: false,
error: "코멘트 삭제 중 오류가 발생했습니다.",
};
}
}
/**
* 첨부파일 업로드
*/
export async function uploadCommentAttachment(
commentId: number,
file: File
): Promise<{ success: boolean; data?: AgreementCommentAttachment; error?: string }> {
try {
const session = await getServerSession(authOptions);
if (!session?.user) {
return { success: false, error: "인증이 필요합니다." };
}
// 코멘트 조회
const [comment] = await db
.select()
.from(agreementComments)
.where(eq(agreementComments.id, commentId));
if (!comment) {
return { success: false, error: "코멘트를 찾을 수 없습니다." };
}
// 파일 저장
const saveResult = await saveFile({
file,
directory: "agreement-comments",
originalName: file.name,
userId: (session.user as any).id?.toString(),
});
if (!saveResult.success) {
return { success: false, error: saveResult.error };
}
// 첨부파일 정보 생성
const newAttachment: AgreementCommentAttachment = {
id: crypto.randomUUID(),
fileName: file.name,
filePath: saveResult.publicPath!,
fileSize: file.size,
uploadedAt: new Date(),
};
// 기존 첨부파일에 추가
let existingAttachments: AgreementCommentAttachment[] = [];
if (comment.attachments) {
try {
const attachmentsStr = typeof comment.attachments === 'string'
? comment.attachments
: JSON.stringify(comment.attachments);
existingAttachments = JSON.parse(attachmentsStr);
} catch (parseError) {
console.error("기존 첨부파일 파싱 실패:", parseError);
}
}
existingAttachments.push(newAttachment);
// DB 업데이트
await db
.update(agreementComments)
.set({
attachments: JSON.stringify(existingAttachments),
updatedAt: new Date(),
} as any)
.where(eq(agreementComments.id, commentId));
revalidateTag(`agreement-comments-${comment.basicContractId}`);
return { success: true, data: newAttachment };
} catch (error) {
console.error("첨부파일 업로드 실패:", error);
return {
success: false,
error: "첨부파일 업로드 중 오류가 발생했습니다.",
};
}
}
/**
* 첨부파일 삭제
*/
export async function deleteCommentAttachment(
commentId: number,
attachmentId: string
): Promise<{ success: boolean; error?: string }> {
try {
const session = await getServerSession(authOptions);
if (!session?.user) {
return { success: false, error: "인증이 필요합니다." };
}
// 코멘트 조회
const [comment] = await db
.select()
.from(agreementComments)
.where(eq(agreementComments.id, commentId));
if (!comment) {
return { success: false, error: "코멘트를 찾을 수 없습니다." };
}
// 첨부파일 목록에서 제거
let attachments: AgreementCommentAttachment[] = [];
if (comment.attachments) {
try {
const attachmentsStr = typeof comment.attachments === 'string'
? comment.attachments
: JSON.stringify(comment.attachments);
attachments = JSON.parse(attachmentsStr);
} catch (parseError) {
console.error("첨부파일 파싱 실패:", parseError);
return { success: false, error: "첨부파일 정보를 읽을 수 없습니다." };
}
}
const targetAttachment = attachments.find((a) => a.id === attachmentId);
if (!targetAttachment) {
return { success: false, error: "첨부파일을 찾을 수 없습니다." };
}
const updatedAttachments = attachments.filter((a) => a.id !== attachmentId);
// DB 업데이트
await db
.update(agreementComments)
.set({
attachments: JSON.stringify(updatedAttachments),
updatedAt: new Date(),
} as any)
.where(eq(agreementComments.id, commentId));
// 파일 시스템에서 삭제
try {
await deleteFile(targetAttachment.filePath);
} catch (fileError) {
console.error("파일 삭제 실패:", fileError);
}
revalidateTag(`agreement-comments-${comment.basicContractId}`);
return { success: true };
} catch (error) {
console.error("첨부파일 삭제 실패:", error);
return {
success: false,
error: "첨부파일 삭제 중 오류가 발생했습니다.",
};
}
}
/**
* 협의 상태 확인 (코멘트가 있는지)
*/
export async function checkNegotiationStatus(
basicContractId: number
): Promise<{ hasComments: boolean; commentCount: number }> {
try {
const comments = await db
.select()
.from(agreementComments)
.where(
and(
eq(agreementComments.basicContractId, basicContractId),
eq(agreementComments.isDeleted, false)
)
);
return {
hasComments: comments.length > 0,
commentCount: comments.length,
};
} catch (error) {
console.error("협의 상태 확인 실패:", error);
return { hasComments: false, commentCount: 0 };
}
}
/**
* 다수의 계약서에 대한 협의 코멘트 상태 조회
*/
export async function checkAgreementCommentsForContracts(
contracts: { id: number }[]
): Promise<Record<number, AgreementCommentSummary>> {
if (!contracts || contracts.length === 0) {
return {};
}
try {
const contractIds = contracts.map(contract => contract.id);
const commentCounts = await db
.select({
contractId: agreementComments.basicContractId,
count: sql<number>`count(*)`,
})
.from(agreementComments)
.where(
and(
inArray(agreementComments.basicContractId, contractIds),
eq(agreementComments.isDeleted, false)
)
)
.groupBy(agreementComments.basicContractId);
const countsMap = new Map<number, number>();
commentCounts.forEach(({ contractId, count }) => {
countsMap.set(contractId, Number(count || 0));
});
const result: Record<number, AgreementCommentSummary> = {};
contractIds.forEach((contractId) => {
const count = countsMap.get(contractId) ?? 0;
result[contractId] = {
hasComments: count > 0,
commentCount: count,
};
});
return result;
} catch (error) {
console.error("협의 상태 조회 실패:", error);
return {};
}
}
/**
* 이메일 알림 발송
*/
async function sendCommentNotificationEmail(params: {
comment: typeof agreementComments.$inferSelect;
contract: typeof basicContract.$inferSelect;
vendor: typeof vendors.$inferSelect | null;
requester: typeof users.$inferSelect | null;
templateName: string | null;
authorType: AgreementCommentAuthorType;
authorName: string;
attachmentCount?: number;
}) {
const { comment, contract, vendor, requester, templateName, authorType, authorName, attachmentCount = 0 } = params;
const documentTypeLabel = getContractDocumentLabel(templateName);
// 수신자 결정
let recipientEmail: string | undefined;
let recipientName: string | undefined;
if (authorType === 'Vendor') {
// 벤더가 작성한 경우 -> SHI 담당자에게 발송
if (requester) {
recipientEmail = requester.email || undefined;
recipientName = requester.name || undefined;
}
} else {
// SHI가 작성한 경우 -> 벤더에게 발송
if (vendor) {
recipientEmail = vendor.email || undefined;
recipientName = vendor.vendorName || undefined;
}
}
if (!recipientEmail) {
console.warn("수신자 이메일을 찾을 수 없습니다.");
return;
}
// 이메일 발송
await sendEmail({
to: recipientEmail,
subject: `[eVCP] ${documentTypeLabel} 협의 코멘트 제출 - ${templateName || '기본계약서'}`,
template: "agreement-comment-notification",
context: {
language: "ko",
recipientName: recipientName || "담당자",
authorName,
authorType: authorType === 'SHI' ? '삼성중공업' : '협력업체',
comment: comment.comment,
templateName: templateName || '기본계약서',
documentTypeLabel,
vendorName: vendor?.vendorName || '',
attachmentCount,
contractUrl: `${process.env.NEXT_PUBLIC_APP_URL}/evcp/basic-contract/${contract.id}`,
systemUrl: process.env.NEXT_PUBLIC_APP_URL || 'https://evcp.com',
currentYear: new Date().getFullYear(),
},
});
}
/**
* 협의 완료 처리
*/
export async function completeNegotiation(
basicContractId: number
): Promise<{ success: boolean; error?: string }> {
try {
const session = await getServerSession(authOptions);
if (!session?.user) {
return { success: false, error: "인증이 필요합니다." };
}
// 기본계약서 정보 조회
const [contract] = await db
.select()
.from(basicContract)
.where(eq(basicContract.id, basicContractId))
.limit(1);
if (!contract) {
return { success: false, error: "계약서를 찾을 수 없습니다." };
}
// 협의 완료 상태로 업데이트
await db
.update(basicContract)
.set({
negotiationCompletedAt: new Date(),
updatedAt: new Date(),
} as any)
.where(eq(basicContract.id, basicContractId));
// 벤더 정보 조회 (이메일 발송용)
let vendor: any = null;
if (contract.vendorId) {
[vendor] = await db
.select()
.from(vendors)
.where(eq(vendors.id, contract.vendorId))
.limit(1);
}
// 요청자 정보 조회 (이메일 발송용)
let requester: any = null;
if (contract.requestedBy) {
[requester] = await db
.select()
.from(users)
.where(eq(users.id, contract.requestedBy))
.limit(1);
}
// 템플릿 이름 조회
const { basicContractTemplates } = await import("@/db/schema");
let templateName: string | null = null;
if (contract.templateId) {
const [template] = await db
.select()
.from(basicContractTemplates)
.where(eq(basicContractTemplates.id, contract.templateId))
.limit(1);
templateName = template?.templateName || null;
}
const documentTypeLabel = getContractDocumentLabel(templateName);
// 이메일 알림 발송
try {
if (requester) {
await sendEmail({
to: requester.email || '',
subject: `[eVCP] ${documentTypeLabel} 협의 완료 - ${templateName || '기본계약서'}`,
template: "negotiation-complete-notification",
context: {
language: "ko",
recipientName: requester.name || "담당자",
templateName: templateName || '기본계약서',
documentTypeLabel,
vendorName: vendor?.vendorName || '',
contractUrl: `${process.env.NEXT_PUBLIC_APP_URL}/evcp/basic-contract/${contract.id}`,
systemUrl: process.env.NEXT_PUBLIC_APP_URL || 'https://evcp.com',
currentYear: new Date().getFullYear(),
},
});
}
} catch (emailError) {
console.error("이메일 발송 실패:", emailError);
// 이메일 실패는 협의 완료 처리 성공에 영향을 주지 않음
}
// 캐시 무효화
revalidateTag(`agreement-comments-${basicContractId}`);
revalidateTag(`basic-contracts`);
return { success: true };
} catch (error) {
console.error("협의 완료 처리 실패:", error);
return {
success: false,
error: "협의 완료 처리 중 오류가 발생했습니다.",
};
}
}
/**
* 계약서 협의 상태 업데이트
* 실제로 DB 상태를 변경하지 않고, gtcData 조회 시 agreementComments 존재 여부로 판단
*/
async function updateContractNegotiationStatus(basicContractId: number) {
// agreementComments 테이블에 코멘트가 있으면
// checkGTCCommentsForContract 함수에서 자동으로 hasComments: true 반환
// 별도 상태 업데이트 불필요
console.log(`✅ 계약서 ${basicContractId} 협의 코멘트 추가됨 - 목록에서 자동 반영`);
}
|