Files
core-prestashop/methods/testPull.js
T
wiktorandClaude Fable 5 b1c7f792de feat: manifest module.json i kod modułu z getModuleInfo zamiast hardcode
- module.json: name/version/dependsOn (core/orders, core/products);
  package.json name -> core.prestashop
- hardcode'y 'core/prestashop' (source.moduleCode w pullerze/imporcie)
  zastąpione Core.getModuleInfo().code; w orders-api stałe top-level
  zamienione na leniwe funkcje, legacy alias 'prestashop' wyliczany z kodu
- make-zip.sh pakuje module.json

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-28 12:03:48 +02:00

61 lines
2.4 KiB
JavaScript

const { Core } = require('@orbito/api-lib');
const PrestashopApi = require('../src/PrestaApi');
const { importFromPSOrder } = require('../src/OrderImporter');
const { isLegacyApi } = require('../src/utils');
const { loadConfig, isConfigured, setLastOrderId } = require('../src/module-config');
const { PERMISSIONS, requirePermission } = require('../src/register-permissions');
module.exports = async function(params, ctx) {
requirePermission(PERMISSIONS.ORDERS_IMPORT, ctx);
const config = await loadConfig();
if (!isConfigured(config)) return { error: 'No active shop configs found' };
const api = new PrestashopApi(config.apiUrl, config.apiKey, isLegacyApi(config.versionCode));
const queryParams = {
display: '[id, reference, date_add]',
sort: 'id_ASC',
limit: 50,
output_format: 'JSON',
'filter[id]': `[${(config.lastOrderId || 0) + 1},999999999]`,
};
const raw = await api.makeRequest('orders', 'GET', queryParams);
const orders = raw?.orders || [];
if (params.import && orders.length > 0) {
const imported = [];
const errors = [];
for (const order of orders) {
const uniqueId = order.reference + ':' + order.id;
const searchResult = await Core.callModule( 'core/orders', 'search', {
// własny kod modułu bierzemy z core (po rejestracji), nie z hardcode'u
source: { moduleCode: Core.getModuleInfo().code, uniqueId }
});
if (searchResult?.data?.length > 0) {
imported.push({ id: order.id, status: 'already_exists' });
continue;
}
try {
await importFromPSOrder(order.id, api, config);
imported.push({ id: order.id, status: 'imported' });
} catch (e) {
errors.push({ id: order.id, error: e.message });
}
}
const maxId = Math.max(...orders.map(o => parseInt(o.id)));
await setLastOrderId(maxId);
return { shopName: config.shopName, imported, errors, newLastOrderId: maxId };
}
return {
shopName: config.shopName,
lastOrderId: config.lastOrderId,
filterFrom: (config.lastOrderId || 0) + 1,
apiResponse: raw,
ordersCount: orders.length,
hint: 'Add "import": true to params to import these orders',
};
};