mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 11:35:24 +02:00
feat: sync Excel data to Supabase on load - detect new Dropbox files automatically
This commit is contained in:
+27
-14
@@ -1,23 +1,36 @@
|
||||
// Vercel serverless function — proxies the Dropbox file server-side (no CORS)
|
||||
const Dropbox = require('dropbox').Dropbox;
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const fileUrl =
|
||||
'https://dl.dropboxusercontent.com/scl/fi/nkyabkq2jdwfudof2ovrl/Data-Matrix.xlsx' +
|
||||
'?rlkey=1cz5fuabwipqqivihii7sybw0&st=io0g4wvb&dl=1';
|
||||
if (req.method === 'GET' && req.query.info === '1') {
|
||||
const DBX = new Dropbox({
|
||||
accessToken: process.env.DROPBOX_ACCESS_TOKEN
|
||||
});
|
||||
|
||||
try {
|
||||
const fileInfo = await DBX.filesGetInfo({ path: '/Data-Matrix.xlsx' });
|
||||
return res.json({
|
||||
rev: fileInfo.result.rev,
|
||||
size: fileInfo.result.size,
|
||||
server_modified: fileInfo.result.server_modified
|
||||
});
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: err.message });
|
||||
}
|
||||
}
|
||||
|
||||
const DBX = new Dropbox({
|
||||
accessToken: process.env.DROPBOX_ACCESS_TOKEN
|
||||
});
|
||||
|
||||
try {
|
||||
const upstream = await fetch(fileUrl, {
|
||||
headers: { 'User-Agent': 'Mozilla/5.0' },
|
||||
});
|
||||
|
||||
if (!upstream.ok) {
|
||||
return res.status(upstream.status).send(`Dropbox error: ${upstream.status}`);
|
||||
}
|
||||
|
||||
const buffer = await upstream.arrayBuffer();
|
||||
const upstream = await DBX.filesDownload({ path: '/Data-Matrix.xlsx' });
|
||||
const buffer = Buffer.from(upstream.result.fileBinary);
|
||||
|
||||
res.setHeader('Content-Type', 'application/octet-stream');
|
||||
res.setHeader('Cache-Control', 'no-store');
|
||||
res.send(Buffer.from(buffer));
|
||||
res.send(buffer);
|
||||
} catch (err) {
|
||||
console.error('Dropbox error:', err);
|
||||
res.status(500).send('Proxy error: ' + err.message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
|
||||
const SUPABASE_URL = process.env.SUPABASE_URL || 'https://hwithddwaapyhnfwcesj.supabase.co';
|
||||
const SUPABASE_KEY = process.env.SUPABASE_SERVICE_KEY || 'sb_publishable_fGXkh0bSrAOqSk2jWKAzSg_NJD9YPCv';
|
||||
|
||||
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
|
||||
|
||||
export default async function handler(req, res) {
|
||||
if (req.method !== 'POST') {
|
||||
return res.status(405).json({ error: 'Method not allowed' });
|
||||
}
|
||||
|
||||
const { rows, fileMeta } = req.body;
|
||||
|
||||
if (!rows || !Array.isArray(rows)) {
|
||||
return res.status(400).json({ error: 'Missing rows data' });
|
||||
}
|
||||
|
||||
const articleNoIdx = 0;
|
||||
|
||||
const productsToUpsert = [];
|
||||
|
||||
for (const row of rows) {
|
||||
const productId = String(row[articleNoIdx]);
|
||||
if (productId && productId.trim() !== '') {
|
||||
productsToUpsert.push({
|
||||
product_id: productId,
|
||||
data: row,
|
||||
status: 'synced',
|
||||
updated_at: new Date().toISOString()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const { error } = await supabase
|
||||
.from('products')
|
||||
.upsert(productsToUpsert, {
|
||||
onConflict: 'product_id',
|
||||
ignoreDuplicates: false
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error('Supabase upsert error:', error);
|
||||
return res.status(500).json({ error: error.message, detail: 'Failed to upsert products' });
|
||||
}
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
syncedCount: productsToUpsert.length
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user