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

122 lines
4.0 KiB
TypeScript
Raw Normal View History

import { requireAuth } from '~/server/utils/auth';
export default defineEventHandler(async (event) => {
console.log('[test-specific-berth] Testing specific berth data...');
// Check authentication
await requireAuth(event);
try {
const query = getQuery(event);
const berthId = query.id as string;
if (!berthId) {
// Get first few berths to find one to test
const config = useRuntimeConfig().nocodb;
const berthsResponse = await $fetch(`${config.url}/api/v2/tables/mczgos9hr3oa9qc/records`, {
headers: {
"xc-token": config.token,
},
params: {
limit: 10,
fields: '*'
}
});
const berths = (berthsResponse as any).list || [];
return {
message: "No berth ID provided. Here are the first 10 berths:",
berths: berths.map((b: any) => ({
id: b.Id,
mooringNumber: b['Mooring Number'],
hasInterestedParties: !!b['Interested Parties'],
interestedPartiesCount: Array.isArray(b['Interested Parties']) ? b['Interested Parties'].length : 0,
interestedPartiesRaw: b['Interested Parties']
}))
};
}
const config = useRuntimeConfig().nocodb;
// Fetch the specific berth
const berth = await $fetch(`${config.url}/api/v2/tables/mczgos9hr3oa9qc/records/${berthId}`, {
headers: {
"xc-token": config.token,
},
params: {
fields: '*'
}
});
console.log('[test-specific-berth] Raw berth data:', JSON.stringify(berth, null, 2));
// Check all fields to find potential interested parties
const allFields = Object.keys(berth as any);
const potentialLinkFields = allFields.filter(field => {
const value = (berth as any)[field];
return value && (Array.isArray(value) || (typeof value === 'object' && !field.includes('Created') && !field.includes('Updated')));
});
// Try to fetch linked records using the links API
let linkedRecordsAttempts = [];
// Try different possible field IDs for interested parties
const possibleFieldIds = [
'cj7v7bb9pa5eyo3', // This is used for "Berths" in interest table
// Add other possible field IDs here if we discover them
];
for (const fieldId of possibleFieldIds) {
try {
const linkUrl = `${config.url}/api/v2/tables/mczgos9hr3oa9qc/links/${fieldId}/records/${berthId}`;
console.log('[test-specific-berth] Trying link URL:', linkUrl);
const linkedRecords = await $fetch(linkUrl, {
headers: {
"xc-token": config.token,
}
}).catch(err => ({
error: err.message || 'Failed',
fieldId: fieldId,
status: err.statusCode || err.status
}));
linkedRecordsAttempts.push({
fieldId,
result: linkedRecords
});
} catch (error) {
console.error('[test-specific-berth] Link fetch error:', error);
}
}
return {
berthId: (berth as any).Id,
mooringNumber: (berth as any)['Mooring Number'],
allFields: allFields,
potentialLinkFields: potentialLinkFields.map(field => ({
fieldName: field,
value: (berth as any)[field],
type: typeof (berth as any)[field],
isArray: Array.isArray((berth as any)[field]),
length: Array.isArray((berth as any)[field]) ? (berth as any)[field].length : undefined
})),
interestedPartiesField: {
exists: 'Interested Parties' in (berth as any),
value: (berth as any)['Interested Parties'],
type: typeof (berth as any)['Interested Parties'],
isArray: Array.isArray((berth as any)['Interested Parties']),
length: Array.isArray((berth as any)['Interested Parties']) ? (berth as any)['Interested Parties'].length : undefined
},
linkedRecordsAttempts: linkedRecordsAttempts,
rawBerth: berth
};
} catch (error: any) {
console.error('[test-specific-berth] Error:', error);
return {
error: error.message || 'Unknown error',
details: error
};
}
});