"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); 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: "벤더 타입 정보를 불러오는데 실패했습니다.", }; } }