UNPKG

@medusajs/core-flows

Version:

Set of workflow definitions for Medusa

152 lines 7.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.markOrderFulfillmentAsDeliveredWorkflow = exports.markOrderFulfillmentAsDeliveredWorkflowId = exports.orderFulfillmentDeliverablilityValidationStep = exports.orderFulfillmentDeliverablilityValidationStepId = void 0; const utils_1 = require("@medusajs/framework/utils"); const workflows_sdk_1 = require("@medusajs/framework/workflows-sdk"); const common_1 = require("../../common"); const fulfillment_1 = require("../../fulfillment"); const register_delivery_1 = require("../steps/register-delivery"); const order_validation_1 = require("../utils/order-validation"); exports.orderFulfillmentDeliverablilityValidationStepId = "order-fulfillment-deliverability-validation"; /** * This step validates that the order fulfillment can be delivered. If the order is cancelled, * the items to mark as delivered don't exist in the order, or the fulfillment doesn't exist in the order, * the step will throw an error. * * :::note * * You can retrieve an order and fulfillment's 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 = orderFulfillmentDeliverablilityValidationStep({ * order: { * id: "order_123", * fulfillments: [ * { * id: "ful_123", * // other fulfillment details... * } * ] * // other order details... * }, * fulfillment: { * id: "ful_123", * // other fulfillment details... * } * }) */ exports.orderFulfillmentDeliverablilityValidationStep = (0, workflows_sdk_1.createStep)(exports.orderFulfillmentDeliverablilityValidationStepId, async ({ fulfillment, order, }) => { (0, order_validation_1.throwIfOrderIsCancelled)({ order }); const orderFulfillment = order.fulfillments?.find((f) => f.id === fulfillment.id); if (!orderFulfillment) { throw new Error(`Fulfillment with id ${fulfillment.id} not found in the order`); } (0, order_validation_1.throwIfItemsDoesNotExistsInOrder)({ order, inputItems: order.items.map((i) => ({ id: i.id, quantity: i.quantity, })), }); }); function prepareRegisterDeliveryData({ fulfillment, order, }) { const orderFulfillment = order.fulfillments.find((f) => f.id === fulfillment.id); const lineItemIds = new Array(...new Set(orderFulfillment.items.map((i) => i.line_item_id))); return { order_id: order.id, reference: utils_1.Modules.FULFILLMENT, reference_id: orderFulfillment.id, items: lineItemIds.map((lineItemId) => { // find order item const orderItem = order.items.find((i) => i.id === lineItemId); // find inventory items const iitems = orderItem.variant?.inventory_items; // find fulfillment item const fitem = orderFulfillment.items.find((i) => i.line_item_id === lineItemId); let quantity = fitem.quantity; // NOTE: if the order item has an inventory kit or `required_qunatity` > 1, fulfillment items wont't match 1:1 with order items. // - for each inventory item in the kit, a fulfillment item will be created i.e. one line item could have multiple fulfillment items // - the quantity of the fulfillment item will be the quantity of the order item multiplied by the required quantity of the inventory item // // We need to take this into account when marking the fulfillment as delivered to compute quantity of line items being delivered based on fulfillment items and qunatities. // NOTE: for now we only need to find one inventory item of a line item to compute this since when a fulfillment is created all inventory items are fulfilled together. // If we allow to cancel partial fulfillments for an order item, we need to change this. // if (iitems?.length) { const iitem = iitems.find((i) => i.inventory.id === fitem.inventory_item_id); quantity = utils_1.MathBN.div(quantity, iitem.required_quantity); } return { id: lineItemId, quantity, }; }), }; } exports.markOrderFulfillmentAsDeliveredWorkflowId = "mark-order-fulfillment-as-delivered-workflow"; /** * This workflow marks a fulfillment in an order as delivered. It's used by the * [Mark Fulfillment as Delivered Admin API Route](https://docs.medusajs.com/api/admin#orders_postordersidfulfillmentsfulfillment_idmarkasdelivered). * * You can use this workflow within your customizations or your own custom workflows, allowing you to wrap custom logic around * marking a fulfillment as delivered. * * @example * const { result } = await markOrderFulfillmentAsDeliveredWorkflow(container) * .run({ * input: { * orderId: "order_123", * fulfillmentId: "ful_123", * } * }) * * @summary * * Mark a fulfillment in an order as delivered. */ exports.markOrderFulfillmentAsDeliveredWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.markOrderFulfillmentAsDeliveredWorkflowId, (input) => { const { fulfillmentId, orderId } = input; const fulfillment = (0, common_1.useRemoteQueryStep)({ entry_point: "fulfillment", fields: ["id"], variables: { id: fulfillmentId }, throw_if_key_not_found: true, list: false, }); const order = (0, common_1.useRemoteQueryStep)({ entry_point: "order", fields: [ "id", "summary", "currency_code", "region_id", "fulfillments.id", "fulfillments.items.id", "fulfillments.items.quantity", "fulfillments.items.line_item_id", "fulfillments.items.inventory_item_id", "items.id", "items.quantity", "items.variant.manage_inventory", "items.variant.inventory_items.inventory.id", "items.variant.inventory_items.required_quantity", ], variables: { id: orderId }, throw_if_key_not_found: true, list: false, }).config({ name: "order-query" }); (0, exports.orderFulfillmentDeliverablilityValidationStep)({ order, fulfillment }); const deliveryData = (0, workflows_sdk_1.transform)({ order, fulfillment }, prepareRegisterDeliveryData); const [deliveredFulfillment] = (0, workflows_sdk_1.parallelize)(fulfillment_1.markFulfillmentAsDeliveredWorkflow.runAsStep({ input: { id: fulfillment.id }, }), (0, register_delivery_1.registerOrderDeliveryStep)(deliveryData)); (0, common_1.emitEventStep)({ eventName: utils_1.FulfillmentWorkflowEvents.DELIVERY_CREATED, data: { id: deliveredFulfillment.id }, }); return new workflows_sdk_1.WorkflowResponse(void 0); }); //# sourceMappingURL=mark-order-fulfillment-as-delivered.js.map