2025-06-15 16:18:29 +02:00
|
|
|
import { getNocoDbConfiguration } from "../utils/nocodb";
|
|
|
|
|
import { requireAuth } from "../utils/auth";
|
|
|
|
|
|
2025-06-03 17:57:08 +02:00
|
|
|
export default defineEventHandler(async (event) => {
|
2025-06-15 16:18:29 +02:00
|
|
|
console.log('[get-berths] Request received');
|
2025-06-03 17:57:08 +02:00
|
|
|
|
2025-06-15 16:18:29 +02:00
|
|
|
// Check authentication (x-tag header OR Keycloak session)
|
|
|
|
|
await requireAuth(event);
|
2025-06-03 17:57:08 +02:00
|
|
|
|
2025-06-09 23:33:20 +02:00
|
|
|
try {
|
|
|
|
|
const config = getNocoDbConfiguration();
|
|
|
|
|
const berthsTableId = "mczgos9hr3oa9qc";
|
|
|
|
|
|
|
|
|
|
console.log('[get-berths] Fetching berths...');
|
|
|
|
|
const berths = await $fetch<{ list: any[] }>(`${config.url}/api/v2/tables/${berthsTableId}/records`, {
|
|
|
|
|
headers: {
|
|
|
|
|
"xc-token": config.token,
|
|
|
|
|
},
|
|
|
|
|
params: {
|
|
|
|
|
limit: 1000,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log('[get-berths] Successfully fetched berths, count:', berths.list?.length || 0);
|
2025-06-12 15:53:12 +02:00
|
|
|
|
|
|
|
|
// Sort berths by letter zone and then by number
|
|
|
|
|
if (berths.list && Array.isArray(berths.list)) {
|
|
|
|
|
berths.list.sort((a, b) => {
|
|
|
|
|
const berthA = a['Berth Number'] || '';
|
|
|
|
|
const berthB = b['Berth Number'] || '';
|
|
|
|
|
|
|
|
|
|
// Extract letter and number parts
|
|
|
|
|
const matchA = berthA.match(/^([A-Za-z]+)(\d+)$/);
|
|
|
|
|
const matchB = berthB.match(/^([A-Za-z]+)(\d+)$/);
|
|
|
|
|
|
|
|
|
|
if (matchA && matchB) {
|
|
|
|
|
const [, letterA, numberA] = matchA;
|
|
|
|
|
const [, letterB, numberB] = matchB;
|
|
|
|
|
|
|
|
|
|
// First sort by letter zone
|
|
|
|
|
const letterCompare = letterA.localeCompare(letterB);
|
|
|
|
|
if (letterCompare !== 0) {
|
|
|
|
|
return letterCompare;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then sort by number within the same letter zone
|
|
|
|
|
return parseInt(numberA) - parseInt(numberB);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fallback to string comparison if pattern doesn't match
|
|
|
|
|
return berthA.localeCompare(berthB);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log('[get-berths] Berths sorted by zone and number');
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 23:33:20 +02:00
|
|
|
return berths;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('[get-berths] Error occurred:', error);
|
|
|
|
|
console.error('[get-berths] Error details:', error instanceof Error ? error.message : 'Unknown error');
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
2025-06-03 17:57:08 +02:00
|
|
|
});
|