diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-08-21 09:10:06 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-08-21 09:10:06 +0000 |
| commit | 94082bfe915d3b0337f8929a2bb27828abb5d3c7 (patch) | |
| tree | 0a100365ac92a921a5abc83326dd62c94c071173 /lib/vendor-info | |
| parent | 02b1cf005cf3e1df64183d20ba42930eb2767a9f (diff) | |
(최겸) 협력업체 기본정보 기능 개발 및 수정
Diffstat (limited to 'lib/vendor-info')
| -rw-r--r-- | lib/vendor-info/service.ts | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/lib/vendor-info/service.ts b/lib/vendor-info/service.ts new file mode 100644 index 00000000..6002179f --- /dev/null +++ b/lib/vendor-info/service.ts @@ -0,0 +1,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: "벤더 타입 정보를 불러오는데 실패했습니다.", + }; + } +} |
