summaryrefslogtreecommitdiff
path: root/lib/swp/vendor-actions.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/swp/vendor-actions.ts')
-rw-r--r--lib/swp/vendor-actions.ts67
1 files changed, 62 insertions, 5 deletions
diff --git a/lib/swp/vendor-actions.ts b/lib/swp/vendor-actions.ts
index f87c41a8..78521fed 100644
--- a/lib/swp/vendor-actions.ts
+++ b/lib/swp/vendor-actions.ts
@@ -18,7 +18,8 @@ import db from "@/db/db";
import { vendors } from "@/db/schema/vendors";
import { contracts } from "@/db/schema/contract";
import { projects } from "@/db/schema/projects";
-import { eq } from "drizzle-orm";
+import { stageDocuments } from "@/db/schema/vendorDocu";
+import { eq, and } from "drizzle-orm";
import {
getDocumentList,
getDocumentDetail,
@@ -347,6 +348,11 @@ export async function fetchVendorSwpStats(projNo?: string) {
// ============================================================================
// 벤더가 업로드한 파일 목록 조회 (Inbox)
+//
+// API 응답 파일 목록 + DB의 업로드 필요 문서 목록을 함께 반환
+// - DB 조회: stageDocuments에서 buyerSystemStatus='Completed'인 문서 중
+// 아직 업로드되지 않은 문서 (vendorDocNumber가 API 응답의 OWN_DOC_NO에 없는 것)
+// - 목적: 벤더에게 업로드를 위한 문서번호 기준(vendorDocNumber)을 제공
// ============================================================================
export async function fetchVendorUploadedFiles(projNo: string) {
@@ -362,7 +368,7 @@ export async function fetchVendorUploadedFiles(projNo: string) {
if (!projNo) {
debugWarn("프로젝트 번호 없음");
- return [];
+ return { files: [], requiredDocs: [] };
}
debugLog("업로드 파일 목록 조회 시작", {
@@ -370,15 +376,66 @@ export async function fetchVendorUploadedFiles(projNo: string) {
vendorCode: vendorInfo.vendorCode
});
- // api-client의 fetchGetExternalInboxList 사용
+ // 1. API에서 업로드된 파일 목록 조회
const { fetchGetExternalInboxList } = await import("./api-client");
const files = await fetchGetExternalInboxList({
projNo,
vndrCd: vendorInfo.vendorCode,
});
- debugSuccess("업로드 파일 목록 조회 성공", { count: files.length });
- return files;
+ debugLog("API 파일 목록 조회 완료", { count: files.length });
+
+ // 2. 프로젝트 ID 조회
+ const project = await db
+ .select({ id: projects.id })
+ .from(projects)
+ .where(eq(projects.code, projNo))
+ .limit(1);
+
+ if (!project[0]) {
+ debugWarn("프로젝트를 찾을 수 없음", { projNo });
+ return { files, requiredDocs: [] };
+ }
+
+ const projectId = project[0].id;
+
+ // 3. stageDocuments에서 buyerSystemStatus='Completed'인 문서 조회
+ const completedDocs = await db
+ .select({
+ vendorDocNumber: stageDocuments.vendorDocNumber,
+ title: stageDocuments.title,
+ buyerSystemComment: stageDocuments.buyerSystemComment,
+ })
+ .from(stageDocuments)
+ .where(
+ and(
+ eq(stageDocuments.projectId, projectId),
+ eq(stageDocuments.vendorId, vendorInfo.vendorId),
+ eq(stageDocuments.buyerSystemStatus, "Completed")
+ )
+ );
+
+ debugLog("stageDocuments 조회 완료", { count: completedDocs.length });
+
+ // 4. API 응답에 이미 존재하는 vendorDocNumber 필터링
+ const uploadedDocNumbers = new Set(
+ files.map((file) => file.OWN_DOC_NO).filter(Boolean)
+ );
+
+ const requiredDocs = completedDocs
+ .filter((doc) => doc.vendorDocNumber && !uploadedDocNumbers.has(doc.vendorDocNumber))
+ .map((doc) => ({
+ vendorDocNumber: doc.vendorDocNumber!,
+ title: doc.title,
+ buyerSystemComment: doc.buyerSystemComment || null,
+ }));
+
+ debugSuccess("업로드 파일 목록 조회 성공", {
+ filesCount: files.length,
+ requiredDocsCount: requiredDocs.length
+ });
+
+ return { files, requiredDocs };
} catch (error) {
debugError("업로드 파일 목록 조회 실패", error);
console.error("[fetchVendorUploadedFiles] 오류:", error);