From 2d52d7a63e7cb694b49f7b9803f39d2e0ee6cfd3 Mon Sep 17 00:00:00 2001 From: Matt Date: Sat, 25 Jan 2025 15:44:52 -0500 Subject: [PATCH] Get product import started --- inventory-server/scripts/import-from-prod.js | 145 +++++++++++-------- 1 file changed, 88 insertions(+), 57 deletions(-) diff --git a/inventory-server/scripts/import-from-prod.js b/inventory-server/scripts/import-from-prod.js index a731392..8248141 100644 --- a/inventory-server/scripts/import-from-prod.js +++ b/inventory-server/scripts/import-from-prod.js @@ -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,64 +478,94 @@ 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, - 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, - vendor_reference, notions_reference, permalink, - image, image_175, image_full, brand, line, subline, artist, - options, tags, moq, uom, rating, reviews, weight, length, - width, height, country_of_origin, location, total_sold, - baskets, notifies, date_last_sold - ) - VALUES ${batch.map(() => '(' + '?,'.repeat(45).slice(0,-1) + ')').join(',')} - ON DUPLICATE KEY UPDATE - title = VALUES(title), - description = VALUES(description), - stock_quantity = VALUES(stock_quantity), - preorder_count = VALUES(preorder_count), - notions_inv_count = VALUES(notions_inv_count), - price = VALUES(price), - regular_price = VALUES(regular_price), - cost_price = VALUES(cost_price), - landing_cost_price = VALUES(landing_cost_price), - barcode = VALUES(barcode), - harmonized_tariff_code = VALUES(harmonized_tariff_code), - updated_at = VALUES(updated_at), - visible = VALUES(visible), - replenishable = VALUES(replenishable), - vendor = VALUES(vendor), - vendor_reference = VALUES(vendor_reference), - notions_reference = VALUES(notions_reference), - permalink = VALUES(permalink), - image = VALUES(image), - image_175 = VALUES(image_175), - image_full = VALUES(image_full), - brand = VALUES(brand), - line = VALUES(line), - subline = VALUES(subline), - artist = VALUES(artist), - options = VALUES(options), - tags = VALUES(tags), - moq = VALUES(moq), - uom = VALUES(uom), - rating = VALUES(rating), - reviews = VALUES(reviews), - weight = VALUES(weight), - length = VALUES(length), - width = VALUES(width), - height = VALUES(height), - country_of_origin = VALUES(country_of_origin), - location = VALUES(location), - total_sold = VALUES(total_sold), - baskets = VALUES(baskets), - notifies = VALUES(notifies), - date_last_sold = VALUES(date_last_sold)`; + // 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(','); - await localConnection.query(insertProductsSQL, values); + 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, + vendor_reference, notions_reference, permalink, + image, image_175, image_full, brand, line, subline, artist, + options, tags, moq, uom, rating, reviews, weight, length, + width, height, country_of_origin, location, total_sold, + baskets, notifies, date_last_sold + ) + VALUES ${placeholders} + ON DUPLICATE KEY UPDATE + title = VALUES(title), + description = VALUES(description), + stock_quantity = VALUES(stock_quantity), + preorder_count = VALUES(preorder_count), + notions_inv_count = VALUES(notions_inv_count), + price = VALUES(price), + regular_price = VALUES(regular_price), + cost_price = VALUES(cost_price), + landing_cost_price = VALUES(landing_cost_price), + barcode = VALUES(barcode), + harmonized_tariff_code = VALUES(harmonized_tariff_code), + updated_at = VALUES(updated_at), + visible = VALUES(visible), + replenishable = VALUES(replenishable), + vendor = VALUES(vendor), + vendor_reference = VALUES(vendor_reference), + notions_reference = VALUES(notions_reference), + permalink = VALUES(permalink), + image = VALUES(image), + image_175 = VALUES(image_175), + image_full = VALUES(image_full), + brand = VALUES(brand), + line = VALUES(line), + subline = VALUES(subline), + artist = VALUES(artist), + options = VALUES(options), + tags = VALUES(tags), + moq = VALUES(moq), + uom = VALUES(uom), + rating = VALUES(rating), + reviews = VALUES(reviews), + weight = VALUES(weight), + length = VALUES(length), + width = VALUES(width), + height = VALUES(height), + country_of_origin = VALUES(country_of_origin), + location = VALUES(location), + total_sold = VALUES(total_sold), + baskets = VALUES(baskets), + notifies = VALUES(notifies), + date_last_sold = VALUES(date_last_sold)`; + + 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);