148 lines
5.1 KiB
TypeScript
148 lines
5.1 KiB
TypeScript
|
|
import { requireAuth } from '~/server/utils/auth';
|
||
|
|
|
||
|
|
export default defineEventHandler(async (event) => {
|
||
|
|
console.log('[test-berth-interested-parties] Testing berth interested parties...');
|
||
|
|
|
||
|
|
// Check authentication
|
||
|
|
await requireAuth(event);
|
||
|
|
|
||
|
|
try {
|
||
|
|
const config = useRuntimeConfig().nocodb;
|
||
|
|
|
||
|
|
// First, let's get all berths and see which ones have interested parties
|
||
|
|
const berthsResponse = await $fetch(`${config.url}/api/v2/tables/mczgos9hr3oa9qc/records`, {
|
||
|
|
headers: {
|
||
|
|
"xc-token": config.token,
|
||
|
|
},
|
||
|
|
params: {
|
||
|
|
limit: 100,
|
||
|
|
fields: '*'
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
const berths = (berthsResponse as any).list || [];
|
||
|
|
console.log('[test-berth-interested-parties] Total berths:', berths.length);
|
||
|
|
|
||
|
|
// Find berths with interested parties
|
||
|
|
const berthsWithParties = berths.filter((b: any) =>
|
||
|
|
b['Interested Parties'] && Array.isArray(b['Interested Parties']) && b['Interested Parties'].length > 0
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log('[test-berth-interested-parties] Berths with interested parties:', berthsWithParties.length);
|
||
|
|
|
||
|
|
if (berthsWithParties.length === 0) {
|
||
|
|
return {
|
||
|
|
message: "No berths found with interested parties",
|
||
|
|
totalBerths: berths.length,
|
||
|
|
berthsWithParties: 0
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
// Take the first berth with interested parties for testing
|
||
|
|
const testBerth = berthsWithParties[0];
|
||
|
|
console.log('[test-berth-interested-parties] Test berth:', {
|
||
|
|
id: testBerth.Id,
|
||
|
|
mooringNumber: testBerth['Mooring Number'],
|
||
|
|
interestedPartiesCount: testBerth['Interested Parties'].length
|
||
|
|
});
|
||
|
|
|
||
|
|
// Log the raw interested parties data
|
||
|
|
console.log('[test-berth-interested-parties] Raw Interested Parties data:',
|
||
|
|
JSON.stringify(testBerth['Interested Parties'], null, 2)
|
||
|
|
);
|
||
|
|
|
||
|
|
// Analyze the structure
|
||
|
|
const firstParty = testBerth['Interested Parties'][0];
|
||
|
|
const partyAnalysis = {
|
||
|
|
type: typeof firstParty,
|
||
|
|
isArray: Array.isArray(firstParty),
|
||
|
|
isObject: firstParty && typeof firstParty === 'object',
|
||
|
|
value: firstParty,
|
||
|
|
keys: firstParty && typeof firstParty === 'object' && !Array.isArray(firstParty)
|
||
|
|
? Object.keys(firstParty)
|
||
|
|
: []
|
||
|
|
};
|
||
|
|
|
||
|
|
console.log('[test-berth-interested-parties] First party analysis:', partyAnalysis);
|
||
|
|
|
||
|
|
// Try to extract IDs
|
||
|
|
const extractedIds = testBerth['Interested Parties'].map((party: any) => {
|
||
|
|
if (typeof party === 'number') return { id: party, type: 'number' };
|
||
|
|
if (typeof party === 'string') return { id: party, type: 'string' };
|
||
|
|
if (party && typeof party === 'object') {
|
||
|
|
return {
|
||
|
|
id: party.Id || party.id || party.ID || party._id || null,
|
||
|
|
type: 'object',
|
||
|
|
keys: Object.keys(party)
|
||
|
|
};
|
||
|
|
}
|
||
|
|
return { id: null, type: 'unknown' };
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('[test-berth-interested-parties] Extracted IDs:', extractedIds);
|
||
|
|
|
||
|
|
// Try to fetch one interest if we have an ID
|
||
|
|
let fetchedInterest = null;
|
||
|
|
const validId = extractedIds.find((item: any) => item.id !== null);
|
||
|
|
|
||
|
|
if (validId && validId.id) {
|
||
|
|
try {
|
||
|
|
const interestId = typeof validId.id === 'string' ? validId.id : validId.id.toString();
|
||
|
|
fetchedInterest = await $fetch(`${config.url}/api/v2/tables/mbs9hjauug4eseo/records/${interestId}`, {
|
||
|
|
headers: {
|
||
|
|
"xc-token": config.token,
|
||
|
|
}
|
||
|
|
});
|
||
|
|
console.log('[test-berth-interested-parties] Successfully fetched interest:', interestId);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('[test-berth-interested-parties] Failed to fetch interest:', error);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
testBerth: {
|
||
|
|
id: testBerth.Id,
|
||
|
|
mooringNumber: testBerth['Mooring Number'],
|
||
|
|
interestedPartiesCount: testBerth['Interested Parties'].length
|
||
|
|
},
|
||
|
|
rawInterestedParties: testBerth['Interested Parties'],
|
||
|
|
firstPartyAnalysis: partyAnalysis,
|
||
|
|
extractedIds: extractedIds,
|
||
|
|
fetchedInterest: fetchedInterest,
|
||
|
|
recommendation: determineRecommendation(testBerth['Interested Parties'])
|
||
|
|
};
|
||
|
|
} catch (error: any) {
|
||
|
|
console.error('[test-berth-interested-parties] Error:', error);
|
||
|
|
return {
|
||
|
|
error: error.message || 'Unknown error',
|
||
|
|
details: error
|
||
|
|
};
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
function determineRecommendation(interestedParties: any[]): string {
|
||
|
|
if (!interestedParties || interestedParties.length === 0) {
|
||
|
|
return "No interested parties found";
|
||
|
|
}
|
||
|
|
|
||
|
|
const firstParty = interestedParties[0];
|
||
|
|
|
||
|
|
if (typeof firstParty === 'number') {
|
||
|
|
return "Interested parties are stored as number IDs. Population logic should handle this format.";
|
||
|
|
}
|
||
|
|
|
||
|
|
if (typeof firstParty === 'string') {
|
||
|
|
return "Interested parties are stored as string IDs. Population logic should parse these.";
|
||
|
|
}
|
||
|
|
|
||
|
|
if (firstParty && typeof firstParty === 'object') {
|
||
|
|
const keys = Object.keys(firstParty);
|
||
|
|
if (keys.includes('Id') || keys.includes('id')) {
|
||
|
|
return "Interested parties are objects with ID fields. Population logic looks correct.";
|
||
|
|
}
|
||
|
|
return `Interested parties are objects but with unexpected keys: ${keys.join(', ')}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
return "Unknown format for interested parties data";
|
||
|
|
}
|