const { Log } = require('@orbito/api-lib');
const ShopConfig = require('../src/models/ShopConfig');
const { toFormResponse, CONNECTION_WITH_DATA, SAVE_WITH_DATA } = require('../src/config-form');
const PAGE_TARGET = 'psConfigPageRoot';
function refreshPage() {
return {
action: 'emit',
event: 'generatePage',
data: {
module: 'core/prestashop',
page: `config/${Date.now()}`,
target: PAGE_TARGET
}
};
}
function successToast(title, content) {
return {
action: 'toast',
type: 'success',
title,
content
};
}
function buildTemplate(form) {
const hasApiKey = Boolean(form.hasApiKey);
return {
type: 'flex',
direction: 'column',
gap: 3,
childs: [
{
type: 'card',
title: 'Konfiguracja PrestaShop',
content: [{
type: 'flex',
direction: 'column',
gap: 3,
childs: [
{
type: 'text',
content: 'Połączenie z Webservice API',
className: 'fw-bold'
},
{
type: 'text',
content: 'Uzupełnij dane dostępowe do sklepu PrestaShop. Klucz wygenerujesz w panelu sklepu w sekcji Zaawansowane → Web Service.',
className: 'text-muted small'
},
{
type: 'input',
id: 'psShopUrlInput',
name: 'storeUrl',
inputType: 'text',
label: 'URL sklepu',
placeholder: 'https://twoj-sklep.pl',
value: form.storeUrl || '',
groups: ['form']
},
{
type: 'input',
id: 'psApiKeyInput',
name: 'apiKey',
inputType: 'password',
label: 'Klucz API',
placeholder: hasApiKey
? 'Zostaw puste, aby zachować zapisany klucz'
: 'Wklej klucz Webservice API',
value: '',
groups: ['form']
},
...(hasApiKey ? [{
type: 'text',
content: 'Zapisany klucz API jest już skonfigurowany. Wpisz nowy tylko jeśli chcesz go zmienić.'
}] : []),
{
type: 'select',
id: 'psVersionInput',
name: 'versionCode',
label: 'Wersja sklepu',
placeholder: 'Wybierz wersję PrestaShop',
value: form.versionCode || '',
options: [
{ id: '9', label: 'PrestaShop 9' },
{ id: '8', label: 'PrestaShop 8' },
{ id: '1.7', label: 'PrestaShop 1.7' },
{ id: '1.6', label: 'PrestaShop 1.6 lub starsze' }
],
groups: ['form']
},
{
type: 'input',
id: 'psShopNameInput',
name: 'shopName',
inputType: 'text',
label: 'Nazwa sklepu (opcjonalnie)',
placeholder: 'np. Sklep główny',
value: form.shopName || '',
groups: ['form']
},
{
type: 'input',
id: 'psShopPrefixInput',
name: 'shopPrefix',
inputType: 'text',
label: 'Prefix sklepu (uniqueId)',
placeholder: 'np. PL',
value: form.shopPrefix || '',
groups: ['form']
},
{
type: 'text',
content: 'Prefix jest używany w uniqueId zamówienia, np. PL:389218. Przed zapisaniem przetestuj połączenie z API.',
style: { color: '#667085' }
},
{
type: 'text',
content: 'Status zamówienia',
className: 'fw-bold mt-2'
},
{
type: 'text',
content: 'Domyślny status Orbito, gdy brak mapowania PS → Orbito. Lista statusów pochodzi z modułu core/orders.',
className: 'text-muted small'
},
{
type: 'select',
id: 'psOrderStatusInput',
name: 'orderStatus',
label: 'Domyślny status po imporcie',
placeholder: 'Wybierz status zamówienia',
value: form.orderStatus || '',
optionsSource: {
module: 'core/orders',
method: 'status.search',
params: {},
labelKey: 'name',
idKey: '_id',
responsePath: 'results'
},
groups: ['form']
}
]
}],
footer: [
{
type: 'button',
label: 'Resetuj',
variant: 'danger',
outline: true,
icon: 'trash',
onClick: {
action: 'confirm',
type: 'alert',
title: 'Reset konfiguracji',
description: 'Czy na pewno chcesz usunąć aktualną konfigurację modułu PrestaShop?',
yesLabel: 'Resetuj',
noLabel: 'Anuluj',
onYes: {
action: 'callMethod',
module: 'core/prestashop',
method: 'config.reset_all',
then: [
refreshPage(),
successToast('Konfiguracja zresetowana', 'Dane konfiguracji zostały usunięte.')
]
}
}
},
{
type: 'button',
label: 'Testuj połączenie',
variant: 'info',
outline: true,
icon: 'plug-connected',
onClick: {
action: 'callMethod',
module: 'core/prestashop',
method: 'config.test_connection',
withData: CONNECTION_WITH_DATA,
then: successToast('Połączenie działa', 'Połączenie z API PrestaShop zostało poprawnie przetestowane.')
}
},
{
type: 'button',
label: 'Zapisz konfigurację',
variant: 'primary',
icon: 'device-floppy',
onClick: {
action: 'callMethod',
module: 'core/prestashop',
method: 'config.save',
withData: SAVE_WITH_DATA,
then: successToast('Zapisano konfigurację', 'Konfiguracja modułu PrestaShop została zapisana.')
}
}
]
},
{
type: 'card',
title: 'Mapowanie statusów PrestaShop → Orbito',
content: [{
type: 'flex',
direction: 'column',
gap: 3,
childs: [
{
type: 'text',
content: 'Pobierz statusy ze sklepu, a następnie przypisz każdy status PrestaShop do odpowiednika w Orbito. Przy imporcie używane jest current_state z zamówienia PS.',
className: 'text-muted small'
},
{
type: 'button',
label: 'Pobierz statusy PrestaShop',
variant: 'secondary',
outline: true,
icon: 'list',
onClick: {
action: 'callMethod',
module: 'core/prestashop',
method: 'config.get_ps_states',
withData: CONNECTION_WITH_DATA,
then: [
{ action: 'reload', target: 'statusMappingList' },
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: ' '
},
{
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']
}
]
}
]
}
}
]
}]
}
]
};
}
module.exports = async function(companyId, instanceId, body, params) {
try {
const config = await ShopConfig.findOne({ companyId, isRemoved: { $ne: true } });
const form = toFormResponse(config);
return {
type: 'ove',
template: buildTemplate(form)
};
} catch (err) {
Log.error(companyId, 'pages/config failed', { error: err?.message, stack: err?.stack });
return {
type: 'ove',
template: {
type: 'card',
title: 'Konfiguracja PrestaShop',
content: [{
type: 'text',
content: `