Remove companyId from handlers, models, cron jobs, and cross-module calls to match the v2 one-module-one-company architecture. Co-authored-by: Cursor <cursoragent@cursor.com>
44 lines
1.7 KiB
JavaScript
44 lines
1.7 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(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');
|
|
}
|
|
};
|