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

40 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-06-03 21:04:22 +02:00
import { createInterest } from "../utils/nocodb";
export default defineEventHandler(async (event) => {
const xTagHeader = getRequestHeader(event, "x-tag");
console.log('[create-interest] Request received with x-tag:', xTagHeader);
2025-06-03 21:04:22 +02:00
if (!xTagHeader || (xTagHeader !== "094ut234" && xTagHeader !== "pjnvü1230")) {
console.error('[create-interest] Authentication failed - invalid x-tag:', xTagHeader);
2025-06-03 21:04:22 +02:00
throw createError({ statusCode: 401, statusMessage: "unauthenticated" });
}
try {
const body = await readBody(event);
console.log('[create-interest] Request body fields:', body ? Object.keys(body) : 'none');
2025-06-03 21:04:22 +02:00
if (!body || Object.keys(body).length === 0) {
console.error('[create-interest] No data provided');
2025-06-03 21:04:22 +02:00
throw createError({ statusCode: 400, statusMessage: "No data provided" });
}
console.log('[create-interest] Creating new interest with fields:', Object.keys(body));
2025-06-03 21:04:22 +02:00
const createdInterest = await createInterest(body);
console.log('[create-interest] Successfully created interest with ID:', createdInterest.Id);
2025-06-03 21:04:22 +02:00
return createdInterest;
} catch (error) {
console.error('[create-interest] Error occurred:', error);
console.error('[create-interest] Error stack:', error instanceof Error ? error.stack : 'No stack trace');
2025-06-03 21:04:22 +02:00
if (error instanceof Error) {
throw createError({ statusCode: 500, statusMessage: error.message });
} else {
throw createError({
statusCode: 500,
statusMessage: "An unexpected error occurred",
});
}
}
});