fix: prefer history data over synced data for internal columns

This commit is contained in:
Christian Vidal Wolf
2026-04-23 19:28:25 +02:00
parent a987a5b750
commit 8ffcae5eef
2 changed files with 4 additions and 23 deletions
+3 -9
View File
@@ -137,7 +137,6 @@ export default function App() {
console.log('Fetching history data from Supabase for merge...'); console.log('Fetching history data from Supabase for merge...');
const historyData = await getHistoryDataForMerge(); const historyData = await getHistoryDataForMerge();
console.log('Supabase synced rows:', Object.keys(syncedData).length, '| History rows:', Object.keys(historyData).length); console.log('Supabase synced rows:', Object.keys(syncedData).length, '| History rows:', Object.keys(historyData).length);
console.log('[DEBUG] historyData["59272EN"] exists:', '59272EN' in historyData, '| value:', historyData['59272EN']?.[103], '| type:', typeof historyData['59272EN']);
// Extend headers with virtual columns if the Excel is shorter than the saved data. // Extend headers with virtual columns if the Excel is shorter than the saved data.
// COLUMNS hardcoded indices (PRODUCT_TYPE=103, ITEM_TO_LOGISTIC=104, etc.) are used // COLUMNS hardcoded indices (PRODUCT_TYPE=103, ITEM_TO_LOGISTIC=104, etc.) are used
@@ -201,7 +200,9 @@ const articleNoIdx = resolvedCols.ARTICLE_NO;
} }
// Merge logic: Prioritize internal control columns from syncedData or historyData // Merge logic: Prioritize internal control columns from syncedData or historyData
const sourceForInternal = synced?.data || hist; // Use synced.data only if it has internal cols (>= 100), otherwise use hist
const syncedHasInternalCols = synced?.data && Object.values(COLUMNS).some(idx => idx >= 100 && synced.data[idx] !== undefined && synced.data[idx] !== null);
const sourceForInternal = syncedHasInternalCols ? synced!.data : (hist || synced?.data);
if (sourceForInternal) { if (sourceForInternal) {
// 1. ALWAYS restore Internal Control Columns (indices >= 100) // 1. ALWAYS restore Internal Control Columns (indices >= 100)
@@ -229,13 +230,6 @@ const articleNoIdx = resolvedCols.ARTICLE_NO;
} }
} }
if (articleNo === '59272EN') {
console.log('[DEBUG 59272EN] hist?.length:', hist?.length, '| hist?.[103]:', hist?.[103], '| hist type:', typeof hist);
console.log('[DEBUG 59272EN] synced?.data?.[103]:', synced?.data?.[103]);
console.log('[DEBUG 59272EN] sourceForInternal?.[103]:', sourceForInternal?.[103]);
console.log('[DEBUG 59272EN] FINAL processedRow[103]:', finalRow[103], '| length:', finalRow.length);
}
return finalRow.map((val: any, idx: number) => { return finalRow.map((val: any, idx: number) => {
if (val === undefined || val === null || val === '') return val; if (val === undefined || val === null || val === '') return val;
const header = (extendedHeaders[idx] || '').toLowerCase(); const header = (extendedHeaders[idx] || '').toLowerCase();
+1 -14
View File
@@ -14,12 +14,8 @@ async function safeFetch(url: string, options: RequestInit = {}): Promise<Respon
}; };
let response = await fetch(url, { ...options, headers }); let response = await fetch(url, { ...options, headers });
console.log('[safeFetch] URL:', url.split('?')[0]);
console.log('[safeFetch] Status:', response.status, '| OK:', response.ok);
console.log('[safeFetch] Session token:', !!session, '| Token length:', token?.length);
if (response.status === 401 && session?.refresh_token) { if (response.status === 401 && session?.refresh_token) {
console.log('[safeFetch] 401 - refreshing session...');
try { try {
const newSession = await refreshSession(session.refresh_token); const newSession = await refreshSession(session.refresh_token);
const newHeaders = { const newHeaders = {
@@ -28,9 +24,8 @@ async function safeFetch(url: string, options: RequestInit = {}): Promise<Respon
'Authorization': `Bearer ${newSession.access_token}`, 'Authorization': `Bearer ${newSession.access_token}`,
}; };
response = await fetch(url, { ...options, headers: newHeaders }); response = await fetch(url, { ...options, headers: newHeaders });
console.log('[safeFetch] After refresh - Status:', response.status);
} catch (refreshError) { } catch (refreshError) {
console.error('[safeFetch] Session refresh failed:', refreshError); console.error('Session refresh failed:', refreshError);
} }
} }
@@ -210,14 +205,6 @@ export async function getHistoryDataForMerge(): Promise<Record<string, ExcelRow>
return {}; return {};
} }
const entries: Array<{ product_id: string; new_data: ExcelRow }> = await response.json(); const entries: Array<{ product_id: string; new_data: ExcelRow }> = await response.json();
console.log('[getHistoryDataForMerge] Raw entries count:', entries.length);
const entry_59272 = entries.find(e => e.product_id === '59272EN');
console.log('[getHistoryDataForMerge] 59272EN entry found:', !!entry_59272);
if (entry_59272) {
console.log('[getHistoryDataForMerge] 59272EN new_data length:', entry_59272.new_data?.length);
console.log('[getHistoryDataForMerge] 59272EN new_data[103]:', entry_59272.new_data?.[103]);
console.log('[getHistoryDataForMerge] 59272EN new_data type:', typeof entry_59272.new_data);
}
const result: Record<string, ExcelRow> = {}; const result: Record<string, ExcelRow> = {};
const seen = new Set<string>(); const seen = new Set<string>();