feat: add interest button

This commit is contained in:
Ron
2025-06-03 22:04:22 +03:00
parent 0437ba6462
commit bc0fa6fbe0
10 changed files with 653 additions and 40 deletions

View File

@@ -0,0 +1,29 @@
import { createInterest } from "../utils/nocodb";
export default defineEventHandler(async (event) => {
const xTagHeader = getRequestHeader(event, "x-tag");
if (!xTagHeader || xTagHeader !== "094ut234") {
throw createError({ statusCode: 401, statusMessage: "unauthenticated" });
}
try {
const body = await readBody(event);
if (!body || Object.keys(body).length === 0) {
throw createError({ statusCode: 400, statusMessage: "No data provided" });
}
const createdInterest = await createInterest(body);
return createdInterest;
} catch (error) {
if (error instanceof Error) {
throw createError({ statusCode: 500, statusMessage: error.message });
} else {
throw createError({
statusCode: 500,
statusMessage: "An unexpected error occurred",
});
}
}
});

View File

@@ -41,39 +41,103 @@ export const updateInterest = async (id: string, data: Partial<Interest>) => {
// Create a clean data object that matches the InterestsRequest schema
// Remove any properties that are not in the schema or shouldn't be sent
const cleanData: Record<string, any> = {};
// Only include fields that are part of the InterestsRequest schema
const allowedFields = [
"Full Name", "Yacht Name", "Length", "Address", "Email Address",
"Sales Process Level", "Phone Number", "Extra Comments", "Berth Size Desired",
"LOI-NDA Document", "Date Added", "Width", "Depth", "Created At",
"Request More Information", "Source", "Place of Residence",
"Contact Method Preferred", "Request Form Sent", "Berth Number",
"EOI Time Sent", "Lead Category", "Request More Info - To Sales",
"EOI Send to Sales", "Time LOI Sent"
"Full Name",
"Yacht Name",
"Length",
"Address",
"Email Address",
"Sales Process Level",
"Phone Number",
"Extra Comments",
"Berth Size Desired",
"LOI-NDA Document",
"Date Added",
"Width",
"Depth",
"Created At",
"Request More Information",
"Source",
"Place of Residence",
"Contact Method Preferred",
"Request Form Sent",
"Berth Number",
"EOI Time Sent",
"Lead Category",
"Request More Info - To Sales",
"EOI Send to Sales",
"Time LOI Sent",
];
// Filter the data to only include allowed fields
for (const field of allowedFields) {
if (field in data) {
cleanData[field] = data[field];
}
}
return $fetch<Interest>(createTableUrl(Table.Interest), {
method: 'PATCH',
method: "PATCH",
headers: {
"xc-token": getNocoDbConfiguration().token,
},
body: {
Id: id, // This identifies the record to update
...cleanData // These are the fields to update
Id: id, // This identifies the record to update
...cleanData, // These are the fields to update
},
});
};
export const createInterest = async (data: Partial<Interest>) => {
// Create a clean data object that matches the InterestsRequest schema
const cleanData: Record<string, any> = {};
// Only include fields that are part of the InterestsRequest schema
const allowedFields = [
"Full Name",
"Yacht Name",
"Length",
"Address",
"Email Address",
"Sales Process Level",
"Phone Number",
"Extra Comments",
"Berth Size Desired",
"Date Added",
"Width",
"Depth",
"Source",
"Place of Residence",
"Contact Method Preferred",
"Lead Category",
];
// Filter the data to only include allowed fields
for (const field of allowedFields) {
if (field in data) {
cleanData[field] = data[field];
}
}
// Remove any computed or relation fields that shouldn't be sent
delete cleanData.Id;
delete cleanData.Berths;
delete cleanData["Berth Recommendations"];
delete cleanData.Berth;
return $fetch<Interest>(createTableUrl(Table.Interest), {
method: "POST",
headers: {
"xc-token": getNocoDbConfiguration().token,
},
body: cleanData,
});
};
export const triggerWebhook = async (url: string, payload: any) =>
$fetch(url, {
method: 'POST',
method: "POST",
body: payload,
});