117 lines
4.2 KiB
JavaScript
117 lines
4.2 KiB
JavaScript
const LEGACY_CONFLICT = 'legacy_conflict';
|
|
|
|
function normalizeStatusId(value) {
|
|
if (value == null || value === '') return null;
|
|
if (typeof value === 'object' && value._id != null) {
|
|
return normalizeStatusId(value._id);
|
|
}
|
|
const str = String(value).trim();
|
|
return /^[a-f0-9]{24}$/i.test(str) ? str : null;
|
|
}
|
|
|
|
function parseStatusMapping(raw) {
|
|
if (!raw) return [];
|
|
if (Array.isArray(raw)) return raw;
|
|
if (typeof raw === 'string') {
|
|
const trimmed = raw.trim();
|
|
if (!trimmed) return [];
|
|
try {
|
|
const parsed = JSON.parse(trimmed);
|
|
return Array.isArray(parsed) ? parsed : [];
|
|
} catch (_) {
|
|
throw new Error('statusMapping musi być poprawną tablicą JSON');
|
|
}
|
|
}
|
|
return [];
|
|
}
|
|
|
|
// Mapuje PrestaShop order.current_state (psStateId) na orbitoStatusId.
|
|
function resolveStatusId(psStateId, statusMapping, fallbackStatusId) {
|
|
const stateId = parseInt(psStateId, 10);
|
|
if (!Array.isArray(statusMapping) || Number.isNaN(stateId)) {
|
|
return normalizeStatusId(fallbackStatusId);
|
|
}
|
|
|
|
const match = statusMapping.find(entry => parseInt(entry.psStateId, 10) === stateId);
|
|
return normalizeStatusId(match?.orbitoStatusId) ?? normalizeStatusId(fallbackStatusId);
|
|
}
|
|
|
|
// 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) {
|
|
const normalizedStatusId = normalizeStatusId(orbitoStatusId);
|
|
if (!normalizedStatusId || !Array.isArray(statusMapping)) return null;
|
|
|
|
const matches = statusMapping
|
|
.filter(entry => normalizeStatusId(entry?.orbitoStatusId) === normalizedStatusId)
|
|
.map(entry => parseInt(entry.psStateId, 10))
|
|
.filter(stateId => !Number.isNaN(stateId));
|
|
|
|
const uniqueMatches = [...new Set(matches)];
|
|
if (uniqueMatches.length > 1) {
|
|
throw new Error(`Status Orbito ${normalizedStatusId} jest przypisany do kilku statusów PrestaShop`);
|
|
}
|
|
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.';
|
|
}
|
|
|
|
return states
|
|
.map(s => {
|
|
const id = s.id ?? s.psStateId;
|
|
const name = extractPsStateName(s.name);
|
|
return `${id} — ${name}`;
|
|
})
|
|
.join('\n');
|
|
}
|
|
|
|
function extractPsStateName(name) {
|
|
if (Array.isArray(name)) {
|
|
return name[0]?.value ?? name[0] ?? '';
|
|
}
|
|
return name ?? '';
|
|
}
|
|
|
|
module.exports = {
|
|
LEGACY_CONFLICT,
|
|
readStatusMapping,
|
|
isPsStateId,
|
|
normalizeStatusId,
|
|
parseStatusMapping,
|
|
resolveStatusId,
|
|
resolvePsStateId,
|
|
formatPsStatesPreview,
|
|
extractPsStateName
|
|
};
|