UNPKG

@medusajs/core-flows

Version:

Set of workflow definitions for Medusa

107 lines 4.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getItemTaxLinesStep = exports.getItemTaxLinesStepId = void 0; const utils_1 = require("@medusajs/framework/utils"); const workflows_sdk_1 = require("@medusajs/framework/workflows-sdk"); function normalizeTaxModuleContext(orderOrCart, forceTaxCalculation, isReturn, shippingAddress) { const address = shippingAddress ?? orderOrCart.shipping_address; const shouldCalculateTax = forceTaxCalculation || orderOrCart.region?.automatic_taxes; if (!shouldCalculateTax) { return null; } if (forceTaxCalculation && !address?.country_code) { throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `country code is required to calculate taxes`); } if (!address?.country_code) { return null; } const customer = orderOrCart.customer && { id: orderOrCart.customer.id, email: orderOrCart.customer.email, customer_groups: orderOrCart.customer.groups?.map((g) => g.id) || [], metadata: orderOrCart.customer.metadata, }; return { address: { country_code: address.country_code, province_code: address.province, address_1: address.address_1, address_2: address.address_2, city: address.city, postal_code: address.postal_code, metadata: address.metadata, }, customer, is_return: isReturn ?? false, }; } function normalizeLineItemsForTax(orderOrCart, items) { return items.map((item) => ({ id: item.id, product_id: item.product_id, product_type_id: item.product_type_id, quantity: item.quantity, unit_price: item.unit_price, currency_code: orderOrCart.currency_code, })); } function normalizeLineItemsForShipping(orderOrCart, shippingMethods) { return shippingMethods.map((shippingMethod) => ({ id: shippingMethod.id, shipping_option_id: shippingMethod.shipping_option_id, unit_price: shippingMethod.amount, currency_code: orderOrCart.currency_code, })); } exports.getItemTaxLinesStepId = "get-item-tax-lines"; /** * This step retrieves the tax lines for an order or cart's line items and shipping methods. * * :::note * * You can retrieve an order, cart, item, shipping method, and address details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query), * or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep). * * ::: * * @example * const data = getItemTaxLinesStep({ * orderOrCart: { * id: "order_123", * // other order details... * }, * items: [ * { * id: "orli_123", * // other order item details... * } * ], * shipping_methods: [ * { * id: "osm_213", * // other shipping method details... * } * ], * }) */ exports.getItemTaxLinesStep = (0, workflows_sdk_1.createStep)(exports.getItemTaxLinesStepId, async (data, { container }) => { const { orderOrCart, items = [], shipping_methods: shippingMethods = [], force_tax_calculation: forceTaxCalculation = false, is_return: isReturn = false, shipping_address: shippingAddress, } = data; const filteredItems = items.filter((item) => !item.is_giftcard || !(0, utils_1.isDefined)(item.is_giftcard)); const taxService = container.resolve(utils_1.Modules.TAX); const taxContext = normalizeTaxModuleContext(orderOrCart, forceTaxCalculation, isReturn, shippingAddress); const stepResponseData = { lineItemTaxLines: [], shippingMethodsTaxLines: [], }; if (!taxContext) { return new workflows_sdk_1.StepResponse(stepResponseData); } if (items.length) { stepResponseData.lineItemTaxLines = (await taxService.getTaxLines(normalizeLineItemsForTax(orderOrCart, filteredItems), taxContext)); } if (shippingMethods.length) { stepResponseData.shippingMethodsTaxLines = (await taxService.getTaxLines(normalizeLineItemsForShipping(orderOrCart, shippingMethods), taxContext)); } return new workflows_sdk_1.StepResponse(stepResponseData); }); //# sourceMappingURL=get-item-tax-lines.js.map