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:
+110
-26
@@ -13,8 +13,8 @@ const VIEW_ID_ALIASES = {
|
||||
/** Pola połączenia — używaj w withData zamiast #form (nie działa w generatePage+target). */
|
||||
const CONNECTION_WITH_DATA = Object.keys(VIEW_ID_ALIASES);
|
||||
|
||||
/** Pełny zapis: połączenie + mapowanie statusów z listy. */
|
||||
const SAVE_WITH_DATA = [...CONNECTION_WITH_DATA, 'statusMappingList'];
|
||||
/** Pełny zapis: połączenie (po id widoku) + mapowanie (grupa form). */
|
||||
const SAVE_WITH_DATA = [...CONNECTION_WITH_DATA, '#form'];
|
||||
|
||||
const EMPTY_FORM = {
|
||||
storeUrl: '',
|
||||
@@ -115,11 +115,89 @@ function hasFormInput(input) {
|
||||
}
|
||||
|
||||
function hasMappingInput(input) {
|
||||
if (input.statusMappingRows != null) return true;
|
||||
return Object.keys(input).some(key => key.startsWith('statusMappingRows.'));
|
||||
if (Array.isArray(input.statusMappingRows) && input.statusMappingRows.length) return true;
|
||||
if (input.statusMappingRows && typeof input.statusMappingRows === 'object' && Object.keys(input.statusMappingRows).length) {
|
||||
return true;
|
||||
}
|
||||
return Object.keys(input).some(key =>
|
||||
key.startsWith('statusMappingRows.') || /^psMapStatus[._]\d+$/.test(key));
|
||||
}
|
||||
|
||||
function mergeStatusMappingEntry(existing, psStateId, orbitoStatusId) {
|
||||
const list = Array.isArray(existing) ? existing.map(entry => ({ ...entry })) : [];
|
||||
const idx = list.findIndex(entry => parseInt(entry.psStateId, 10) === psStateId);
|
||||
|
||||
if (!orbitoStatusId) {
|
||||
if (idx >= 0) list.splice(idx, 1);
|
||||
return list;
|
||||
}
|
||||
|
||||
const entry = { psStateId, orbitoStatusId };
|
||||
if (idx >= 0) list[idx] = entry;
|
||||
else list.push(entry);
|
||||
return list;
|
||||
}
|
||||
|
||||
function extractAllStatusMappingRows(input) {
|
||||
const rows = input.statusMappingRows;
|
||||
|
||||
if (Array.isArray(rows)) {
|
||||
return rows
|
||||
.map(row => ({
|
||||
psStateId: parseInt(row?.psStateId, 10),
|
||||
orbitoStatusId: normalizeSelectValue(row?.orbitoStatusId)
|
||||
}))
|
||||
.filter(row => !Number.isNaN(row.psStateId));
|
||||
}
|
||||
|
||||
if (rows && typeof rows === 'object') {
|
||||
return Object.values(rows)
|
||||
.map(row => ({
|
||||
psStateId: parseInt(row?.psStateId, 10),
|
||||
orbitoStatusId: normalizeSelectValue(row?.orbitoStatusId)
|
||||
}))
|
||||
.filter(row => !Number.isNaN(row.psStateId));
|
||||
}
|
||||
|
||||
const indexed = {};
|
||||
for (const [key, value] of Object.entries(input)) {
|
||||
const match = key.match(/^statusMappingRows\.(\d+)\.(psStateId|orbitoStatusId)$/);
|
||||
if (!match) continue;
|
||||
indexed[match[1]] = indexed[match[1]] || {};
|
||||
indexed[match[1]][match[2]] = value;
|
||||
}
|
||||
|
||||
return Object.values(indexed)
|
||||
.map(row => ({
|
||||
psStateId: parseInt(row.psStateId, 10),
|
||||
orbitoStatusId: normalizeSelectValue(row.orbitoStatusId)
|
||||
}))
|
||||
.filter(row => !Number.isNaN(row.psStateId));
|
||||
}
|
||||
|
||||
function mergeStatusMappingFromForm(existing, formRows) {
|
||||
let merged = Array.isArray(existing) ? existing.map(entry => ({ ...entry })) : [];
|
||||
for (const row of formRows) {
|
||||
merged = mergeStatusMappingEntry(merged, row.psStateId, row.orbitoStatusId);
|
||||
}
|
||||
return merged;
|
||||
}
|
||||
|
||||
function extractStatusMappingRows(input) {
|
||||
const byViewId = [];
|
||||
let sawMappingSelect = false;
|
||||
for (const [key, value] of Object.entries(input)) {
|
||||
const match = key.match(/^psMapStatus[._](\d+)$/);
|
||||
if (!match) continue;
|
||||
sawMappingSelect = true;
|
||||
const psStateId = parseInt(match[1], 10);
|
||||
const orbitoStatusId = normalizeSelectValue(value);
|
||||
if (!Number.isNaN(psStateId) && orbitoStatusId) {
|
||||
byViewId.push({ psStateId, orbitoStatusId });
|
||||
}
|
||||
}
|
||||
if (sawMappingSelect) return byViewId;
|
||||
|
||||
const rows = input.statusMappingRows;
|
||||
|
||||
if (Array.isArray(rows)) {
|
||||
@@ -158,7 +236,10 @@ function extractStatusMappingRows(input) {
|
||||
|
||||
function pickString(input, existing, inputKey, existingKey = inputKey) {
|
||||
if (Object.prototype.hasOwnProperty.call(input, inputKey)) {
|
||||
return String(input[inputKey] ?? '').trim();
|
||||
const value = String(input[inputKey] ?? '').trim();
|
||||
// Pola wstępnie wypełnione, których użytkownik nie ruszył, potrafią wrócić
|
||||
// puste ze zbierania withData — nie nadpisujemy nimi zapisanej wartości.
|
||||
if (value !== '') return value;
|
||||
}
|
||||
const prev = existing?.[existingKey];
|
||||
if (prev !== undefined && prev !== null && String(prev).trim() !== '') {
|
||||
@@ -170,44 +251,44 @@ function pickString(input, existing, inputKey, existingKey = inputKey) {
|
||||
function buildSavePayload(params, existing) {
|
||||
const input = normalizeFormParams(params);
|
||||
|
||||
const hasConnectionInput = ['storeUrl', 'apiUrl', 'apiKey', 'shopPrefix', 'shopName', 'versionCode', 'orderStatus']
|
||||
.some(key => Object.prototype.hasOwnProperty.call(input, key));
|
||||
|
||||
const storeUrl = pickString(input, hasConnectionInput ? null : existing, 'storeUrl', 'apiUrl');
|
||||
const apiKey = Object.prototype.hasOwnProperty.call(input, 'apiKey')
|
||||
? ((input.apiKey || '').trim() || existing?.apiKey || '')
|
||||
: (hasConnectionInput ? '' : (existing?.apiKey || ''));
|
||||
// Zawsze przekazujemy `existing` do pickString: puste pole formularza (np. URL,
|
||||
// którego użytkownik nie edytował) nie kasuje zapisanej wartości.
|
||||
const storeUrl = pickString(input, existing, 'storeUrl', 'apiUrl');
|
||||
const apiKey = (input.apiKey || '').trim() || existing?.apiKey || '';
|
||||
|
||||
const data = {
|
||||
apiUrl: storeUrl,
|
||||
apiKey,
|
||||
shopPrefix: pickString(input, hasConnectionInput ? null : existing, 'shopPrefix'),
|
||||
shopName: pickString(input, hasConnectionInput ? null : existing, 'shopName'),
|
||||
shopPrefix: pickString(input, existing, 'shopPrefix'),
|
||||
shopName: pickString(input, existing, 'shopName'),
|
||||
};
|
||||
|
||||
const versionCode = normalizeSelectValue(input.versionCode);
|
||||
if (versionCode || Object.prototype.hasOwnProperty.call(input, 'versionCode')) {
|
||||
if (versionCode) {
|
||||
data.versionCode = versionCode;
|
||||
} else if (existing?.versionCode) {
|
||||
data.versionCode = existing.versionCode;
|
||||
}
|
||||
|
||||
const orderStatus = normalizeSelectValue(input.orderStatus);
|
||||
if (orderStatus || Object.prototype.hasOwnProperty.call(input, 'orderStatus')) {
|
||||
data.orderStatus = orderStatus || null;
|
||||
if (orderStatus) {
|
||||
data.orderStatus = orderStatus;
|
||||
} else if (existing?.orderStatus) {
|
||||
data.orderStatus = existing.orderStatus;
|
||||
}
|
||||
|
||||
const mappingRows = extractStatusMappingRows(input);
|
||||
if (mappingRows.length) {
|
||||
data.statusMapping = mappingRows;
|
||||
} else if (hasMappingInput(input)) {
|
||||
data.statusMapping = [];
|
||||
} else if (typeof input.statusMappingJson !== 'undefined' || typeof input.statusMapping !== 'undefined') {
|
||||
data.statusMapping = parseStatusMapping(input.statusMapping ?? input.statusMappingJson);
|
||||
} else if (existing?.statusMapping) {
|
||||
data.statusMapping = existing.statusMapping;
|
||||
const formMappingRows = extractAllStatusMappingRows(input);
|
||||
if (formMappingRows.length) {
|
||||
data.statusMapping = mergeStatusMappingFromForm(existing?.statusMapping, formMappingRows);
|
||||
} else {
|
||||
const mappingRows = extractStatusMappingRows(input);
|
||||
if (mappingRows.length) {
|
||||
data.statusMapping = mappingRows;
|
||||
} else if (typeof input.statusMappingJson !== 'undefined' || typeof input.statusMapping !== 'undefined') {
|
||||
data.statusMapping = parseStatusMapping(input.statusMapping ?? input.statusMappingJson);
|
||||
} else if (existing?.statusMapping) {
|
||||
data.statusMapping = existing.statusMapping;
|
||||
}
|
||||
}
|
||||
|
||||
if (Array.isArray(existing?.psStates) && existing.psStates.length) {
|
||||
@@ -230,6 +311,9 @@ module.exports = {
|
||||
hasFormInput,
|
||||
hasMappingInput,
|
||||
extractStatusMappingRows,
|
||||
extractAllStatusMappingRows,
|
||||
mergeStatusMappingEntry,
|
||||
mergeStatusMappingFromForm,
|
||||
toFormResponse,
|
||||
buildSavePayload
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user