summaryrefslogtreecommitdiff
path: root/lib/general-contract-template/service.ts
blob: 9b3eda68310d7bdefe29f6f40cfda8ba7efc3656 (plain)
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
"use server";

import { revalidateTag, revalidatePath, unstable_noStore } from "next/cache";
import db from "@/db/db";
import { getErrorMessage } from "@/lib/handle-error";
import { unstable_cache } from "@/lib/unstable-cache";
import { asc, desc, ilike, inArray, and, or, eq, type SQL, sql } from "drizzle-orm";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";

import { GeneralContractTemplate, generalContractTemplates, users } from "@/db/schema";
import { deleteFile, saveFile } from "@/lib/file-stroage";
import { 
  selectContractTemplates, 
  selectContractTemplatesWithUsers,
  countContractTemplates, 
  insertContractTemplate,
  getContractTemplateById as getContractTemplateByIdFromRepo,
  updateContractTemplate,
  deleteContractTemplates,
  findAllContractTemplates
} from "./repository";
import {
  GetContractTemplatesSchema,
  CreateContractTemplateSchema,
  UpdateContractTemplateSchema,
  DeleteContractTemplateSchema
} from "./validations";

// ----------------------------------------------------------------------------------------------------

/* HELPER FUNCTION FOR GETTING CURRENT USER ID */
async function getCurrentUserId(): Promise<number> {
  try {
    const session = await getServerSession(authOptions);
    return session?.user?.id ? Number(session.user.id) : 3; // 기본값 3, 실제 환경에서는 적절한 기본값 설정
  } catch (error) {
    console.error('Error getting current user ID:', error);
    return 3; // 기본값 3
  }
}

// ----------------------------------------------------------------------------------------------------

// =================================================================================
// Contract Template Functions
// =================================================================================

export async function getContractTemplates(
  input: GetContractTemplatesSchema
) {
  return unstable_cache(
    async () => {
      try {
    const { data, total } = await db.transaction(async (tx) => {
          // 필터 조건 구성
          let whereCondition: any = undefined;

          if (input.search) {
            const s = `%${input.search}%`;
            whereCondition = or(
              ilike(generalContractTemplates.contractTemplateName, s),
              ilike(generalContractTemplates.contractTemplateType, s),
              ilike(generalContractTemplates.fileName, s)
            );
          }

          // 필터 추가 (기본적으로 모든 상태 표시)
          let statusCondition: any = undefined;
          
          if (input.filters && input.filters.length > 0) {
            const statusFilter = input.filters.find(f => f.id === 'status');
            if (statusFilter && statusFilter.value.length > 0) {
              // statusFilter.value가 문자열이면 배열로 변환
              const statusValues = Array.isArray(statusFilter.value) 
                ? statusFilter.value 
                : [statusFilter.value];
              
              // "ALL"이 포함되어 있으면 상태 필터를 제거 (모든 상태 표시)
              if (statusValues.includes('ALL')) {
                statusCondition = undefined;
              } else {
                statusCondition = inArray(generalContractTemplates.status, statusValues);
              }
            }
            
            // 다른 필터들 처리
            const otherFilters = input.filters.filter(f => f.id !== 'status' && f.value.length > 0);
            for (const filter of otherFilters) {
              let filterCondition: any;
              
              // 계약문서명은 부분 일치 검색 (ilike)
              if (filter.id === 'contractTemplateName') {
                const searchValue = `%${filter.value}%`;
                filterCondition = ilike(generalContractTemplates.contractTemplateName, searchValue);
              } else {
                // 다른 필터들은 정확히 일치 검색 (inArray)
                filterCondition = inArray(
                  generalContractTemplates[filter.id as keyof typeof generalContractTemplates] as any,
                  filter.value
                );
              }
              
              whereCondition = whereCondition 
                ? and(whereCondition, filterCondition)
                : filterCondition;
            }
          }

          // 최종 where 조건
          if (statusCondition) {
            if (whereCondition) {
              whereCondition = and(whereCondition, statusCondition);
            } else {
              whereCondition = statusCondition;
            }
          }
          

          // 정렬 조건
          const orderBy =
            input.sort && input.sort.length > 0
              ? input.sort.map((item) =>
                item.desc
                  ? desc(generalContractTemplates[item.id as keyof typeof generalContractTemplates] as any)
                  : asc(generalContractTemplates[item.id as keyof typeof generalContractTemplates] as any)
              )
              : [desc(generalContractTemplates.createdAt)];

          // 데이터 조회 (사용자 정보 포함)
          const offset = (input.page - 1) * input.perPage;
          const dataResult = await selectContractTemplatesWithUsers(tx, {
            where: whereCondition,
            orderBy,
            offset,
            limit: input.perPage,
          });

          // 총 개수 조회
          const totalCount = await countContractTemplates(tx, whereCondition);
          
          return { data: dataResult, total: totalCount };
        });

    const pageCount = Math.ceil(total / input.perPage);

        return { data, pageCount };
      } catch (error) {
        console.error("getContractTemplates 에러:", error);
        return { data: [], pageCount: 0 };
      }
    },
    [JSON.stringify(input)],
    {
      revalidate: 3600,
      tags: ["general-contract-templates"],
    }
  )();
}

/**
 * ID로 계약 템플릿 조회
 */
export async function getContractTemplateById(id: string) {
  return unstable_cache(
    async () => {
      try {
        const template = await db
          .select()
          .from(generalContractTemplates)
          .where(eq(generalContractTemplates.id, parseInt(id)))
          .limit(1);
        return template[0] || null;
      } catch (error) {
        console.error("getContractTemplateById 에러:", error);
        return null;
      }
    },
    [id],
    { revalidate: 3600, tags: ["general-contract-templates"] }
  )();
}

/**
 * 템플릿 이름 조회
 */
export async function getExistingTemplateNamesById(id: number): Promise<string> {
  const rows = await db
    .select({
      contractTemplateName: generalContractTemplates.contractTemplateName,
    })
    .from(generalContractTemplates)
    .where(and(eq(generalContractTemplates.status, "ACTIVE"), eq(generalContractTemplates.id, id)))
    .limit(1);

  return rows[0]?.contractTemplateName || "";
}

/**
 * 템플릿 파일 저장 서버 액션
 */
export async function saveTemplateFile(templateId: number, formData: FormData) {
  try {
    const file = formData.get("file") as File;
    
    if (!file) {
      return { error: "파일이 필요합니다." };
    }

    // 기존 템플릿 정보 조회
    const existingTemplate = await db
      .select()
      .from(generalContractTemplates)
      .where(eq(generalContractTemplates.id, templateId))
      .limit(1);

    if (existingTemplate.length === 0) {
      return { error: "템플릿을 찾을 수 없습니다." };
    }

    const template = existingTemplate[0];
    if (!template.filePath) {
      return { error: "파일 경로가 없습니다." };
    }

    // 파일 저장 로직 (실제 파일 시스템에 저장)
    const { writeFile, mkdir } = await import("fs/promises");
    const { join } = await import("path");

    const bytes = await file.arrayBuffer();
    const buffer = Buffer.from(bytes);

    // 기존 파일 경로 사용 (덮어쓰기) - general-contract-templates 경로로 통일
    const uploadPath = join(process.cwd(), "public", template.filePath.replace(/^\//, ""));
    
    // 디렉토리 확인 및 생성
    const dirPath = uploadPath.substring(0, uploadPath.lastIndexOf('/'));
    await mkdir(dirPath, { recursive: true });

    // 파일 저장
    await writeFile(uploadPath, buffer);

    // 캐시 무효화 (목록/상세 모두 고려)
    revalidatePath(`/evcp/general-contract-template/${templateId}`);
    revalidateTag("general-contract-templates");

    return { success: true, message: "파일이 성공적으로 저장되었습니다." };
  } catch (error) {
    console.error("saveTemplateFile 에러:", error);
    return { error: error instanceof Error ? error.message : "파일 저장 중 오류가 발생했습니다." };
  }
}

// 새 리비전 생성 (basic-contract의 createBasicContractTemplateRevision 패턴 반영)
export async function createGeneralContractTemplateRevision(input: {
  baseTemplateId: number;
  contractTemplateType: string;
  contractTemplateName: string;
  revision: number;
  legalReviewRequired: boolean;
  status: 'ACTIVE' | 'INACTIVE' | 'DISPOSED';
  fileName: string;
  filePath: string;
}) {
  unstable_noStore();

  try {
    // 기본 템플릿 존재 확인
    const base = await db
      .select()
      .from(generalContractTemplates)
      .where(eq(generalContractTemplates.id, input.baseTemplateId))
      .limit(1);
    if (base.length === 0) {
      return { data: null, error: '기본 템플릿을 찾을 수 없습니다.' };
    }

    // 동일 이름/타입에 해당 리비전이 이미 존재하는지 검사
    const exists = await db
      .select({ rev: generalContractTemplates.revision })
      .from(generalContractTemplates)
      .where(
        and(
          eq(generalContractTemplates.contractTemplateName, input.contractTemplateName),
          eq(generalContractTemplates.contractTemplateType, input.contractTemplateType),
          eq(generalContractTemplates.revision, input.revision)
        )
      )
      .limit(1);
    if (exists.length > 0) {
      return { data: null, error: `${input.contractTemplateName} v${input.revision} 리비전이 이미 존재합니다.` };
    }

    // 기존 리비전 확인 - baseTemplateId가 있으면 해당 템플릿의 최대 리비전을 찾음
    let maxRevision = 0;
    if (input.baseTemplateId) {
      const max = await db
        .select({ rev: generalContractTemplates.revision })
        .from(generalContractTemplates)
        .where(eq(generalContractTemplates.id, input.baseTemplateId))
        .limit(1);
      maxRevision = max[0]?.rev ?? 0;
    } else {
      // baseTemplateId가 없으면 이름과 타입으로 찾음
      const max = await db
        .select({ rev: generalContractTemplates.revision })
        .from(generalContractTemplates)
        .where(
          and(
            eq(generalContractTemplates.contractTemplateName, input.contractTemplateName),
            eq(generalContractTemplates.contractTemplateType, input.contractTemplateType)
          )
        )
        .orderBy(desc(generalContractTemplates.revision))
        .limit(1);
      maxRevision = max[0]?.rev ?? 0;
    }
    
    if (input.revision <= maxRevision) {
      return { data: null, error: `새 리비전 번호는 현재 최대 리비전(v${maxRevision})보다 커야 합니다.` };
    }

    const currentUserId = await getCurrentUserId();

    const newRow = await db.transaction(async (tx) => {
      const [row] = await insertContractTemplate(tx, {
        contractTemplateType: input.contractTemplateType,
        contractTemplateName: input.contractTemplateName,
        revision: input.revision,
        status: input.status,
        legalReviewRequired: input.legalReviewRequired,
        fileName: input.fileName,
        filePath: input.filePath,
        createdAt: new Date(),
        createdBy: currentUserId,
        updatedAt: new Date(),
        updatedBy: currentUserId,
      });
      return row;
    });

    revalidateTag('general-contract-templates');
    return { data: newRow, error: null };
  } catch (error) {
    return { data: null, error: getErrorMessage(error) };
  }
}

// Contract Template 생성
export async function createContractTemplate(input: CreateContractTemplateSchema) {
  unstable_noStore();
  
  try {
    // 현재 로그인한 사용자 ID 가져오기
    const currentUserId = await getCurrentUserId();
    
    const newTemplate = await db.transaction(async (tx) => {
      const [row] = await insertContractTemplate(tx, {
        contractTemplateType: (input as any).contractTemplateType ?? (input as any).contractType,
        contractTemplateName: (input as any).contractTemplateName ?? (input as any).contractName,
        revision: input.revision || 1,
        status: input.status || "ACTIVE",
        legalReviewRequired: input.legalReviewRequired || false,
        fileName: input.fileName || null,
        filePath: input.filePath || null,
        createdAt: new Date(),
        createdBy: currentUserId,
        updatedAt: new Date(),
        updatedBy: currentUserId,
      });
      return row;
    });
    
    revalidateTag("general-contract-templates");
    revalidatePath("/evcp/general-contract-template");
    return { data: newTemplate, error: null };
  } catch (error) {
    console.log(error);
    return { data: null, error: getErrorMessage(error) };
  }
}

// Contract Template 수정
// Basic Contract 방식과 동일한 통합 업데이트 함수
export async function updateTemplate({
  id,
  formData
}: {
  id: number;
  formData: FormData;
}): Promise<{ success?: boolean; error?: string }> {
  unstable_noStore();

  try {
    // 필수값 파싱
    const contractTemplateType = formData.get("contractTemplateType") as string | null;
    const contractTemplateName = formData.get("contractTemplateName") as string | null;
    const legalReviewRequired = formData.get("legalReviewRequired") === "true";
    
    // 리비전 처리: basic-contract와 동일하게 FormData에서 그대로 사용 (없으면 1)

    if (!contractTemplateType || !contractTemplateName) {
      return { error: "계약 종류와 문서명은 필수입니다." };
    }

    // 기존 템플릿 조회
    const existing = await db
      .select()
      .from(generalContractTemplates)
      .where(eq(generalContractTemplates.id, id))
      .limit(1);

    if (existing.length === 0) {
      return { error: "템플릿을 찾을 수 없습니다." };
    }

    const prev = existing[0] as any;

    // 모든 경우에 기존 레코드 업데이트 (새 리비전 생성하지 않음)

    // 파일 처리
    const file = formData.get("file") as File | null;
    let fileName: string | undefined = undefined;
    let filePath: string | undefined = undefined;

    if (file) {
      // 1) 새 파일 저장 (원본 파일명 유지 + 충돌 시 접미사)
      const { mkdir, writeFile, access } = await import('fs/promises');
      const { join, extname, basename } = await import('path');

      const ext = extname(file.name);
      const base = basename(file.name, ext)
        .replace(/[<>:"'|?*\\\/]/g, '_')
        .replace(/[\x00-\x1f]/g, '')
        .replace(/\s+/g, ' ')
        .trim()
        .substring(0, 200);

      const dirAbs = join(process.cwd(), 'public', 'general-contract-templates');
      await mkdir(dirAbs, { recursive: true });

      let candidate = `${base}${ext}`;
      let absPath = join(dirAbs, candidate);
      let counter = 1;
      while (true) {
        try {
          await access(absPath);
          candidate = `${base} (${counter})${ext}`;
          absPath = join(dirAbs, candidate);
          counter += 1;
        } catch {
          break;
        }
      }

      const bytes = await file.arrayBuffer();
      await writeFile(absPath, Buffer.from(bytes));

      fileName = candidate;
      filePath = `/general-contract-templates/${candidate}`;

      // 2) 기존 파일 삭제
      if (prev.filePath) {
        const deleted = await deleteFile(prev.filePath);
        if (deleted) {
          console.log(`✅ 기존 파일 삭제됨: ${prev.filePath}`);
        } else {
          console.log(`⚠️ 기존 파일 삭제 실패: ${prev.filePath}`);
        }
      }
    }

    // 모든 경우에 기존 레코드 업데이트 (revision은 위 정책에 따라 결정)
    const currentUserId = await getCurrentUserId();
    
    // 리비전 처리: FormData에 있으면 사용, 없으면 현재 리비전 + 1
    const revisionFromForm = formData.get("revision")?.toString();
    let nextRevision: number;
    
    if (revisionFromForm) {
      nextRevision = Number(revisionFromForm) || 1;
    } else {
      nextRevision = (prev.revision ?? 0) + 1; // FormData에 없으면 현재 리비전 + 1
    }

    const updateData: Record<string, any> = {
      contractTemplateType,
      contractTemplateName,
      legalReviewRequired,
      revision: nextRevision, // 최종 결정된 리비전 사용
      updatedAt: new Date(),
      updatedBy: currentUserId,
    };

    if (fileName && filePath) {
      updateData.fileName = fileName;
      updateData.filePath = filePath;
    }

    await db.transaction(async (tx) => {
      await tx
        .update(generalContractTemplates)
        .set(updateData)
        .where(eq(generalContractTemplates.id, id));
    });

    revalidateTag('general-contract-templates');
    revalidatePath('/evcp/general-contract-template');
    return { success: true };
  } catch (error) {
    console.error("템플릿 업데이트 오류:", error);
    return {
      error: error instanceof Error
        ? error.message
        : "템플릿 업데이트 중 오류가 발생했습니다.",
    };
  }
}

// 기존 함수는 호환성을 위해 유지
export async function updateContractTemplateById(
  id: number, 
  input: UpdateContractTemplateSchema
) {
  unstable_noStore();
  
  try {
    const currentUserId = await getCurrentUserId();
    const updatedTemplate = await db.transaction(async (tx) => {
      const [row] = await updateContractTemplate(tx, id, input, currentUserId);
      return row;
    });
    
    revalidateTag("general-contract-templates");
    revalidatePath("/evcp/general-contract-template");
    return { data: updatedTemplate, error: null };
  } catch (error) {
    console.log(error);
    return { data: null, error: getErrorMessage(error) };
  }
}

// Contract Template 삭제
export async function removeTemplates({
  ids
}: {
  ids: number[];
}): Promise<{ success?: boolean; error?: string }> {
  if (!ids || ids.length === 0) {
    return { error: "삭제할 템플릿이 선택되지 않았습니다." };
  }

  // unstable_noStore를 최상단에 배치
  unstable_noStore();

  try {
    // 파일 삭제를 위한 템플릿 정보 조회 및 DB 삭제를 직접 트랜잭션으로 처리
    // withTransaction 대신 db.transaction 직접 사용 (createContractTemplate와 일관성 유지)
    const templateFiles: { id: number; filePath: string }[] = [];

    const result = await db.transaction(async (tx) => {
      // 각 템플릿의 파일 경로 가져오기
      for (const id of ids) {
        const template = await getContractTemplateByIdFromRepo(tx, id);
        if (template && template.filePath) {
          templateFiles.push({
            id: template.id,
            filePath: template.filePath
          });
        }
      }

      // DB에서 템플릿 삭제
      const { data, error } = await deleteContractTemplates(tx, ids);

      if (error) {
        throw new Error(`템플릿 DB 삭제 실패: ${error}`);
      }

      return { data };
    });

    // 파일 시스템 삭제는 트랜잭션 성공 후 수행
    for (const template of templateFiles) {
      const deleted = await deleteFile(template.filePath);
      
      if (deleted) {
        console.log(`✅ 파일 삭제됨: ${template.filePath}`);
      } else {
        console.log(`⚠️ 파일 삭제 실패: ${template.filePath}`);
      }
    }

    revalidateTag("general-contract-templates");
    revalidateTag("template-status-counts");

    // 디버깅을 위한 로그
    console.log("캐시 무효화 완료:", ids);

    return { success: true };
  } catch (error) {
    console.error("템플릿 삭제 중 오류 발생:", error);
    return {
      error: error instanceof Error
        ? error.message
        : "템플릿 삭제 중 오류가 발생했습니다."
    };
  }
}

// 모든 활성 Contract Template 목록 (간단한 선택용)
export async function getAllActiveContractTemplates() {
  unstable_noStore();
  
  try {
    const templates = await db.transaction(async (tx) => {
      return await findAllContractTemplates(tx);
    });
    
    return { data: templates, error: null };
  } catch (error) {
    console.log(error);
    return { data: [], error: getErrorMessage(error) };
  }
}