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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EDD4j8Y2gTf72v7dMgSHz8
This commit is contained in:
2026-10-15 14:46:57 +02:00
co-authored by Claude Opus 5.5
parent cc3ab1fd4a
commit 6ffbd2effa
8 changed files with 109 additions and 11 deletions
+11
View File
@@ -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
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "core/prestashop",
"version": "1.2.6",
"version": "1.3.0",
"singleton": false,
"dependsOn": [
"core/orders",
+2 -2
View File
@@ -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",
+1 -1
View File
@@ -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": {
+8 -2
View File
@@ -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,
+2 -1
View File
@@ -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: {
+39 -3
View File
@@ -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 };
+45 -1
View File
@@ -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);
});