port-nimara-client-portal/server/api/update-interest.ts

59 lines
2.0 KiB
TypeScript
Raw Normal View History

import { updateInterest } 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('[update-interest] 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
try {
const body = await readBody(event);
const { id, data } = body;
2025-06-09 23:42:31 +02:00
console.log('[update-interest] Request body:', {
id,
idType: typeof id,
dataKeys: data ? Object.keys(data) : 'none'
});
2025-06-03 17:57:08 +02:00
if (!id) {
console.error('[update-interest] Missing ID in request');
2025-06-03 17:57:08 +02:00
throw createError({ statusCode: 400, statusMessage: "ID is required" });
}
2025-06-09 23:42:31 +02:00
// Ensure ID is a string
const interestId = String(id);
console.log('[update-interest] Using ID:', interestId, 'Type:', typeof interestId);
2025-06-03 17:57:08 +02:00
if (!data || Object.keys(data).length === 0) {
console.error('[update-interest] No data provided for update');
2025-06-03 17:57:08 +02:00
throw createError({ statusCode: 400, statusMessage: "No data provided for update" });
}
// Remove Id from data if it exists to avoid duplication
const updateData = { ...data };
if ('Id' in updateData) {
delete updateData.Id;
console.log('[update-interest] Removed Id from update data');
2025-06-03 17:57:08 +02:00
}
2025-06-09 23:42:31 +02:00
console.log('[update-interest] Updating interest:', interestId, 'with fields:', Object.keys(updateData));
const updatedInterest = await updateInterest(interestId, updateData);
console.log('[update-interest] Successfully updated interest:', interestId);
2025-06-03 17:57:08 +02:00
return updatedInterest;
} catch (error) {
console.error('[update-interest] Error occurred:', error);
console.error('[update-interest] Error stack:', error instanceof Error ? error.stack : 'No stack trace');
2025-06-03 17:57:08 +02:00
if (error instanceof Error) {
throw createError({ statusCode: 500, statusMessage: error.message });
} else {
throw createError({
statusCode: 500,
statusMessage: "An unexpected error occurred",
});
}
}
});