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
+18
View File
@@ -0,0 +1,18 @@
const { Log } = require('@orbito/api-lib');
const ShopConfig = require('../../src/models/ShopConfig');
const { toFormResponse } = require('../../src/config-form');
module.exports = async function(companyId, instanceId, params, ctx) {
const config = await ShopConfig.findOne({ companyId, isRemoved: { $ne: true } });
const form = toFormResponse(config);
Log.debug(companyId, 'config.get', {
found: Boolean(config),
storeUrl: form.storeUrl,
shopPrefix: form.shopPrefix,
hasApiKey: form.hasApiKey,
psStatesCount: form.psStates?.length ?? 0
});
return { data: form };
};
+61
View File
@@ -0,0 +1,61 @@
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(companyId, instanceId, params, ctx) {
const existing = await ShopConfig.findOne({ companyId, 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(companyId, 'PrestaShop order states saved', { configId: existing._id, count: psStates.length });
} else {
await ShopConfig.create({
companyId,
...payload,
shopPrefix: (input.shopPrefix || '').trim()
});
Log.info(companyId, 'PrestaShop config created with order states', { count: psStates.length });
}
return methodOk({ count: psStates.length, psStates });
} catch (e) {
Log.warn(companyId, 'config.get_ps_states failed', { storeUrl, error: e.message });
return methodFail('Nie udało się pobrać statusów: ' + (e.message || 'błąd API PrestaShop'));
}
};
+13
View File
@@ -0,0 +1,13 @@
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(companyId, instanceId, params, ctx) {
const config = await ShopConfig.findOne({ companyId, 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);
};
+14
View File
@@ -0,0 +1,14 @@
const { Log } = require('@orbito/api-lib');
const ShopConfig = require('../../src/models/ShopConfig');
const { methodOk } = require('../../src/method-result');
module.exports = async function(companyId, instanceId, params, ctx) {
const configs = await ShopConfig.find({ companyId, isRemoved: { $ne: true } });
for (const config of configs) {
await ShopConfig.update({ _id: config._id }, { isRemoved: true });
}
Log.info(companyId, 'PrestaShop config reset', { removed: configs.length });
return methodOk({ removed: configs.length });
};
+43
View File
@@ -0,0 +1,43 @@
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(companyId, instanceId, params, ctx) {
try {
const input = normalizeFormParams(params);
const existing = await ShopConfig.findOne({ companyId, isRemoved: { $ne: true } });
Log.debug(companyId, '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 = { companyId, ...data };
if (existing) {
await ShopConfig.update({ _id: existing._id }, payload);
Log.info(companyId, '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(companyId, 'PrestaShop config created', { configId: created._id });
return methodOk({ data: toFormResponse(created) });
} catch (err) {
Log.error(companyId, 'config.save failed', { error: err?.message });
return methodFail(err?.message || 'Nie udało się zapisać konfiguracji');
}
};
+38
View File
@@ -0,0 +1,38 @@
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(companyId, instanceId, params, ctx) {
const existing = await ShopConfig.findOne({ companyId, 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(companyId, 'PrestaShop connection test OK', { storeUrl, statusesCount: count });
return methodOk({ statusesCount: count });
} catch (e) {
Log.warn(companyId, 'PrestaShop connection test failed', { storeUrl, error: e.message });
return methodFail('Połączenie nieudane: ' + (e.message || 'błąd API PrestaShop'));
}
};