Initial commit: core-prestashop module v1.1.1
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
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);
|
||||
}
|
||||
|
||||
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 = {
|
||||
normalizeStatusId,
|
||||
parseStatusMapping,
|
||||
resolveStatusId,
|
||||
formatPsStatesPreview,
|
||||
extractPsStateName
|
||||
};
|
||||
Reference in New Issue
Block a user