fix: resolve vendor data upload error by implementing client-side parsing and sequential batching

This commit is contained in:
Christian Vidal Wolf
2026-02-20 13:32:26 +01:00
parent a8258398f7
commit 85c789bf52
5 changed files with 162 additions and 65 deletions
+42 -34
View File
@@ -1,6 +1,7 @@
import type { VercelRequest, VercelResponse } from '@vercel/node';
import { createClient } from '@supabase/supabase-js';
import Papa from 'papaparse';
import { VendorDailyRow } from '../types';
const supabase = createClient(
process.env.SUPABASE_URL || '',
@@ -34,43 +35,51 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
if (req.method !== 'POST') return res.status(405).json({ error: 'Method not allowed' });
try {
const csvText = typeof req.body === 'string' ? req.body : req.body?.toString() || '';
let rows: VendorDailyRow[] = [];
if (!csvText.trim()) {
return res.status(400).json({ error: 'Empty CSV body' });
// Check if the body is already parsed JSON (array of rows)
if (Array.isArray(req.body)) {
console.log(`[upload-vendor-data] Received ${req.body.length} rows as JSON`);
rows = req.body;
} else {
const csvText = typeof req.body === 'string' ? req.body : req.body?.toString() || '';
if (!csvText.trim()) {
return res.status(400).json({ error: 'Empty CSV body' });
}
const parsed = Papa.parse<CSVRow>(csvText, {
header: true,
skipEmptyLines: true,
});
if (parsed.errors.length > 0) {
console.error('[upload-vendor-data] Parse errors:', parsed.errors.slice(0, 5));
}
rows = parsed.data
.filter(row => row['Date'] && row['Market'] && row['ASIN'])
.map(row => ({
date: row['Date'],
market: row['Market'],
asin: row['ASIN'],
product_title: row['Product Title'] || null,
tags: row['Tags'] || null,
bsr_top_rank: parseIntSafe(row['Top Level Category (Rank)']),
bsr_top_category: row['Top Level Category (Name)'] || null,
bsr_detail_rank: parseIntSafe(row['Detail Level Category (Rank)']),
bsr_detail_category: row['Detail Level Category (Name)'] || null,
avg_rating: parseEUNumber(row['Average Rating']),
num_reviews: parseIntSafe(row['Number of Reviews']),
buybox_owner: row['Buybox Seller Name'] || null,
buybox_price: parseEUNumber(row['Buybox Price']),
amazon_has_buybox: row['Amazon Has Buybox'] === '1',
glance_views: parseIntSafe(row['Glance Views']),
}));
}
const parsed = Papa.parse<CSVRow>(csvText, {
header: true,
skipEmptyLines: true,
});
if (parsed.errors.length > 0) {
console.error('[upload-vendor-data] Parse errors:', parsed.errors.slice(0, 5));
}
const rows = parsed.data
.filter(row => row['Date'] && row['Market'] && row['ASIN'])
.map(row => ({
date: row['Date'],
market: row['Market'],
asin: row['ASIN'],
product_title: row['Product Title'] || null,
tags: row['Tags'] || null,
bsr_top_rank: parseIntSafe(row['Top Level Category (Rank)']),
bsr_top_category: row['Top Level Category (Name)'] || null,
bsr_detail_rank: parseIntSafe(row['Detail Level Category (Rank)']),
bsr_detail_category: row['Detail Level Category (Name)'] || null,
avg_rating: parseEUNumber(row['Average Rating']),
num_reviews: parseIntSafe(row['Number of Reviews']),
buybox_owner: row['Buybox Seller Name'] || null,
buybox_price: parseEUNumber(row['Buybox Price']),
amazon_has_buybox: row['Amazon Has Buybox'] === '1',
glance_views: parseIntSafe(row['Glance Views']),
}));
if (rows.length === 0) {
return res.status(400).json({ error: 'No valid rows found in CSV' });
return res.status(400).json({ error: 'No valid rows found' });
}
// Upsert in batches of 500
@@ -94,7 +103,6 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
res.status(200).json({
success: true,
rowsParsed: parsed.data.length,
rowsUpserted: totalUpserted,
});
} catch (error: any) {