From 15c3ae6536c264db0508e4fc4aaa59c3e6d1af30 Mon Sep 17 00:00:00 2001 From: dujinkim Date: Tue, 15 Jul 2025 00:50:39 +0000 Subject: (대표님) 기본계약 및 정기평가 작업사항, OCR 변경사항 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/forms/services.ts | 67 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 4 deletions(-) (limited to 'lib/forms') diff --git a/lib/forms/services.ts b/lib/forms/services.ts index 7c1219d2..02333095 100644 --- a/lib/forms/services.ts +++ b/lib/forms/services.ts @@ -1072,6 +1072,7 @@ async function transformDataToSEDPFormat( formCode: string, objectCode: string, projectNo: string, + contractItemId: number, // Add contractItemId parameter designerNo: string = "253213" ): Promise { // Create a map for quick column lookup @@ -1092,10 +1093,67 @@ async function transformDataToSEDPFormat( // Cache for UOM factors to avoid duplicate API calls const uomFactorCache = new Map(); + // Cache for packageCode to avoid duplicate DB queries for same tag + const packageCodeCache = new Map(); + // Transform each row const transformedItems = []; for (const row of tableData) { + // Get packageCode for this specific tag + let packageCode = formCode; // fallback to formCode + + if (row.TAG_NO && contractItemId) { + // Check cache first + const cacheKey = `${contractItemId}-${row.TAG_NO}`; + + if (packageCodeCache.has(cacheKey)) { + packageCode = packageCodeCache.get(cacheKey)!; + } else { + try { + // Query to get packageCode for this specific tag + const tagResult = await db.query.tags.findFirst({ + where: and( + eq(tags.contractItemId, contractItemId), + eq(tags.tagNo, row.TAG_NO) + ) + }); + + if (tagResult) { + // Get the contract item + const contractItemResult = await db.query.contractItems.findFirst({ + where: eq(contractItems.id, tagResult.contractItemId) + }); + + if (contractItemResult) { + // Get the first item with this itemId + const itemResult = await db.query.items.findFirst({ + where: eq(items.id, contractItemResult.itemId) + }); + + if (itemResult && itemResult.packageCode) { + packageCode = itemResult.packageCode; + console.log(`Found packageCode for tag ${row.TAG_NO}: ${packageCode}`); + } else { + console.warn(`No item found for contractItem.itemId: ${contractItemResult.itemId}, using fallback`); + } + } else { + console.warn(`No contractItem found for tag ${row.TAG_NO}, using fallback`); + } + } else { + console.warn(`No tag found for contractItemId: ${contractItemId}, tagNo: ${row.TAG_NO}, using fallback`); + } + + // Cache the result (even if it's the fallback value) + packageCodeCache.set(cacheKey, packageCode); + } catch (error) { + console.error(`Error fetching packageCode for tag ${row.TAG_NO}:`, error); + // Use fallback value and cache it + packageCodeCache.set(cacheKey, packageCode); + } + } + } + // Create base SEDP item with required fields const sedpItem: SEDPDataItem = { TAG_NO: row.TAG_NO || "", @@ -1110,7 +1168,7 @@ async function transformDataToSEDPFormat( LAST_REV_YN: true, CRTER_NO: designerNo, CHGER_NO: designerNo, - TYPE: formCode, + TYPE: packageCode, // Use packageCode instead of formCode PROJ_NO: projectNo, REV_NO: "00", CRTE_DTM: currentTimestamp, @@ -1202,19 +1260,19 @@ export async function transformFormDataToSEDP( formCode: string, objectCode: string, projectNo: string, + contractItemId: number, // Add contractItemId parameter designerNo: string = "253213" ): Promise { - // Use the utility function within the async Server Action return transformDataToSEDPFormat( tableData, columnsJSON, formCode, objectCode, projectNo, + contractItemId, // Pass contractItemId designerNo ); } - /** * Get project code by project ID */ @@ -1330,7 +1388,8 @@ export async function sendFormDataToSEDP( columns, formCode, objectCode, - projectCode + projectCode, + contractItemId // Add contractItemId parameter ); // 4. Send to SEDP API -- cgit v1.2.3