diff --git a/module.json b/module.json index 43ff9f7..e016bf0 100644 --- a/module.json +++ b/module.json @@ -1,6 +1,6 @@ { "name": "core/prestashop", - "version": "1.1.12", + "version": "1.1.13", "singleton": false, "dependsOn": [ "core/orders", diff --git a/package-lock.json b/package-lock.json index 7eda898..2a99258 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "core.prestashop", - "version": "1.1.12", + "version": "1.1.13", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "core.prestashop", - "version": "1.1.12", + "version": "1.1.13", "license": "ISC", "dependencies": { "@orbito/api-lib": "^3.3.3", diff --git a/package.json b/package.json index a6d93c4..f21704a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "core.prestashop", - "version": "1.1.12", + "version": "1.1.13", "description": "Integracja zamówień i płatności PrestaShop", "main": "index.js", "scripts": { diff --git a/src/OrderImporter.js b/src/OrderImporter.js index 8526df3..941ed80 100644 --- a/src/OrderImporter.js +++ b/src/OrderImporter.js @@ -4,7 +4,6 @@ const { normalizeStatusId } = require('./status-mapper'); const { paymentTransactionId, parsePrestashopDate, upsertMapping } = require('./automation-jobs'); const { buildProductSourceId, - parseProductSourceId, resolveProductMetadata, } = require('./product-metadata'); @@ -101,6 +100,9 @@ async function importFromPSOrder(orderId, api, config) { tax: taxRate ? Math.round(taxRate * 100) / 100 : 0, qty: parseFloat(product.product_quantity) }; + // Jednorazowa migracja starej tożsamości produktu odbywa się już po + // stronie core/products przez synchroniczny kontrakt core/orders. + p.previousSourceId = productId; const psProduct = await api.getProduct(productId); Log.debug( "Fetched product details: " + getPSMultiLangField(psProduct.name)); @@ -149,6 +151,7 @@ async function importFromPSOrder(orderId, api, config) { ean: psItem.ean13, qty: parseFloat(packItem.quantity) }; + pData.previousSourceId = packProductId; let packCombination = null; if (packCombinationId !== '0') { @@ -187,32 +190,6 @@ async function importFromPSOrder(orderId, api, config) { products.push(p); } - const productsForUpsert = products.flatMap(product => [product, ...(Array.isArray(product.pack) ? product.pack : [])]); - for (const product of productsForUpsert.filter(item => item.sourceId && item.name)) { - const sourceIdentity = parseProductSourceId(product.sourceId); - const upsert = { - // własny kod modułu bierzemy z core (po rejestracji), nie z hardcode'u - sourceModule: Core.getModuleInfo().code, - sourceId: String(product.sourceId), - previousSourceId: sourceIdentity?.productId || undefined, - sku: product.sku || '', - name: product.name, - }; - if (Object.hasOwn(product, 'baseUnitPrice') && product.baseUnitPrice !== undefined) { - upsert.unitPrice = product.baseUnitPrice; - } - for (const field of ['tax', 'weight', 'ean', 'coverImageUrl', 'brand', 'warehouseLocation']) { - if (Object.hasOwn(product, field) && product[field] !== undefined) upsert[field] = product[field]; - } - await Core.callModule('core/products', 'upsert', upsert).catch(error => { - Log.warn('PrestaShop product catalog upsert failed', { - sourceId: product.sourceId, - sku: product.sku || '', - error: error?.message, - }); - }); - } - const fallbackStatusId = await resolveImportStatusId(config); if (!fallbackStatusId) { throw new Error(`Cannot import order ${orderId} — no valid order status configured`); diff --git a/src/automation-jobs.js b/src/automation-jobs.js index 7e56aa0..e2e5d18 100644 --- a/src/automation-jobs.js +++ b/src/automation-jobs.js @@ -126,6 +126,7 @@ async function importOrdersForShop(config, api, dependencies = {}) { const orders = stableOrders(Array.isArray(response?.orders) ? response.orders : [], now()); let cursor = startingCursor; let processed = 0; + let hasGap = false; for (const order of orders) { if (!isLockValid()) throw new Error('PrestaShop orders lock was lost'); @@ -142,14 +143,13 @@ async function importOrdersForShop(config, api, dependencies = {}) { throw new Error(`Importer returned an invalid result for order ${order.id}`); } } - cursor = Number(order.id); + if (!hasGap) cursor = Number(order.id); processed += 1; } catch (error) { - const previousCursor = cursor; - cursor = Number(order.id); - Log.error('Failed to import PS order; skipping order and continuing', { + hasGap = true; + Log.error('Failed to import PS order; keeping cursor before the gap and continuing', { orderId: order.id, - previousCursor, + previousCursor: cursor, cursor, error: error.message, }); diff --git a/test/automation-jobs.test.js b/test/automation-jobs.test.js index 9df1630..3cec272 100644 --- a/test/automation-jobs.test.js +++ b/test/automation-jobs.test.js @@ -29,7 +29,7 @@ test('first run safely starts at the newest order without backfilling history', assert.deepEqual(result, { status: 'initialized', cursor: 812, processed: 0 }); }); -test('a failed order is skipped so later orders can still be imported', async () => { +test('a failed order leaves a retryable cursor gap while later orders are still inspected', async () => { const writes = []; const imported = []; const errors = []; @@ -49,15 +49,15 @@ test('a failed order is skipped so later orders can still be imported', async () }); assert.deepEqual(imported, [11, 12, 13]); - assert.deepEqual(writes, [13]); - assert.equal(config.lastOrderId, 13); - assert.deepEqual(result, { status: 'processed', cursor: 13, processed: 2 }); + assert.deepEqual(writes, [11]); + assert.equal(config.lastOrderId, 11); + assert.deepEqual(result, { status: 'processed', cursor: 11, processed: 2 }); assert.deepEqual(errors, [{ - message: 'Failed to import PS order; skipping order and continuing', + message: 'Failed to import PS order; keeping cursor before the gap and continuing', details: { orderId: 12, previousCursor: 11, - cursor: 12, + cursor: 11, error: 'invalid order', }, }]); diff --git a/test/order-importer-metadata.test.js b/test/order-importer-metadata.test.js index 2f1c988..7301c9d 100644 --- a/test/order-importer-metadata.test.js +++ b/test/order-importer-metadata.test.js @@ -19,7 +19,7 @@ function loadImporter(Core, PsOrder) { } } -test('order import upserts verified metadata for the ordered product and pack items', async () => { +test('order import passes verified metadata and pack facts only through core/orders', async () => { const upserts = []; let createdOrder = null; const Core = { @@ -95,35 +95,18 @@ test('order import upserts verified metadata for the ordered product and pack it }; await importFromPSOrder('1', api, { _id: 'default', orderStatus: '000000000000000000000001', statusMapping: [] }); - assert.equal(upserts.length, 4); - assert.deepEqual(upserts.find(product => product.sourceId === 'product:10:combination:100'), { - sourceModule: 'core/prestashop', sourceId: 'product:10:combination:100', previousSourceId: '10', - sku: 'PACK-V', name: 'Pack', unitPrice: 9.5, tax: 0.23, weight: 2.5, - ean: '', brand: 'Pack Brand', warehouseLocation: 'PACK-V-BIN', - }); - assert.deepEqual(upserts.find(product => product.sourceId === 'product:20:combination:200'), { - sourceModule: 'core/prestashop', sourceId: 'product:20:combination:200', previousSourceId: '20', - sku: 'ITEM-V', name: 'Item', unitPrice: 3.25, weight: 4.5, - brand: 'Item Brand', warehouseLocation: 'ITEM-V-BIN', - }); - assert.deepEqual( - upserts.filter(product => product.sourceId.startsWith('product:30:')).map(product => product.sourceId), - ['product:30:combination:300', 'product:30:combination:301'] - ); - assert.deepEqual( - upserts.filter(product => product.sourceId.startsWith('product:30:')).map(product => product.sku), - ['VARIANT-BLUE', 'VARIANT-RED'] - ); - assert.deepEqual(upserts.find(product => product.sourceId === 'product:30:combination:300'), { - sourceModule: 'core/prestashop', sourceId: 'product:30:combination:300', previousSourceId: '30', - sku: 'VARIANT-BLUE', name: 'Variant Blue', unitPrice: 17, tax: 0.23, weight: 1.1, - ean: '5901234123457', brand: 'Variant Brand', warehouseLocation: 'BLUE-BIN', - }); - assert.deepEqual(upserts.find(product => product.sourceId === 'product:30:combination:301'), { - sourceModule: 'core/prestashop', sourceId: 'product:30:combination:301', previousSourceId: '30', - sku: 'VARIANT-RED', name: 'Variant Red', unitPrice: 18, tax: 0.23, weight: 1.2, - ean: '5901234123464', brand: 'Variant Brand', warehouseLocation: 'RED-BIN', - }); + assert.equal(upserts.length, 0, 'PrestaShop must not write the catalog beside core/orders'); + assert.equal(createdOrder.products[0].sourceId, 'product:10:combination:100'); + assert.equal(createdOrder.products[0].previousSourceId, '10'); + assert.equal(createdOrder.products[0].sku, 'PACK-V'); + assert.equal(createdOrder.products[0].brand, 'Pack Brand'); + assert.equal(createdOrder.products[0].warehouseLocation, 'PACK-V-BIN'); + assert.equal(createdOrder.products[0].pack[0].sourceId, 'product:20:combination:200'); + assert.equal(createdOrder.products[0].pack[0].previousSourceId, '20'); + assert.equal(createdOrder.products[0].pack[0].baseUnitPrice, 3.25); + assert.deepEqual(createdOrder.products.slice(1).map(product => product.sku), [ + 'VARIANT-BLUE', 'VARIANT-RED', + ]); assert.equal(createdOrder.products[0].unitPrice, 10, 'order keeps its transactional net price'); assert.equal(createdOrder.products[0].baseUnitPrice, 9.5, 'order also carries the catalog base price'); });