port-nimara-client-portal/server/api/unlink-berths-from-interest.ts

45 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

import { withBerthQueue } from '~/server/utils/operation-lock';
import { getNocoDbConfiguration } from '~/server/utils/nocodb';
import { requireAuth } from '~/server/utils/auth';
2025-06-03 17:57:08 +02:00
export default defineEventHandler(async (event) => {
console.log('[unlink-berths] Request received');
2025-06-03 17:57:08 +02:00
// Check authentication (x-tag header OR Keycloak session)
await requireAuth(event);
2025-06-03 17:57:08 +02:00
const body = await readBody(event);
const { interestId, berthIds } = body;
if (!interestId || !berthIds || !Array.isArray(berthIds)) {
throw createError({
statusCode: 400,
statusMessage: "interestId and berthIds array are required"
});
}
// Use queuing system to handle rapid berth operations gracefully
return await withBerthQueue(interestId, async () => {
const config = getNocoDbConfiguration();
const interestsTableId = "mbs9hjauug4eseo";
const berthsLinkFieldId = "cj7v7bb9pa5eyo3"; // Berths field
// Format the berth IDs for the API
const berthRecords = berthIds.map(id => ({ Id: id }));
const result = await $fetch(
`${config.url}/api/v2/tables/${interestsTableId}/links/${berthsLinkFieldId}/records/${interestId}`,
{
method: 'DELETE',
headers: {
"xc-token": config.token,
},
body: berthRecords,
}
);
console.log('[unlink-berths] Successfully unlinked berths from interest:', interestId);
return result;
});
2025-06-03 17:57:08 +02:00
});