- Modify import scripts to handle edge cases with empty arrays and null conditions
- Improve parameter handling in incremental update queries for purchase orders and products
This commit is contained in:
@@ -222,10 +222,10 @@ async function importOrders(prodConnection, localConnection, incrementalUpdate =
|
||||
|
||||
// Pre-check all products at once instead of per batch
|
||||
const allOrderPids = [...new Set(orderItems.map(item => item.pid))];
|
||||
const [existingProducts] = await localConnection.query(
|
||||
const [existingProducts] = allOrderPids.length > 0 ? await localConnection.query(
|
||||
"SELECT pid FROM products WHERE pid IN (?)",
|
||||
[allOrderPids]
|
||||
);
|
||||
) : [[]];
|
||||
const existingPids = new Set(existingProducts.map(p => p.pid));
|
||||
|
||||
// Process in larger batches
|
||||
@@ -312,8 +312,15 @@ async function importOrders(prodConnection, localConnection, incrementalUpdate =
|
||||
// Import missing products if any
|
||||
if (missingProducts.size > 0) {
|
||||
try {
|
||||
// Setup temporary tables again since they were dropped
|
||||
await setupTemporaryTables(localConnection);
|
||||
await materializeCalculations(prodConnection, localConnection);
|
||||
|
||||
await importMissingProducts(prodConnection, localConnection, Array.from(missingProducts));
|
||||
|
||||
// Clean up temporary tables after missing products import
|
||||
await cleanupTemporaryTables(localConnection);
|
||||
|
||||
// Retry skipped orders after importing products
|
||||
if (skippedOrders.size > 0) {
|
||||
outputProgress({
|
||||
@@ -322,7 +329,8 @@ async function importOrders(prodConnection, localConnection, incrementalUpdate =
|
||||
message: `Retrying import of ${skippedOrders.size} orders with previously missing products`
|
||||
});
|
||||
|
||||
const [skippedProdOrders] = await prodConnection.query(`
|
||||
const skippedOrdersArray = Array.from(skippedOrders);
|
||||
const [skippedProdOrders] = skippedOrdersArray.length > 0 ? await prodConnection.query(`
|
||||
SELECT
|
||||
o.order_id,
|
||||
CASE
|
||||
@@ -353,7 +361,7 @@ async function importOrders(prodConnection, localConnection, incrementalUpdate =
|
||||
FROM order_items oi
|
||||
JOIN _order o ON oi.order_id = o.order_id
|
||||
WHERE o.order_id IN (?)
|
||||
`, [Array.from(skippedOrders)]);
|
||||
`, [skippedOrdersArray]) : [[]];
|
||||
|
||||
// Prepare values for insertion
|
||||
const skippedOrderValues = skippedProdOrders.flatMap(order => {
|
||||
@@ -420,7 +428,7 @@ async function importOrders(prodConnection, localConnection, incrementalUpdate =
|
||||
}
|
||||
}
|
||||
|
||||
// Update sync status - do this even if missing products import fails
|
||||
// Only update sync status if we get here (no errors thrown)
|
||||
await localConnection.query(`
|
||||
INSERT INTO sync_status (table_name, last_sync_timestamp)
|
||||
VALUES ('orders', NOW())
|
||||
|
||||
Reference in New Issue
Block a user