Initial commit: core-prestashop module v1.1.1

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
fatequod
2026-07-07 14:30:30 +02:00
co-authored by Cursor
commit f163100ad4
33 changed files with 2605 additions and 0 deletions
+345
View File
@@ -0,0 +1,345 @@
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: '<span class="text-muted small">Zapisany klucz API jest już skonfigurowany. Wpisz nowy tylko jeśli chcesz go zmienić.</span>'
}] : []),
{
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 <code>uniqueId</code> zamówienia, np. <code>PL:389218</code>. 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 <strong>core/orders</strong>.',
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 <code>current_state</code> 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: '<span class="badge" style="background-color:{{ statusMappingList.$.psStateColor }};min-width:12px">&nbsp;</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']
}
]
}
]
}
}
]
}]
}
]
};
}
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: `<div class="alert alert-danger">Nie udało się załadować konfiguracji: ${escapeHtml(err?.message || 'nieznany błąd')}</div>`
}]
}
};
}
};
function escapeHtml(str) {
return String(str)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
}
+135
View File
@@ -0,0 +1,135 @@
const { Log, Core, Utils, Pages } = require('@orbito/api-lib');
const ShopConfig = require('../src/models/ShopConfig');
module.exports = async function(companyId, instanceId, body, params) {
const configId = params.shopId;
const isEdit = !!configId;
let config = null;
if (isEdit) {
config = await ShopConfig.findOne({ _id: configId, companyId, isRemoved: { $ne: true } });
if (!config) throw new Error('Config not found');
}
const res = await Core.callModule(companyId, 'core/orders', 'status.search', {});
const orderStatuses = res.results || [];
return {
type: 'ove',
template: {
type: 'card',
title: isEdit ? `Edytuj konfigurację: ${config.shopName || configId}` : 'Nowa konfiguracja sklepu',
description: isEdit
? 'Zaktualizuj dane połączenia ze sklepem Prestashop.'
: 'Uzupełnij dane połączenia z nowym sklepem Prestashop.',
content: [
{
type: 'row',
cols: [
{
size: 6,
childs: [
{
type: 'input',
name: 'shopName',
inputType: 'text',
label: 'Nazwa sklepu',
value: config?.shopName || '',
groups: ['config-data']
}
]
},
{
size: 6,
childs: [
{
type: 'select',
name: 'versionCode',
label: 'Wersja sklepu',
value: config?.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: ['config-data']
}
]
},
{
size: 12,
childs: [
{
type: 'input',
name: 'apiUrl',
inputType: 'text',
label: 'API URL',
placeholder: 'https://sklep.pl/api',
value: config?.apiUrl || '',
groups: ['config-data']
},
{
type: 'input',
name: 'apiKey',
inputType: 'text',
label: 'API Key',
placeholder: 'Klucz API Prestashop',
value: config?.apiKey || '',
groups: ['config-data']
},
{
type: 'select',
name: 'orderStatus',
label: 'Domyślny status zamówienia po pobraniu z Prestashop',
value: config?.orderStatus || '',
options: orderStatuses.map(status => ({ id: status._id, label: status.name })),
groups: ['config-data']
}
]
}
]
}
],
footer: [
{
type: 'button',
label: 'Wróć do listy',
variant: 'secondary',
outline: true,
icon: 'arrow-left',
onClick: {
action: 'emit',
event: 'generatePage',
data: {
module: 'core/prestashop',
page: 'config'
}
}
},
{
type: 'button',
label: isEdit ? 'Zapisz zmiany' : 'Utwórz konfigurację',
variant: 'primary',
icon: 'device-floppy',
groups: ['config-data'],
onClick: {
action: 'callMethod',
module: 'core/prestashop',
method: 'updateConfig',
params: isEdit ? { _id: configId } : {},
withData: ['#config-data'],
then: {
action: 'emit',
event: 'generatePage',
data: {
module: 'core/prestashop',
page: 'config'
}
}
}
}
]
}
};
}