diff options
Diffstat (limited to 'lib/forms/services.ts')
| -rw-r--r-- | lib/forms/services.ts | 67 |
1 files changed, 63 insertions, 4 deletions
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<SEDPDataItem[]> { // 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<string, number>(); + // Cache for packageCode to avoid duplicate DB queries for same tag + const packageCodeCache = new Map<string, string>(); + // 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<SEDPDataItem[]> { - // 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 |
