summaryrefslogtreecommitdiff
path: root/lib/project-doc-templates/service.ts
blob: a5bccce5e6b1aa74ec2ac8d1b0407e9fad487335 (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
// lib/project-doc-templates/service.ts
"use server";

import db from "@/db/db";
import { projectDocTemplates, projectDocTemplateUsage, projects, type NewProjectDocTemplate, type DocTemplateVariable } from "@/db/schema";
import { eq, and, desc, asc, isNull, sql, inArray, count, or, ilike } from "drizzle-orm";
import { revalidatePath, revalidateTag } from "next/cache";
import { GetDOCTemplatesSchema } from "./validations";
import { filterColumns } from "@/lib/filter-columns";
import { getServerSession } from "next-auth/next"
import { authOptions } from "@/app/api/auth/[...nextauth]/route"

// 기본 변수 정의
const DEFAULT_VARIABLES: DocTemplateVariable[] = [
  {
    name: "document_number",
    displayName: "문서번호",
    type: "text",
    required: true,
    description: "문서 고유 번호",
  },
  {
    name: "project_code",
    displayName: "프로젝트 코드",
    type: "text",
    required: true,
    description: "프로젝트 식별 코드",
  },
  {
    name: "project_name",
    displayName: "프로젝트명",
    type: "text",
    required: true,
    description: "프로젝트 이름",
  },
  {
    name: "created_date",
    displayName: "작성일",
    type: "date",
    required: false,
    defaultValue: "{{today}}",
    description: "문서 작성 날짜",
  },
  {
    name: "author_name",
    displayName: "작성자",
    type: "text",
    required: false,
    description: "문서 작성자 이름",
  },
  {
    name: "department",
    displayName: "부서명",
    type: "text",
    required: false,
    description: "작성 부서",
  },
];


export async function getProjectDocTemplates(
  input: GetDOCTemplatesSchema
) {
  try {
    const offset = (input.page - 1) * input.perPage;

    // 고급 필터 조건
    const advancedWhere = filterColumns({
      table: projectDocTemplates,
      filters: input.filters,
      joinOperator: input.joinOperator,
    });

    // 전역 검색 조건 (중복 제거됨)
    let globalWhere: SQL | undefined = undefined;
    if (input.search?.trim()) {
      const searchTerm = `%${input.search.trim()}%`;
      globalWhere = or(
        ilike(projectDocTemplates.templateName, searchTerm),
        ilike(projectDocTemplates.templateCode, searchTerm),
        ilike(projectDocTemplates.projectCode, searchTerm),
        ilike(projectDocTemplates.projectName, searchTerm),
        ilike(projectDocTemplates.documentType, searchTerm),
        ilike(projectDocTemplates.fileName, searchTerm), // 중복 제거
        ilike(projectDocTemplates.createdByName, searchTerm),
        ilike(projectDocTemplates.updatedByName, searchTerm),
        ilike(projectDocTemplates.status, searchTerm),
        ilike(projectDocTemplates.description, searchTerm) // 추가 고려
      );
    }

    // WHERE 조건 결합
    const whereCondition = and(advancedWhere, globalWhere,  eq(projectDocTemplates.isLatest,true));

    // 정렬 조건 (타입 안정성 개선)
    const orderBy = input.sort.length > 0
      ? input.sort.map((item) => {
          const column = projectDocTemplates[item.id as keyof typeof projectDocTemplates];
          if (!column) {
            console.warn(`Invalid sort column: ${item.id}`);
            return null;
          }
          return item.desc ? desc(column) : asc(column);
        }).filter(Boolean) as SQL[]
      : [desc(projectDocTemplates.createdAt)];

    // 데이터 조회 (프로젝트 정보 조인 추가 고려)
    const [data, totalCount] = await Promise.all([
      db
        .select({
          ...projectDocTemplates,
          // 프로젝트 정보가 필요한 경우
          // project: projects
        })
        .from(projectDocTemplates)
        // .leftJoin(projects, eq(projectDocTemplates.projectId, projects.id))
        .where(whereCondition)
        .orderBy(...orderBy)
        .limit(input.perPage)
        .offset(offset),
      db
        .select({ count: count() })
        .from(projectDocTemplates)
        .where(whereCondition)
        .then((res) => res[0]?.count ?? 0),
    ]);

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

    return {
      data,
      pageCount,
      totalCount,
    };
  } catch (error) {
    console.error("Failed to fetch project doc templates:", error);
    throw new Error("템플릿 목록을 불러오는데 실패했습니다.");
  }
}

// 템플릿 생성
export async function createProjectDocTemplate(data: {
  templateName: string;
  templateCode?: string;
  description?: string;
  projectId?: number;
  templateType: "PROJECT" | "COMPANY_WIDE";
  documentType: string;
  filePath: string;
  fileName: string;
  fileSize?: number;
  mimeType?: string;
  variables?: DocTemplateVariable[];
  isPublic?: boolean;
  requiresApproval?: boolean;
  createdBy?: string;
}) {
  try {
    // 템플릿 코드 자동 생성 (없을 경우)
    const templateCode = data.templateCode || `TPL_${Date.now()}`;
    const session = await getServerSession(authOptions)
    if (!session?.user?.id) {
      throw new Error("인증이 필요합니다.")
    }


    // 프로젝트 정보 조회 (projectId가 있는 경우)
    let projectInfo = null;
    if (data.projectId) {
      projectInfo = await db
        .select()
        .from(projects)
        .where(eq(projects.id, data.projectId))
        .then((res) => res[0]);
    }

    // 변수 정보 설정 (기본 변수 + 사용자 정의 변수)
    const variables = [...DEFAULT_VARIABLES, ...(data.variables || [])];
    const requiredVariables = variables
      .filter((v) => v.required)
      .map((v) => v.name);

    const newTemplate: NewProjectDocTemplate = {
      templateName: data.templateName,
      templateCode,
      description: data.description,
      projectId: data.projectId,
      projectCode: projectInfo?.code,
      projectName: projectInfo?.name,
      templateType: data.templateType,
      documentType: data.documentType,
      filePath: data.filePath,
      fileName: data.fileName,
      fileSize: data.fileSize,
      mimeType: data.mimeType,
      variables,
      requiredVariables,
      isPublic: data.isPublic || false,
      requiresApproval: data.requiresApproval || false,
      status: "ACTIVE",
      createdBy: Number(session.user.id),
      createdByName: Number(session.user.name),
    };

    const [template] = await db
      .insert(projectDocTemplates)
      .values(newTemplate)
      .returning();

    revalidateTag("project-doc-templates");
    revalidatePath("/project-doc-templates");

    return { success: true, data: template };
  } catch (error) {
    console.error("Failed to create template:", error);
    return { success: false, error: "템플릿 생성에 실패했습니다." };
  }
}

// 템플릿 상세 조회
export async function getProjectDocTemplateById(templateId: number) {
  try {
    const template = await db
      .select({
        template: projectDocTemplates,
        project: projects,
      })
      .from(projectDocTemplates)
      .leftJoin(projects, eq(projectDocTemplates.projectId, projects.id))
      .where(eq(projectDocTemplates.id, templateId))
      .then((res) => res[0]);

    if (!template) {
      throw new Error("템플릿을 찾을 수 없습니다.");
    }

    // 버전 히스토리 조회
    const versionHistory = await db
      .select()
      .from(projectDocTemplates)
      .where(
        and(
          eq(projectDocTemplates.templateCode, template.template.templateCode),
          isNull(projectDocTemplates.deletedAt)
        )
      )
      .orderBy(desc(projectDocTemplates.version));

    // 사용 이력 조회 (최근 10건)
    const usageHistory = await db
      .select()
      .from(projectDocTemplateUsage)
      .where(eq(projectDocTemplateUsage.templateId, templateId))
      .orderBy(desc(projectDocTemplateUsage.usedAt))
      .limit(10);

    return {
      ...template.template,
      project: template.project,
      versionHistory,
      usageHistory,
    };
  } catch (error) {
    console.error("Failed to fetch template details:", error);
    throw new Error("템플릿 상세 정보를 불러오는데 실패했습니다.");
  }
}

// 템플릿 업데이트
export async function updateProjectDocTemplate(
  templateId: number,
  data: Partial<{
    templateName: string;
    description: string;
    documentType: string;
    variables: TemplateVariable[];
    isPublic: boolean;
    requiresApproval: boolean;
    status: string;
    updatedBy: string;
  }>
) {
  try {
    // 변수 정보 업데이트 시 requiredVariables도 함께 업데이트
    const updateData: any = { ...data, updatedAt: new Date() };
    
    if (data.variables) {
      updateData.variables = data.variables;
      updateData.requiredVariables = data.variables
        .filter((v) => v.required)
        .map((v) => v.name);
    }

    const [updated] = await db
      .update(projectDocTemplates)
      .set(updateData)
      .where(eq(projectDocTemplates.id, templateId))
      .returning();

    revalidateTag("project-doc-templates");
    revalidatePath("/project-doc-templates");

    return { success: true, data: updated };
  } catch (error) {
    console.error("Failed to update template:", error);
    return { success: false, error: "템플릿 업데이트에 실패했습니다." };
  }
}

// 새 버전 생성
export async function createTemplateVersion(
  templateId: number,
  data: {
    filePath: string;
    fileName: string;
    fileSize?: number;
    mimeType?: string;
    variables?: DocTemplateVariable[];
    createdBy?: string;
  }
) {
  try {

    const session = await getServerSession(authOptions)
    if (!session?.user?.id) {
      throw new Error("인증이 필요합니다.")
    }


    // 기존 템플릿 조회
    const existingTemplate = await db
      .select()
      .from(projectDocTemplates)
      .where(eq(projectDocTemplates.id, templateId))
      .then((res) => res[0]);

    if (!existingTemplate) {
      throw new Error("템플릿을 찾을 수 없습니다.");
    }

    // 모든 버전의 isLatest를 false로 업데이트
    await db
      .update(projectDocTemplates)
      .set({ isLatest: false })
      .where(eq(projectDocTemplates.templateCode, existingTemplate.templateCode));

    // 새 버전 생성
    const newVersion = existingTemplate.version + 1;
    const variables = data.variables || existingTemplate.variables;

    const [newTemplate] = await db
      .insert(projectDocTemplates)
      .values({
        ...existingTemplate,
        id: undefined, // 새 ID 자동 생성
        version: newVersion,
        isLatest: true,
        parentTemplateId: templateId,
        filePath: data.filePath,
        fileName: data.fileName,
        fileSize: data.fileSize,
        mimeType: data.mimeType,
        variables,
        requiredVariables: variables
          .filter((v: DocTemplateVariable) => v.required)
          .map((v: DocTemplateVariable) => v.name),
        createdBy:Number(session.user.id),
        creaetedByName:session.user.name,
        updatedBy:Number(session.user.id),
        updatedByName:session.user.name,
        createdAt: new Date(),
        updatedAt: new Date(),
      })
      .returning();

    revalidateTag("project-doc-templates");
    revalidatePath("/project-doc-templates");

    return { success: true, data: newTemplate };
  } catch (error) {
    console.error("Failed to create template version:", error);
    return { success: false, error: "새 버전 생성에 실패했습니다." };
  }
}

// 템플릿 삭제 (soft delete)
export async function deleteProjectDocTemplate(templateId: number) {
  try {
    await db
      .update(projectDocTemplates)
      .set({ 
        deletedAt: new Date(),
        status: "ARCHIVED" 
      })
      .where(eq(projectDocTemplates.id, templateId));

    revalidateTag("project-doc-templates");
    revalidatePath("/project-doc-templates");

    return { success: true };
  } catch (error) {
    console.error("Failed to delete template:", error);
    return { success: false, error: "템플릿 삭제에 실패했습니다." };
  }
}

// 템플릿 파일 저장
export async function tlsaveTemplateFile(
  templateId: number,
  formData: FormData
) {
  try {
    const file = formData.get("file") as File;
    if (!file) {
      throw new Error("파일이 없습니다.");
    }

    // 파일 저장 로직 (실제 구현 필요)
    const fileName = file.name;
    const filePath = `/uploads/templates/${templateId}/${fileName}`;

    // 템플릿 파일 경로 업데이트
    await db
      .update(projectDocTemplates)
      .set({
        filePath,
        fileName,
        updatedAt: new Date(),
      })
      .where(eq(projectDocTemplates.id, templateId));

    revalidateTag("project-doc-templates");

    return { success: true, filePath };
  } catch (error) {
    console.error("Failed to save template file:", error);
    return { success: false, error: "파일 저장에 실패했습니다." };
  }
}

// 사용 가능한 프로젝트 목록 조회
export async function getAvailableProjects() {
  try {
    const projectList = await db
      .select()
      .from(projects)
      // .where(eq(projects.status, "ACTIVE"))
      .orderBy(projects.code);

    return projectList;
  } catch (error) {
    console.error("Failed to fetch projects:", error);
    return [];
  }
}

// 템플릿 사용 기록 생성
export async function recordTemplateUsage(
  templateId: number,
  data: {
    generatedDocumentId: string;
    generatedFilePath: string;
    generatedFileName: string;
    usedVariables: Record<string, any>;
    usedInProjectId?: number;
    usedInProjectCode?: string;
    usedBy: string;
    metadata?: any;
  }
) {
  try {
    const [usage] = await db
      .insert(projectDocTemplateUsage)
      .values({
        templateId,
        ...data,
      })
      .returning();

    return { success: true, data: usage };
  } catch (error) {
    console.error("Failed to record template usage:", error);
    return { success: false, error: "사용 기록 생성에 실패했습니다." };
  }
}