port-nimara-client-portal/server/utils/nocodb.ts

144 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-05-29 07:32:13 +02:00
export interface PageInfo {
pageSize: number;
totalRows: number;
isFirstPage: boolean;
isLastPage: boolean;
page: number;
}
export interface InterestsResponse {
list: Interest[];
PageInfo: PageInfo;
}
export enum Table {
Interest = "mbs9hjauug4eseo",
}
export const getNocoDbConfiguration = () => useRuntimeConfig().nocodb;
export const createTableUrl = (table: Table) =>
`${getNocoDbConfiguration().url}/api/v2/tables/${table}/records`;
export const getInterests = async () =>
$fetch<InterestsResponse>(createTableUrl(Table.Interest), {
headers: {
"xc-token": getNocoDbConfiguration().token,
},
params: {
limit: 1000,
},
});
export const getInterestById = async (id: string) =>
$fetch<Interest>(`${createTableUrl(Table.Interest)}/${id}`, {
headers: {
"xc-token": getNocoDbConfiguration().token,
},
});
2025-06-03 17:57:08 +02:00
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> = {};
2025-06-03 21:04:22 +02:00
2025-06-03 17:57:08 +02:00
// Only include fields that are part of the InterestsRequest schema
const allowedFields = [
2025-06-03 21:04:22 +02:00
"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",
2025-06-03 17:57:08 +02:00
];
2025-06-03 21:04:22 +02:00
2025-06-03 17:57:08 +02:00
// Filter the data to only include allowed fields
for (const field of allowedFields) {
if (field in data) {
cleanData[field] = data[field];
}
}
2025-06-03 21:04:22 +02:00
2025-06-03 17:57:08 +02:00
return $fetch<Interest>(createTableUrl(Table.Interest), {
2025-06-03 21:04:22 +02:00
method: "PATCH",
2025-06-03 17:57:08 +02:00
headers: {
"xc-token": getNocoDbConfiguration().token,
},
body: {
2025-06-03 21:04:22 +02:00
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,
2025-06-03 17:57:08 +02:00
},
2025-06-03 21:04:22 +02:00
body: cleanData,
2025-06-03 17:57:08 +02:00
});
};
export const triggerWebhook = async (url: string, payload: any) =>
$fetch(url, {
2025-06-03 21:04:22 +02:00
method: "POST",
2025-06-03 17:57:08 +02:00
body: payload,
});