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 };
+65 -19
View File
@@ -4,11 +4,14 @@ const js2xmlparser = require("js2xmlparser");
const NodeCache = require("node-cache");
module.exports = class PrestashopApi {
constructor(shopUrl, apiKey, legacy = true) {
constructor(shopUrl, apiKey, legacy = true, requestOptions = {}) {
this.shopUrl = new URL(shopUrl);
this.apiKey = apiKey;
this.cache = new NodeCache({ stdTTL: 600, checkperiod: 100 });
this.legacy = legacy; // czy używać legacy API (dla PrestaShop < 8)
this.requestTimeoutMs = requestOptions.timeoutMs || 15000;
this.maxGetAttempts = requestOptions.maxGetAttempts || 3;
this.retryBaseMs = requestOptions.retryBaseMs || 250;
}
getUrl(endpoint = "") {
@@ -37,8 +40,9 @@ module.exports = class PrestashopApi {
// @todo przemyśleć czy nie dodac tutaj na sztywno do kazdego zapytania output_format=JSON
let url = this.getUrl(endpoint);
const method = String(metod || 'GET').toUpperCase();
let options = {
method: metod,
method,
headers: {
"Authorization": "Basic " + Buffer.from(this.apiKey + ":").toString("base64"),
'content-type': 'application/xml',
@@ -47,7 +51,7 @@ module.exports = class PrestashopApi {
};
if (data) {
if (metod.toLowerCase() === 'get') {
if (method === 'GET') {
const params = new URLSearchParams(data);
url += (url.includes('?') ? '&' : '?') + params.toString();
} else {
@@ -60,23 +64,58 @@ module.exports = class PrestashopApi {
}
}
try {
let r = await Core.fetch(url, options);
return r.json();
} catch (error) {
// Jeśli allowPartialSuccess jest true i mamy błąd 500, sprawdź czy odpowiedź zawiera użyteczne dane
if (allowPartialSuccess && error.statusCode === 500 && error.response && error.response.body) {
try {
let parsedResponse = JSON.parse(error.response.body);
// Jeśli odpowiedź zawiera dane mimo błędu, zwróć je
if (parsedResponse && typeof parsedResponse === 'object') {
return parsedResponse;
const maxAttempts = method === 'GET' ? this.maxGetAttempts : 1;
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
try {
const response = await this.fetchWithTimeout(url, options);
const body = response.text();
let parsed = {};
if (body) {
try {
parsed = JSON.parse(body);
} catch (parseError) {
const error = new Error(`PrestaShop returned invalid JSON (HTTP ${response.status})`);
error.statusCode = response.status;
throw error;
}
} catch (parseError) {
// Jeśli nie można sparsować odpowiedzi, rzuć oryginalny błąd
}
if (response.status < 200 || response.status >= 300) {
if (allowPartialSuccess && response.status === 500 && parsed && typeof parsed === 'object' && Object.keys(parsed).length > 0) {
Log.warn('PrestaShop returned usable data with HTTP 500', { endpoint });
return parsed;
}
const error = new Error(`PrestaShop request failed with HTTP ${response.status}`);
error.statusCode = response.status;
error.responseBody = body.slice(0, 1000);
throw error;
}
return parsed;
} catch (error) {
const retryableStatus = [408, 425, 429].includes(error.statusCode) || error.statusCode >= 500;
const retryable = method === 'GET' && (retryableStatus || !error.statusCode);
if (!retryable || attempt >= maxAttempts) throw error;
await new Promise(resolve => setTimeout(resolve, this.retryBaseMs * (2 ** (attempt - 1))));
}
throw error;
}
}
async fetchWithTimeout(url, options) {
let timer;
try {
return await Promise.race([
Core.fetch(url, options),
new Promise((_, reject) => {
timer = setTimeout(() => {
const error = new Error(`PrestaShop request timed out after ${this.requestTimeoutMs} ms`);
error.code = 'PRESTASHOP_TIMEOUT';
reject(error);
}, this.requestTimeoutMs);
timer.unref?.();
}),
]);
} finally {
clearTimeout(timer);
}
}
@@ -241,8 +280,13 @@ module.exports = class PrestashopApi {
}
}
// PrestaShop image type (not original). home_default ≈ 250×250 in classic theme.
static PRODUCT_IMAGE_TYPE = 'home_default';
getProductImageUrl(productId, imageId) {
const url = this.getUrl('images/products/' + productId + '/' + imageId);
const url = this.getUrl(
'images/products/' + productId + '/' + imageId + '/' + PrestashopApi.PRODUCT_IMAGE_TYPE
);
const sep = url.includes('?') ? '&' : '?';
return url + sep + 'ws_key=' + encodeURIComponent(this.apiKey);
}
@@ -261,7 +305,9 @@ module.exports = class PrestashopApi {
async getProductImageBuffer(productId, imageId) {
let r = await Request({
url: this.getUrl('images/products/' + productId + '/' + imageId),
url: this.getUrl(
'images/products/' + productId + '/' + imageId + '/' + PrestashopApi.PRODUCT_IMAGE_TYPE
),
encoding: null,
headers : { "Authorization" : "Basic " + Buffer.from(this.apiKey + ":").toString("base64") }
});
+286
View File
@@ -0,0 +1,286 @@
const DEFAULT_PAGE_SIZE = 50;
const DEFAULT_MAX_PAGES = 10;
function money(value) {
const number = Number(value);
return Number.isFinite(number) ? Math.round(number * 100) / 100 : 0;
}
function asOrders(result) {
if (Array.isArray(result)) return result;
if (Array.isArray(result?.data)) return result.data;
if (Array.isArray(result?.results)) return result.results;
return [];
}
function paymentTransactionId(config, psOrderId, cumulativeTotal) {
const shopId = String(config?._id || 'default').replace(/[^a-zA-Z0-9_.-]/g, '_');
return `prestashop:${shopId}:${psOrderId}:paid:${Math.round(money(cumulativeTotal) * 100)}`;
}
function orderUniqueId(order) {
return `${order.reference}:${order.id}`;
}
function parsePrestashopDate(value, fallback = new Date()) {
if (!value) return fallback;
const parsed = new Date(String(value).replace(' ', 'T'));
return Number.isNaN(parsed.getTime()) ? fallback : parsed;
}
async function upsertMapping(PsOrder, data) {
const key = {
shopConfigId: String(data.shopConfigId),
psOrderId: Number(data.psOrderId),
};
const existing = await PsOrder.findOne(key);
const set = {
orbitoOrderId: data.orbitoOrderId,
reference: data.reference || existing?.reference || '',
syncedAt: data.syncedAt || existing?.syncedAt || new Date(),
};
for (const field of ['paymentTotalSynced', 'paymentSyncedAt', 'paymentCheckedAt', 'nextPaymentCheckAt']) {
if (data[field] !== undefined) set[field] = data[field];
}
await PsOrder.update(key, { $set: set }, { upsert: true });
return { ...existing, ...key, ...set };
}
async function ensureExistingOrderMapping({ config, api, psOrderSummary, orbitoOrder, PsOrder }) {
const key = { shopConfigId: String(config._id), psOrderId: Number(psOrderSummary.id) };
const existing = await PsOrder.findOne(key);
if (existing?.orbitoOrderId) return existing;
// A create may have succeeded while saving the mapping failed. Establish a conservative
// baseline from both systems so replay cannot append the same cumulative payment twice.
const psOrder = await api.getOrder(psOrderSummary.id);
if (!psOrder) throw new Error(`PrestaShop order ${psOrderSummary.id} not found while restoring mapping`);
const psPaid = money(psOrder.total_paid_real);
const orbitoPaid = money(orbitoOrder?.summary?.paid);
return upsertMapping(PsOrder, {
...key,
orbitoOrderId: orbitoOrder._id,
reference: psOrderSummary.reference || psOrder.reference || '',
syncedAt: parsePrestashopDate(psOrder.date_add),
paymentTotalSynced: Math.min(psPaid, orbitoPaid),
paymentCheckedAt: new Date(),
nextPaymentCheckAt: new Date(),
});
}
function stableOrders(orders, nowMs = Date.now()) {
const cutoff = nowMs - 30 * 1000;
const newestDate = orders[orders.length - 1]?.date_add;
let offsetMs = 0;
if (newestDate) {
const drift = parsePrestashopDate(newestDate).getTime() - nowMs;
if (drift > 60000) offsetMs = Math.round(drift / 3600000) * 3600000;
}
const stable = [];
for (const order of orders) {
// Only return a contiguous prefix. Skipping a still-changing order and
// advancing to a later id would make that skipped id unreachable by the cursor.
if (parsePrestashopDate(order.date_add).getTime() - offsetMs >= cutoff) break;
stable.push(order);
}
return stable;
}
async function importOrdersForShop(config, api, dependencies = {}) {
const {
Core,
PsOrder,
importFromPSOrder,
setLastOrderId,
Log = console,
now = () => Date.now(),
isLockValid = () => true,
} = dependencies;
if (config.lastOrderId == null || !Number.isFinite(Number(config.lastOrderId))) {
const response = await api.makeRequest('orders', 'GET', {
display: '[id]', sort: 'id_DESC', limit: 1, output_format: 'JSON',
});
const latestId = Number(response?.orders?.[0]?.id);
if (Number.isFinite(latestId)) {
await setLastOrderId(latestId);
config.lastOrderId = latestId;
Log.info('PrestaShop import initialized at the newest order; historical orders were not backfilled', {
shopConfigId: config._id, lastOrderId: latestId,
});
}
return { status: 'initialized', cursor: config.lastOrderId ?? null, processed: 0 };
}
const startingCursor = Number(config.lastOrderId);
const response = await api.makeRequest('orders', 'GET', {
display: '[id, reference, date_add]',
sort: 'id_ASC',
limit: DEFAULT_PAGE_SIZE,
output_format: 'JSON',
'filter[id]': `[${startingCursor + 1},999999999]`,
});
const orders = stableOrders(Array.isArray(response?.orders) ? response.orders : [], now());
let cursor = startingCursor;
let processed = 0;
for (const order of orders) {
if (!isLockValid()) throw new Error('PrestaShop orders lock was lost');
try {
const result = await Core.callModule('core/orders', 'search', {
source: { moduleCode: 'core/prestashop', uniqueId: orderUniqueId(order) },
});
const existingOrder = asOrders(result)[0];
if (existingOrder) {
await ensureExistingOrderMapping({ config, api, psOrderSummary: order, orbitoOrder: existingOrder, PsOrder });
} else {
const importResult = await importFromPSOrder(order.id, api, config);
if (!importResult || !['imported', 'existing'].includes(importResult.status)) {
throw new Error(`Importer returned an invalid result for order ${order.id}`);
}
}
cursor = Number(order.id);
processed += 1;
} catch (error) {
Log.error('Failed to import PS order; cursor remains before this order', {
orderId: order.id, cursor, error: error.message,
});
break;
}
}
if (cursor > startingCursor) {
await setLastOrderId(cursor);
config.lastOrderId = cursor;
}
return { status: 'processed', cursor, processed };
}
function duePaymentsQuery(config, since, until, nowDate) {
const syncedAt = since && until
? { $gte: since, $lt: until }
: (since ? { $gte: since } : { $lt: until });
return {
shopConfigId: String(config._id),
orbitoOrderId: { $exists: true, $ne: null },
syncedAt,
$or: [
{ nextPaymentCheckAt: { $exists: false } },
{ nextPaymentCheckAt: null },
{ nextPaymentCheckAt: { $lte: nowDate } },
],
};
}
async function processPaymentMapping(mapping, config, api, dependencies) {
const { Core, PsOrder, nowDate, nextCheckMs, retryCheckMs, Log = console } = dependencies;
try {
const orderResult = await Core.callModule('core/orders', 'search', { _id: mapping.orbitoOrderId });
const order = asOrders(orderResult)[0];
if (!order) throw new Error(`Orbito order ${mapping.orbitoOrderId} not found`);
const psOrder = await api.getOrder(mapping.psOrderId);
if (!psOrder) throw new Error(`PrestaShop order ${mapping.psOrderId} not found`);
const psPaid = money(psOrder.total_paid_real);
const currentPaid = money(order?.summary?.paid);
const gross = money(order?.summary?.gross);
let recorded = Number(mapping.paymentTotalSynced);
if (!Number.isFinite(recorded)) {
// Legacy rows did not record their PS payment baseline. Never guess that an
// already-present Orbito payment should be appended again.
recorded = Math.min(psPaid, currentPaid);
}
const delta = money(psPaid - recorded);
const remaining = Math.max(0, money(gross - currentPaid));
const amount = Math.min(delta, remaining);
if (amount > 0) {
const transactionId = paymentTransactionId(config, mapping.psOrderId, psPaid);
await Core.callModule('core/orders', 'payments.upsert', {
orderId: String(mapping.orbitoOrderId),
payment: {
amount,
method: psOrder.payment || 'PrestaShop',
date: parsePrestashopDate(psOrder.date_upd || psOrder.date_add, nowDate),
transactionId,
},
});
Log.info('CRON: PS payment delta synced', {
psOrderId: mapping.psOrderId, amount, cumulativeTotal: psPaid, transactionId,
});
}
await PsOrder.update({ _id: mapping._id }, { $set: {
// PrestaShop may temporarily report a lower total during refund/update
// processing. This integration does not create negative payments, so its
// confirmed high-water mark must not move backwards and duplicate money later.
paymentTotalSynced: Math.max(recorded, psPaid),
paymentSyncedAt: nowDate,
paymentCheckedAt: nowDate,
nextPaymentCheckAt: new Date(nowDate.getTime() + nextCheckMs),
} });
return { success: true, amount };
} catch (error) {
await PsOrder.update({ _id: mapping._id }, { $set: {
paymentCheckedAt: nowDate,
nextPaymentCheckAt: new Date(nowDate.getTime() + retryCheckMs),
} }).catch(() => {});
Log.warn('CRON: PS payment sync failed', { psOrderId: mapping.psOrderId, error: error.message });
return { success: false, error };
}
}
async function syncPaymentsForShop(config, api, since, until, options = {}, dependencies = {}) {
const {
PsOrder,
now = () => new Date(),
isLockValid = () => true,
} = dependencies;
const pageSize = options.pageSize || DEFAULT_PAGE_SIZE;
const maxPages = options.maxPages || DEFAULT_MAX_PAGES;
const nextCheckMs = options.nextCheckMs || 30 * 60 * 1000;
const retryCheckMs = options.retryCheckMs || 5 * 60 * 1000;
let processed = 0;
let failures = 0;
for (let page = 0; page < maxPages; page += 1) {
if (!isLockValid()) throw new Error('PrestaShop payments lock was lost');
const nowDate = now();
const mappings = await PsOrder.find(
duePaymentsQuery(config, since, until, nowDate),
pageSize,
0,
{ nextPaymentCheckAt: 1, syncedAt: 1, _id: 1 }
);
if (!mappings?.length) break;
for (const mapping of mappings) {
if (!isLockValid()) throw new Error('PrestaShop payments lock was lost');
const result = await processPaymentMapping(mapping, config, api, {
...dependencies, nowDate: now(), nextCheckMs, retryCheckMs,
});
processed += 1;
if (!result.success) failures += 1;
}
if (mappings.length < pageSize) break;
}
return { processed, failures };
}
module.exports = {
money,
asOrders,
paymentTransactionId,
parsePrestashopDate,
upsertMapping,
importOrdersForShop,
syncPaymentsForShop,
processPaymentMapping,
duePaymentsQuery,
};
+56
View File
@@ -0,0 +1,56 @@
function createJobRuntime(Core, Log = console) {
const activeRuns = new Set();
let stopped = false;
function track(operation) {
if (stopped) return Promise.resolve(null);
const promise = Promise.resolve().then(operation);
activeRuns.add(promise);
promise.then(
() => activeRuns.delete(promise),
() => activeRuns.delete(promise)
);
return promise;
}
async function withRenewedLock(lockName, ttlMs, operation) {
const lockId = await Core.getLock(lockName, ttlMs);
if (!lockId) return null;
let lockValid = true;
let renewal = null;
const timer = setInterval(() => {
if (renewal || !lockValid) return;
renewal = Core.extendLockTime(lockId, ttlMs)
.catch(error => {
lockValid = false;
Log.error('PrestaShop cron lock renewal failed', { lockName, error: error.message });
})
.finally(() => { renewal = null; });
}, Math.max(1000, Math.floor(ttlMs / 3)));
timer.unref?.();
try {
return await operation(() => lockValid && !stopped);
} finally {
clearInterval(timer);
if (renewal) await renewal;
await Core.releaseLock(lockId).catch(error => {
Log.warn('PrestaShop cron lock release failed', { lockName, error: error.message });
});
}
}
function start() {
stopped = false;
}
async function stop() {
stopped = true;
await Promise.allSettled([...activeRuns]);
}
return { track, withRenewedLock, start, stop, isStopped: () => stopped };
}
module.exports = { createJobRuntime };
+5
View File
@@ -8,6 +8,10 @@ const schema = new DB.Schema('ps_orders', {
reference: { type: 'string', optional: true, default: '' },
syncedAt: { type: 'date', optional: true, default: () => new Date() },
paymentSyncedAt: { type: 'date', optional: true },
// Ostatnia kumulatywna kwota total_paid_real potwierdzona w core/orders.
paymentTotalSynced: { type: 'number', optional: true },
paymentCheckedAt: { type: 'date', optional: true },
nextPaymentCheckAt: { type: 'date', optional: true },
});
schema.pre('validate', function (data) {});
@@ -16,5 +20,6 @@ schema.post('create', function (data) {});
DB.createIndex('ps_orders', { psOrderId: 1, shopConfigId: 1 }, { unique: true });
DB.createIndex('ps_orders', { orbitoOrderId: 1 });
DB.createIndex('ps_orders', { syncedAt: -1 });
DB.createIndex('ps_orders', { shopConfigId: 1, nextPaymentCheckAt: 1, syncedAt: 1 });
module.exports = schema;