diff options
Diffstat (limited to 'lib/forms/services.ts')
| -rw-r--r-- | lib/forms/services.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/forms/services.ts b/lib/forms/services.ts index 02333095..b517ff18 100644 --- a/lib/forms/services.ts +++ b/lib/forms/services.ts @@ -1049,6 +1049,7 @@ interface SEDPAttribute { interface SEDPDataItem { TAG_NO: string; TAG_DESC: string; + CLS_ID:string; ATTRIBUTES: SEDPAttribute[]; SCOPE: string; TOOLID: string; @@ -1096,12 +1097,16 @@ async function transformDataToSEDPFormat( // Cache for packageCode to avoid duplicate DB queries for same tag const packageCodeCache = new Map<string, string>(); + // Cache for tagClass code to avoid duplicate DB queries for same tag + const tagClassCodeCache = 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 + let tagClassCode = ""; // for CLS_ID if (row.TAG_NO && contractItemId) { // Check cache first @@ -1120,6 +1125,28 @@ async function transformDataToSEDPFormat( }); if (tagResult) { + // Get tagClass code if tagClassId exists + if (tagResult.tagClassId) { + // Check tagClass cache first + if (tagClassCodeCache.has(cacheKey)) { + tagClassCode = tagClassCodeCache.get(cacheKey)!; + } else { + const tagClassResult = await db.query.tagClasses.findFirst({ + where: eq(tagClasses.id, tagResult.tagClassId) + }); + + if (tagClassResult) { + tagClassCode = tagClassResult.code; + console.log(`Found tagClass code for tag ${row.TAG_NO}: ${tagClassCode}`); + } else { + console.warn(`No tagClass found for tagClassId: ${tagResult.tagClassId}`); + } + + // Cache the tagClass code result + tagClassCodeCache.set(cacheKey, tagClassCode); + } + } + // Get the contract item const contractItemResult = await db.query.contractItems.findFirst({ where: eq(contractItems.id, tagResult.contractItemId) @@ -1152,6 +1179,38 @@ async function transformDataToSEDPFormat( packageCodeCache.set(cacheKey, packageCode); } } + + // Get tagClass code if not already retrieved above + if (!tagClassCode && tagClassCodeCache.has(cacheKey)) { + tagClassCode = tagClassCodeCache.get(cacheKey)!; + } else if (!tagClassCode) { + try { + const tagResult = await db.query.tags.findFirst({ + where: and( + eq(tags.contractItemId, contractItemId), + eq(tags.tagNo, row.TAG_NO) + ) + }); + + if (tagResult && tagResult.tagClassId) { + const tagClassResult = await db.query.tagClasses.findFirst({ + where: eq(tagClasses.id, tagResult.tagClassId) + }); + + if (tagClassResult) { + tagClassCode = tagClassResult.code; + console.log(`Found tagClass code for tag ${row.TAG_NO}: ${tagClassCode}`); + } + } + + // Cache the tagClass code result + tagClassCodeCache.set(cacheKey, tagClassCode); + } catch (error) { + console.error(`Error fetching tagClass code for tag ${row.TAG_NO}:`, error); + // Cache empty string as fallback + tagClassCodeCache.set(cacheKey, ""); + } + } } // Create base SEDP item with required fields @@ -1169,6 +1228,7 @@ async function transformDataToSEDPFormat( CRTER_NO: designerNo, CHGER_NO: designerNo, TYPE: packageCode, // Use packageCode instead of formCode + CLS_ID: tagClassCode, // Add CLS_ID with tagClass code PROJ_NO: projectNo, REV_NO: "00", CRTE_DTM: currentTimestamp, |
