UNPKG

@medusajs/order

Version:
137 lines 5.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyChangesToOrder = applyChangesToOrder; const utils_1 = require("@medusajs/framework/utils"); const calculate_order_change_1 = require("./calculate-order-change"); async function applyChangesToOrder(orders, actionsMap, options) { const itemsToUpsert = []; const creditLinesToCreate = []; const shippingMethodsToUpsert = []; const summariesToUpsert = []; const orderToUpdate = []; const orderEditableAttributes = [ "customer_id", "sales_channel_id", "email", "no_notification", ]; const calculatedOrders = {}; for (const order of orders) { const calculated = (0, calculate_order_change_1.calculateOrderChange)({ order: order, actions: actionsMap[order.id], transactions: order.transactions ?? [], options, }); (0, utils_1.createRawPropertiesFromBigNumber)(calculated); const version = actionsMap[order.id]?.[0]?.version ?? order.version; const orderAttributes = {}; // Editable attributes that have changed for (const attr of orderEditableAttributes) { if (order[attr] !== calculated.order[attr]) { orderAttributes[attr] = calculated.order[attr]; } } for (const item of calculated.order.items) { if (utils_1.MathBN.lte(item.quantity, 0)) { continue; } const isExistingItem = item.id === item.detail?.item_id; const orderItem = isExistingItem ? item.detail : item; const itemId = isExistingItem ? orderItem.item_id : item.id; const itemToUpsert = { id: orderItem.version === version ? orderItem.id : undefined, item_id: itemId, order_id: order.id, version, quantity: orderItem.quantity, unit_price: item.unit_price ?? orderItem.unit_price, compare_at_unit_price: item.compare_at_unit_price ?? orderItem.compare_at_unit_price, fulfilled_quantity: orderItem.fulfilled_quantity ?? 0, delivered_quantity: orderItem.delivered_quantity ?? 0, shipped_quantity: orderItem.shipped_quantity ?? 0, return_requested_quantity: orderItem.return_requested_quantity ?? 0, return_received_quantity: orderItem.return_received_quantity ?? 0, return_dismissed_quantity: orderItem.return_dismissed_quantity ?? 0, written_off_quantity: orderItem.written_off_quantity ?? 0, metadata: orderItem.metadata, }; itemsToUpsert.push(itemToUpsert); } const creditLines = (calculated.order.credit_lines ?? []).filter((creditLine) => !("id" in creditLine)); for (const creditLine of creditLines) { const creditLineToCreate = { order_id: order.id, amount: creditLine.amount, reference: creditLine.reference, reference_id: creditLine.reference_id, metadata: creditLine.metadata, }; creditLinesToCreate.push(creditLineToCreate); } if (version > order.version) { for (const shippingMethod of calculated.order.shipping_methods ?? []) { const shippingMethod_ = shippingMethod; const isNewShippingMethod = !(0, utils_1.isDefined)(shippingMethod_?.detail); if (!shippingMethod_) { continue; } let associatedMethodId; let hasShippingMethod = false; if (isNewShippingMethod) { associatedMethodId = shippingMethod_.actions?.find((sm) => { return (sm.action === utils_1.ChangeActionType.SHIPPING_ADD && sm.reference_id); }); hasShippingMethod = !!associatedMethodId; } else { associatedMethodId = shippingMethod_?.detail?.shipping_method_id; } const sm = { ...(isNewShippingMethod ? shippingMethod_ : shippingMethod_.detail), version, shipping_method_id: associatedMethodId, }; delete sm.id; if (!hasShippingMethod) { shippingMethodsToUpsert.push(sm); } } orderAttributes.version = version; } // Including tax lines and adjustments for added items and shipping methods if (options?.includeTaxLinesAndAdjustementsToPreview) { await options?.includeTaxLinesAndAdjustementsToPreview(calculated.order, itemsToUpsert, shippingMethodsToUpsert); (0, utils_1.decorateCartTotals)(calculated.order); } const orderSummary = order.summary; const upsertSummary = { id: orderSummary?.version === version ? orderSummary.id : undefined, order_id: order.id, version, totals: calculated.getSummaryFromOrder(calculated.order), }; (0, utils_1.createRawPropertiesFromBigNumber)(upsertSummary); summariesToUpsert.push(upsertSummary); if (Object.keys(orderAttributes).length > 0) { orderToUpdate.push({ selector: { id: order.id, }, data: { ...orderAttributes, }, }); } calculatedOrders[order.id] = calculated; } return { itemsToUpsert, creditLinesToCreate, shippingMethodsToUpsert, summariesToUpsert, orderToUpdate, calculatedOrders, }; } //# sourceMappingURL=apply-order-changes.js.map