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:
@@ -0,0 +1,90 @@
|
||||
const { Config } = require('@orbito/api-lib');
|
||||
|
||||
/** Stały identyfikator sklepu (jeden sklep na firmę) — używany w PsOrder.shopConfigId. */
|
||||
const SHOP_ID = 'default';
|
||||
|
||||
function isConfigured(config) {
|
||||
return !!(config && config.apiUrl && config.apiKey);
|
||||
}
|
||||
|
||||
async function loadPsStates() {
|
||||
const states = await Config.get('runtime.psStates', []);
|
||||
return Array.isArray(states) ? states : [];
|
||||
}
|
||||
|
||||
async function loadStatusMapping(psStates) {
|
||||
const states = Array.isArray(psStates) ? psStates : await loadPsStates();
|
||||
const statusMapping = [];
|
||||
|
||||
await Promise.all(states.map(async (state) => {
|
||||
const id = parseInt(state.id ?? state.psStateId, 10);
|
||||
if (Number.isNaN(id)) return;
|
||||
const orbitoStatusId = await Config.get(`mapping.state_${id}`, '');
|
||||
if (orbitoStatusId) {
|
||||
statusMapping.push({ psStateId: id, orbitoStatusId: String(orbitoStatusId) });
|
||||
}
|
||||
}));
|
||||
|
||||
return statusMapping;
|
||||
}
|
||||
|
||||
// Shape kompatybilny z dotychczasowym użyciem (OrderImporter, CRON, PrestaApi).
|
||||
async function loadConfig() {
|
||||
const [
|
||||
apiUrl,
|
||||
apiKey,
|
||||
shopName,
|
||||
shopPrefix,
|
||||
versionCode,
|
||||
orderStatus,
|
||||
lastOrderId,
|
||||
psStates
|
||||
] = await Promise.all([
|
||||
Config.get('connection.apiUrl', ''),
|
||||
Config.get('connection.apiKey', ''),
|
||||
Config.get('connection.shopName', ''),
|
||||
Config.get('connection.shopPrefix', ''),
|
||||
Config.get('connection.versionCode', ''),
|
||||
Config.get('import.orderStatus', ''),
|
||||
Config.get('runtime.lastOrderId', null),
|
||||
loadPsStates()
|
||||
]);
|
||||
|
||||
const statusMapping = await loadStatusMapping(psStates);
|
||||
|
||||
return {
|
||||
_id: SHOP_ID,
|
||||
apiUrl: apiUrl || '',
|
||||
apiKey: apiKey || '',
|
||||
shopName: shopName || '',
|
||||
shopPrefix: shopPrefix || '',
|
||||
versionCode: versionCode || '',
|
||||
orderStatus: orderStatus || '',
|
||||
lastOrderId: lastOrderId == null ? null : Number(lastOrderId),
|
||||
psStates,
|
||||
statusMapping
|
||||
};
|
||||
}
|
||||
|
||||
async function loadEnabledConfigs() {
|
||||
const config = await loadConfig();
|
||||
return isConfigured(config) ? [config] : [];
|
||||
}
|
||||
|
||||
async function setLastOrderId(lastOrderId) {
|
||||
await Config.set('runtime.lastOrderId', Number(lastOrderId));
|
||||
}
|
||||
|
||||
async function setPsStates(psStates) {
|
||||
await Config.set('runtime.psStates', Array.isArray(psStates) ? psStates : []);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
SHOP_ID,
|
||||
isConfigured,
|
||||
loadConfig,
|
||||
loadEnabledConfigs,
|
||||
loadPsStates,
|
||||
setLastOrderId,
|
||||
setPsStates
|
||||
};
|
||||
Reference in New Issue
Block a user