mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 12:15:24 +02:00
fix: use raw fetch for Dropbox API instead of SDK, add better error logging
This commit is contained in:
+45
-38
@@ -6,46 +6,53 @@ const SUPABASE_KEY = process.env.SUPABASE_SERVICE_KEY || 'sb_publishable_fGXkh0b
|
||||
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()
|
||||
});
|
||||
try {
|
||||
if (req.method !== 'POST') {
|
||||
return res.status(405).json({ error: 'Method not allowed' });
|
||||
}
|
||||
}
|
||||
|
||||
const { error } = await supabase
|
||||
.from('products')
|
||||
.upsert(productsToUpsert, {
|
||||
onConflict: 'product_id',
|
||||
ignoreDuplicates: false
|
||||
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()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Upserting', productsToUpsert.length, 'products to Supabase...');
|
||||
|
||||
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
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error('Supabase upsert error:', error);
|
||||
return res.status(500).json({ error: error.message, detail: 'Failed to upsert products' });
|
||||
} catch (err) {
|
||||
console.error('Handler error:', err);
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
syncedCount: productsToUpsert.length
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user