Migrate PrestaShop config to Config API blocks and add permissions.
Replace custom ShopConfig pages/models with registerConfigBlocks, load config from Config store, and enforce RBAC on methods. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
const { Log } = require('@orbito/api-lib');
|
||||
const ShopConfig = require('../../src/models/ShopConfig');
|
||||
const { toFormResponse } = require('../../src/config-form');
|
||||
|
||||
module.exports = async function(params, ctx) {
|
||||
const config = await ShopConfig.findOne({ isRemoved: { $ne: true } });
|
||||
const form = toFormResponse(config);
|
||||
|
||||
Log.debug( 'config.get', {
|
||||
found: Boolean(config),
|
||||
storeUrl: form.storeUrl,
|
||||
shopPrefix: form.shopPrefix,
|
||||
hasApiKey: form.hasApiKey,
|
||||
psStatesCount: form.psStates?.length ?? 0
|
||||
});
|
||||
|
||||
return { data: form };
|
||||
};
|
||||
@@ -1,60 +0,0 @@
|
||||
const { Log } = require('@orbito/api-lib');
|
||||
const PrestashopApi = require('../../src/PrestaApi');
|
||||
const ShopConfig = require('../../src/models/ShopConfig');
|
||||
const { isLegacyApi } = require('../../src/utils');
|
||||
const { extractPsStateName } = require('../../src/status-mapper');
|
||||
const { normalizeFormParams } = require('../../src/config-form');
|
||||
const { methodOk, methodFail } = require('../../src/method-result');
|
||||
|
||||
module.exports = async function(params, ctx) {
|
||||
const existing = await ShopConfig.findOne({ isRemoved: { $ne: true } });
|
||||
const input = normalizeFormParams(params);
|
||||
|
||||
const storeUrl = (input.storeUrl || existing?.apiUrl || '').trim();
|
||||
const apiKey = (input.apiKey || '').trim() || existing?.apiKey || '';
|
||||
const versionCode = input.versionCode ?? existing?.versionCode ?? '';
|
||||
|
||||
if (!storeUrl || !apiKey) {
|
||||
return methodFail('Podaj URL sklepu i klucz API (zapisz konfigurację lub uzupełnij formularz)');
|
||||
}
|
||||
|
||||
let api;
|
||||
try {
|
||||
api = new PrestashopApi(storeUrl, apiKey, isLegacyApi(versionCode));
|
||||
} catch (e) {
|
||||
return methodFail('Nieprawidłowy URL sklepu: ' + e.message);
|
||||
}
|
||||
|
||||
try {
|
||||
const rawStates = await api.getStatuses();
|
||||
const psStates = (Array.isArray(rawStates) ? rawStates : [])
|
||||
.filter(s => !s.deleted || s.deleted === '0' || s.deleted === 0)
|
||||
.map(s => ({
|
||||
id: parseInt(s.id, 10),
|
||||
name: extractPsStateName(s.name),
|
||||
color: s.color || '#cccccc'
|
||||
}));
|
||||
|
||||
const payload = {
|
||||
apiUrl: storeUrl,
|
||||
apiKey,
|
||||
psStates
|
||||
};
|
||||
|
||||
if (existing) {
|
||||
await ShopConfig.update({ _id: existing._id }, payload);
|
||||
Log.info( 'PrestaShop order states saved', { configId: existing._id, count: psStates.length });
|
||||
} else {
|
||||
await ShopConfig.create({
|
||||
...payload,
|
||||
shopPrefix: (input.shopPrefix || '').trim()
|
||||
});
|
||||
Log.info( 'PrestaShop config created with order states', { count: psStates.length });
|
||||
}
|
||||
|
||||
return methodOk({ count: psStates.length, psStates });
|
||||
} catch (e) {
|
||||
Log.warn( 'config.get_ps_states failed', { storeUrl, error: e.message });
|
||||
return methodFail('Nie udało się pobrać statusów: ' + (e.message || 'błąd API PrestaShop'));
|
||||
}
|
||||
};
|
||||
@@ -1,13 +0,0 @@
|
||||
const ShopConfig = require('../../src/models/ShopConfig');
|
||||
const { buildMappingRows } = require('../../src/config-form');
|
||||
|
||||
// Zwraca wiersze UI mapowania statusów PS → Orbito (tablica dla widoku type: list).
|
||||
module.exports = async function(params, ctx) {
|
||||
const config = await ShopConfig.findOne({ isRemoved: { $ne: true } });
|
||||
if (!config) return [];
|
||||
|
||||
const psStates = Array.isArray(config.psStates) ? config.psStates : [];
|
||||
const statusMapping = Array.isArray(config.statusMapping) ? config.statusMapping : [];
|
||||
|
||||
return buildMappingRows(psStates, statusMapping);
|
||||
};
|
||||
@@ -1,14 +0,0 @@
|
||||
const { Log } = require('@orbito/api-lib');
|
||||
const ShopConfig = require('../../src/models/ShopConfig');
|
||||
const { methodOk } = require('../../src/method-result');
|
||||
|
||||
module.exports = async function(params, ctx) {
|
||||
const configs = await ShopConfig.find({ isRemoved: { $ne: true } });
|
||||
|
||||
for (const config of configs) {
|
||||
await ShopConfig.update({ _id: config._id }, { isRemoved: true });
|
||||
}
|
||||
|
||||
Log.info( 'PrestaShop config reset', { removed: configs.length });
|
||||
return methodOk({ removed: configs.length });
|
||||
};
|
||||
@@ -1,43 +0,0 @@
|
||||
const { Log } = require('@orbito/api-lib');
|
||||
const ShopConfig = require('../../src/models/ShopConfig');
|
||||
const { toFormResponse, buildSavePayload, normalizeFormParams } = require('../../src/config-form');
|
||||
const { methodOk, methodFail } = require('../../src/method-result');
|
||||
|
||||
module.exports = async function(params, ctx) {
|
||||
try {
|
||||
const input = normalizeFormParams(params);
|
||||
const existing = await ShopConfig.findOne({ isRemoved: { $ne: true } });
|
||||
|
||||
Log.debug( 'config.save input', {
|
||||
keys: Object.keys(input),
|
||||
storeUrl: input.storeUrl,
|
||||
shopPrefix: input.shopPrefix,
|
||||
shopName: input.shopName
|
||||
});
|
||||
|
||||
const data = buildSavePayload(input, existing);
|
||||
|
||||
if (!data.apiUrl) {
|
||||
return methodFail('URL sklepu jest wymagany. Wpisz adres w polu „URL sklepu” i spróbuj ponownie.');
|
||||
}
|
||||
if (!data.apiKey) {
|
||||
return methodFail('Klucz API jest wymagany. Wpisz klucz Webservice API lub zachowaj już zapisany.');
|
||||
}
|
||||
|
||||
const payload = { ...data };
|
||||
|
||||
if (existing) {
|
||||
await ShopConfig.update({ _id: existing._id }, payload);
|
||||
Log.info( 'PrestaShop config updated', { configId: existing._id });
|
||||
const updated = await ShopConfig.findOne({ _id: existing._id });
|
||||
return methodOk({ data: toFormResponse(updated) });
|
||||
}
|
||||
|
||||
const created = await ShopConfig.create(payload);
|
||||
Log.info( 'PrestaShop config created', { configId: created._id });
|
||||
return methodOk({ data: toFormResponse(created) });
|
||||
} catch (err) {
|
||||
Log.error( 'config.save failed', { error: err?.message });
|
||||
return methodFail(err?.message || 'Nie udało się zapisać konfiguracji');
|
||||
}
|
||||
};
|
||||
@@ -1,41 +0,0 @@
|
||||
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');
|
||||
}
|
||||
};
|
||||
@@ -1,38 +0,0 @@
|
||||
const { Log } = require('@orbito/api-lib');
|
||||
const PrestashopApi = require('../../src/PrestaApi');
|
||||
const ShopConfig = require('../../src/models/ShopConfig');
|
||||
const { isLegacyApi } = require('../../src/utils');
|
||||
const { normalizeFormParams } = require('../../src/config-form');
|
||||
const { methodOk, methodFail } = require('../../src/method-result');
|
||||
|
||||
module.exports = async function(params, ctx) {
|
||||
const existing = await ShopConfig.findOne({ isRemoved: { $ne: true } });
|
||||
const input = normalizeFormParams(params);
|
||||
|
||||
const storeUrl = (input.storeUrl || existing?.apiUrl || '').trim();
|
||||
const apiKey = (input.apiKey || '').trim() || existing?.apiKey || '';
|
||||
|
||||
if (!storeUrl || !apiKey) {
|
||||
return methodFail('Podaj URL sklepu oraz klucz API przed testem połączenia');
|
||||
}
|
||||
|
||||
let api;
|
||||
try {
|
||||
api = new PrestashopApi(storeUrl, apiKey, isLegacyApi(input.versionCode ?? existing?.versionCode));
|
||||
} catch (e) {
|
||||
return methodFail('Nieprawidłowy URL sklepu: ' + e.message);
|
||||
}
|
||||
|
||||
try {
|
||||
const statuses = await api.getStatuses();
|
||||
if (!statuses) {
|
||||
return methodFail('API nie zwróciło danych (sprawdź klucz Webservice i uprawnienia)');
|
||||
}
|
||||
const count = Array.isArray(statuses) ? statuses.length : undefined;
|
||||
Log.info( 'PrestaShop connection test OK', { storeUrl, statusesCount: count });
|
||||
return methodOk({ statusesCount: count });
|
||||
} catch (e) {
|
||||
Log.warn( 'PrestaShop connection test failed', { storeUrl, error: e.message });
|
||||
return methodFail('Połączenie nieudane: ' + (e.message || 'błąd API PrestaShop'));
|
||||
}
|
||||
};
|
||||
@@ -1,14 +0,0 @@
|
||||
const { DB, Utils, Log } = require('@orbito/api-lib');
|
||||
const ShopConfigSchema = require('../src/models/ShopConfig');
|
||||
|
||||
module.exports = async function(params, ctx) {
|
||||
const { _id } = params;
|
||||
if (!_id) throw new Error('Missing _id');
|
||||
|
||||
const config = await ShopConfigSchema.findOne({ _id, isRemoved: { $ne: true } });
|
||||
if (!config) throw new Error('Config not found');
|
||||
|
||||
await ShopConfigSchema.update({ _id }, { isRemoved: true });
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
const { DB, Utils, Log } = require('@orbito/api-lib');
|
||||
const ShopConfigSchema = require('../src/models/ShopConfig');
|
||||
const PrestashopApi = require('../src/PrestaApi');
|
||||
|
||||
module.exports = async function(params, ctx) {
|
||||
const page = parseInt(params.page) || 1;
|
||||
const perPage = parseInt(params.perPage) || 10;
|
||||
const skip = (page - 1) * perPage;
|
||||
|
||||
const query = { isRemoved: { $ne: true } };
|
||||
|
||||
const results = await ShopConfigSchema.find(query, perPage, skip, { createdAt: -1 });
|
||||
const total = await ShopConfigSchema.count(query);
|
||||
|
||||
const resultsWithStatus = await Promise.all(results.map(async (config) => {
|
||||
if (!config.apiUrl || !config.apiKey) {
|
||||
return { ...config, connected: false };
|
||||
}
|
||||
|
||||
try {
|
||||
const api = new PrestashopApi(config.apiUrl, config.apiKey);
|
||||
await api.getStatuses();
|
||||
return { ...config, connected: true };
|
||||
} catch (e) {
|
||||
return { ...config, connected: false };
|
||||
}
|
||||
}));
|
||||
|
||||
return { results: resultsWithStatus, total, page, perPage };
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
const { getFilters } = require('../../src/orders-filter-store');
|
||||
const { PERMISSIONS, requirePermission } = require('../../src/register-permissions');
|
||||
|
||||
module.exports = async function(params, ctx) {
|
||||
requirePermission(PERMISSIONS.ORDERS_VIEW, ctx);
|
||||
|
||||
module.exports = async function() {
|
||||
const filters = await getFilters();
|
||||
return {
|
||||
page: parseInt(filters.page, 10) || 1,
|
||||
|
||||
@@ -2,8 +2,11 @@ const { Log } = require('@orbito/api-lib');
|
||||
const { searchOrders } = require('../../src/orders-api');
|
||||
const { normalizeFilterParams, FILTER_KEYS } = require('../../src/orders-filter-form');
|
||||
const { getFilters, mergeFilters, clearFilters } = require('../../src/orders-filter-store');
|
||||
const { PERMISSIONS, requirePermission } = require('../../src/register-permissions');
|
||||
|
||||
module.exports = async function(params, ctx) {
|
||||
requirePermission(PERMISSIONS.ORDERS_VIEW, ctx);
|
||||
|
||||
const input = { ...(params || {}) };
|
||||
|
||||
if (input.clearFilters) {
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
const ShopConfig = require('../../src/models/ShopConfig');
|
||||
const PrestashopApi = require('../../src/PrestaApi');
|
||||
const { isLegacyApi } = require('../../src/utils');
|
||||
const { loadConfig, isConfigured } = require('../../src/module-config');
|
||||
const { PERMISSIONS, requirePermission } = require('../../src/register-permissions');
|
||||
|
||||
module.exports = async function(params, ctx) {
|
||||
requirePermission(PERMISSIONS.ORDERS_VIEW, ctx);
|
||||
|
||||
const productIds = Array.isArray(params?.productIds) ? params.productIds : [];
|
||||
if (productIds.length === 0) return {};
|
||||
|
||||
const configs = await ShopConfig.find({ isRemoved: { $ne: true } });
|
||||
const config = configs.find(c => c.apiUrl && c.apiKey);
|
||||
if (!config) return {};
|
||||
const config = await loadConfig();
|
||||
if (!isConfigured(config)) return {};
|
||||
|
||||
const api = new PrestashopApi(config.apiUrl, config.apiKey, isLegacyApi(config.versionCode));
|
||||
const result = {};
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
const { Core } = require('@orbito/api-lib');
|
||||
const { PERMISSIONS, requirePermission } = require('../../src/register-permissions');
|
||||
|
||||
// Proxy do core/orders — statusy są współdzielone i zarządzane w core/orders.
|
||||
module.exports = async function(params, ctx) {
|
||||
requirePermission(PERMISSIONS.ORDERS_VIEW, ctx);
|
||||
return Core.callModule('core/orders', 'status.get_with_counts', params || {});
|
||||
};
|
||||
|
||||
+6
-11
@@ -1,20 +1,15 @@
|
||||
const { Core } = require('@orbito/api-lib');
|
||||
const ShopConfig = require('../src/models/ShopConfig');
|
||||
const PrestashopApi = require('../src/PrestaApi');
|
||||
const { importFromPSOrder } = require('../src/OrderImporter');
|
||||
const { isLegacyApi } = require('../src/utils');
|
||||
const { loadConfig, isConfigured, setLastOrderId } = require('../src/module-config');
|
||||
const { PERMISSIONS, requirePermission } = require('../src/register-permissions');
|
||||
|
||||
module.exports = async function(params, ctx) {
|
||||
const configs = await ShopConfig.find({ isRemoved: { $ne: true } });
|
||||
const active = configs.filter(c => c.apiUrl && c.apiKey);
|
||||
requirePermission(PERMISSIONS.ORDERS_IMPORT, ctx);
|
||||
|
||||
if (active.length === 0) return { error: 'No active shop configs found' };
|
||||
|
||||
const config = params.configId
|
||||
? active.find(c => String(c._id) === String(params.configId))
|
||||
: active[0];
|
||||
|
||||
if (!config) return { error: 'Config not found' };
|
||||
const config = await loadConfig();
|
||||
if (!isConfigured(config)) return { error: 'No active shop configs found' };
|
||||
|
||||
const api = new PrestashopApi(config.apiUrl, config.apiKey, isLegacyApi(config.versionCode));
|
||||
|
||||
@@ -49,7 +44,7 @@ module.exports = async function(params, ctx) {
|
||||
}
|
||||
}
|
||||
const maxId = Math.max(...orders.map(o => parseInt(o.id)));
|
||||
await ShopConfig.update({ _id: config._id }, { lastOrderId: maxId });
|
||||
await setLastOrderId(maxId);
|
||||
return { shopName: config.shopName, imported, errors, newLastOrderId: maxId };
|
||||
}
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
const { DB, Utils, Log } = require('@orbito/api-lib');
|
||||
const ShopConfigSchema = require('../src/models/ShopConfig');
|
||||
|
||||
module.exports = async function(params, ctx) {
|
||||
const editableFields = ['apiUrl', 'apiKey', 'shopName', 'versionCode', 'orderStatus'];
|
||||
const dataToUpdate = {};
|
||||
for (const field of editableFields) {
|
||||
if (typeof params[field] !== 'undefined') {
|
||||
dataToUpdate[field] = params[field];
|
||||
}
|
||||
}
|
||||
|
||||
// Jeśli podano _id – aktualizuj konkretny rekord
|
||||
if (params._id) {
|
||||
const config = await ShopConfigSchema.findOne({ _id: params._id, isRemoved: { $ne: true } });
|
||||
if (!config) throw new Error('Config not found');
|
||||
|
||||
await ShopConfigSchema.update({ _id: config._id }, dataToUpdate);
|
||||
return await ShopConfigSchema.findOne({ _id: config._id });
|
||||
}
|
||||
|
||||
// Brak _id – twórz nowy rekord
|
||||
const newConfig = await ShopConfigSchema.create(dataToUpdate);
|
||||
return newConfig;
|
||||
}
|
||||
Reference in New Issue
Block a user