Migrate to @orbito/api-lib 2.0.0 for single-company container model.
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>
This commit is contained in:
@@ -2,11 +2,11 @@ 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 } });
|
||||
module.exports = async function(params, ctx) {
|
||||
const config = await ShopConfig.findOne({ isRemoved: { $ne: true } });
|
||||
const form = toFormResponse(config);
|
||||
|
||||
Log.debug(companyId, 'config.get', {
|
||||
Log.debug( 'config.get', {
|
||||
found: Boolean(config),
|
||||
storeUrl: form.storeUrl,
|
||||
shopPrefix: form.shopPrefix,
|
||||
|
||||
@@ -6,8 +6,8 @@ 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 } });
|
||||
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();
|
||||
@@ -43,19 +43,18 @@ module.exports = async function(companyId, instanceId, params, ctx) {
|
||||
|
||||
if (existing) {
|
||||
await ShopConfig.update({ _id: existing._id }, payload);
|
||||
Log.info(companyId, 'PrestaShop order states saved', { configId: existing._id, count: psStates.length });
|
||||
Log.info( '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 });
|
||||
Log.info( '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 });
|
||||
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'));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -2,8 +2,8 @@ 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 } });
|
||||
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 : [];
|
||||
|
||||
@@ -2,13 +2,13 @@ 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 } });
|
||||
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(companyId, 'PrestaShop config reset', { removed: configs.length });
|
||||
Log.info( 'PrestaShop config reset', { removed: configs.length });
|
||||
return methodOk({ removed: configs.length });
|
||||
};
|
||||
|
||||
@@ -3,12 +3,12 @@ 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) {
|
||||
module.exports = async function(params, ctx) {
|
||||
try {
|
||||
const input = normalizeFormParams(params);
|
||||
const existing = await ShopConfig.findOne({ companyId, isRemoved: { $ne: true } });
|
||||
const existing = await ShopConfig.findOne({ isRemoved: { $ne: true } });
|
||||
|
||||
Log.debug(companyId, 'config.save input', {
|
||||
Log.debug( 'config.save input', {
|
||||
keys: Object.keys(input),
|
||||
storeUrl: input.storeUrl,
|
||||
shopPrefix: input.shopPrefix,
|
||||
@@ -24,20 +24,20 @@ module.exports = async function(companyId, instanceId, params, ctx) {
|
||||
return methodFail('Klucz API jest wymagany. Wpisz klucz Webservice API lub zachowaj już zapisany.');
|
||||
}
|
||||
|
||||
const payload = { companyId, ...data };
|
||||
const payload = { ...data };
|
||||
|
||||
if (existing) {
|
||||
await ShopConfig.update({ _id: existing._id }, payload);
|
||||
Log.info(companyId, 'PrestaShop config updated', { configId: existing._id });
|
||||
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(companyId, 'PrestaShop config created', { configId: created._id });
|
||||
Log.info( 'PrestaShop config created', { configId: created._id });
|
||||
return methodOk({ data: toFormResponse(created) });
|
||||
} catch (err) {
|
||||
Log.error(companyId, 'config.save failed', { error: err?.message });
|
||||
Log.error( 'config.save failed', { error: err?.message });
|
||||
return methodFail(err?.message || 'Nie udało się zapisać konfiguracji');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -5,8 +5,8 @@ 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 } });
|
||||
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();
|
||||
@@ -29,10 +29,10 @@ module.exports = async function(companyId, instanceId, params, ctx) {
|
||||
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 });
|
||||
Log.info( 'PrestaShop connection test OK', { storeUrl, statusesCount: count });
|
||||
return methodOk({ statusesCount: count });
|
||||
} catch (e) {
|
||||
Log.warn(companyId, 'PrestaShop connection test failed', { storeUrl, error: e.message });
|
||||
Log.warn( 'PrestaShop connection test failed', { storeUrl, error: e.message });
|
||||
return methodFail('Połączenie nieudane: ' + (e.message || 'błąd API PrestaShop'));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
const { DB, Utils, Log } = require('@orbito/api-lib');
|
||||
const ShopConfigSchema = require('../src/models/ShopConfig');
|
||||
|
||||
module.exports = async function(companyId, instanceId, params, ctx) {
|
||||
module.exports = async function(params, ctx) {
|
||||
const { _id } = params;
|
||||
if (!_id) throw new Error('Missing _id');
|
||||
|
||||
const config = await ShopConfigSchema.findOne({ _id, companyId, isRemoved: { $ne: true } });
|
||||
const config = await ShopConfigSchema.findOne({ _id, isRemoved: { $ne: true } });
|
||||
if (!config) throw new Error('Config not found');
|
||||
|
||||
await ShopConfigSchema.update({ _id }, { isRemoved: true });
|
||||
|
||||
@@ -2,12 +2,12 @@ const { DB, Utils, Log } = require('@orbito/api-lib');
|
||||
const ShopConfigSchema = require('../src/models/ShopConfig');
|
||||
const PrestashopApi = require('../src/PrestaApi');
|
||||
|
||||
module.exports = async function(companyId, instanceId, params, ctx) {
|
||||
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 = { companyId, isRemoved: { $ne: true } };
|
||||
const query = { isRemoved: { $ne: true } };
|
||||
|
||||
const results = await ShopConfigSchema.find(query, perPage, skip, { createdAt: -1 });
|
||||
const total = await ShopConfigSchema.count(query);
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
const { getFilters } = require('../../src/orders-filter-store');
|
||||
|
||||
// Zwraca zapisaną paginację / filtry listy zamówień (np. przed powrotem ze szczegółów).
|
||||
module.exports = async function(companyId, instanceId) {
|
||||
const filters = await getFilters(companyId, instanceId);
|
||||
module.exports = async function() {
|
||||
const filters = await getFilters();
|
||||
return {
|
||||
page: parseInt(filters.page, 10) || 1,
|
||||
perPage: parseInt(filters.perPage, 10) || 20,
|
||||
|
||||
@@ -3,32 +3,25 @@ const { searchOrders } = require('../../src/orders-api');
|
||||
const { normalizeFilterParams, FILTER_KEYS } = require('../../src/orders-filter-form');
|
||||
const { getFilters, mergeFilters, clearFilters } = require('../../src/orders-filter-store');
|
||||
|
||||
// orders.search — proxy do core/orders 'search' (tylko PrestaShop).
|
||||
// Akceptuje stary format: { searchQuery, paidOnly, pagination: { page, limit, sort } }
|
||||
// oraz pola filtrów (city, customerEmail lub filterCity, filterCustomerEmail z withData).
|
||||
// saveFilters: true — zapisuje filtry z żądania (np. przycisk Filtruj), żeby reload tabeli je zachował.
|
||||
// clearFilters: true — czyści zapisane filtry.
|
||||
module.exports = async function(companyId, instanceId, params, ctx) {
|
||||
module.exports = async function(params, ctx) {
|
||||
const input = { ...(params || {}) };
|
||||
|
||||
if (input.clearFilters) {
|
||||
clearFilters(companyId, instanceId);
|
||||
await clearFilters();
|
||||
delete input.clearFilters;
|
||||
Log.info(companyId, 'orders.search clearFilters', { instanceId });
|
||||
Log.info('orders.search clearFilters');
|
||||
}
|
||||
|
||||
if (input.saveFilters) {
|
||||
await mergeFilters(companyId, instanceId, input);
|
||||
await mergeFilters(input);
|
||||
delete input.saveFilters;
|
||||
Log.info(companyId, 'orders.search saveFilters', { instanceId, filters: await getFilters(companyId, instanceId) });
|
||||
Log.info('orders.search saveFilters', { filters: await getFilters() });
|
||||
}
|
||||
|
||||
const stored = await getFilters(companyId, instanceId);
|
||||
const stored = await getFilters();
|
||||
const normalizedInput = normalizeFilterParams(input);
|
||||
const merged = { ...stored, ...normalizedInput };
|
||||
|
||||
// Puste pola filtrów z żądania datatable (reload) nie mogą skasować
|
||||
// filtrów zapisanych przez saveFilters.
|
||||
for (const key of FILTER_KEYS) {
|
||||
if (normalizedInput[key] === '' && stored[key]) {
|
||||
merged[key] = stored[key];
|
||||
@@ -41,16 +34,14 @@ module.exports = async function(companyId, instanceId, params, ctx) {
|
||||
const storedPage = parseInt(stored.page, 10) || 1;
|
||||
const incomingPage = parseInt(merged.page, 10) || 1;
|
||||
|
||||
// Po powrocie ze szczegółów datatable startuje z page=1 — przywróć zapisaną stronę.
|
||||
// explicitPage=true oznacza świadomą zmianę strony w pagerze (nie nadpisuj).
|
||||
if (!explicitPage && !input.saveFilters && storedPage > 1 && incomingPage === 1) {
|
||||
merged.page = String(storedPage);
|
||||
}
|
||||
|
||||
const finalParams = normalizeFilterParams(merged);
|
||||
const result = await searchOrders(companyId, finalParams);
|
||||
const result = await searchOrders(finalParams);
|
||||
|
||||
await mergeFilters(companyId, instanceId, {
|
||||
await mergeFilters({
|
||||
page: String(finalParams.page || 1),
|
||||
perPage: String(finalParams.perPage || 20),
|
||||
});
|
||||
|
||||
@@ -2,11 +2,11 @@ const ShopConfig = require('../../src/models/ShopConfig');
|
||||
const PrestashopApi = require('../../src/PrestaApi');
|
||||
const { isLegacyApi } = require('../../src/utils');
|
||||
|
||||
module.exports = async function (companyId, instanceId, params, ctx) {
|
||||
module.exports = async function(params, ctx) {
|
||||
const productIds = Array.isArray(params?.productIds) ? params.productIds : [];
|
||||
if (productIds.length === 0) return {};
|
||||
|
||||
const configs = await ShopConfig.find({ companyId, isRemoved: { $ne: true } });
|
||||
const configs = await ShopConfig.find({ isRemoved: { $ne: true } });
|
||||
const config = configs.find(c => c.apiUrl && c.apiKey);
|
||||
if (!config) return {};
|
||||
|
||||
|
||||
@@ -1,25 +1,22 @@
|
||||
const { getOrderStatuses, searchOrders } = require('../../src/orders-api');
|
||||
|
||||
// status.get_with_counts — lista statusów zamówień (z core/orders) wraz z liczbą
|
||||
// zamówień PrestaShop w każdym statusie. Wynik krótko cache'owany per firma,
|
||||
// bo zlicza N zapytań (po jednym na status).
|
||||
const cache = new Map();
|
||||
const CACHE_TTL = 5000;
|
||||
const CACHE_KEY = 'default';
|
||||
|
||||
module.exports = async function(companyId, instanceId, params, ctx) {
|
||||
const key = companyId?.toString?.() ?? companyId;
|
||||
module.exports = async function(params, ctx) {
|
||||
const now = Date.now();
|
||||
const cached = cache.get(key);
|
||||
const cached = cache.get(CACHE_KEY);
|
||||
if (cached && now - cached.ts < CACHE_TTL) {
|
||||
return { data: cached.data };
|
||||
}
|
||||
|
||||
const statuses = await getOrderStatuses(companyId);
|
||||
const statuses = await getOrderStatuses();
|
||||
const data = [];
|
||||
|
||||
for (const status of statuses) {
|
||||
const statusId = status._id?.toString?.() ?? status._id;
|
||||
const result = await searchOrders(companyId, {
|
||||
const result = await searchOrders({
|
||||
statusId,
|
||||
page: 1,
|
||||
perPage: 1
|
||||
@@ -36,6 +33,6 @@ module.exports = async function(companyId, instanceId, params, ctx) {
|
||||
|
||||
data.sort((a, b) => (a.positionInGroup ?? 0) - (b.positionInGroup ?? 0));
|
||||
|
||||
cache.set(key, { ts: now, data });
|
||||
cache.set(CACHE_KEY, { ts: now, data });
|
||||
return { data };
|
||||
};
|
||||
|
||||
+4
-4
@@ -4,8 +4,8 @@ const PrestashopApi = require('../src/PrestaApi');
|
||||
const { importFromPSOrder } = require('../src/OrderImporter');
|
||||
const { isLegacyApi } = require('../src/utils');
|
||||
|
||||
module.exports = async function (companyId, instanceId, params, ctx) {
|
||||
const configs = await ShopConfig.find({ companyId, isRemoved: { $ne: true } });
|
||||
module.exports = async function(params, ctx) {
|
||||
const configs = await ShopConfig.find({ isRemoved: { $ne: true } });
|
||||
const active = configs.filter(c => c.apiUrl && c.apiKey);
|
||||
|
||||
if (active.length === 0) return { error: 'No active shop configs found' };
|
||||
@@ -34,7 +34,7 @@ module.exports = async function (companyId, instanceId, params, ctx) {
|
||||
const errors = [];
|
||||
for (const order of orders) {
|
||||
const uniqueId = order.reference + ':' + order.id;
|
||||
const searchResult = await Core.callModule(companyId, 'core/orders', 'search', {
|
||||
const searchResult = await Core.callModule( 'core/orders', 'search', {
|
||||
source: { moduleCode: 'core/prestashop', uniqueId }
|
||||
});
|
||||
if (searchResult?.data?.length > 0) {
|
||||
@@ -42,7 +42,7 @@ module.exports = async function (companyId, instanceId, params, ctx) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
await importFromPSOrder(companyId, order.id, api, config);
|
||||
await importFromPSOrder(order.id, api, config);
|
||||
imported.push({ id: order.id, status: 'imported' });
|
||||
} catch (e) {
|
||||
errors.push({ id: order.id, error: e.message });
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
const { DB, Utils, Log } = require('@orbito/api-lib');
|
||||
const ShopConfigSchema = require('../src/models/ShopConfig');
|
||||
|
||||
module.exports = async function(companyId, instanceId, params, ctx) {
|
||||
module.exports = async function(params, ctx) {
|
||||
const editableFields = ['apiUrl', 'apiKey', 'shopName', 'versionCode', 'orderStatus'];
|
||||
const dataToUpdate = { companyId };
|
||||
const dataToUpdate = {};
|
||||
for (const field of editableFields) {
|
||||
if (typeof params[field] !== 'undefined') {
|
||||
dataToUpdate[field] = params[field];
|
||||
@@ -12,7 +12,7 @@ module.exports = async function(companyId, instanceId, params, ctx) {
|
||||
|
||||
// Jeśli podano _id – aktualizuj konkretny rekord
|
||||
if (params._id) {
|
||||
const config = await ShopConfigSchema.findOne({ _id: params._id, companyId, isRemoved: { $ne: true } });
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user