feat: sync Excel data to Supabase on load - detect new Dropbox files automatically

This commit is contained in:
Christian Vidal Wolf
2026-04-10 10:54:50 +02:00
parent 077e78513c
commit 90e60017da
6 changed files with 400 additions and 64 deletions
+71 -49
View File
@@ -47,79 +47,97 @@ export default function App() {
useEffect(() => {
const loadDefaultData = async () => {
// In dev: Vite proxy handles /dropbox-file (see vite.config.ts)
// In prod: Vercel serverless function at /api/dropbox-proxy handles it
const fileUrl = import.meta.env.DEV
? '/dropbox-file/scl/fi/nkyabkq2jdwfudof2ovrl/Data-Matrix.xlsx?rlkey=1cz5fuabwipqqivihii7sybw0&st=io0g4wvb&dl=1'
: '/api/dropbox-proxy';
setIsLoadingDefault(true);
setDefaultLoadError(null);
try {
console.log('Fetching Data-Matrix.xlsx via Vite proxy...');
const response = await fetch(fileUrl);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const arrayBuffer = await response.arrayBuffer();
if (arrayBuffer.byteLength < 100) {
throw new Error('File too small — possibly empty or error response');
}
const wb = XLSX.read(arrayBuffer, { type: 'array' });
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
const data = XLSX.utils.sheet_to_json<any[]>(ws, { header: 1 });
if (data.length > 0) {
const rawHeaders = data[0];
const rawRows = data.slice(1);
// Find ASIN column index from headers (case insensitive)
const asinIdx = (rawHeaders as string[]).findIndex((h: string) =>
String(h).toLowerCase().trim() === 'asin'
);
if (asinIdx !== -1) {
console.log('ASIN column found at index:', asinIdx);
const isDev = import.meta.env.DEV;
let rows: any[][];
let fileMeta = { rev: '', size: 0 };
if (isDev) {
const fileUrl = '/dropbox-file/scl/fi/nkyabkq2jdwfudof2ovrl/Data-Matrix.xlsx?rlkey=1cz5fuabwipqqivihii7sybw0&st=io0g4wvb&dl=1';
console.log('Fetching Data-Matrix.xlsx from Dropbox...');
const response = await fetch(fileUrl);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const arrayBuffer = await response.arrayBuffer();
if (arrayBuffer.byteLength < 100) throw new Error('File too small');
const wb = XLSX.read(arrayBuffer, { type: 'array' });
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
const data = XLSX.utils.sheet_to_json<any[]>(ws, { header: 1 });
rows = data.slice(1);
fileMeta = { rev: 'dev', size: arrayBuffer.byteLength };
} else {
console.log('Fetching file info from Dropbox...');
const infoRes = await fetch('/api/dropbox-proxy?info=1');
if (infoRes.ok) {
fileMeta = await infoRes.json();
console.log('File meta:', fileMeta);
}
console.log('Fetching Data-Matrix.xlsx from proxy...');
const response = await fetch('/api/dropbox-proxy');
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const arrayBuffer = await response.arrayBuffer();
if (arrayBuffer.byteLength < 100) throw new Error('File too small');
const wb = XLSX.read(arrayBuffer, { type: 'array' });
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
const data = XLSX.utils.sheet_to_json<any[]>(ws, { header: 1 });
rows = data.slice(1);
}
console.log('Applying Supabase overrides...');
if (rows.length > 0) {
const rawHeaders = (data: any[]) => data[0];
console.log('Syncing Excel data to Supabase...');
const syncRes = await fetch('/api/dropbox-sync', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ rows, fileMeta })
});
if (syncRes.ok) {
const syncResult = await syncRes.json();
console.log('Supabase sync result:', syncResult);
} else {
console.warn('Supabase sync failed, falling back to direct Excel data');
}
console.log('Fetching synced data from Supabase...');
const syncedData = await getAllSyncedRows();
const articleNoIdx = COLUMNS.ARTICLE_NO;
const processedRows = rawRows.map(row => {
const processedRows = rows.map(row => {
const articleNo = String(row[articleNoIdx]);
const synced = syncedData[articleNo];
const finalRow = synced ? synced.data : row;
// Sync status_check
if (synced && synced.status === 'pending') {
setRowStatuses(prev => ({ ...prev, [articleNo]: 'pending' }));
}
// Format numeric/price fields to 2 decimal places
return finalRow.map((val, idx) => {
return finalRow.map((val: any, idx: number) => {
if (val === undefined || val === null || val === '') return val;
const header = (rawHeaders[idx] || '').toLowerCase();
// Skip Article No, Barcodes, and other code-like fields
// But allow if it's a weight/measure column (e.g. Article NW (kg))
if ((header.includes('id') || header.includes('no') || header.includes('code') ||
header.includes('art.') || header.includes('barcode') || header.includes('article')) &&
!(header.includes('nw') || header.includes('gw') || header.includes('weight') || header.includes('kg'))) {
return val;
}
const formatKeywords = ['price', 'eur', 'cost', 'msrp', 'net', 'gross', 'netto', 'brutto', 'pp', 'pph', 'uvp', 'vpe', 'stk', 'nw', 'gw', 'weight', 'kg'];
const shouldFormat = formatKeywords.some(kw => header.includes(kw));
if (typeof val === 'number') {
return Number(val.toFixed(2));
}
if (typeof val === 'string') {
const normalized = val.trim().replace(',', '.');
const num = parseFloat(normalized);
@@ -130,9 +148,13 @@ export default function App() {
return val;
});
});
const asinIdx = (rawHeaders(rows) as string[]).findIndex((h: string) =>
String(h).toLowerCase().trim() === 'asin'
);
setAppState({
headers: rawHeaders,
headers: rawHeaders(rows),
data: processedRows,
fileName: 'Data-Matrix.xlsx (Cloud Sync)',
fileDate: new Date(),