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:
+103
-78
@@ -1,6 +1,6 @@
|
||||
const { Log } = require('@orbito/api-lib');
|
||||
const { Log, Core } = require('@orbito/api-lib');
|
||||
const ShopConfig = require('../src/models/ShopConfig');
|
||||
const { toFormResponse, CONNECTION_WITH_DATA, SAVE_WITH_DATA } = require('../src/config-form');
|
||||
const { toFormResponse, CONNECTION_WITH_DATA } = require('../src/config-form');
|
||||
|
||||
const PAGE_TARGET = 'psConfigPageRoot';
|
||||
|
||||
@@ -25,9 +25,90 @@ function successToast(title, content) {
|
||||
};
|
||||
}
|
||||
|
||||
function buildTemplate(form) {
|
||||
function buildMappingRowViews(form, orbitoStatusOptions) {
|
||||
const rows = Array.isArray(form.statusMappingRows) ? form.statusMappingRows : [];
|
||||
|
||||
if (!rows.length) {
|
||||
return [{
|
||||
type: 'text',
|
||||
content: 'Brak statusów PrestaShop — kliknij „Pobierz statusy PrestaShop”.',
|
||||
className: 'text-muted'
|
||||
}];
|
||||
}
|
||||
|
||||
// Wiersze renderujemy po stronie serwera, aby każdy select miał LITERALNE,
|
||||
// unikalne id (psMapStatus_<psStateId>). Handlebars nie działa w polu `id`,
|
||||
// a `#form` nie zbiera danych przy renderze przez generatePage+target — dlatego
|
||||
// zapis (przycisk oraz onChange) odwołuje się do konkretnych id widoków.
|
||||
return rows.map(row => {
|
||||
const selectId = `psMapStatus_${row.psStateId}`;
|
||||
return {
|
||||
type: 'row',
|
||||
cols: [
|
||||
{
|
||||
size: 5,
|
||||
childs: [{
|
||||
type: 'flex',
|
||||
direction: 'row',
|
||||
align: 'center',
|
||||
gap: 2,
|
||||
childs: [
|
||||
{
|
||||
type: 'html',
|
||||
content: `<span class="badge" style="background-color:${escapeHtml(row.psStateColor)};min-width:12px"> </span>`
|
||||
},
|
||||
{
|
||||
type: 'flex',
|
||||
direction: 'column',
|
||||
gap: 0,
|
||||
childs: [
|
||||
{
|
||||
type: 'text',
|
||||
content: row.psStateName,
|
||||
className: 'fw-semibold'
|
||||
},
|
||||
{
|
||||
type: 'text',
|
||||
content: `ID PrestaShop: ${row.psStateId}`,
|
||||
className: 'text-muted small'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
size: 7,
|
||||
childs: [{
|
||||
type: 'select',
|
||||
id: selectId,
|
||||
name: selectId,
|
||||
label: 'Status Orbito',
|
||||
placeholder: 'Wybierz status Orbito',
|
||||
value: row.orbitoStatusId || '',
|
||||
allowClear: true,
|
||||
options: orbitoStatusOptions,
|
||||
onChange: {
|
||||
action: 'callMethod',
|
||||
module: 'core/prestashop',
|
||||
method: 'config.save_mapping_row',
|
||||
params: { psStateId: row.psStateId },
|
||||
withData: [selectId]
|
||||
}
|
||||
}]
|
||||
}
|
||||
]
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function buildTemplate(form, orbitoStatusOptions) {
|
||||
const hasApiKey = Boolean(form.hasApiKey);
|
||||
|
||||
const mappingSelectIds = (Array.isArray(form.statusMappingRows) ? form.statusMappingRows : [])
|
||||
.map(row => `psMapStatus_${row.psStateId}`);
|
||||
const saveWithData = [...CONNECTION_WITH_DATA, ...mappingSelectIds];
|
||||
|
||||
return {
|
||||
type: 'flex',
|
||||
direction: 'column',
|
||||
@@ -194,7 +275,7 @@ function buildTemplate(form) {
|
||||
action: 'callMethod',
|
||||
module: 'core/prestashop',
|
||||
method: 'config.save',
|
||||
withData: SAVE_WITH_DATA,
|
||||
withData: saveWithData,
|
||||
then: successToast('Zapisano konfigurację', 'Konfiguracja modułu PrestaShop została zapisana.')
|
||||
}
|
||||
}
|
||||
@@ -225,84 +306,16 @@ function buildTemplate(form) {
|
||||
method: 'config.get_ps_states',
|
||||
withData: CONNECTION_WITH_DATA,
|
||||
then: [
|
||||
{ action: 'reload', target: 'statusMappingList' },
|
||||
refreshPage(),
|
||||
successToast('Statusy pobrane', 'Lista statusów PrestaShop została załadowana.')
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
type: 'list',
|
||||
id: 'statusMappingList',
|
||||
module: 'core/prestashop',
|
||||
method: 'config.get_status_mapping',
|
||||
emptyMessage: 'Brak statusów PrestaShop — kliknij „Pobierz statusy PrestaShop”.',
|
||||
loadingMessage: 'Ładowanie mapowania statusów...',
|
||||
template: {
|
||||
type: 'row',
|
||||
cols: [
|
||||
{
|
||||
size: 5,
|
||||
childs: [{
|
||||
type: 'flex',
|
||||
direction: 'row',
|
||||
align: 'center',
|
||||
gap: 2,
|
||||
childs: [
|
||||
{
|
||||
type: 'html',
|
||||
content: '<span class="badge" style="background-color:{{ statusMappingList.$.psStateColor }};min-width:12px"> </span>'
|
||||
},
|
||||
{
|
||||
type: 'flex',
|
||||
direction: 'column',
|
||||
gap: 0,
|
||||
childs: [
|
||||
{
|
||||
type: 'text',
|
||||
content: '{{ statusMappingList.$.psStateName }}',
|
||||
className: 'fw-semibold'
|
||||
},
|
||||
{
|
||||
type: 'text',
|
||||
content: 'ID PrestaShop: {{ statusMappingList.$.psStateId }}',
|
||||
className: 'text-muted small'
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}]
|
||||
},
|
||||
{
|
||||
size: 7,
|
||||
childs: [
|
||||
{
|
||||
type: 'input',
|
||||
name: 'statusMappingRows.$.psStateId',
|
||||
inputType: 'hidden',
|
||||
value: '{{ statusMappingList.$.psStateId }}',
|
||||
groups: ['form']
|
||||
},
|
||||
{
|
||||
type: 'select',
|
||||
name: 'statusMappingRows.$.orbitoStatusId',
|
||||
label: 'Status Orbito',
|
||||
placeholder: 'Wybierz status Orbito',
|
||||
value: '{{ statusMappingList.$.orbitoStatusId }}',
|
||||
allowClear: true,
|
||||
optionsSource: {
|
||||
module: 'core/orders',
|
||||
method: 'status.search',
|
||||
params: {},
|
||||
labelKey: 'name',
|
||||
idKey: '_id',
|
||||
responsePath: 'results'
|
||||
},
|
||||
groups: ['form']
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
type: 'flex',
|
||||
direction: 'column',
|
||||
gap: 2,
|
||||
childs: buildMappingRowViews(form, orbitoStatusOptions)
|
||||
}
|
||||
]
|
||||
}]
|
||||
@@ -311,14 +324,26 @@ function buildTemplate(form) {
|
||||
};
|
||||
}
|
||||
|
||||
async function loadOrbitoStatusOptions() {
|
||||
try {
|
||||
const res = await Core.callModule('core/orders', 'status.search', {});
|
||||
const results = res?.results || res?.data?.results || [];
|
||||
return results.map(status => ({ id: String(status._id), label: status.name }));
|
||||
} catch (err) {
|
||||
Log.warn('pages/config: nie udało się pobrać statusów Orbito', { error: err?.message });
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = async function(body, params) {
|
||||
try {
|
||||
const config = await ShopConfig.findOne({ isRemoved: { $ne: true } });
|
||||
const form = toFormResponse(config);
|
||||
const orbitoStatusOptions = await loadOrbitoStatusOptions();
|
||||
|
||||
return {
|
||||
type: 'ove',
|
||||
template: buildTemplate(form)
|
||||
template: buildTemplate(form, orbitoStatusOptions)
|
||||
};
|
||||
} catch (err) {
|
||||
Log.error( 'pages/config failed', { error: err?.message, stack: err?.stack });
|
||||
|
||||
Reference in New Issue
Block a user