Improve config form UI and add mapping row save endpoint.

Refactor status counts and config pages for the updated PrestaShop integration workflow.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
fatequod
2026-07-15 09:19:53 +02:00
co-authored by Cursor
parent 1a6a978a21
commit 139ac5cb52
4 changed files with 257 additions and 139 deletions
+41
View File
@@ -0,0 +1,41 @@
const { Log } = require('@orbito/api-lib');
const ShopConfig = require('../../src/models/ShopConfig');
const { normalizeFormParams, normalizeSelectValue, mergeStatusMappingEntry } = require('../../src/config-form');
const { methodOk, methodFail } = require('../../src/method-result');
function extractOrbitoStatusId(input) {
for (const [key, value] of Object.entries(input)) {
if (/^psMapStatus[._]\d+$/.test(key)) {
return normalizeSelectValue(value);
}
}
return normalizeSelectValue(input.orbitoStatusId ?? input.value ?? '');
}
module.exports = async function(params, ctx) {
try {
const existing = await ShopConfig.findOne({ isRemoved: { $ne: true } });
if (!existing) {
return methodFail('Najpierw zapisz konfigurację połączenia ze sklepem.');
}
const input = normalizeFormParams(params);
const psStateId = parseInt(input.psStateId ?? params.psStateId, 10);
if (Number.isNaN(psStateId)) {
return methodFail('Brak ID statusu PrestaShop.');
}
const orbitoStatusId = normalizeSelectValue(
input.orbitoStatusId ?? params.orbitoStatusId ?? extractOrbitoStatusId(input)
);
const statusMapping = mergeStatusMappingEntry(existing.statusMapping, psStateId, orbitoStatusId);
await ShopConfig.update({ _id: existing._id }, { statusMapping });
Log.debug('config.save_mapping_row', { psStateId, orbitoStatusId: orbitoStatusId || null });
return methodOk({ psStateId, orbitoStatusId, statusMapping });
} catch (err) {
Log.error('config.save_mapping_row failed', { error: err?.message });
return methodFail(err?.message || 'Nie udało się zapisać mapowania statusu');
}
};
+3 -35
View File
@@ -1,38 +1,6 @@
const { getOrderStatuses, searchOrders } = require('../../src/orders-api');
const cache = new Map();
const CACHE_TTL = 5000;
const CACHE_KEY = 'default';
const { Core } = require('@orbito/api-lib');
// Proxy do core/orders — statusy są współdzielone i zarządzane w core/orders.
module.exports = async function(params, ctx) {
const now = Date.now();
const cached = cache.get(CACHE_KEY);
if (cached && now - cached.ts < CACHE_TTL) {
return { data: cached.data };
}
const statuses = await getOrderStatuses();
const data = [];
for (const status of statuses) {
const statusId = status._id?.toString?.() ?? status._id;
const result = await searchOrders({
statusId,
page: 1,
perPage: 1
});
data.push({
_id: statusId,
name: status.name ?? '',
color: status.color ?? 'secondary',
positionInGroup: status.positionInGroup ?? 0,
count: result.pagination?.total ?? 0
});
}
data.sort((a, b) => (a.positionInGroup ?? 0) - (b.positionInGroup ?? 0));
cache.set(CACHE_KEY, { ts: now, data });
return { data };
return Core.callModule('core/orders', 'status.get_with_counts', params || {});
};