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:
@@ -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');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user