From cc2cc282b90c3bedb9e906181dde245dc53f580f Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 9 Jun 2025 23:42:31 +0200 Subject: [PATCH] fixes --- docs/email-system-fixes.md | 25 ++++++++++++++++++------- server/api/update-interest.ts | 16 ++++++++++++---- server/utils/nocodb.ts | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/docs/email-system-fixes.md b/docs/email-system-fixes.md index ec61776..44fcbc5 100644 --- a/docs/email-system-fixes.md +++ b/docs/email-system-fixes.md @@ -81,14 +81,25 @@ - Added "Waiting for Signatures" as option 2 in EOI Status - New order: "Awaiting Further Details" → "Waiting for Signatures" → "Signed" -### 11. 404 Errors for Newly Created Records -- **Problem**: Newly created records sometimes show 404 when immediately updating/linking +### 11. 404 Errors for Existing Records During Updates +- **Problem**: Records that exist in the database return 404 when trying to update them - **Solution**: - - Added retry mechanism with exponential backoff to `updateInterest` function - - Will retry up to 3 times with increasing delays (1s, 2s, 3s) - - `get-interest-berths` returns empty list instead of throwing error for new records - - This handles NocoDB synchronization delays where records need time to propagate - - After 3 retries, provides detailed error message explaining possible causes + - Added verification step to first try fetching the record before updating + - Added retry mechanism with exponential backoff (3 retries with 1s, 2s, 3s delays) + - Enhanced logging to show exact URLs, headers, and request bodies + - Added Content-Type header to PATCH requests + - `get-interest-berths` returns empty list instead of throwing error for 404s + +**Investigation Steps Added:** +1. Verify record exists with GET before attempting PATCH +2. Log complete request details including URLs and tokens +3. Retry with exponential backoff in case of timing issues + +**If the issue persists:** +- Check if the NocoDB API token has write permissions +- Verify the table ID hasn't changed +- Try updating a different record to see if it's record-specific +- Check NocoDB logs for more details about the 404 error ## Required Environment Variables diff --git a/server/api/update-interest.ts b/server/api/update-interest.ts index 68cbe0c..70ddf97 100644 --- a/server/api/update-interest.ts +++ b/server/api/update-interest.ts @@ -12,13 +12,21 @@ export default defineEventHandler(async (event) => { try { const body = await readBody(event); const { id, data } = body; - console.log('[update-interest] Request body:', { id, dataKeys: data ? Object.keys(data) : 'none' }); + console.log('[update-interest] Request body:', { + id, + idType: typeof id, + dataKeys: data ? Object.keys(data) : 'none' + }); if (!id) { console.error('[update-interest] Missing ID in request'); throw createError({ statusCode: 400, statusMessage: "ID is required" }); } + // Ensure ID is a string + const interestId = String(id); + console.log('[update-interest] Using ID:', interestId, 'Type:', typeof interestId); + if (!data || Object.keys(data).length === 0) { console.error('[update-interest] No data provided for update'); throw createError({ statusCode: 400, statusMessage: "No data provided for update" }); @@ -31,9 +39,9 @@ export default defineEventHandler(async (event) => { console.log('[update-interest] Removed Id from update data'); } - console.log('[update-interest] Updating interest:', id, 'with fields:', Object.keys(updateData)); - const updatedInterest = await updateInterest(id, updateData); - console.log('[update-interest] Successfully updated interest:', id); + console.log('[update-interest] Updating interest:', interestId, 'with fields:', Object.keys(updateData)); + const updatedInterest = await updateInterest(interestId, updateData); + console.log('[update-interest] Successfully updated interest:', interestId); return updatedInterest; } catch (error) { diff --git a/server/utils/nocodb.ts b/server/utils/nocodb.ts index 5537ead..da94ea6 100644 --- a/server/utils/nocodb.ts +++ b/server/utils/nocodb.ts @@ -15,10 +15,17 @@ export enum Table { Interest = "mbs9hjauug4eseo", } -export const getNocoDbConfiguration = () => useRuntimeConfig().nocodb; +export const getNocoDbConfiguration = () => { + const config = useRuntimeConfig().nocodb; + console.log('[nocodb] Configuration URL:', config.url); + return config; +}; -export const createTableUrl = (table: Table) => - `${getNocoDbConfiguration().url}/api/v2/tables/${table}/records`; +export const createTableUrl = (table: Table) => { + const url = `${getNocoDbConfiguration().url}/api/v2/tables/${table}/records`; + console.log('[nocodb] Table URL:', url); + return url; +}; export const getInterests = async () => $fetch(createTableUrl(Table.Interest), { @@ -41,6 +48,20 @@ export const updateInterest = async (id: string, data: Partial, retryC console.log('[nocodb.updateInterest] Updating interest:', id, 'Retry:', retryCount); console.log('[nocodb.updateInterest] Data fields:', Object.keys(data)); + // 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'); + } + } + } + // 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 = {}; @@ -97,10 +118,16 @@ export const updateInterest = async (id: string, data: Partial, retryC console.log('[nocodb.updateInterest] URL:', url); try { + 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)); + const result = await $fetch(url, { method: "PATCH", headers: { "xc-token": getNocoDbConfiguration().token, + "Content-Type": "application/json" }, body: cleanData });