44 lines
1.8 KiB
JavaScript
44 lines
1.8 KiB
JavaScript
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');
|
|
}
|
|
};
|