diff --git a/index.js b/index.js index 14f95d5..2e869b9 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ const { Log, Core, Utils } = require('@orbito/api-lib'); -const CronJob = require('cron').CronJob; +const { CronJob } = require('cron'); const PsOrder = require('./src/models/PsOrder'); const PrestashopApi = require('./src/PrestaApi'); const { importFromPSOrder } = require('./src/OrderImporter'); @@ -7,222 +7,92 @@ const { isLegacyApi } = require('./src/utils'); const { loadEnabledConfigs, setLastOrderId } = require('./src/module-config'); const { registerPrestashopConfigBlocks } = require('./src/register-config'); const { registerPrestashopPermissions } = require('./src/register-permissions'); +const { importOrdersForShop, syncPaymentsForShop } = require('./src/automation-jobs'); +const { createJobRuntime } = require('./src/job-runtime'); const NEW_ORDERS_WINDOW_MS = 20 * 60 * 1000; -const OLDER_ORDERS_WINDOW_MS = 5 * 24 * 60 * 60 * 1000; -const unpaidFilter = { $or: [{ paymentSyncedAt: { $exists: false } }, { paymentSyncedAt: null }] }; +const runtime = createJobRuntime(Core, Log); Utils.loadMethods(__dirname + '/methods'); -registerPrestashopConfigBlocks().catch(err => { - Log.error('PrestaShop: nie udało się zarejestrować bloków Config', { error: err?.message }); +registerPrestashopConfigBlocks().catch(error => { + Log.error('PrestaShop: nie udało się zarejestrować bloków Config', { error: error?.message }); +}); +registerPrestashopPermissions().catch(error => { + Log.error('PrestaShop: nie udało się zarejestrować uprawnień', { error: error?.message }); }); -registerPrestashopPermissions().catch(err => { - Log.error('PrestaShop: nie udało się zarejestrować uprawnień', { error: err?.message }); -}); +function apiFor(config) { + return new PrestashopApi(config.apiUrl, config.apiKey, isLegacyApi(config.versionCode)); +} -const ordersPuller = new CronJob("0 */5 * * * *", async () => { - const lockId = await Core.getLock('prestashop-orders-puller', 290000); - if (!lockId) return; - - try { - let enabledShops = []; +async function runOrdersPuller(isLockValid) { + const configs = await loadEnabledConfigs(); + for (const config of configs) { + if (!isLockValid()) break; try { - enabledShops = await loadEnabledConfigs(); + await importOrdersForShop(config, apiFor(config), { + Core, PsOrder, importFromPSOrder, setLastOrderId, Log, isLockValid, + }); } catch (error) { - Log.error( 'Error fetching shop configs', { error: error.message }); - } - - for (const config of enabledShops) { - try { - const api = new PrestashopApi(config.apiUrl, config.apiKey, isLegacyApi(config.versionCode)); - if (!config.lastOrderId) { - const res = await api.makeRequest('orders', 'GET', { - display: '[id]', - sort: 'id_DESC', - limit: 1, - output_format: "JSON" - }); - - if (res.orders && res.orders.length > 0) { - const lastOrderId = parseInt(res.orders[0].id); - await setLastOrderId(lastOrderId); - config.lastOrderId = lastOrderId; - } - - continue; - } - - let queryParams = { - display: '[id, reference, date_add]', - sort: 'id_ASC', - limit: 50, - output_format: "JSON", - 'filter[id]': `[${config.lastOrderId + 1},999999999]` - }; - - let res = await api.makeRequest('orders', 'GET', queryParams); - - if (!res.orders || res.orders.length == 0) { - continue; - } - - const nowMs = Date.now(); - const thirtySecondsAgo = new Date(nowMs - 30 * 1000); - - const newestDateStr = res.orders[res.orders.length - 1]?.date_add; - let offsetMs = 0; - if (newestDateStr) { - const parsedNewest = new Date(newestDateStr.replace(' ', 'T')); - const drift = parsedNewest.getTime() - nowMs; - if (drift > 60000) { - offsetMs = Math.round(drift / 3600000) * 3600000; - } - } - - res.orders = res.orders.filter(order => { - const parsed = new Date(order.date_add.replace(' ', 'T')); - const corrected = new Date(parsed.getTime() - offsetMs); - return corrected < thirtySecondsAgo; - }); - - if (!res.orders || res.orders.length == 0) { - continue; - } - - for (const order of res.orders) { - try { - const searchResult = await Core.callModule( 'core/orders', 'search', { - source: { - // własny kod modułu bierzemy z core (po rejestracji), nie z hardcode'u - moduleCode: Core.getModuleInfo().code, - uniqueId: order.reference + ':' + order.id - } - }); - - if (searchResult?.data?.length > 0) { - config.lastProcessedId = Math.max(config.lastProcessedId || 0, parseInt(order.id)); - continue; - } - - await importFromPSOrder(order.id, api, config); - config.lastProcessedId = Math.max(config.lastProcessedId || 0, parseInt(order.id)); - } catch (orderErr) { - Log.error( 'Failed to import PS order', { orderId: order.id, error: orderErr.message }); - config.lastProcessedId = Math.max(config.lastProcessedId || 0, parseInt(order.id)); - } - } - - if (config.lastProcessedId) { - await setLastOrderId(config.lastProcessedId); - } - } catch (error) { - Log.error( 'Error pulling orders for shop config', { shopConfigId: config._id, error: error.message }); - } - } - } finally { - await Core.releaseLock(lockId); - } -}); -ordersPuller.start(); - - -async function syncPaymentsForShop(config, api, since, until, limit = 30) { - const syncedAtRange = until ? { $gte: since, $lt: until } : { $gte: since }; - - const mappings = await PsOrder.find( - { - shopConfigId: config._id, - orbitoOrderId: { $exists: true, $ne: null }, - syncedAt: syncedAtRange, - ...unpaidFilter, - }, - limit, 0, { syncedAt: -1 } - ); - if (!mappings?.length) return; - - for (const m of mappings) { - try { - const orderRes = await Core.callModule( 'core/orders', 'search', { _id: m.orbitoOrderId }); - const orders = Array.isArray(orderRes) ? orderRes : (orderRes?.data ?? []); - const order = orders[0]; - if (order?.isPaid) { - await PsOrder.update({ _id: m._id }, { $set: { paymentSyncedAt: new Date() } }); - continue; - } - - const psOrder = await api.getOrder(m.psOrderId); - if (!psOrder) continue; - - const totalPaidReal = parseFloat(psOrder.total_paid_real) || 0; - if (totalPaidReal <= 0) continue; - - const currentPaid = order?.summary?.paid ?? 0; - const gross = order?.summary?.gross ?? totalPaidReal; - const remaining = Math.round((gross - currentPaid) * 100) / 100; - - if (remaining > 0) { - const paidAmount = Math.min(remaining, Math.round(totalPaidReal * 100) / 100); - await Core.callModule( 'core/orders', 'order.payments_add', { - orderId: m.orbitoOrderId?.toString?.() ?? m.orbitoOrderId, - payment: { - amount: paidAmount, - method: psOrder.payment || 'PrestaShop', - date: new Date(psOrder.date_add), - }, - }); - Log.info( 'CRON: PS payment synced', { psOrderId: m.psOrderId, amount: paidAmount }); - } - await PsOrder.update({ _id: m._id }, { $set: { paymentSyncedAt: new Date() } }); - } catch (err) { - Log.warn( 'CRON: PS payment sync failed', { psOrderId: m.psOrderId, error: err.message }); + Log.error('Error pulling orders for shop config', { shopConfigId: config._id, error: error.message }); } } } -const paymentsSyncNew = new CronJob('0 */3 * * * *', async () => { - const lockId = await Core.getLock('prestashop-payments-new', 120000); - if (!lockId) return; - try { - const configs = await loadEnabledConfigs(); - const since = new Date(Date.now() - NEW_ORDERS_WINDOW_MS); - for (const config of configs) { - try { - const api = new PrestashopApi(config.apiUrl, config.apiKey, isLegacyApi(config.versionCode)); - await syncPaymentsForShop(config, api, since, null, 50); - } catch (err) { - Log.warn('CRON: PS payments-new error', { error: err.message }); - } +async function runPaymentSync(kind, isLockValid) { + const configs = await loadEnabledConfigs(); + const now = Date.now(); + const recent = kind === 'recent'; + const since = recent ? new Date(now - NEW_ORDERS_WINDOW_MS) : null; + const until = recent ? null : new Date(now - NEW_ORDERS_WINDOW_MS); + for (const config of configs) { + if (!isLockValid()) break; + try { + await syncPaymentsForShop(config, apiFor(config), since, until, { + pageSize: 50, + maxPages: recent ? 4 : 10, + nextCheckMs: recent ? 3 * 60 * 1000 : 30 * 60 * 1000, + retryCheckMs: recent ? 3 * 60 * 1000 : 10 * 60 * 1000, + }, { Core, PsOrder, Log, isLockValid }); + } catch (error) { + Log.warn(`CRON: PS payments-${kind} error`, { shopConfigId: config._id, error: error.message }); } - } finally { - await Core.releaseLock(lockId); } -}); -paymentsSyncNew.start(); +} -const paymentsSyncOlder = new CronJob('0 */30 * * * *', async () => { - const lockId = await Core.getLock('prestashop-payments-older', 300000); - if (!lockId) return; - try { - const configs = await loadEnabledConfigs(); - const now = Date.now(); - const since = new Date(now - OLDER_ORDERS_WINDOW_MS); - const until = new Date(now - NEW_ORDERS_WINDOW_MS); - for (const config of configs) { - try { - const api = new PrestashopApi(config.apiUrl, config.apiKey, isLegacyApi(config.versionCode)); - await syncPaymentsForShop(config, api, since, until, 50); - } catch (err) { - Log.warn('CRON: PS payments-older error', { error: err.message }); - } - } - } finally { - await Core.releaseLock(lockId); - } -}); -paymentsSyncOlder.start(); +const jobs = [ + new CronJob('0 */5 * * * *', () => runtime.track(() => + runtime.withRenewedLock('prestashop-orders-puller', 5 * 60 * 1000, runOrdersPuller) + )), + new CronJob('0 */3 * * * *', () => runtime.track(() => + runtime.withRenewedLock('prestashop-payments-sync', 5 * 60 * 1000, valid => runPaymentSync('recent', valid)) + )), + new CronJob('30 */30 * * * *', () => runtime.track(() => + runtime.withRenewedLock('prestashop-payments-sync', 5 * 60 * 1000, valid => runPaymentSync('older', valid)) + )), +]; +function startJobs() { + runtime.start(); + for (const job of jobs) if (!job.isActive) job.start(); +} + +async function stopJobs() { + for (const job of jobs) job.stop(); + await runtime.stop(); +} + +// Preserve the module's existing startup behavior; lifecycle callbacks make repeated +// enable/disable transitions safe and ensure no cron remains active while disabled. +startJobs(); Core.onEnabled(async () => { + startJobs(); Log.info('Module enabled'); }); +Core.onDisabled(async () => { + await stopJobs(); + Log.info('Module disabled; PrestaShop cron jobs stopped'); +}); + +module.exports = { startJobs, stopJobs, runOrdersPuller, runPaymentSync }; diff --git a/module.json b/module.json index b4c4f76..54b5f86 100644 --- a/module.json +++ b/module.json @@ -1,6 +1,6 @@ { "name": "core/prestashop", - "version": "1.1.1", + "version": "1.1.2", "singleton": false, "dependsOn": [ "core/orders", diff --git a/package-lock.json b/package-lock.json index a4b3b73..c6405c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "core.prestashop", - "version": "1.0.0", + "version": "1.1.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "core.prestashop", - "version": "1.0.0", + "version": "1.1.2", "license": "ISC", "dependencies": { "@orbito/api-lib": "^2.2.1", diff --git a/package.json b/package.json index efe8d71..72fc118 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "core.prestashop", - "version": "1.0.0", - "description": "Sample orbito module", + "version": "1.1.2", + "description": "Integracja zamówień i płatności PrestaShop", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", + "test": "node --test --test-concurrency=1 test/*.test.js", "dev": "node dev-server.js" }, "author": "", diff --git a/src/OrderImporter.js b/src/OrderImporter.js index 419254f..7ab12ca 100644 --- a/src/OrderImporter.js +++ b/src/OrderImporter.js @@ -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 }; diff --git a/src/PrestaApi.js b/src/PrestaApi.js index 1821c83..0f1afb3 100644 --- a/src/PrestaApi.js +++ b/src/PrestaApi.js @@ -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") } }); diff --git a/src/automation-jobs.js b/src/automation-jobs.js new file mode 100644 index 0000000..07d795c --- /dev/null +++ b/src/automation-jobs.js @@ -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, +}; diff --git a/src/job-runtime.js b/src/job-runtime.js new file mode 100644 index 0000000..f091f6f --- /dev/null +++ b/src/job-runtime.js @@ -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 }; diff --git a/src/models/PsOrder.js b/src/models/PsOrder.js index 303024c..9f1cdfe 100644 --- a/src/models/PsOrder.js +++ b/src/models/PsOrder.js @@ -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; diff --git a/test/automation-jobs.test.js b/test/automation-jobs.test.js new file mode 100644 index 0000000..dcceec3 --- /dev/null +++ b/test/automation-jobs.test.js @@ -0,0 +1,190 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); + +const { + importOrdersForShop, + paymentTransactionId, + processPaymentMapping, + syncPaymentsForShop, +} = require('../src/automation-jobs'); + +const silentLog = { info() {}, warn() {}, error() {} }; + +function oldOrder(id, reference = `REF-${id}`) { + return { id, reference, date_add: '2026-01-01 10:00:00' }; +} + +test('first run safely starts at the newest order without backfilling history', async () => { + const writes = []; + const config = { _id: 'default', lastOrderId: null }; + const result = await importOrdersForShop(config, { + makeRequest: async () => ({ orders: [{ id: '812' }] }), + }, { + setLastOrderId: async id => writes.push(id), + Log: silentLog, + }); + + assert.deepEqual(writes, [812]); + assert.equal(config.lastOrderId, 812); + assert.deepEqual(result, { status: 'initialized', cursor: 812, processed: 0 }); +}); + +test('cursor advances only through confirmed successes and stops before failed order', async () => { + const writes = []; + const imported = []; + const config = { _id: 'default', lastOrderId: 10 }; + const api = { makeRequest: async () => ({ orders: [oldOrder(11), oldOrder(12), oldOrder(13)] }) }; + const result = await importOrdersForShop(config, api, { + Core: { callModule: async () => ({ data: [] }) }, + PsOrder: {}, + importFromPSOrder: async id => { + imported.push(id); + if (id === 12) throw new Error('invalid order'); + return { status: 'imported', psOrderId: id, orbitoOrderId: `orbito-${id}` }; + }, + setLastOrderId: async id => writes.push(id), + Log: silentLog, + now: () => new Date('2026-01-02T00:00:00Z').getTime(), + }); + + assert.deepEqual(imported, [11, 12]); + assert.deepEqual(writes, [11]); + assert.equal(config.lastOrderId, 11); + assert.deepEqual(result, { status: 'processed', cursor: 11, processed: 1 }); +}); + +test('existing Orbito order repairs a missing mapping before advancing the cursor', async () => { + const updates = []; + const writes = []; + const config = { _id: 'default', lastOrderId: 20 }; + const api = { + makeRequest: async () => ({ orders: [oldOrder(21)] }), + getOrder: async () => ({ id: 21, reference: 'REF-21', date_add: '2026-01-01 10:00:00', total_paid_real: '25.00' }), + }; + const PsOrder = { + findOne: async () => null, + update: async (...args) => updates.push(args), + }; + + await importOrdersForShop(config, api, { + Core: { callModule: async () => ({ data: [{ _id: 'orbito-21', summary: { paid: 25 } }] }) }, + PsOrder, + importFromPSOrder: async () => assert.fail('must not import an existing order'), + setLastOrderId: async id => writes.push(id), + Log: silentLog, + now: () => new Date('2026-01-02T00:00:00Z').getTime(), + }); + + assert.equal(updates.length, 1); + assert.equal(updates[0][1].$set.orbitoOrderId, 'orbito-21'); + assert.equal(updates[0][1].$set.paymentTotalSynced, 25); + assert.deepEqual(updates[0][2], { upsert: true }); + assert.deepEqual(writes, [21]); +}); + +test('payment sync appends only the cumulative delta with a deterministic transaction id', async () => { + const calls = []; + const updates = []; + const mapping = { _id: 'map-1', psOrderId: 44, orbitoOrderId: 'order-1', paymentTotalSynced: 20 }; + const config = { _id: 'shop-a' }; + const result = await processPaymentMapping(mapping, config, { + getOrder: async () => ({ total_paid_real: '55.00', payment: 'Card', date_upd: '2026-01-02 12:00:00' }), + }, { + Core: { callModule: async (module, method, params) => { + calls.push({ module, method, params }); + if (method === 'search') return { data: [{ summary: { paid: 20, gross: 100 } }] }; + return { success: true }; + } }, + PsOrder: { update: async (...args) => updates.push(args) }, + nowDate: new Date('2026-01-02T12:01:00Z'), + nextCheckMs: 1000, + retryCheckMs: 1000, + Log: silentLog, + }); + + assert.equal(result.amount, 35); + assert.equal(calls[1].method, 'payments.upsert'); + assert.equal(calls[1].params.payment.amount, 35); + assert.equal(calls[1].params.payment.transactionId, 'prestashop:shop-a:44:paid:5500'); + assert.equal(updates[0][1].$set.paymentTotalSynced, 55); +}); + +test('legacy mapping establishes a conservative baseline instead of duplicating a payment', async () => { + const methods = []; + const updates = []; + await processPaymentMapping({ _id: 'map-2', psOrderId: 45, orbitoOrderId: 'order-2' }, { _id: 'default' }, { + getOrder: async () => ({ total_paid_real: '50.00' }), + }, { + Core: { callModule: async (_module, method) => { + methods.push(method); + return { data: [{ summary: { paid: 50, gross: 100 } }] }; + } }, + PsOrder: { update: async (...args) => updates.push(args) }, + nowDate: new Date(), nextCheckMs: 1000, retryCheckMs: 1000, Log: silentLog, + }); + + assert.deepEqual(methods, ['search']); + assert.equal(updates[0][1].$set.paymentTotalSynced, 50); +}); + +test('a mapping write failure retries the same payment transaction id safely', async () => { + const transactionIds = []; + let failMappingWrite = true; + const mapping = { _id: 'map-3', psOrderId: 46, orbitoOrderId: 'order-3', paymentTotalSynced: 10 }; + const dependencies = { + Core: { callModule: async (_module, method, params) => { + if (method === 'search') return { data: [{ summary: { paid: 10, gross: 100 } }] }; + transactionIds.push(params.payment.transactionId); + return { success: true }; + } }, + PsOrder: { update: async (_query, update) => { + if (update.$set.paymentTotalSynced !== undefined && failMappingWrite) { + failMappingWrite = false; + throw new Error('temporary DB failure'); + } + } }, + nowDate: new Date(), nextCheckMs: 1000, retryCheckMs: 1000, Log: silentLog, + }; + const api = { getOrder: async () => ({ total_paid_real: '30.00' }) }; + + const first = await processPaymentMapping(mapping, { _id: 'default' }, api, dependencies); + const second = await processPaymentMapping(mapping, { _id: 'default' }, api, dependencies); + + assert.equal(first.success, false); + assert.equal(second.success, true); + assert.deepEqual(transactionIds, [ + paymentTransactionId({ _id: 'default' }, 46, 30), + paymentTransactionId({ _id: 'default' }, 46, 30), + ]); +}); + +test('payment scheduler drains multiple pages and includes orders older than five days', async () => { + const findQueries = []; + let page = 0; + const PsOrder = { + find: async query => { + findQueries.push(query); + page += 1; + if (page === 1) return [{ _id: 'a', psOrderId: 1, orbitoOrderId: 'o1', paymentTotalSynced: 0 }]; + if (page === 2) return [{ _id: 'b', psOrderId: 2, orbitoOrderId: 'o2', paymentTotalSynced: 0 }]; + return []; + }, + update: async () => {}, + }; + const Core = { callModule: async (_module, method) => method === 'search' + ? { data: [{ summary: { paid: 0, gross: 10 } }] } + : { success: true } }; + + const result = await syncPaymentsForShop( + { _id: 'default' }, + { getOrder: async () => ({ total_paid_real: 0 }) }, + null, + new Date('2026-01-10T00:00:00Z'), + { pageSize: 1, maxPages: 3 }, + { Core, PsOrder, Log: silentLog, now: () => new Date('2026-01-10T01:00:00Z') } + ); + + assert.equal(result.processed, 2); + assert.equal(findQueries.length, 3); + assert.deepEqual(findQueries[0].syncedAt, { $lt: new Date('2026-01-10T00:00:00Z') }); +}); diff --git a/test/job-runtime.test.js b/test/job-runtime.test.js new file mode 100644 index 0000000..3c158f6 --- /dev/null +++ b/test/job-runtime.test.js @@ -0,0 +1,31 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { createJobRuntime } = require('../src/job-runtime'); + +test('lock is released after success and after failure', async () => { + const released = []; + const Core = { + getLock: async name => name, + extendLockTime: async () => true, + releaseLock: async id => released.push(id), + }; + const runtime = createJobRuntime(Core, { error() {}, warn() {} }); + + assert.equal(await runtime.withRenewedLock('ok', 5000, async valid => valid() && 7), 7); + await assert.rejects(() => runtime.withRenewedLock('fail', 5000, async () => { throw new Error('boom'); }), /boom/); + assert.deepEqual(released, ['ok', 'fail']); +}); + +test('stop waits for tracked work and blocks subsequent runs until start', async () => { + const runtime = createJobRuntime({}, console); + let finish; + const active = runtime.track(() => new Promise(resolve => { finish = resolve; })); + await new Promise(resolve => setImmediate(resolve)); + const stopping = runtime.stop(); + finish('done'); + await stopping; + assert.equal(await active, 'done'); + assert.equal(await runtime.track(async () => 'blocked'), null); + runtime.start(); + assert.equal(await runtime.track(async () => 'running'), 'running'); +}); diff --git a/test/presta-api.test.js b/test/presta-api.test.js new file mode 100644 index 0000000..832c7c6 --- /dev/null +++ b/test/presta-api.test.js @@ -0,0 +1,74 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const Module = require('node:module'); +const Core = { fetch: null }; +const originalLoad = Module._load; +Module._load = function(request, parent, isMain) { + if (request === '@orbito/api-lib') return { Core, Log: { warn() {} } }; + return originalLoad.call(this, request, parent, isMain); +}; +const PrestashopApi = require('../src/PrestaApi'); +Module._load = originalLoad; + +function response(status, body) { + return { status, text: () => typeof body === 'string' ? body : JSON.stringify(body) }; +} + +test('rejects HTTP errors and retries retryable GET responses', async t => { + const originalFetch = Core.fetch; + t.after(() => { Core.fetch = originalFetch; }); + let calls = 0; + Core.fetch = async () => { + calls += 1; + return calls < 3 ? response(503, { error: 'busy' }) : response(200, { orders: [] }); + }; + const api = new PrestashopApi('https://shop.example', 'secret', true, { + maxGetAttempts: 3, retryBaseMs: 1, timeoutMs: 100, + }); + + assert.deepEqual(await api.makeRequest('orders', 'GET'), { orders: [] }); + assert.equal(calls, 3); +}); + +test('does not retry mutating requests', async t => { + const originalFetch = Core.fetch; + t.after(() => { Core.fetch = originalFetch; }); + let calls = 0; + Core.fetch = async () => { calls += 1; return response(500, { error: 'failed' }); }; + const api = new PrestashopApi('https://shop.example', 'secret', true, { retryBaseMs: 1 }); + + await assert.rejects(() => api.makeRequest('orders/1', 'PATCH', { order: { id: 1 } }), /HTTP 500/); + assert.equal(calls, 1); +}); + +test('rejects non-retryable 4xx and invalid JSON', async t => { + const originalFetch = Core.fetch; + t.after(() => { Core.fetch = originalFetch; }); + const api = new PrestashopApi('https://shop.example', 'secret', true, { retryBaseMs: 1 }); + + Core.fetch = async () => response(401, { error: 'unauthorized' }); + await assert.rejects(() => api.makeRequest('orders', 'GET'), /HTTP 401/); + + Core.fetch = async () => response(200, 'not json'); + await assert.rejects(() => api.makeRequest('orders', 'GET'), /invalid JSON/); +}); + +test('times out stalled requests', async t => { + const originalFetch = Core.fetch; + t.after(() => { Core.fetch = originalFetch; }); + Core.fetch = async () => new Promise(() => {}); + const api = new PrestashopApi('https://shop.example', 'secret', true, { + timeoutMs: 5, maxGetAttempts: 1, + }); + + await assert.rejects(() => api.makeRequest('orders', 'GET'), /timed out/); +}); + +test('preserves the explicitly allowed product partial-success compatibility path', async t => { + const originalFetch = Core.fetch; + t.after(() => { Core.fetch = originalFetch; }); + Core.fetch = async () => response(500, { product: { id: 1 } }); + const api = new PrestashopApi('https://shop.example', 'secret', true, { maxGetAttempts: 1 }); + + assert.deepEqual(await api.makeRequest('products/1', 'GET', null, true), { product: { id: 1 } }); +});