mirror of
https://github.com/christianvidalwolf-prog/Craze-Data-check.git
synced 2026-08-03 12:15:24 +02:00
fix(sync): trust Dropbox data
This commit is contained in:
+23
-122
@@ -10,86 +10,6 @@ function setCors(req, res) {
|
||||
applyCors(req, res, 'POST, OPTIONS');
|
||||
}
|
||||
|
||||
const COLUMN_PATTERNS = {
|
||||
CLASSIFICATION: ['classification'],
|
||||
LONG_DE: ['long', 'description', 'german'],
|
||||
LONG_EN: ['long', 'description', 'english'],
|
||||
SHORT_DE: ['short', 'description', 'german'],
|
||||
SHORT_EN: ['short', 'description', 'english'],
|
||||
DETAILS_DE: ['details', 'german'],
|
||||
DETAILS_EN: ['details', 'english'],
|
||||
INNER_L: ['inner', 'l'],
|
||||
INNER_W: ['inner', 'w'],
|
||||
INNER_H: ['inner', 'h'],
|
||||
OUTER_L: ['outer', 'l'],
|
||||
OUTER_W: ['outer', 'w'],
|
||||
OUTER_H: ['outer', 'h'],
|
||||
UNITS_OUTER: ['units', 'outer'],
|
||||
MOQ: ['moq'],
|
||||
VERIFIED_DIMS: ['verified', 'dims'],
|
||||
VALIDATED_CHECK: ['validated', 'check'],
|
||||
VALIDATED_NOTE: ['validated', 'note'],
|
||||
PRODUCT_TYPE: ['product', 'type'],
|
||||
ITEM_TO_LOGISTIC: ['item', 'logistic'],
|
||||
CPNP_NO: ['cpnp'],
|
||||
};
|
||||
|
||||
function resolveEditableColumns(headers) {
|
||||
if (!headers || !Array.isArray(headers)) {
|
||||
// Fallback default editable column indices
|
||||
return new Set([
|
||||
9, 10, 11, 28, 33, 43, 44, 45, 48, 49, 50, 64, 65, 66, 67,
|
||||
100, 101, 102, 103, 104, 107,
|
||||
18, 19, 20, 21, 22, 23, 24, 25, 26, 27
|
||||
]);
|
||||
}
|
||||
|
||||
const h = headers.map(val => String(val || '').toLowerCase());
|
||||
const resolved = {};
|
||||
|
||||
Object.entries(COLUMN_PATTERNS).forEach(([key, patterns]) => {
|
||||
const idx = h.findIndex(headerText =>
|
||||
patterns.every(p => headerText.includes(p.toLowerCase()))
|
||||
);
|
||||
if (idx >= 0) {
|
||||
resolved[key] = idx;
|
||||
}
|
||||
});
|
||||
|
||||
const editableColumns = new Set([
|
||||
resolved.CLASSIFICATION,
|
||||
resolved.LONG_DE,
|
||||
resolved.LONG_EN,
|
||||
resolved.SHORT_DE,
|
||||
resolved.SHORT_EN,
|
||||
resolved.DETAILS_DE,
|
||||
resolved.DETAILS_EN,
|
||||
resolved.INNER_L,
|
||||
resolved.INNER_W,
|
||||
resolved.INNER_H,
|
||||
resolved.OUTER_L,
|
||||
resolved.OUTER_W,
|
||||
resolved.OUTER_H,
|
||||
resolved.UNITS_OUTER,
|
||||
resolved.MOQ,
|
||||
resolved.VERIFIED_DIMS,
|
||||
resolved.VALIDATED_CHECK,
|
||||
resolved.VALIDATED_NOTE,
|
||||
resolved.PRODUCT_TYPE,
|
||||
resolved.ITEM_TO_LOGISTIC,
|
||||
resolved.CPNP_NO
|
||||
].filter(val => val !== undefined));
|
||||
|
||||
headers.forEach((header, i) => {
|
||||
const headerText = String(header || '').toLowerCase();
|
||||
if (headerText.includes('srp') || headerText.includes('uvp') || headerText.includes('price')) {
|
||||
editableColumns.add(i);
|
||||
}
|
||||
});
|
||||
|
||||
return editableColumns;
|
||||
}
|
||||
|
||||
export default async function handler(req, res) {
|
||||
setCors(req, res);
|
||||
|
||||
@@ -107,66 +27,47 @@ export default async function handler(req, res) {
|
||||
return res.status(405).json({ error: 'Method not allowed' });
|
||||
}
|
||||
|
||||
const { rows, headers } = req.body;
|
||||
const { rows } = req.body;
|
||||
|
||||
if (!rows || !Array.isArray(rows)) {
|
||||
return res.status(400).json({ error: 'Missing rows data' });
|
||||
}
|
||||
|
||||
const articleNoIdx = 0;
|
||||
// Fetch data and status of ALL products so we can merge non-editable columns for manually edited ones
|
||||
// Fetch current product rows so we can preserve internal-only columns
|
||||
// that do not exist in the live Dropbox workbook.
|
||||
const { data: existingProducts } = await supabase
|
||||
.from('products')
|
||||
.select('product_id, data, status');
|
||||
|
||||
// Protect any product that has history entries
|
||||
const { data: historyProducts } = await supabase
|
||||
.from('products_history')
|
||||
.select('product_id');
|
||||
const historyIds = new Set((historyProducts || []).map(h => h.product_id));
|
||||
|
||||
const manuallyEditedProducts = new Map();
|
||||
(existingProducts || []).forEach(p => {
|
||||
const isProtected = (p.status && p.status !== 'excel') || historyIds.has(p.product_id);
|
||||
if (isProtected) {
|
||||
manuallyEditedProducts.set(p.product_id, p);
|
||||
}
|
||||
});
|
||||
|
||||
const editableColumns = resolveEditableColumns(headers);
|
||||
const existingProductMap = new Map((existingProducts || []).map(p => [p.product_id, p]));
|
||||
const productsToUpsert = [];
|
||||
|
||||
for (const row of rows) {
|
||||
const productId = String(row[articleNoIdx]);
|
||||
if (!productId || productId.trim() === '') continue;
|
||||
|
||||
const manuallyEdited = manuallyEditedProducts.get(productId);
|
||||
const existing = existingProductMap.get(productId);
|
||||
const dbRowData = existing?.data;
|
||||
|
||||
if (manuallyEdited) {
|
||||
const dbRowData = manuallyEdited.data;
|
||||
if (dbRowData && Array.isArray(dbRowData)) {
|
||||
const mergedData = [...dbRowData];
|
||||
while (mergedData.length < row.length) {
|
||||
mergedData.push(null);
|
||||
}
|
||||
let hasChange = false;
|
||||
for (let i = 0; i < row.length; i++) {
|
||||
if (i >= 100) continue; // Protect virtual columns
|
||||
if (editableColumns.has(i)) continue; // Protect editable columns
|
||||
if (mergedData[i] !== row[i]) {
|
||||
mergedData[i] = row[i];
|
||||
hasChange = true;
|
||||
}
|
||||
}
|
||||
if (hasChange) {
|
||||
productsToUpsert.push({
|
||||
product_id: productId,
|
||||
data: mergedData,
|
||||
status: manuallyEdited.status || 'edited',
|
||||
updated_at: new Date().toISOString()
|
||||
});
|
||||
if (dbRowData && Array.isArray(dbRowData)) {
|
||||
const mergedData = [...row];
|
||||
while (mergedData.length < dbRowData.length) {
|
||||
mergedData.push(null);
|
||||
}
|
||||
|
||||
for (let i = 100; i < dbRowData.length; i++) {
|
||||
const internalValue = dbRowData[i];
|
||||
if (internalValue !== undefined && internalValue !== null && internalValue !== '') {
|
||||
mergedData[i] = internalValue;
|
||||
}
|
||||
}
|
||||
|
||||
productsToUpsert.push({
|
||||
product_id: productId,
|
||||
data: mergedData,
|
||||
status: existing?.status || 'excel',
|
||||
updated_at: new Date().toISOString()
|
||||
});
|
||||
} else {
|
||||
// Normal excel sync
|
||||
productsToUpsert.push({
|
||||
|
||||
Reference in New Issue
Block a user