feat(prestashop): delegate product ingestion to orders (1.1.13)

This commit is contained in:
fatequod
2026-09-22 10:49:24 +02:00
parent aa5297c5d8
commit 3030e9439c
7 changed files with 32 additions and 72 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "core/prestashop",
"version": "1.1.12",
"version": "1.1.13",
"singleton": false,
"dependsOn": [
"core/orders",
+2 -2
View File
@@ -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",
+1 -1
View File
@@ -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": {
+4 -27
View File
@@ -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`);
+5 -5
View File
@@ -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,
});
+6 -6
View File
@@ -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',
},
}]);
+13 -30
View File
@@ -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');
});