summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-08-21 09:10:06 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-08-21 09:10:06 +0000
commit94082bfe915d3b0337f8929a2bb27828abb5d3c7 (patch)
tree0a100365ac92a921a5abc83326dd62c94c071173 /lib
parent02b1cf005cf3e1df64183d20ba42930eb2767a9f (diff)
(최겸) 협력업체 기본정보 기능 개발 및 수정
Diffstat (limited to 'lib')
-rw-r--r--lib/vendor-info/service.ts139
-rw-r--r--lib/vendors/service.ts16
2 files changed, 155 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: "벤더 타입 정보를 불러오는데 실패했습니다.",
+ };
+ }
+}
diff --git a/lib/vendors/service.ts b/lib/vendors/service.ts
index 9cb653ea..4cca3b12 100644
--- a/lib/vendors/service.ts
+++ b/lib/vendors/service.ts
@@ -2537,6 +2537,8 @@ export async function getVendorBasicInfo(vendorId: number) {
vendorCode: vendor.vendorCode,
taxId: vendor.taxId,
address: vendor.address,
+ addressDetail: vendor.addressDetail || "",
+ postalCode: vendor.postalCode || "",
businessSize: vendor.businessSize || "", // vendorsWithTypesView에 businessSize 필드가 없을 경우 대비
country: vendor.country,
phone: vendor.phone,
@@ -2606,6 +2608,20 @@ export async function getVendorBasicInfo(vendorId: number) {
capacityInfo: null,
+ // 누락된 필수 필드들 추가
+ processInfo: {
+ processCount: 0,
+ processPIC: "",
+ processApprovalDate: "",
+ implementationApproval: ""
+ },
+
+ contractInfo: {
+ contractRegistrationNumber: "",
+ contractPeriod: "",
+ lastEquipmentInspection: ""
+ },
+
calculatedMetrics: null, // 구현 시 { "20231231": { debtRatio: 0, ... }, "20221231": { ... } } 형태로 YYYYMMDD 키 사용
};
});