port-nimara-client-portal/server/api/test-berth-with-parties.ts

96 lines
3.3 KiB
TypeScript

import { getBerthById, getInterestById } from '~/server/utils/nocodb';
import { requireAuth } from '~/server/utils/auth';
export default defineEventHandler(async (event) => {
console.log('[test-berth-with-parties] Testing berth with interested parties...');
// Check authentication
await requireAuth(event);
try {
// Get a berth ID from query params or use a default
const query = getQuery(event);
const berthId = query.id as string || '1'; // Default to berth ID 1 (A1)
console.log('[test-berth-with-parties] Fetching berth:', berthId);
const berth = await getBerthById(berthId);
// Extract interested parties info
const interestedParties = berth['Interested Parties'];
let partiesInfo = null;
let directFetchComparison = null;
if (interestedParties) {
if (Array.isArray(interestedParties)) {
partiesInfo = {
type: 'array',
count: interestedParties.length,
parties: interestedParties.map(party => ({
id: party.Id,
name: party['Full Name'],
email: party['Email Address'],
salesLevel: party['Sales Process Level'],
eoiStatus: party['EOI Status'],
contractStatus: party['Contract Status'],
allFields: Object.keys(party).sort()
}))
};
// Compare with direct fetch of first party
if (interestedParties.length > 0) {
try {
const firstPartyId = interestedParties[0].Id;
const directFetch = await getInterestById(firstPartyId.toString());
directFetchComparison = {
fromLinksAPI: {
id: interestedParties[0].Id,
name: interestedParties[0]['Full Name'],
salesLevel: interestedParties[0]['Sales Process Level'],
eoiStatus: interestedParties[0]['EOI Status'],
contractStatus: interestedParties[0]['Contract Status'],
fieldCount: Object.keys(interestedParties[0]).length
},
fromDirectFetch: {
id: directFetch.Id,
name: directFetch['Full Name'],
salesLevel: directFetch['Sales Process Level'],
eoiStatus: directFetch['EOI Status'],
contractStatus: directFetch['Contract Status'],
fieldCount: Object.keys(directFetch).length,
allFields: Object.keys(directFetch).sort()
}
};
} catch (err) {
directFetchComparison = { error: 'Failed to fetch directly' };
}
}
} else if (typeof interestedParties === 'number') {
partiesInfo = {
type: 'number',
count: interestedParties,
note: 'This is just a count - should have been replaced with actual data'
};
} else {
partiesInfo = {
type: typeof interestedParties,
value: interestedParties
};
}
}
return {
berthId: berth.Id,
mooringNumber: berth['Mooring Number'],
interestedPartiesField: partiesInfo,
directFetchComparison,
success: Array.isArray(interestedParties) && interestedParties.length > 0
};
} catch (error: any) {
console.error('[test-berth-with-parties] Error:', error);
return {
error: error.message || 'Unknown error',
details: error
};
}
});