feat: update

This commit is contained in:
Ron
2025-06-03 18:57:08 +03:00
parent b4313dd815
commit b3651e7f43
17 changed files with 2066 additions and 490 deletions

View File

@@ -0,0 +1,50 @@
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);
const { interestId } = body;
if (!interestId) {
throw createError({ statusCode: 400, statusMessage: "Interest ID is required" });
}
// Get the interest data
const interest = await getInterestById(interestId);
// Prepare the webhook payload
const webhookPayload = {
"type": "records.after.trigger",
"id": crypto.randomUUID(), // Generate a random UUID
"data": {
"table_id": "mbs9hjauug4eseo",
"table_name": "Interests",
"rows": [interest]
}
};
// Trigger the webhook
const webhookUrl = "https://automation.portnimara.com/api/v1/webhooks/cCRKsqPB9AHuj4XjFFiPr";
await triggerWebhook(webhookUrl, webhookPayload);
// Update the interest to mark that the EOI was sent
await updateInterest(interestId, {
"EOI Send to Sales": new Date().toISOString()
});
return { success: true, message: "EOI send to sales triggered successfully" };
} catch (error) {
if (error instanceof Error) {
throw createError({ statusCode: 500, statusMessage: error.message });
} else {
throw createError({
statusCode: 500,
statusMessage: "An unexpected error occurred",
});
}
}
});

21
server/api/get-berths.ts Normal file
View File

@@ -0,0 +1,21 @@
export default defineEventHandler(async (event) => {
const xTagHeader = getRequestHeader(event, "x-tag");
if (!xTagHeader || xTagHeader !== "094ut234") {
throw createError({ statusCode: 401, statusMessage: "unauthenticated" });
}
const config = getNocoDbConfiguration();
const berthsTableId = "mczgos9hr3oa9qc";
const berths = await $fetch(`${config.url}/api/v2/tables/${berthsTableId}/records`, {
headers: {
"xc-token": config.token,
},
params: {
limit: 1000,
},
});
return berths;
});

View File

@@ -0,0 +1,47 @@
export default defineEventHandler(async (event) => {
const xTagHeader = getRequestHeader(event, "x-tag");
if (!xTagHeader || xTagHeader !== "094ut234") {
throw createError({ statusCode: 401, statusMessage: "unauthenticated" });
}
const query = getQuery(event);
const { interestId, linkType } = query;
if (!interestId || !linkType) {
throw createError({
statusCode: 400,
statusMessage: "interestId and linkType are required"
});
}
const config = getNocoDbConfiguration();
const interestsTableId = "mbs9hjauug4eseo";
// Determine which link field to use
let linkFieldId;
if (linkType === 'berths') {
linkFieldId = "cj7v7bb9pa5eyo3"; // Berths field
} else if (linkType === 'recommendations') {
linkFieldId = "cgthyq2e95ajc52"; // Berth Recommendations field
} else {
throw createError({
statusCode: 400,
statusMessage: "linkType must be 'berths' or 'recommendations'"
});
}
const result = await $fetch(
`${config.url}/api/v2/tables/${interestsTableId}/links/${linkFieldId}/records/${interestId}`,
{
headers: {
"xc-token": config.token,
},
params: {
limit: 1000,
},
}
);
return result;
});

View File

@@ -0,0 +1,37 @@
export default defineEventHandler(async (event) => {
const xTagHeader = getRequestHeader(event, "x-tag");
if (!xTagHeader || xTagHeader !== "094ut234") {
throw createError({ statusCode: 401, statusMessage: "unauthenticated" });
}
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"
});
}
const config = getNocoDbConfiguration();
const interestsTableId = "mbs9hjauug4eseo";
const berthRecommendationsLinkFieldId = "cgthyq2e95ajc52"; // Berth Recommendations 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/${berthRecommendationsLinkFieldId}/records/${interestId}`,
{
method: 'POST',
headers: {
"xc-token": config.token,
},
body: berthRecords,
}
);
return result;
});

View File

@@ -0,0 +1,37 @@
export default defineEventHandler(async (event) => {
const xTagHeader = getRequestHeader(event, "x-tag");
if (!xTagHeader || xTagHeader !== "094ut234") {
throw createError({ statusCode: 401, statusMessage: "unauthenticated" });
}
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"
});
}
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: 'POST',
headers: {
"xc-token": config.token,
},
body: berthRecords,
}
);
return result;
});

View File

@@ -0,0 +1,50 @@
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);
const { interestId } = body;
if (!interestId) {
throw createError({ statusCode: 400, statusMessage: "Interest ID is required" });
}
// Get the interest data
const interest = await getInterestById(interestId);
// Prepare the webhook payload
const webhookPayload = {
"type": "records.after.trigger",
"id": crypto.randomUUID(), // Generate a random UUID
"data": {
"table_id": "mbs9hjauug4eseo",
"table_name": "Interests",
"rows": [interest]
}
};
// Trigger the webhook
const webhookUrl = "https://automation.portnimara.com/api/v1/webhooks/vEWQdpe4CXS24E86tV2Cb";
await triggerWebhook(webhookUrl, webhookPayload);
// Update the interest to mark that the request was sent
await updateInterest(interestId, {
"Request More Info - To Sales": new Date().toISOString()
});
return { success: true, message: "Request more info to sales triggered successfully" };
} 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

@@ -0,0 +1,50 @@
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);
const { interestId } = body;
if (!interestId) {
throw createError({ statusCode: 400, statusMessage: "Interest ID is required" });
}
// Get the interest data
const interest = await getInterestById(interestId);
// Prepare the webhook payload
const webhookPayload = {
"type": "records.after.trigger",
"id": crypto.randomUUID(), // Generate a random UUID
"data": {
"table_id": "mbs9hjauug4eseo",
"table_name": "Interests",
"rows": [interest]
}
};
// Trigger the webhook
const webhookUrl = "https://automation.portnimara.com/api/v1/webhooks/B6lnXZoospLXcVJJTABh0";
await triggerWebhook(webhookUrl, webhookPayload);
// Update the interest to mark that the request was sent
await updateInterest(interestId, {
"Request More Information": new Date().toISOString()
});
return { success: true, message: "Request more information triggered successfully" };
} 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

@@ -0,0 +1,37 @@
export default defineEventHandler(async (event) => {
const xTagHeader = getRequestHeader(event, "x-tag");
if (!xTagHeader || xTagHeader !== "094ut234") {
throw createError({ statusCode: 401, statusMessage: "unauthenticated" });
}
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"
});
}
const config = getNocoDbConfiguration();
const interestsTableId = "mbs9hjauug4eseo";
const berthRecommendationsLinkFieldId = "cgthyq2e95ajc52"; // Berth Recommendations 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/${berthRecommendationsLinkFieldId}/records/${interestId}`,
{
method: 'DELETE',
headers: {
"xc-token": config.token,
},
body: berthRecords,
}
);
return result;
});

View File

@@ -0,0 +1,37 @@
export default defineEventHandler(async (event) => {
const xTagHeader = getRequestHeader(event, "x-tag");
if (!xTagHeader || xTagHeader !== "094ut234") {
throw createError({ statusCode: 401, statusMessage: "unauthenticated" });
}
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"
});
}
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,
}
);
return result;
});

View File

@@ -0,0 +1,38 @@
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);
const { id, data } = body;
if (!id) {
throw createError({ statusCode: 400, statusMessage: "ID is required" });
}
if (!data || Object.keys(data).length === 0) {
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;
}
const updatedInterest = await updateInterest(id, updateData);
return updatedInterest;
} 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

@@ -36,3 +36,44 @@ export const getInterestById = async (id: string) =>
"xc-token": getNocoDbConfiguration().token,
},
});
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"
];
// 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',
headers: {
"xc-token": getNocoDbConfiguration().token,
},
body: {
Id: id, // This identifies the record to update
...cleanData // These are the fields to update
},
});
};
export const triggerWebhook = async (url: string, payload: any) =>
$fetch(url, {
method: 'POST',
body: payload,
});