Get product import started

This commit is contained in:
2025-01-25 15:44:52 -05:00
parent c1cf78973d
commit 2d52d7a63e

View File

@@ -465,6 +465,7 @@ async function importProducts(prodConnection, localConnection) {
console.log(`Inserting ${batch.length} products`);
// First insert the products
const values = batch.flatMap(r => [
r.product_id, r.title, r.description, r.SKU, r.created_at, r.first_received,
r.stock_quantity, r.preorder_count, r.notions_inv_count, r.price, r.regular_price,
@@ -477,10 +478,14 @@ async function importProducts(prodConnection, localConnection) {
r.baskets, r.notifies, r.date_last_sold
]);
// Create a constant for the SQL query to improve readability
const insertProductsSQL = `
INSERT INTO products (
product_id, title, description, SKU, created_at, first_received,
// Get the number of columns from the first row's values
const columnsPerRow = values.length / batch.length;
const placeholders = batch.map(() =>
`(${Array(columnsPerRow).fill('?').join(',')})`
).join(',');
const sql = `INSERT INTO products (
pid, title, description, SKU, created_at, first_received,
stock_quantity, preorder_count, notions_inv_count, price, regular_price,
cost_price, landing_cost_price, barcode, harmonized_tariff_code,
updated_at, visible, managing_stock, replenishable, vendor,
@@ -490,7 +495,7 @@ async function importProducts(prodConnection, localConnection) {
width, height, country_of_origin, location, total_sold,
baskets, notifies, date_last_sold
)
VALUES ${batch.map(() => '(' + '?,'.repeat(45).slice(0,-1) + ')').join(',')}
VALUES ${placeholders}
ON DUPLICATE KEY UPDATE
title = VALUES(title),
description = VALUES(description),
@@ -534,7 +539,33 @@ async function importProducts(prodConnection, localConnection) {
notifies = VALUES(notifies),
date_last_sold = VALUES(date_last_sold)`;
await localConnection.query(insertProductsSQL, values);
await localConnection.query(sql, values);
// Then insert the category relationships for this batch
const categoryPromises = batch.map(async product => {
// Get the category IDs from the production query
const [categoryRows] = await prodConnection.query(`
SELECT DISTINCT cat_id
FROM product_category_index
WHERE pid = ? AND cat_id IN (
SELECT cat_id FROM product_categories WHERE hidden = 0
)
`, [product.product_id]);
return categoryRows.map(row => [row.cat_id, product.product_id]);
});
const categoryResults = await Promise.all(categoryPromises);
const categoryRelationships = categoryResults.flat();
if (categoryRelationships.length > 0) {
const catPlaceholders = categoryRelationships.map(() => '(?, ?)').join(',');
await localConnection.query(`
INSERT INTO product_categories (cat_id, pid)
VALUES ${catPlaceholders}
ON DUPLICATE KEY UPDATE cat_id = VALUES(cat_id)
`, categoryRelationships.flat());
}
current += batch.length;
updateProgress(current, total, 'Products import', startTime);