From 397023b00de5c8416a508e73c620b8266692ae95 Mon Sep 17 00:00:00 2001 From: Christian Vidal Wolf Date: Thu, 23 Apr 2026 15:04:43 +0200 Subject: [PATCH] Fix: Restore missing user edits by refining the data merge logic. - Migrated 126 manual edits to 'edited' status. - Updated App.tsx to merge rows with 'edited' or 'synced' status. - Protected manual edits from being overwritten by automated Dropbox syncs. --- api/dropbox-sync.js | 20 ++++++++++++-------- src/App.tsx | 4 ++-- src/lib/supabase.ts | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/api/dropbox-sync.js b/api/dropbox-sync.js index f3b3fb7..79562e1 100644 --- a/api/dropbox-sync.js +++ b/api/dropbox-sync.js @@ -40,29 +40,33 @@ export default async function handler(req, res) { } const articleNoIdx = 0; + // Fetch IDs of products that have been manually edited so we don't overwrite them + const { data: protectedProducts } = await supabase + .from('products') + .select('product_id') + .or('status.eq.edited,status.eq.pending'); + + const protectedIds = new Set((protectedProducts || []).map(p => p.product_id)); const productsToUpsert = []; - for (const row of rows) { const productId = String(row[articleNoIdx]); - if (productId && productId.trim() !== '') { + if (productId && productId.trim() !== '' && !protectedIds.has(productId)) { productsToUpsert.push({ product_id: productId, data: row, - status: 'synced', + status: 'excel', updated_at: new Date().toISOString() }); } } - console.log('Upserting', productsToUpsert.length, 'products to Supabase...'); + console.log('Upserting', productsToUpsert.length, 'new/updated products to Supabase...'); + // We can now use a normal upsert because we filtered out the manual edits const { error } = await supabase .from('products') - .upsert(productsToUpsert, { - onConflict: 'product_id', - ignoreDuplicates: true // Keep this: will be overwritten by App.tsx logic if there are pending edits - }); + .upsert(productsToUpsert, { onConflict: 'product_id' }); if (error) { console.error('Supabase upsert error:', error); diff --git a/src/App.tsx b/src/App.tsx index fd305d3..6e2f0e2 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -166,8 +166,8 @@ export default function App() { // Start with Dropbox values (default) let finalRow = [...row]; - // Only preserve Supabase values if there's a PENDING edit for this article - if (synced && synced.status === 'pending') { + // Only preserve Supabase values if there's a PENDING or EDITED edit for this article + if (synced && (synced.status === 'pending' || synced.status === 'edited' || synced.status === 'synced')) { // Keep the manually edited values from Supabase editableColumns.forEach(idx => { if (synced.data[idx] !== undefined && synced.data[idx] !== null) { diff --git a/src/lib/supabase.ts b/src/lib/supabase.ts index 7bcf2c3..11d7735 100644 --- a/src/lib/supabase.ts +++ b/src/lib/supabase.ts @@ -77,7 +77,7 @@ export async function saveRowToSupabase(articleNo: string, rowData: ExcelRow): P body: JSON.stringify({ product_id: articleNo, data: rowData, - status: 'synced', + status: 'edited', updated_at: new Date().toISOString() }) }