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:
fatequod
2026-07-08 11:21:35 +02:00
co-authored by Cursor
parent f163100ad4
commit 1a6a978a21
25 changed files with 365 additions and 189 deletions
+13 -17
View File
@@ -25,7 +25,7 @@ const ordersPuller = new CronJob("0 */5 * * * *", async () => {
let shopConfigs = await ShopConfigSchema.find({ isRemoved: { $ne: true } });
enabledShops = shopConfigs.filter(config => config.apiUrl && config.apiKey);
} catch (error) {
Log.error(null, 'Error fetching shop configs', { error: error.message });
Log.error( 'Error fetching shop configs', { error: error.message });
}
for (const config of enabledShops) {
@@ -86,7 +86,7 @@ const ordersPuller = new CronJob("0 */5 * * * *", async () => {
for (const order of res.orders) {
try {
const searchResult = await Core.callModule(config.companyId, 'core/orders', 'search', {
const searchResult = await Core.callModule( 'core/orders', 'search', {
source: {
moduleCode: 'core/prestashop',
uniqueId: order.reference + ':' + order.id
@@ -98,17 +98,17 @@ const ordersPuller = new CronJob("0 */5 * * * *", async () => {
continue;
}
await importFromPSOrder(config.companyId, order.id, api, config);
await importFromPSOrder(order.id, api, config);
config.lastProcessedId = Math.max(config.lastProcessedId || 0, parseInt(order.id));
} catch (orderErr) {
Log.error(config.companyId, 'Failed to import PS order', { orderId: order.id, error: orderErr.message });
Log.error( 'Failed to import PS order', { orderId: order.id, error: orderErr.message });
config.lastProcessedId = Math.max(config.lastProcessedId || 0, parseInt(order.id));
}
}
await ShopConfigSchema.update({ _id: config._id }, { lastOrderId: config.lastProcessedId });
} catch (error) {
Log.error(config.companyId, 'Error pulling orders for shop config', { shopConfigId: config._id, error: error.message });
Log.error( 'Error pulling orders for shop config', { shopConfigId: config._id, error: error.message });
}
}
} finally {
@@ -119,12 +119,10 @@ ordersPuller.start();
async function syncPaymentsForShop(config, api, since, until, limit = 30) {
const companyId = config.companyId?.toString?.() ?? config.companyId;
const syncedAtRange = until ? { $gte: since, $lt: until } : { $gte: since };
const mappings = await PsOrder.find(
{
companyId: new DB.ObjectId(companyId),
shopConfigId: config._id,
orbitoOrderId: { $exists: true, $ne: null },
syncedAt: syncedAtRange,
@@ -136,7 +134,7 @@ async function syncPaymentsForShop(config, api, since, until, limit = 30) {
for (const m of mappings) {
try {
const orderRes = await Core.callModule(companyId, 'core/orders', 'search', { _id: m.orbitoOrderId });
const orderRes = await Core.callModule( 'core/orders', 'search', { _id: m.orbitoOrderId });
const orders = Array.isArray(orderRes) ? orderRes : (orderRes?.data ?? []);
const order = orders[0];
if (order?.isPaid) {
@@ -156,7 +154,7 @@ async function syncPaymentsForShop(config, api, since, until, limit = 30) {
if (remaining > 0) {
const paidAmount = Math.min(remaining, Math.round(totalPaidReal * 100) / 100);
await Core.callModule(companyId, 'core/orders', 'order.payments_add', {
await Core.callModule( 'core/orders', 'order.payments_add', {
orderId: m.orbitoOrderId?.toString?.() ?? m.orbitoOrderId,
payment: {
amount: paidAmount,
@@ -164,11 +162,11 @@ async function syncPaymentsForShop(config, api, since, until, limit = 30) {
date: new Date(psOrder.date_add),
},
});
Log.info(companyId, 'CRON: PS payment synced', { psOrderId: m.psOrderId, amount: paidAmount });
Log.info( 'CRON: PS payment synced', { psOrderId: m.psOrderId, amount: paidAmount });
}
await PsOrder.update({ _id: m._id }, { $set: { paymentSyncedAt: new Date() } });
} catch (err) {
Log.warn(companyId, 'CRON: PS payment sync failed', { psOrderId: m.psOrderId, error: err.message });
Log.warn( 'CRON: PS payment sync failed', { psOrderId: m.psOrderId, error: err.message });
}
}
}
@@ -184,8 +182,7 @@ const paymentsSyncNew = new CronJob('0 */3 * * * *', async () => {
const api = new PrestashopApi(config.apiUrl, config.apiKey, isLegacyApi(config.versionCode));
await syncPaymentsForShop(config, api, since, null, 50);
} catch (err) {
const cid = config.companyId?.toString?.() ?? config.companyId;
Log.warn(cid, 'CRON: PS payments-new error', { error: err.message });
Log.warn('CRON: PS payments-new error', { error: err.message });
}
}
} finally {
@@ -207,8 +204,7 @@ const paymentsSyncOlder = new CronJob('0 */30 * * * *', async () => {
const api = new PrestashopApi(config.apiUrl, config.apiKey, isLegacyApi(config.versionCode));
await syncPaymentsForShop(config, api, since, until, 50);
} catch (err) {
const cid = config.companyId?.toString?.() ?? config.companyId;
Log.warn(cid, 'CRON: PS payments-older error', { error: err.message });
Log.warn('CRON: PS payments-older error', { error: err.message });
}
}
} finally {
@@ -217,6 +213,6 @@ const paymentsSyncOlder = new CronJob('0 */30 * * * *', async () => {
});
paymentsSyncOlder.start();
Core.onEnabled(async (companyId, instanceId) => {
Log.info('Module enabled', { companyId, instanceId });
Core.onEnabled(async () => {
Log.info('Module enabled');
});