fix(prestashop): poll orders every minute in release 1.1.7
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const Module = require('node:module');
|
||||
const path = require('node:path');
|
||||
|
||||
function loadImporter(Core, PsOrder) {
|
||||
const importerPath = path.resolve(__dirname, '../src/OrderImporter.js');
|
||||
const originalLoad = Module._load;
|
||||
Module._load = function(request, parent, isMain) {
|
||||
if (request === '@orbito/api-lib') return { Core, Log: { debug() {}, info() {}, warn() {}, error() {} } };
|
||||
if (request === './models/PsOrder') return PsOrder;
|
||||
return originalLoad.call(this, request, parent, isMain);
|
||||
};
|
||||
delete require.cache[importerPath];
|
||||
try {
|
||||
return require(importerPath);
|
||||
} finally {
|
||||
Module._load = originalLoad;
|
||||
}
|
||||
}
|
||||
|
||||
test('order import upserts verified metadata for the ordered product and pack items', async () => {
|
||||
const upserts = [];
|
||||
const Core = {
|
||||
getModuleInfo: () => ({ code: 'core/prestashop' }),
|
||||
callModule: async (module, method, params) => {
|
||||
if (module === 'core/orders' && method === 'status.search') return { results: [{ _id: '000000000000000000000001', positionInGroup: 1 }] };
|
||||
if (module === 'core/products' && method === 'upsert') {
|
||||
upserts.push(params);
|
||||
return {};
|
||||
}
|
||||
if (module === 'core/orders' && method === 'create') return { _id: 'order-1' };
|
||||
throw new Error(`Unexpected core call: ${module}.${method}`);
|
||||
},
|
||||
};
|
||||
const PsOrder = { findOne: async () => null, update: async () => {} };
|
||||
const { importFromPSOrder } = loadImporter(Core, PsOrder);
|
||||
const order = {
|
||||
id: '1', reference: 'PS-1', current_state: '1', id_customer: '5', id_address_delivery: '6',
|
||||
id_currency: '1', id_carrier: '0', payment: 'Card', date_add: '2026-01-01 10:00:00',
|
||||
total_discounts: '0', total_paid_real: '0', total_shipping_tax_excl: '0',
|
||||
associations: {
|
||||
order_rows: [{
|
||||
product_id: '10', product_attribute_id: '100', product_name: 'Pack', product_reference: 'PACK-V',
|
||||
product_ean13: '', unit_price_tax_excl: '10', unit_price_tax_incl: '12.3', product_quantity: '1',
|
||||
}],
|
||||
},
|
||||
};
|
||||
const products = {
|
||||
'10': {
|
||||
id: '10', name: 'Pack', reference: 'PACK-BASE', manufacturer_name: 'Pack Brand', location: 'BASE-BIN',
|
||||
weight: '1', type: 'pack', associations: { product_bundle: [{ id: '20', id_product_attribute: '200', quantity: '2' }] },
|
||||
},
|
||||
'20': {
|
||||
id: '20', name: 'Item', reference: 'ITEM-BASE', manufacturer_name: 'Item Brand', location: 'ITEM-BASE-BIN',
|
||||
weight: '2', type: 'standard', associations: {},
|
||||
},
|
||||
};
|
||||
const combinations = {
|
||||
'100': { id: '100', id_product: '10', reference: 'PACK-V', weight: '1.5', location: 'PACK-V-BIN' },
|
||||
'200': { id: '200', id_product: '20', reference: 'ITEM-V', weight: '2.5', location: 'ITEM-V-BIN' },
|
||||
};
|
||||
const api = {
|
||||
isBLSupported: async () => false,
|
||||
makeRequest: async endpoint => {
|
||||
if (endpoint === 'orders/1') return { order };
|
||||
if (endpoint === 'customers/5') return { customer: { firstname: 'Ada', lastname: 'Lovelace', email: 'ada@test' } };
|
||||
if (endpoint === 'addresses/6') return { address: { firstname: 'Ada', lastname: 'Lovelace', address1: 'Street 1', city: 'Warsaw', postcode: '00-001', id_country: '1' } };
|
||||
throw new Error(`Unexpected API request: ${endpoint}`);
|
||||
},
|
||||
getProduct: async id => products[id],
|
||||
getProductCombination: async id => combinations[id],
|
||||
getProductCoverImageUrl: async () => null,
|
||||
getCurrency: async () => ({ iso_code: 'PLN' }),
|
||||
getCountry: async () => ({ iso_code: 'PL' }),
|
||||
getStockAvailabilities: async () => [],
|
||||
getManufacturer: async () => null,
|
||||
};
|
||||
await importFromPSOrder('1', api, { _id: 'default', orderStatus: '000000000000000000000001', statusMapping: [] });
|
||||
|
||||
assert.equal(upserts.length, 2);
|
||||
assert.deepEqual(upserts.find(product => product.sourceId === '10'), {
|
||||
sourceModule: 'core/prestashop', sourceId: '10', sku: 'PACK-V', name: 'Pack', unitPrice: 10,
|
||||
ean: '', brand: 'Pack Brand', warehouseLocation: 'PACK-V-BIN',
|
||||
});
|
||||
assert.deepEqual(upserts.find(product => product.sourceId === '20'), {
|
||||
sourceModule: 'core/prestashop', sourceId: '20', sku: 'ITEM-V', name: 'Item',
|
||||
brand: 'Item Brand', warehouseLocation: 'ITEM-V-BIN',
|
||||
});
|
||||
});
|
||||
@@ -80,3 +80,30 @@ test('preserves the explicitly allowed product partial-success compatibility pat
|
||||
|
||||
assert.deepEqual(await api.makeRequest('products/1', 'GET', null, true), { product: { id: 1 } });
|
||||
});
|
||||
|
||||
test('looks up combinations and stock locations with exact product filters', async t => {
|
||||
const originalFetch = Core.fetch;
|
||||
t.after(() => { Core.fetch = originalFetch; });
|
||||
const urls = [];
|
||||
Core.fetch = async url => {
|
||||
urls.push(url);
|
||||
if (url.includes('combinations')) return response(200, { combinations: [{ id: '10' }] });
|
||||
return response(200, { stock_availables: [{ id: '20' }] });
|
||||
};
|
||||
const api = new PrestashopApi('https://shop.example', 'secret', true, { maxGetAttempts: 1 });
|
||||
|
||||
assert.deepEqual(await api.findProductCombinationsByReference('1', 'SKU-1'), [{ id: '10' }]);
|
||||
assert.deepEqual(await api.getStockAvailabilities('1', '10'), [{ id: '20' }]);
|
||||
assert.match(urls[0], /filter%5Bid_product%5D=%5B1%5D/);
|
||||
assert.match(urls[0], /filter%5Breference%5D=%5BSKU-1%5D/);
|
||||
assert.match(urls[1], /filter%5Bid_product_attribute%5D=%5B10%5D/);
|
||||
});
|
||||
|
||||
test('does not turn an unsafe reference filter into an empty combination match', async t => {
|
||||
const originalFetch = Core.fetch;
|
||||
t.after(() => { Core.fetch = originalFetch; });
|
||||
Core.fetch = async () => assert.fail('must not issue a broadened combination lookup');
|
||||
const api = new PrestashopApi('https://shop.example', 'secret', true, { maxGetAttempts: 1 });
|
||||
|
||||
assert.equal(await api.findProductCombinationsByReference('1', 'SKU[1]'), null);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,209 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const Module = require('node:module');
|
||||
const path = require('node:path');
|
||||
|
||||
function loadMethod({ onGetProduct, onFindCombinations, onManufacturer, onStocks }) {
|
||||
const methodPath = path.resolve(__dirname, '../methods/product/get_metadata.js');
|
||||
const originalLoad = Module._load;
|
||||
Module._load = function(request, parent, isMain) {
|
||||
if (request === '../../src/PrestaApi') {
|
||||
return class PrestashopApi {
|
||||
async getProduct(id) { return onGetProduct(id); }
|
||||
async findProductCombinationsByReference(id, sku) { return onFindCombinations(id, sku); }
|
||||
async getManufacturer(id) { return onManufacturer(id); }
|
||||
async getStockAvailabilities(id, attributeId) { return onStocks(id, attributeId); }
|
||||
};
|
||||
}
|
||||
if (request === '../../src/utils') return { isLegacyApi: () => false };
|
||||
if (request === '../../src/module-config') {
|
||||
return { loadConfig: async () => ({ apiUrl: 'https://shop.test', apiKey: 'key' }), isConfigured: () => true };
|
||||
}
|
||||
if (request === '../../src/register-permissions') {
|
||||
return { PERMISSIONS: { ORDERS_VIEW: 'orders.view' }, requirePermission() {} };
|
||||
}
|
||||
return originalLoad.call(this, request, parent, isMain);
|
||||
};
|
||||
delete require.cache[methodPath];
|
||||
delete require.cache[path.resolve(__dirname, '../src/product-metadata.js')];
|
||||
try {
|
||||
return require(methodPath);
|
||||
} finally {
|
||||
Module._load = originalLoad;
|
||||
}
|
||||
}
|
||||
|
||||
test('metadata lookup deduplicates source/SKU pairs and preserves confirmed empty values', async () => {
|
||||
const calls = [];
|
||||
const method = loadMethod({
|
||||
onGetProduct: async id => ({
|
||||
id,
|
||||
reference: id === '1' ? 'base-1' : 'base-2',
|
||||
id_manufacturer: id === '1' ? '8' : '0',
|
||||
location: id === '1' ? 'A-01' : '',
|
||||
}),
|
||||
onFindCombinations: async () => [],
|
||||
onManufacturer: async id => ({ id, name: 'Acme' }),
|
||||
onStocks: async () => [],
|
||||
});
|
||||
|
||||
const result = await method({ products: [
|
||||
{ sourceId: '1', sku: 'base-1' },
|
||||
{ sourceId: '1', sku: 'base-1' },
|
||||
{ sourceId: '1', sku: 'BASE-1' },
|
||||
{ sourceId: '2', sku: 'base-2' },
|
||||
] }, {});
|
||||
|
||||
assert.equal(result.results.length, 2);
|
||||
assert.deepEqual(result.results.find(item => item.sourceId === '1'), {
|
||||
sourceId: '1', sku: 'base-1', brand: 'Acme', warehouseLocation: 'A-01',
|
||||
});
|
||||
assert.deepEqual(result.results.find(item => item.sourceId === '2'), {
|
||||
sourceId: '2', sku: 'base-2', brand: '', warehouseLocation: '',
|
||||
});
|
||||
assert.equal(calls.length, 0);
|
||||
});
|
||||
|
||||
test('metadata lookup returns a variant only for exactly one verified reference match', async () => {
|
||||
const method = loadMethod({
|
||||
onGetProduct: async id => ({ id, reference: 'base', manufacturer_name: 'Brand', location: 'BASE' }),
|
||||
onFindCombinations: async () => [
|
||||
{ id: '10', id_product: '1', reference: 'variant', location: 'BIN-1' },
|
||||
],
|
||||
onManufacturer: async () => null,
|
||||
onStocks: async () => [],
|
||||
});
|
||||
|
||||
const result = await method({ products: [{ sourceId: '1', sku: ' VaRiAnT ' }] }, {});
|
||||
assert.deepEqual(result, {
|
||||
results: [{ sourceId: '1', sku: 'VaRiAnT', brand: 'Brand', warehouseLocation: 'BIN-1' }],
|
||||
});
|
||||
});
|
||||
|
||||
test('metadata lookup omits synthetic SKUs and ambiguous variant matches', async () => {
|
||||
let requestedCombinations = 0;
|
||||
const method = loadMethod({
|
||||
onGetProduct: async id => ({ id, reference: 'base', manufacturer_name: 'Brand', location: 'BASE' }),
|
||||
onFindCombinations: async () => {
|
||||
requestedCombinations += 1;
|
||||
return [
|
||||
{ id: '10', id_product: '1', reference: 'variant', location: 'BIN-1' },
|
||||
{ id: '11', id_product: '1', reference: 'VARIANT', location: 'BIN-2' },
|
||||
];
|
||||
},
|
||||
onManufacturer: async () => null,
|
||||
onStocks: async () => [],
|
||||
});
|
||||
|
||||
const result = await method({ products: [
|
||||
{ sourceId: '1', sku: '~:core/prestashop:1' },
|
||||
{ sourceId: '1', sku: 'variant' },
|
||||
] }, {});
|
||||
|
||||
assert.deepEqual(result, { results: [] });
|
||||
assert.equal(requestedCombinations, 1);
|
||||
});
|
||||
|
||||
test('stock fallback only accepts a unique or default-shop record', async () => {
|
||||
const { chooseStockLocation } = require('../src/product-metadata');
|
||||
const product = { id: '1', id_shop_default: '2' };
|
||||
|
||||
assert.equal(chooseStockLocation([
|
||||
{ id_product: '1', id_product_attribute: '0', id_shop: '1', location: 'OTHER' },
|
||||
{ id_product: '1', id_product_attribute: '0', id_shop: '2', location: 'DEFAULT' },
|
||||
], product, 0).location, 'DEFAULT');
|
||||
assert.equal(chooseStockLocation([
|
||||
{ id_product: '1', id_product_attribute: '0', id_shop: '1', location: 'A' },
|
||||
{ id_product: '1', id_product_attribute: '0', id_shop: '3', location: 'B' },
|
||||
], product, 0).status, 'ambiguous');
|
||||
});
|
||||
|
||||
test('stock availability is authoritative over stale legacy product location, including a confirmed empty value', async () => {
|
||||
const { resolveProductMetadata } = require('../src/product-metadata');
|
||||
const api = {
|
||||
getManufacturer: async () => null,
|
||||
getStockAvailabilities: async () => [{
|
||||
id_product: '1', id_product_attribute: '0', id_shop: '1', location: 'A2',
|
||||
}],
|
||||
};
|
||||
const product = { id: '1', id_shop_default: '1', reference: 'sku', manufacturer_name: 'Brand', location: 'STALE' };
|
||||
|
||||
assert.deepEqual(await resolveProductMetadata(api, product, { sku: 'SKU' }), {
|
||||
brand: 'Brand', warehouseLocation: 'A2',
|
||||
});
|
||||
|
||||
api.getStockAvailabilities = async () => [{
|
||||
id_product: '1', id_product_attribute: '0', id_shop: '1', location: '',
|
||||
}];
|
||||
assert.deepEqual(await resolveProductMetadata(api, product, { sku: 'SKU' }), {
|
||||
brand: 'Brand', warehouseLocation: '',
|
||||
});
|
||||
});
|
||||
|
||||
test('a missing ordered combination never falls back to the base product metadata', async () => {
|
||||
const { resolveProductMetadata } = require('../src/product-metadata');
|
||||
const api = {
|
||||
getManufacturer: async () => null,
|
||||
getStockAvailabilities: async () => [{ id_product: '1', id_product_attribute: '0', location: 'BASE' }],
|
||||
};
|
||||
const product = { id: '1', reference: 'SKU', manufacturer_name: 'Brand', location: 'LEGACY' };
|
||||
|
||||
assert.deepEqual(await resolveProductMetadata(api, product, {
|
||||
sku: 'SKU', requiresCombination: true,
|
||||
}), {});
|
||||
});
|
||||
|
||||
test('failed or ambiguous stock lookups do not clear metadata through an empty legacy location', async () => {
|
||||
const { resolveProductMetadata } = require('../src/product-metadata');
|
||||
const product = { id: '1', reference: 'SKU', manufacturer_name: 'Brand', location: '' };
|
||||
const failedStockApi = {
|
||||
getManufacturer: async () => null,
|
||||
getStockAvailabilities: async () => { throw new Error('forbidden'); },
|
||||
};
|
||||
|
||||
const baseResult = await resolveProductMetadata(failedStockApi, product, { sku: 'SKU' });
|
||||
assert.deepEqual(baseResult, { brand: 'Brand' });
|
||||
assert.equal(Object.hasOwn(baseResult, 'warehouseLocation'), false);
|
||||
|
||||
const variantResult = await resolveProductMetadata(failedStockApi, product, {
|
||||
sku: 'VARIANT',
|
||||
combination: { id: '10', id_product: '1', reference: 'VARIANT', location: '' },
|
||||
});
|
||||
assert.deepEqual(variantResult, { brand: 'Brand' });
|
||||
assert.equal(Object.hasOwn(variantResult, 'warehouseLocation'), false);
|
||||
|
||||
const ambiguousStockApi = {
|
||||
getManufacturer: async () => null,
|
||||
getStockAvailabilities: async () => [
|
||||
{ id_product: '1', id_product_attribute: '0', id_shop: '2', location: 'A' },
|
||||
{ id_product: '1', id_product_attribute: '0', id_shop: '3', location: 'B' },
|
||||
],
|
||||
};
|
||||
const ambiguousResult = await resolveProductMetadata(ambiguousStockApi, product, { sku: 'SKU' });
|
||||
assert.equal(Object.hasOwn(ambiguousResult, 'warehouseLocation'), false);
|
||||
});
|
||||
|
||||
test('metadata lookup skips a base match when the variation reference cannot be filtered safely', async () => {
|
||||
const { findMetadataBySourceAndSku } = require('../src/product-metadata');
|
||||
const api = {
|
||||
getProduct: async id => ({ id, reference: 'SKU[1]', manufacturer_name: 'Brand' }),
|
||||
findProductCombinationsByReference: async () => null,
|
||||
};
|
||||
|
||||
assert.equal(await findMetadataBySourceAndSku(api, { sourceId: '1', sku: 'SKU[1]' }), null);
|
||||
});
|
||||
|
||||
test('metadata lookup skips combination API when product declares no combinations', async () => {
|
||||
const method = loadMethod({
|
||||
onGetProduct: async id => ({
|
||||
id, reference: 'base', manufacturer_name: 'Brand', location: 'LEGACY', associations: { combinations: [] },
|
||||
}),
|
||||
onFindCombinations: async () => assert.fail('must not request combinations for a known empty association'),
|
||||
onManufacturer: async () => null,
|
||||
onStocks: async () => [],
|
||||
});
|
||||
|
||||
assert.deepEqual(await method({ products: [{ sourceId: '1', sku: 'base' }] }, {}), {
|
||||
results: [{ sourceId: '1', sku: 'base', brand: 'Brand', warehouseLocation: 'LEGACY' }],
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user