summaryrefslogtreecommitdiff
path: root/lib/vendor-info/service.ts
blob: 6002179f65a27baf8f7179089eb20a15928deb75 (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
"use server";

import db from "@/db/db";
import { vendorAttachments, evaluationTargets, periodicEvaluations, vendors, vendorTypes } from "@/db/schema";
import { eq, desc } from "drizzle-orm";

// 벤더 첨부파일 조회
export async function getVendorAttachmentsByType(vendorId: number) {
  try {
    const attachments = await db
      .select({
        id: vendorAttachments.id,
        fileName: vendorAttachments.fileName,
        filePath: vendorAttachments.filePath,
        attachmentType: vendorAttachments.attachmentType,
        fileType: vendorAttachments.fileType,
        createdAt: vendorAttachments.createdAt,
      })
      .from(vendorAttachments)
      .where(eq(vendorAttachments.vendorId, vendorId));

    // 타입별로 그룹화
    const attachmentsByType = attachments.reduce((acc, attachment) => {
      const type = attachment.attachmentType || 'GENERAL';
      if (!acc[type]) {
        acc[type] = [];
      }
      acc[type].push(attachment);
      return acc;
    }, {} as Record<string, typeof attachments>);

    return {
      success: true,
      data: attachmentsByType,
    };
  } catch (error) {
    console.error("첨부파일 조회 오류:", error);
    return {
      success: false,
      error: "첨부파일을 불러오는데 실패했습니다.",
    };
  }
}

// 정기평가 등급 조회
export async function getVendorPeriodicGrade(vendorId: number) {
  try {
    // evaluation_targets에서 vendorId로 조회하여 평가 대상 ID 찾기
    const evaluationTarget = await db
      .select({
        id: evaluationTargets.id,
      })
      .from(evaluationTargets)
      .where(eq(evaluationTargets.vendorId, vendorId))
      .limit(1);

    if (evaluationTarget.length === 0) {
      return {
        success: true,
        data: null, // 평가 대상이 없음
      };
    }

    // periodic_evaluations에서 최신 finalGrade 조회
    const latestEvaluation = await db
      .select({
        finalGrade: periodicEvaluations.finalGrade,
        evaluationPeriod: periodicEvaluations.evaluationPeriod,
        finalizedAt: periodicEvaluations.finalizedAt,
      })
      .from(periodicEvaluations)
      .where(eq(periodicEvaluations.evaluationTargetId, evaluationTarget[0].id))
      .orderBy(desc(periodicEvaluations.finalizedAt))
      .limit(1);

    return {
      success: true,
      data: latestEvaluation[0] || null,
    };
  } catch (error) {
    console.error("정기평가 등급 조회 오류:", error);
    return {
      success: false,
      error: "정기평가 등급을 불러오는데 실패했습니다.",
    };
  }
}

// 첨부파일 개수 조회 (특정 타입)
export async function getAttachmentCount(vendorId: number, attachmentType: string) {
  try {
    const count = await db
      .select({ count: vendorAttachments.id })
      .from(vendorAttachments)
      .where(
        eq(vendorAttachments.vendorId, vendorId) &&
        eq(vendorAttachments.attachmentType, attachmentType)
      );

    return count.length;
  } catch (error) {
    console.error(`${attachmentType} 첨부파일 개수 조회 오류:`, error);
    return 0;
  }
}

// 벤더 타입 정보 조회 (잠재업체/정규업체 구분)
export async function getVendorTypeInfo(vendorId: number) {
  try {
    const vendorWithType = await db
      .select({
        vendorTypeName: vendorTypes.nameKo,
        vendorTypeCode: vendorTypes.code,
        vendorTypeNameEn: vendorTypes.nameEn,
      })
      .from(vendors)
      .leftJoin(vendorTypes, eq(vendors.vendorTypeId, vendorTypes.id))
      .where(eq(vendors.id, vendorId))
      .limit(1);

    if (vendorWithType.length === 0) {
      return {
        success: true,
        data: null,
      };
    }

    return {
      success: true,
      data: vendorWithType[0],
    };
  } catch (error) {
    console.error("벤더 타입 정보 조회 오류:", error);
    return {
      success: false,
      error: "벤더 타입 정보를 불러오는데 실패했습니다.",
    };
  }
}