From 6ffbd2effa2ac817f9b73896ec5a0e38660d1140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Ko=C5=BAmi=C5=84ski?= Date: Thu, 15 Oct 2026 14:46:57 +0200 Subject: [PATCH] Release 1.3.0: configurable start order ID for import Saving a new "start order ID" in the import block moves the import cursor to X-1, so the next run imports from order X inclusive. The block also shows the current cursor. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EDD4j8Y2gTf72v7dMgSHz8 --- README.md | 11 +++++++ module.json | 2 +- package-lock.json | 4 +-- package.json | 2 +- src/module-config.js | 10 +++++-- src/register-config.js | 3 +- src/status-config.js | 42 ++++++++++++++++++++++++-- test/register-config-status.test.js | 46 ++++++++++++++++++++++++++++- 8 files changed, 109 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 868b92e..fc58655 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,17 @@ zapisanych zamówień ani istniejących płatności. Niepoprawne daty korzystaj z czasu importu; powtórzona godzina przy zmianie czasu oznacza jej pierwsze wystąpienie, ponieważ data PrestaShop nie rozróżnia obu wystąpień. +## ID startowe importu + +Pierwszy import ustawia kursor na najnowszym zamówieniu w sklepie i nie +pobiera zamówień historycznych. W bloku „Import zamówień” widać ostatnio +przetworzone zamówienie PrestaShop. Zapis nowej wartości „ID startowe +zamówienia PrestaShop” przesuwa kursor tak, by następny import zaczął od tego +zamówienia włącznie — do przodu lub wstecz, także na działającej instancji. +Zapis tej samej wartości ani wyczyszczenie pola nie zmieniają kursora. +Zamówienia już obecne w Orbito nie są duplikowane, ale cofnięcie kursora może +ponownie zaimportować zamówienia usunięte w Orbito. + ## Statusy zamówień Orbito jest źródłem prawdy dla statusów zamówień. Przy ładowaniu zakładki diff --git a/module.json b/module.json index 040fbf3..402eaf3 100644 --- a/module.json +++ b/module.json @@ -1,6 +1,6 @@ { "name": "core/prestashop", - "version": "1.2.6", + "version": "1.3.0", "singleton": false, "dependsOn": [ "core/orders", diff --git a/package-lock.json b/package-lock.json index 2b29468..d60dba6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "core.prestashop", - "version": "1.2.6", + "version": "1.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "core.prestashop", - "version": "1.2.6", + "version": "1.3.0", "license": "ISC", "dependencies": { "@orbito/api-lib": "^4.4.1", diff --git a/package.json b/package.json index 119d20b..8dcc245 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "core.prestashop", - "version": "1.2.6", + "version": "1.3.0", "description": "Integracja zamówień i płatności PrestaShop", "main": "index.js", "scripts": { diff --git a/src/module-config.js b/src/module-config.js index 841c73f..49fdfe3 100644 --- a/src/module-config.js +++ b/src/module-config.js @@ -14,6 +14,11 @@ async function loadPsStates() { return Array.isArray(states) ? states : []; } +async function loadLastOrderId() { + const lastOrderId = await Config.get('runtime.lastOrderId', null); + return lastOrderId == null ? null : Number(lastOrderId); +} + async function loadStatusMapping() { return readStatusMapping(await Config.getAll()); } @@ -57,7 +62,7 @@ async function loadConfig() { Config.get('connection.versionCode', ''), Config.get('connection.timeZone', DEFAULT_SHOP_TIME_ZONE), Config.get('import.orderStatus', ''), - Config.get('runtime.lastOrderId', null), + loadLastOrderId(), loadPsStates() ]); @@ -72,7 +77,7 @@ async function loadConfig() { versionCode: versionCode || '', timeZone: String(timeZone || DEFAULT_SHOP_TIME_ZONE).trim() || DEFAULT_SHOP_TIME_ZONE, orderStatus: orderStatus || '', - lastOrderId: lastOrderId == null ? null : Number(lastOrderId), + lastOrderId, psStates, statusMapping }; @@ -96,6 +101,7 @@ module.exports = { isConfigured, loadConfig, loadEnabledConfigs, + loadLastOrderId, loadPsStates, loadStatusMapping, initializeOutboundMapping, diff --git a/src/register-config.js b/src/register-config.js index 6aae5a9..18b0f2b 100644 --- a/src/register-config.js +++ b/src/register-config.js @@ -3,7 +3,7 @@ const { PropertyType, DisplayFormat } = Config; const PrestashopApi = require('./PrestaApi'); const { isLegacyApi } = require('./utils'); const { extractPsStateName } = require('./status-mapper'); -const { importStatusSchema, outboundMappingSchema, validateOutboundMapping } = require('./status-config'); +const { importStatusSchema, validateImportBlock, outboundMappingSchema, validateOutboundMapping } = require('./status-config'); const { setPsStates, loadPsStates, initializeOutboundMapping } = require('./module-config'); const { ensureShopIcon } = require('./shop-icon'); const { DEFAULT_SHOP_TIME_ZONE, validateTimeZone } = require('./prestashop-date'); @@ -154,6 +154,7 @@ async function registerPrestashopConfigBlocks() { title: { pl: 'Import zamówień', en: 'Order import' }, scope: 'global', schema: importStatusSchema, + validate: validateImportBlock, }, mapping: { diff --git a/src/status-config.js b/src/status-config.js index 76e6809..7ccc5b3 100644 --- a/src/status-config.js +++ b/src/status-config.js @@ -1,6 +1,7 @@ const { Config, Core } = require('@orbito/api-lib'); const { requestHook } = require('./hook-contract'); const { normalizeStatusId, readStatusMapping, isPsStateId, LEGACY_CONFLICT } = require('./status-mapper'); +const { loadLastOrderId, setLastOrderId } = require('./module-config'); function info(pl, en = pl) { return { type: Config.PropertyType.INFO, name: { pl, en } }; @@ -16,9 +17,22 @@ async function loadOrbitoStatuses() { } } +function importCursorInfo(lastOrderId) { + if (!Number.isFinite(lastOrderId)) { + return info( + 'Pierwszy import zacznie od najnowszego zamówienia w sklepie, chyba że podasz ID startowe.', + 'The first import starts from the newest order in the shop unless you set a start order ID.', + ); + } + return info( + `Ostatnio przetworzone zamówienie PrestaShop: #${lastOrderId} — następny import zacznie od #${lastOrderId + 1}.`, + `Last processed PrestaShop order: #${lastOrderId} — the next import starts from #${lastOrderId + 1}.`, + ); +} + async function importStatusSchema() { - const [{ statuses, error }, savedId] = await Promise.all([ - loadOrbitoStatuses(), Config.get('import.orderStatus', ''), + const [{ statuses, error }, savedId, lastOrderId] = await Promise.all([ + loadOrbitoStatuses(), Config.get('import.orderStatus', ''), loadLastOrderId(), ]); const values = statuses.map(status => ({ id: String(status._id), label: status.name })); if (savedId && !values.some(value => value.id === String(savedId))) { @@ -35,9 +49,31 @@ async function importStatusSchema() { name: { pl: 'Domyślny status po imporcie', en: 'Default status after import' }, options: { values }, }, + cursor: importCursorInfo(lastOrderId), + startOrderId: { + type: Config.PropertyType.NUMBER, + name: { pl: 'ID startowe zamówienia PrestaShop', en: 'PrestaShop start order ID' }, + description: { + pl: 'Opcjonalnie. Po zapisie nowej wartości import zacznie od tego zamówienia (włącznie), także wstecz. Cofnięcie może ponownie zaimportować zamówienia usunięte w Orbito.', + en: 'Optional. After saving a new value the import starts from this order (inclusive), also backwards. Moving back may re-import orders deleted in Orbito.', + }, + }, }; } +// Runs right before the core saves the block; moves the import cursor only when the start ID changes. +async function validateImportBlock(params, report) { + const value = params?.startOrderId; + if (value == null || value === '') return; + const startOrderId = Number(value); + if (!Number.isInteger(startOrderId) || startOrderId < 1) { + report.addError('startOrderId', 'Podaj dodatnią liczbę całkowitą (ID zamówienia PrestaShop) albo zostaw pole puste.'); + return; + } + if (Number(await Config.get('import.startOrderId', null)) === startOrderId) return; + await setLastOrderId(startOrderId - 1); +} + async function outboundMappingSchema(refreshPsStates) { const [config, { statuses, error }, { states, refreshError }] = await Promise.all([ Config.getAll(), loadOrbitoStatuses(), refreshPsStates(), @@ -92,4 +128,4 @@ function validateOutboundMapping(params, report) { } } -module.exports = { importStatusSchema, outboundMappingSchema, validateOutboundMapping }; +module.exports = { importStatusSchema, validateImportBlock, outboundMappingSchema, validateOutboundMapping }; diff --git a/test/register-config-status.test.js b/test/register-config-status.test.js index 6b81c16..275352a 100644 --- a/test/register-config-status.test.js +++ b/test/register-config-status.test.js @@ -32,7 +32,7 @@ function loadConfigModule({ states = [], cachedStates = [], saved = {}, provider }); } const Config = { - PropertyType: { INFO: 'info', STRING: 'string' }, + PropertyType: { INFO: 'info', STRING: 'string', NUMBER: 'number' }, DisplayFormat: { URL: 'url', SECRET: 'secret' }, registerConfigBlocks: async value => { blocks = value; }, get: async (key, fallback) => Object.hasOwn(store, key) ? store[key] : fallback, @@ -233,3 +233,47 @@ test('separate shop instances keep their own target IDs and options', async () = assert.equal(a.store[`mapping.orbito_${STATUS_A}`], '2'); assert.equal(b.store[`mapping.orbito_${STATUS_A}`], '7'); }); + +test('import block shows the import cursor or the first-import hint', async () => { + const withCursor = await (await loadConfigModule({ saved: { 'runtime.lastOrderId': 100 } }).register()).import.schema(); + assert.match(withCursor.cursor.name.pl, /#100 — następny import zacznie od #101/); + assert.match(withCursor.cursor.name.en, /#100 — the next import starts from #101/); + assert.equal(withCursor.startOrderId.type, 'number'); + const withoutCursor = await (await loadConfigModule().register()).import.schema(); + assert.match(withoutCursor.cursor.name.pl, /Pierwszy import zacznie od najnowszego zamówienia/); +}); + +test('a changed start order ID moves the import cursor to the previous order, forwards and backwards', async () => { + const module = loadConfigModule({ saved: { 'runtime.lastOrderId': 100, 'import.startOrderId': 50 } }); + const blocks = await module.register(); + const errors = []; + const report = { addError: (...args) => errors.push(args) }; + await blocks.import.validate({ orderStatus: STATUS_A, startOrderId: 150 }, report); + assert.equal(module.store['runtime.lastOrderId'], 149); + module.store['import.startOrderId'] = 150; + await blocks.import.validate({ orderStatus: STATUS_A, startOrderId: '20' }, report); + assert.equal(module.store['runtime.lastOrderId'], 19); + assert.deepEqual(errors, []); +}); + +test('an unchanged or cleared start order ID keeps the import cursor', async () => { + const module = loadConfigModule({ saved: { 'runtime.lastOrderId': 100, 'import.startOrderId': 50 } }); + const blocks = await module.register(); + const errors = []; + for (const startOrderId of [50, '50', '', null, undefined]) { + await blocks.import.validate({ orderStatus: STATUS_B, startOrderId }, { addError: (...args) => errors.push(args) }); + } + assert.equal(module.store['runtime.lastOrderId'], 100); + assert.deepEqual(errors, []); +}); + +test('an invalid start order ID is reported and keeps the import cursor', async () => { + const module = loadConfigModule({ saved: { 'runtime.lastOrderId': 100 } }); + const blocks = await module.register(); + for (const startOrderId of [0, -5, 1.5, 'abc']) { + const errors = []; + await blocks.import.validate({ startOrderId }, { addError: (...args) => errors.push(args) }); + assert.deepEqual(errors.map(([field]) => field), ['startOrderId']); + } + assert.equal(module.store['runtime.lastOrderId'], 100); +});