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

383 lines
13 KiB
TypeScript
Raw Normal View History

import { resilientHttp } from './resilient-http';
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",
}
2025-06-09 23:42:31 +02:00
export const getNocoDbConfiguration = () => {
const config = useRuntimeConfig().nocodb;
console.log('[nocodb] Configuration URL:', config.url);
return config;
};
2025-05-29 07:32:13 +02:00
2025-06-09 23:42:31 +02:00
export const createTableUrl = (table: Table) => {
const url = `${getNocoDbConfiguration().url}/api/v2/tables/${table}/records`;
console.log('[nocodb] Table URL:', url);
return url;
};
2025-05-29 07:32:13 +02:00
// Helper function for resilient NocoDB requests
async function nocoDbFetch<T>(url: string, options: RequestInit = {}): Promise<T> {
const response = await resilientHttp.fetchWithRetry(
url,
{
...options,
headers: {
'xc-token': getNocoDbConfiguration().token,
'Content-Type': 'application/json',
...options.headers
},
serviceName: 'nocodb'
2025-05-29 07:32:13 +02:00
},
{
maxRetries: options.method === 'POST' || options.method === 'PATCH' || options.method === 'DELETE' ? 2 : 3,
timeout: 15000
}
);
if (!response.ok) {
const errorText = await response.text();
throw new Error(`NocoDB request failed: ${response.status} - ${errorText}`);
}
return response.json();
}
export const getInterests = async () => {
const url = createTableUrl(Table.Interest);
return nocoDbFetch<InterestsResponse>(url + '?limit=1000');
};
2025-05-29 07:32:13 +02:00
2025-06-11 16:05:19 +02:00
export const getInterestById = async (id: string) => {
console.log('[nocodb.getInterestById] Fetching interest ID:', id);
const url = `${createTableUrl(Table.Interest)}/${id}`;
const result = await nocoDbFetch<any>(url);
2025-06-11 16:05:19 +02:00
console.log('[nocodb.getInterestById] Raw result from NocoDB:', {
id: result.Id,
documensoID: result['documensoID'],
documensoID_type: typeof result['documensoID'],
documensoID_value: JSON.stringify(result['documensoID']),
signatureLinks: {
client: result['Signature Link Client'],
cc: result['Signature Link CC'],
developer: result['Signature Link Developer']
}
});
return result;
};
2025-06-03 17:57:08 +02:00
2025-06-09 23:38:35 +02:00
export const updateInterest = async (id: string, data: Partial<Interest>, retryCount = 0): Promise<Interest> => {
console.log('[nocodb.updateInterest] Updating interest:', id, 'Retry:', retryCount);
console.log('[nocodb.updateInterest] Data fields:', Object.keys(data));
2025-06-09 23:42:31 +02:00
// First, try to verify the record exists
if (retryCount === 0) {
try {
console.log('[nocodb.updateInterest] Verifying record exists...');
const existingRecord = await getInterestById(id);
console.log('[nocodb.updateInterest] Record exists with ID:', existingRecord.Id);
} catch (verifyError: any) {
console.error('[nocodb.updateInterest] Failed to verify record:', verifyError);
if (verifyError.statusCode === 404 || verifyError.status === 404) {
console.error('[nocodb.updateInterest] Record verification failed - record not found');
}
}
}
2025-06-03 17:57:08 +02:00
// 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
2025-06-09 23:48:00 +02:00
// Removed webhook fields: "Request More Information", "Request More Info - To Sales", "EOI Send to Sales"
2025-06-03 17:57:08 +02:00
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",
"Source",
"Contact Method Preferred",
"Request Form Sent",
"Berth Number",
"EOI Time Sent",
"Lead Category",
"Time LOI Sent",
2025-06-03 23:48:44 +02:00
"EOI Status",
"Berth Info Sent Status",
"Contract Sent Status",
"Deposit 10% Status",
"Contract Status",
// Add the EOI link fields
"EOI Client Link",
"EOI David Link",
"EOI Oscar Link",
"EOI Document",
// Add the new signature link fields
"Signature Link Client",
"Signature Link CC",
2025-06-11 14:28:03 +02:00
"Signature Link Developer",
2025-06-11 18:59:16 +02:00
// Add the embedded signature link fields
"EmbeddedSignatureLinkClient",
"EmbeddedSignatureLinkCC",
"EmbeddedSignatureLinkDeveloper",
2025-06-11 14:28:03 +02:00
// Add the Documenso document ID field
"documensoID"
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) {
2025-06-09 23:48:00 +02:00
const value = (data as any)[field];
// Skip webhook-type fields and other object fields that shouldn't be sent
if (value && typeof value === 'object' && !Array.isArray(value)) {
console.log(`[nocodb.updateInterest] Skipping object field: ${field}`, value);
continue;
}
// Handle clearing fields - NocoDB requires null for clearing, not undefined
if (value === undefined) {
cleanData[field] = null;
console.log(`[nocodb.updateInterest] Converting undefined to null for field: ${field}`);
} else {
cleanData[field] = value;
}
2025-06-03 17:57:08 +02:00
}
}
2025-06-03 21:04:22 +02:00
console.log('[nocodb.updateInterest] Clean data fields:', Object.keys(cleanData));
2025-06-10 00:15:36 +02:00
// PATCH requires ID in the body (not in URL)
// Ensure ID is an integer
cleanData.Id = parseInt(id);
const url = createTableUrl(Table.Interest);
console.log('[nocodb.updateInterest] URL:', url);
try {
2025-06-09 23:42:31 +02:00
console.log('[nocodb.updateInterest] Sending PATCH request with headers:', {
"xc-token": getNocoDbConfiguration().token ? "***" + getNocoDbConfiguration().token.slice(-4) : "not set"
});
console.log('[nocodb.updateInterest] Request body:', JSON.stringify(cleanData, null, 2));
2025-06-10 00:15:36 +02:00
// Try sending as a single object first (as shown in the API docs)
const result = await nocoDbFetch<any>(url, {
method: "PATCH",
body: JSON.stringify(cleanData)
});
console.log('[nocodb.updateInterest] Update successful for ID:', id);
return result;
} catch (error: any) {
console.error('[nocodb.updateInterest] Update failed:', error);
console.error('[nocodb.updateInterest] Error details:', error instanceof Error ? error.message : 'Unknown error');
2025-06-09 23:38:35 +02:00
// If it's a 404 error and we haven't retried too many times, wait and retry
if ((error.statusCode === 404 || error.status === 404) && retryCount < 3) {
console.error('[nocodb.updateInterest] 404 Error - Record not found. This might be a sync delay.');
console.error(`Retrying in ${(retryCount + 1) * 1000}ms... (Attempt ${retryCount + 1}/3)`);
// Wait with exponential backoff
await new Promise(resolve => setTimeout(resolve, (retryCount + 1) * 1000));
// Retry the update
return updateInterest(id, data, retryCount + 1);
}
// If it's still a 404 after retries, provide detailed error
if (error.statusCode === 404 || error.status === 404) {
2025-06-09 23:38:35 +02:00
console.error('[nocodb.updateInterest] 404 Error - Record not found after 3 retries. This might happen if:');
console.error('1. The record ID is incorrect');
2025-06-09 23:38:35 +02:00
console.error('2. The record was deleted');
console.error('3. There is a synchronization issue with the database');
console.error('Attempted URL:', url);
}
throw error;
}
2025-06-03 21:04:22 +02:00
};
export const createInterest = async (data: Partial<any>) => {
console.log('[nocodb.createInterest] Creating interest with fields:', Object.keys(data));
2025-06-03 21:04:22 +02:00
// 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",
"Contact Method Preferred",
"Lead Category",
2025-06-03 23:48:44 +02:00
"EOI Status",
"Berth Info Sent Status",
"Contract Sent Status",
"Deposit 10% Status",
"Contract Status",
2025-06-03 21:04:22 +02:00
];
// Filter the data to only include allowed fields
for (const field of allowedFields) {
if (field in data) {
cleanData[field] = (data as any)[field];
2025-06-03 21:04:22 +02:00
}
}
// Remove any computed or relation fields that shouldn't be sent
delete cleanData.Id;
delete cleanData.Berths;
delete cleanData["Berth Recommendations"];
delete cleanData.Berth;
console.log('[nocodb.createInterest] Clean data fields:', Object.keys(cleanData));
const url = createTableUrl(Table.Interest);
console.log('[nocodb.createInterest] URL:', url);
try {
const result = await nocoDbFetch<any>(url, {
method: "POST",
body: JSON.stringify(cleanData)
});
console.log('[nocodb.createInterest] Created interest with ID:', result.Id);
return result;
} catch (error) {
console.error('[nocodb.createInterest] Create failed:', error);
console.error('[nocodb.createInterest] Error details:', error instanceof Error ? error.message : 'Unknown error');
throw error;
}
2025-06-03 17:57:08 +02:00
};
export const deleteInterest = async (id: string) => {
2025-06-10 12:54:22 +02:00
const startTime = Date.now();
console.log('[nocodb.deleteInterest] =========================');
console.log('[nocodb.deleteInterest] DELETE operation started at:', new Date().toISOString());
console.log('[nocodb.deleteInterest] Target ID:', id);
2025-06-10 12:31:00 +02:00
const url = createTableUrl(Table.Interest);
console.log('[nocodb.deleteInterest] URL:', url);
2025-06-10 12:54:22 +02:00
const requestBody = {
"Id": parseInt(id)
};
console.log('[nocodb.deleteInterest] Request configuration:');
console.log(' Method: DELETE');
console.log(' URL:', url);
console.log(' Headers:', {
"xc-token": getNocoDbConfiguration().token ? "***" + getNocoDbConfiguration().token.slice(-4) : "not set",
"Content-Type": "application/json"
});
console.log(' Body:', JSON.stringify(requestBody, null, 2));
try {
2025-06-10 12:31:00 +02:00
// According to NocoDB API docs, DELETE requires ID in the body
const result = await nocoDbFetch<any>(url, {
method: "DELETE",
body: JSON.stringify(requestBody)
});
2025-06-10 12:54:22 +02:00
console.log('[nocodb.deleteInterest] DELETE successful');
console.log('[nocodb.deleteInterest] Response:', JSON.stringify(result, null, 2));
console.log('[nocodb.deleteInterest] Duration:', Date.now() - startTime, 'ms');
console.log('[nocodb.deleteInterest] =========================');
return result;
2025-06-10 12:54:22 +02:00
} catch (error: any) {
console.error('[nocodb.deleteInterest] =========================');
console.error('[nocodb.deleteInterest] DELETE FAILED');
console.error('[nocodb.deleteInterest] Error type:', error.constructor.name);
console.error('[nocodb.deleteInterest] Error message:', error.message);
console.error('[nocodb.deleteInterest] Error status:', error.statusCode || error.status || 'unknown');
console.error('[nocodb.deleteInterest] Error data:', error.data);
console.error('[nocodb.deleteInterest] Error stack:', error.stack || 'No stack trace');
console.error('[nocodb.deleteInterest] Full error:', JSON.stringify(error, null, 2));
console.error('[nocodb.deleteInterest] Duration:', Date.now() - startTime, 'ms');
console.error('[nocodb.deleteInterest] =========================');
throw error;
}
};
export const triggerWebhook = async (url: string, payload: any) => {
// Webhooks might not need the same resilience as data operations
const response = await fetch(url, {
2025-06-03 21:04:22 +02:00
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
2025-06-03 17:57:08 +02:00
});
if (!response.ok) {
throw new Error(`Webhook failed: ${response.status}`);
}
return response.json();
};
export const updateInterestEOIDocument = async (id: string, documentData: any) => {
console.log('[nocodb.updateInterestEOIDocument] Updating EOI document for interest:', id);
// Get existing EOI Document array or create new one
const interest = await getInterestById(id);
const existingDocuments = interest['EOI Document'] || [];
// Add the new document to the array
const updatedDocuments = [...existingDocuments, documentData];
// Update the interest with the new EOI Document array
return updateInterest(id, {
'EOI Document': updatedDocuments
});
};
export const getInterestByFieldAsync = async (fieldName: string, value: any): Promise<Interest | null> => {
try {
const response = await getInterests();
const interests = response.list || [];
// Find interest where the field matches the value
const interest = interests.find(i => (i as any)[fieldName] === value);
return interest || null;
} catch (error) {
console.error('Error fetching interest by field:', error);
return null;
}
};