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:
+19
-23
@@ -17,8 +17,8 @@ function getPSMultiLangField(param, langId) {
|
||||
return param;
|
||||
}
|
||||
|
||||
async function resolveImportStatusId(companyId, config) {
|
||||
const statusResult = await Core.callModule(companyId, 'core/orders', 'status.search', {});
|
||||
async function resolveImportStatusId(config) {
|
||||
const statusResult = await Core.callModule( 'core/orders', 'status.search', {});
|
||||
const statuses = statusResult?.results ?? [];
|
||||
const byId = new Map();
|
||||
|
||||
@@ -38,19 +38,19 @@ async function resolveImportStatusId(companyId, config) {
|
||||
return normalizeStatusId(sorted[0]?._id);
|
||||
}
|
||||
|
||||
async function importFromPSOrder(companyId, orderId, api, config) {
|
||||
Log.debug(companyId, "Checking if BL is supported", { orderId });
|
||||
async function importFromPSOrder(orderId, api, config) {
|
||||
Log.debug( "Checking if BL is supported", { orderId });
|
||||
let blSupported = await api.isBLSupported();
|
||||
let endpoint = blSupported ? 'bl_order' : 'orders';
|
||||
|
||||
Log.debug(companyId, "Fetching order details", { endpoint: endpoint + '/' + orderId });
|
||||
Log.debug( "Fetching order details", { endpoint: endpoint + '/' + orderId });
|
||||
let { order } = await api.makeRequest(endpoint + '/' + orderId, 'GET', { output_format: "JSON" });
|
||||
if (!order) {
|
||||
Log.error(companyId, "Order not found", orderId);
|
||||
Log.error( "Order not found", orderId);
|
||||
return;
|
||||
}
|
||||
|
||||
Log.info(companyId, "Importing order: " + order.reference);
|
||||
Log.info( "Importing order: " + order.reference);
|
||||
|
||||
let products = [];
|
||||
for (let product of order.associations.order_rows) {
|
||||
@@ -68,7 +68,7 @@ async function importFromPSOrder(companyId, orderId, api, config) {
|
||||
};
|
||||
|
||||
const psProduct = await api.getProduct(product.product_id);
|
||||
Log.debug(companyId, "Fetched product details: " + getPSMultiLangField(psProduct.name));
|
||||
Log.debug( "Fetched product details: " + getPSMultiLangField(psProduct.name));
|
||||
p.weight = parseFloat(psProduct.weight);
|
||||
|
||||
try {
|
||||
@@ -122,7 +122,7 @@ async function importFromPSOrder(companyId, orderId, api, config) {
|
||||
await Promise.all(
|
||||
products
|
||||
.filter(p => p.sourceId && p.name)
|
||||
.map(p => Core.callModule(companyId, 'core/orders', 'product.upsert', {
|
||||
.map(p => Core.callModule( 'core/orders', 'product.upsert', {
|
||||
sourceModule: 'core/prestashop',
|
||||
sourceId: String(p.sourceId),
|
||||
sku: p.sku || '',
|
||||
@@ -133,23 +133,23 @@ async function importFromPSOrder(companyId, orderId, api, config) {
|
||||
}).catch(() => {}))
|
||||
);
|
||||
|
||||
const fallbackStatusId = await resolveImportStatusId(companyId, config);
|
||||
const fallbackStatusId = await resolveImportStatusId(config);
|
||||
if (!fallbackStatusId) {
|
||||
Log.error(companyId, 'Cannot import order — no valid order status configured', { orderId });
|
||||
Log.error( 'Cannot import order — no valid order status configured', { orderId });
|
||||
return;
|
||||
}
|
||||
|
||||
const statusMapping = Array.isArray(config.statusMapping) ? config.statusMapping : [];
|
||||
const statusId = resolveStatusId(order.current_state, statusMapping, fallbackStatusId);
|
||||
if (!statusId) {
|
||||
Log.error(companyId, 'Cannot import order — could not resolve status', {
|
||||
Log.error( 'Cannot import order — could not resolve status', {
|
||||
orderId,
|
||||
psStateId: order.current_state
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
Log.info(companyId, 'Resolved order status', {
|
||||
Log.info( 'Resolved order status', {
|
||||
orderId,
|
||||
psStateId: order.current_state,
|
||||
orbitoStatusId: statusId
|
||||
@@ -157,7 +157,7 @@ async function importFromPSOrder(companyId, orderId, api, config) {
|
||||
|
||||
let res = await api.makeRequest('customers/' + order.id_customer, 'GET', { output_format: "JSON" });
|
||||
const customer = res.customer;
|
||||
Log.debug(companyId, "Fetched customer details", { email: customer.email, name: customer.firstname + " " + customer.lastname });
|
||||
Log.debug( "Fetched customer details", { email: customer.email, name: customer.firstname + " " + customer.lastname });
|
||||
|
||||
res = await api.makeRequest('addresses/' + order.id_address_delivery, 'GET', { output_format: "JSON" });
|
||||
const address = res.address;
|
||||
@@ -170,7 +170,7 @@ async function importFromPSOrder(companyId, orderId, api, config) {
|
||||
carrierName = carrier.name || "Brak nazwy";
|
||||
}
|
||||
} catch (e) {
|
||||
Log.warn(companyId, "Error fetching carrier for order", orderId, e.message);
|
||||
Log.warn( "Error fetching carrier for order", orderId, e.message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,10 +197,10 @@ async function importFromPSOrder(companyId, orderId, api, config) {
|
||||
// Opcjonalnie: można dodać imię i nazwisko z adresu faktury, jeśli są inne i potrzebne
|
||||
// invoiceDataForOrder.naturalPerson = (invoiceAddress.firstname || '') + ' ' + (invoiceAddress.lastname || '');
|
||||
} else {
|
||||
Log.warn(companyId, `Could not fetch invoice address details for address ID: ${order.id_address_invoice}, order ID: ${orderId}`);
|
||||
Log.warn( `Could not fetch invoice address details for address ID: ${order.id_address_invoice}, order ID: ${orderId}`);
|
||||
}
|
||||
} catch (e) {
|
||||
Log.error(companyId, `Error fetching invoice address for order ID ${orderId}, address ID ${order.id_address_invoice}: ${e.message}`);
|
||||
Log.error( `Error fetching invoice address for order ID ${orderId}, address ID ${order.id_address_invoice}: ${e.message}`);
|
||||
// W przypadku błędu, invoiceDataForOrder pozostaje { isNeeded: false }
|
||||
}
|
||||
}
|
||||
@@ -249,7 +249,7 @@ async function importFromPSOrder(companyId, orderId, api, config) {
|
||||
});
|
||||
}
|
||||
|
||||
const coreOrder = await Core.callModule(companyId, 'core/orders', 'create', {
|
||||
const coreOrder = await Core.callModule( 'core/orders', 'create', {
|
||||
statusId: statusId,
|
||||
customer: {
|
||||
name: customer.firstname + " " + customer.lastname,
|
||||
@@ -303,7 +303,6 @@ async function importFromPSOrder(companyId, orderId, api, config) {
|
||||
const alreadyPaid = totalPaidReal > 0 && payments.length > 0;
|
||||
try {
|
||||
await PsOrder.create({
|
||||
companyId: new DB.ObjectId(companyId),
|
||||
shopConfigId: config._id,
|
||||
psOrderId: parseInt(order.id),
|
||||
orbitoOrderId: coreOrder._id,
|
||||
@@ -312,12 +311,9 @@ async function importFromPSOrder(companyId, orderId, api, config) {
|
||||
paymentSyncedAt: alreadyPaid ? new Date() : undefined,
|
||||
});
|
||||
} catch (e) {
|
||||
Log.warn(companyId, 'PS order mapping save failed', { psOrderId: order.id, error: e.message });
|
||||
Log.warn( 'PS order mapping save failed', { psOrderId: order.id, error: e.message });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { importFromPSOrder };
|
||||
module.exports = { importFromPSOrder };
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user