fix(prestashop): poll orders every minute in release 1.1.7
This commit is contained in:
+37
-11
@@ -2,6 +2,7 @@ const { Log, Core } = require("@orbito/api-lib");
|
||||
const PsOrder = require('./models/PsOrder');
|
||||
const { resolveStatusId, normalizeStatusId } = require('./status-mapper');
|
||||
const { paymentTransactionId, parsePrestashopDate, upsertMapping } = require('./automation-jobs');
|
||||
const { resolveProductMetadata } = require('./product-metadata');
|
||||
|
||||
function getPSMultiLangField(param, langId) {
|
||||
if (Array.isArray(param)) {
|
||||
@@ -76,14 +77,25 @@ async function importFromPSOrder(orderId, api, config) {
|
||||
if (imgUrl) p.coverImageUrl = imgUrl;
|
||||
} catch (_) { /* no image available */ }
|
||||
|
||||
let productCombination = null;
|
||||
if (parseInt(product.product_attribute_id) > 0) {
|
||||
const combination = await api.getProductCombination(product.product_attribute_id);
|
||||
if (combination) {
|
||||
productCombination = combination;
|
||||
const w = parseFloat(combination.weight);
|
||||
if (!isNaN(w)) p.weight = w;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Object.assign(p, await resolveProductMetadata(api, psProduct, {
|
||||
sourceId: p.sourceId,
|
||||
sku: p.sku,
|
||||
combination: productCombination,
|
||||
requiresCombination: parseInt(product.product_attribute_id) > 0,
|
||||
}));
|
||||
} catch (_) { /* optional product metadata must not block order import */ }
|
||||
|
||||
if (psProduct.type == "pack" && psProduct.associations.product_bundle) {
|
||||
p.isPack = true;
|
||||
p.pack = [];
|
||||
@@ -98,15 +110,26 @@ async function importFromPSOrder(orderId, api, config) {
|
||||
qty: parseFloat(packItem.quantity)
|
||||
};
|
||||
|
||||
let packCombination = null;
|
||||
if (parseInt(packItem.id_product_attribute) > 0) {
|
||||
const combination = await api.getProductCombination(packItem.id_product_attribute);
|
||||
if (combination) {
|
||||
packCombination = combination;
|
||||
const w = parseFloat(combination.weight);
|
||||
if (!isNaN(w)) pData.weight = w;
|
||||
pData.sku = combination.reference || pData.sku;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
Object.assign(pData, await resolveProductMetadata(api, psItem, {
|
||||
sourceId: pData.sourceId,
|
||||
sku: pData.sku,
|
||||
combination: packCombination,
|
||||
requiresCombination: parseInt(packItem.id_product_attribute) > 0,
|
||||
}));
|
||||
} catch (_) { /* optional product metadata must not block order import */ }
|
||||
|
||||
try {
|
||||
const imgUrl = await api.getProductCoverImageUrl(packItem.id);
|
||||
if (imgUrl) pData.coverImageUrl = imgUrl;
|
||||
@@ -119,19 +142,22 @@ async function importFromPSOrder(orderId, api, config) {
|
||||
products.push(p);
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
products
|
||||
.filter(p => p.sourceId && p.name)
|
||||
.map(p => Core.callModule( 'core/products', 'upsert', {
|
||||
const productsForUpsert = products.flatMap(product => [product, ...(Array.isArray(product.pack) ? product.pack : [])]);
|
||||
await Promise.all(productsForUpsert
|
||||
.filter(product => product.sourceId && product.name)
|
||||
.map(product => {
|
||||
const upsert = {
|
||||
// własny kod modułu bierzemy z core (po rejestracji), nie z hardcode'u
|
||||
sourceModule: Core.getModuleInfo().code,
|
||||
sourceId: String(p.sourceId),
|
||||
sku: p.sku || '',
|
||||
name: p.name,
|
||||
unitPrice: p.unitPrice,
|
||||
ean: p.ean,
|
||||
coverImageUrl: p.coverImageUrl || undefined,
|
||||
}).catch(() => {}))
|
||||
sourceId: String(product.sourceId),
|
||||
sku: product.sku || '',
|
||||
name: product.name,
|
||||
};
|
||||
for (const field of ['unitPrice', 'ean', 'coverImageUrl', 'brand', 'warehouseLocation']) {
|
||||
if (Object.hasOwn(product, field) && product[field] !== undefined) upsert[field] = product[field];
|
||||
}
|
||||
return Core.callModule('core/products', 'upsert', upsert).catch(() => {});
|
||||
})
|
||||
);
|
||||
|
||||
const fallbackStatusId = await resolveImportStatusId(config);
|
||||
|
||||
Reference in New Issue
Block a user