From d0632b283983b64482d93633d71def5695612cd2 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 17 Jun 2025 16:27:32 +0200 Subject: [PATCH] FEAT: Enhance getBerths and getBerthById functions to populate interested parties details --- server/utils/nocodb.ts | 60 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/server/utils/nocodb.ts b/server/utils/nocodb.ts index 2271e84..08bac17 100644 --- a/server/utils/nocodb.ts +++ b/server/utils/nocodb.ts @@ -442,8 +442,35 @@ export const getBerths = async () => { console.log('[nocodb.getBerths] Successfully fetched berths, count:', result.list?.length || 0); - // Sort berths by letter zone and then by number using Mooring Number + // Process each berth to populate interested parties details if (result.list && Array.isArray(result.list)) { + console.log('[nocodb.getBerths] Processing berths to populate interested parties...'); + + await Promise.all( + result.list.map(async (berth) => { + if (berth['Interested Parties'] && Array.isArray(berth['Interested Parties'])) { + const interestedPartiesDetails = await Promise.all( + berth['Interested Parties'].map(async (party: any) => { + if (party && (party.Id || party.id)) { + const interestId = party.Id || party.id; + try { + const interestDetails = await getInterestById(interestId.toString()); + return interestDetails; + } catch (error) { + console.error('[nocodb.getBerths] Failed to fetch interest details for ID:', interestId, error); + return party; // Return original party if fetch fails + } + } + return party; + }) + ); + + berth['Interested Parties'] = interestedPartiesDetails; + } + }) + ); + + // Sort berths by letter zone and then by number using Mooring Number result.list.sort((a, b) => { const berthA = a['Mooring Number'] || ''; const berthB = b['Mooring Number'] || ''; @@ -470,7 +497,7 @@ export const getBerths = async () => { return berthA.localeCompare(berthB); }); - console.log('[nocodb.getBerths] Berths sorted by zone and number'); + console.log('[nocodb.getBerths] Berths sorted by zone and number with populated interested parties'); } return result; @@ -485,18 +512,43 @@ export const getBerthById = async (id: string) => { console.log('[nocodb.getBerthById] Fetching berth ID:', id); try { - // Use basic query first to avoid 404 field expansion errors + // First fetch the basic berth data const result = await $fetch(`${createTableUrl(Table.Berth)}/${id}`, { headers: { "xc-token": getNocoDbConfiguration().token, }, params: { - // Start with basic fields only fields: '*' } }); console.log('[nocodb.getBerthById] Successfully fetched berth:', result.Id); + + // Now fetch and populate the interested parties details + if (result['Interested Parties'] && Array.isArray(result['Interested Parties'])) { + console.log('[nocodb.getBerthById] Fetching details for interested parties:', result['Interested Parties'].length); + + const interestedPartiesDetails = await Promise.all( + result['Interested Parties'].map(async (party: any) => { + if (party && (party.Id || party.id)) { + const interestId = party.Id || party.id; + try { + console.log('[nocodb.getBerthById] Fetching interest details for ID:', interestId); + const interestDetails = await getInterestById(interestId.toString()); + return interestDetails; + } catch (error) { + console.error('[nocodb.getBerthById] Failed to fetch interest details for ID:', interestId, error); + return party; // Return original party if fetch fails + } + } + return party; + }) + ); + + result['Interested Parties'] = interestedPartiesDetails; + console.log('[nocodb.getBerthById] Populated interested parties details'); + } + return result; } catch (error: any) { console.error('[nocodb.getBerthById] Error fetching berth:', error);