fix: harden PrestaShop automation jobs

This commit is contained in:
fatequod
2026-08-03 11:24:03 +02:00
parent 3480a325a0
commit cb9bae552a
12 changed files with 805 additions and 248 deletions
+23 -24
View File
@@ -1,6 +1,7 @@
const { Log, Core, DB } = require("@orbito/api-lib");
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');
function getPSMultiLangField(param, langId) {
if (Array.isArray(param)) {
@@ -46,8 +47,7 @@ async function importFromPSOrder(orderId, api, config) {
Log.debug( "Fetching order details", { endpoint: endpoint + '/' + orderId });
let { order } = await api.makeRequest(endpoint + '/' + orderId, 'GET', { output_format: "JSON" });
if (!order) {
Log.error( "Order not found", orderId);
return;
throw new Error(`PrestaShop order ${orderId} not found`);
}
Log.info( "Importing order: " + order.reference);
@@ -136,18 +136,13 @@ async function importFromPSOrder(orderId, api, config) {
const fallbackStatusId = await resolveImportStatusId(config);
if (!fallbackStatusId) {
Log.error( 'Cannot import order — no valid order status configured', { orderId });
return;
throw new Error(`Cannot import order ${orderId} — no valid order status configured`);
}
const statusMapping = Array.isArray(config.statusMapping) ? config.statusMapping : [];
const statusId = resolveStatusId(order.current_state, statusMapping, fallbackStatusId);
if (!statusId) {
Log.error( 'Cannot import order — could not resolve status', {
orderId,
psStateId: order.current_state
});
return;
throw new Error(`Cannot import order ${orderId} — could not resolve status for PS state ${order.current_state}`);
}
Log.info( 'Resolved order status', {
@@ -247,6 +242,7 @@ async function importFromPSOrder(orderId, api, config) {
amount: Math.round(totalPaidReal * 100) / 100,
method: order.payment || 'PrestaShop',
date: new Date(order.date_add),
transactionId: paymentTransactionId(config, order.id, totalPaidReal),
});
}
@@ -301,21 +297,24 @@ async function importFromPSOrder(orderId, api, config) {
createdAt: new Date(order.date_add),
});
if (coreOrder?._id) {
const alreadyPaid = totalPaidReal > 0 && payments.length > 0;
try {
await PsOrder.create({
shopConfigId: config._id,
psOrderId: parseInt(order.id),
orbitoOrderId: coreOrder._id,
reference: order.reference || '',
syncedAt: new Date(),
paymentSyncedAt: alreadyPaid ? new Date() : undefined,
});
} catch (e) {
Log.warn( 'PS order mapping save failed', { psOrderId: order.id, error: e.message });
}
if (!coreOrder?._id) {
throw new Error(`core/orders create did not return an id for PrestaShop order ${order.id}`);
}
const now = new Date();
await upsertMapping(PsOrder, {
shopConfigId: config._id,
psOrderId: parseInt(order.id),
orbitoOrderId: coreOrder._id,
reference: order.reference || '',
syncedAt: parsePrestashopDate(order.date_add, now),
paymentTotalSynced: totalPaidReal,
paymentSyncedAt: totalPaidReal > 0 ? now : undefined,
paymentCheckedAt: now,
nextPaymentCheckAt: now,
});
return { status: 'imported', psOrderId: Number(order.id), orbitoOrderId: coreOrder._id };
}
module.exports = { importFromPSOrder };