Use hook-backed outbound status mapping
This commit is contained in:
+33
-2
@@ -1,3 +1,5 @@
|
||||
const LEGACY_CONFLICT = 'legacy_conflict';
|
||||
|
||||
function normalizeStatusId(value) {
|
||||
if (value == null || value === '') return null;
|
||||
if (typeof value === 'object' && value._id != null) {
|
||||
@@ -34,8 +36,7 @@ function resolveStatusId(psStateId, statusMapping, fallbackStatusId) {
|
||||
return normalizeStatusId(match?.orbitoStatusId) ?? normalizeStatusId(fallbackStatusId);
|
||||
}
|
||||
|
||||
// Mapowanie jest konfigurowane w wierszach PrestaShop -> Orbito, ale po
|
||||
// zapisaniu służy wyłącznie do wysyłki statusu w przeciwnym kierunku.
|
||||
// Kilka statusów Orbito może wskazywać ten sam status PrestaShop.
|
||||
// Jeden status Orbito nie może wskazywać kilku statusów PrestaShop, bo wtedy
|
||||
// wynik synchronizacji zależałby od kolejności pól formularza.
|
||||
function resolvePsStateId(orbitoStatusId, statusMapping) {
|
||||
@@ -54,6 +55,33 @@ function resolvePsStateId(orbitoStatusId, statusMapping) {
|
||||
return uniqueMatches[0] ?? null;
|
||||
}
|
||||
|
||||
// Legacy keys remain untouched. An explicitly saved empty outbound field
|
||||
// disables its legacy assignment; a missing field inherits it. Read all keys,
|
||||
// including states no longer returned by the shop, to avoid losing mappings.
|
||||
function readStatusMapping(config) {
|
||||
const legacy = [];
|
||||
const outbound = new Map();
|
||||
for (const [key, value] of Object.entries(config)) {
|
||||
const oldMatch = /^mapping\.state_(\d+)$/.exec(key);
|
||||
const newMatch = /^mapping\.orbito_([a-f0-9]{24})$/i.exec(key);
|
||||
if (oldMatch && normalizeStatusId(value)) {
|
||||
legacy.push({ psStateId: Number(oldMatch[1]), orbitoStatusId: normalizeStatusId(value) });
|
||||
}
|
||||
if (newMatch && value !== LEGACY_CONFLICT) outbound.set(newMatch[1], value);
|
||||
}
|
||||
const result = legacy.filter(entry => !outbound.has(entry.orbitoStatusId));
|
||||
for (const [orbitoStatusId, value] of outbound) {
|
||||
if (value == null || value === '') continue;
|
||||
if (!isPsStateId(value)) throw new Error(`Niepoprawny status PrestaShop dla statusu Orbito ${orbitoStatusId}`);
|
||||
result.push({ orbitoStatusId, psStateId: Number(value) });
|
||||
}
|
||||
return result.sort((a, b) => a.psStateId - b.psStateId);
|
||||
}
|
||||
|
||||
function isPsStateId(value) {
|
||||
return /^\d+$/.test(String(value)) && Number.isSafeInteger(Number(value)) && Number(value) > 0;
|
||||
}
|
||||
|
||||
function formatPsStatesPreview(states) {
|
||||
if (!Array.isArray(states) || !states.length) {
|
||||
return 'Brak statusów PrestaShop — najpierw przetestuj połączenie.';
|
||||
@@ -76,6 +104,9 @@ function extractPsStateName(name) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
LEGACY_CONFLICT,
|
||||
readStatusMapping,
|
||||
isPsStateId,
|
||||
normalizeStatusId,
|
||||
parseStatusMapping,
|
||||
resolveStatusId,
|
||||
|
||||
Reference in New Issue
Block a user