@medusajs/core-flows
Version:
Set of workflow definitions for Medusa
201 lines • 8.32 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createExchangeShippingMethodWorkflow = exports.createExchangeShippingMethodWorkflowId = exports.createExchangeShippingMethodValidationStep = void 0;
const utils_1 = require("@medusajs/framework/utils");
const workflows_sdk_1 = require("@medusajs/framework/workflows-sdk");
const common_1 = require("../../../common");
const steps_1 = require("../../steps");
const create_order_shipping_methods_1 = require("../../steps/create-order-shipping-methods");
const order_validation_1 = require("../../utils/order-validation");
const prepare_shipping_method_1 = require("../../utils/prepare-shipping-method");
const create_order_change_actions_1 = require("../create-order-change-actions");
const update_tax_lines_1 = require("../update-tax-lines");
const fetch_shipping_option_1 = require("../fetch-shipping-option");
/**
* This step validates that an inbound or outbound shipping method can be created for an exchange.
* If the order or exchange is canceled, or the order change is not active, the step will throw an error.
*
* :::note
*
* You can retrieve an order, order exchange, and order change 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 = createExchangeShippingMethodValidationStep({
* order: {
* id: "order_123",
* // other order details...
* },
* orderChange: {
* id: "orch_123",
* // other order change details...
* },
* orderExchange: {
* id: "exchange_123",
* // other order exchange details...
* },
* })
*/
exports.createExchangeShippingMethodValidationStep = (0, workflows_sdk_1.createStep)("validate-create-exchange-shipping-method", async function ({ order, orderChange, orderExchange, }) {
(0, order_validation_1.throwIfIsCancelled)(order, "Order");
(0, order_validation_1.throwIfIsCancelled)(orderExchange, "Exchange");
(0, order_validation_1.throwIfOrderChangeIsNotActive)({ orderChange });
});
exports.createExchangeShippingMethodWorkflowId = "create-exchange-shipping-method";
/**
* This workflow creates an inbound (return) or outbound (delivery of new items) shipping method for an exchange.
* It's used by the [Add Inbound Shipping Admin API Route](https://docs.medusajs.com/api/admin#exchanges_postexchangesidinboundshippingmethod)
* and the [Add Outbound Shipping Admin API Route](https://docs.medusajs.com/api/admin#exchanges_postexchangesidoutboundshippingmethod).
*
* You can use this workflow within your customizations or your own custom workflows, allowing you to create a shipping method
* for an exchange in your custom flow.
*
* @example
* To create an outbound shipping method for the exchange:
*
* ```ts
* const { result } = await createExchangeShippingMethodWorkflow(container)
* .run({
* input: {
* exchange_id: "exchange_123",
* shipping_option_id: "so_123"
* }
* })
* ```
*
* To create an inbound shipping method, pass the ID of the return associated with the exchange:
*
* ```ts
* const { result } = await createExchangeShippingMethodWorkflow(container)
* .run({
* input: {
* exchange_id: "exchange_123",
* return_id: "return_123",
* shipping_option_id: "so_123"
* }
* })
* ```
*
* @summary
*
* Create an inbound or outbound shipping method for an exchange.
*/
exports.createExchangeShippingMethodWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.createExchangeShippingMethodWorkflowId, function (input) {
const orderExchange = (0, common_1.useRemoteQueryStep)({
entry_point: "order_exchange",
fields: ["id", "status", "order_id", "canceled_at"],
variables: { id: input.exchange_id },
list: false,
throw_if_key_not_found: true,
});
const order = (0, common_1.useRemoteQueryStep)({
entry_point: "orders",
fields: ["id", "status", "currency_code", "canceled_at"],
variables: { id: orderExchange.order_id },
list: false,
throw_if_key_not_found: true,
}).config({ name: "order-query" });
const isReturn = (0, workflows_sdk_1.transform)(input, (data) => {
return !!data.return_id;
});
const orderChange = (0, common_1.useRemoteQueryStep)({
entry_point: "order_change",
fields: ["id", "status", "version", "actions.*"],
variables: {
filters: {
order_id: orderExchange.order_id,
exchange_id: orderExchange.id,
status: [utils_1.OrderChangeStatus.PENDING, utils_1.OrderChangeStatus.REQUESTED],
},
},
list: false,
}).config({ name: "order-change-query" });
const fetchShippingOptionInput = (0, workflows_sdk_1.transform)({ input, isReturn, orderChange, order }, (data) => {
const changeActionType = data.isReturn
? utils_1.ChangeActionType.RETURN_ITEM
: utils_1.ChangeActionType.ITEM_ADD;
const items = data.orderChange.actions
.filter((action) => action.action === changeActionType)
.map((a) => ({
id: a.details?.reference_id,
quantity: a.details?.quantity,
}));
const context = data.isReturn
? {
return_id: data.input.return_id,
return_items: items,
}
: {
exchange_id: data.input.exchange_id,
exchange_items: items,
};
return {
order_id: data.order.id,
currency_code: data.order.currency_code,
shipping_option_id: data.input.shipping_option_id,
custom_amount: data.input.custom_amount,
context: context,
};
});
const shippingOption = fetch_shipping_option_1.fetchShippingOptionForOrderWorkflow.runAsStep({
input: fetchShippingOptionInput,
});
const shippingOptions = (0, workflows_sdk_1.transform)(shippingOption, (shippingOption) => {
return [shippingOption];
});
(0, exports.createExchangeShippingMethodValidationStep)({
order,
orderExchange,
orderChange,
});
const shippingMethodInput = (0, workflows_sdk_1.transform)({
relatedEntity: orderExchange,
shippingOptions,
customPrice: input.custom_amount,
orderChange,
input,
}, (0, prepare_shipping_method_1.prepareShippingMethod)("exchange_id"));
const createdMethods = (0, create_order_shipping_methods_1.createOrderShippingMethods)({
shipping_methods: [shippingMethodInput],
});
const shippingMethodIds = (0, workflows_sdk_1.transform)(createdMethods, (createdMethods) => {
return createdMethods.map((item) => item.id);
});
update_tax_lines_1.updateOrderTaxLinesWorkflow.runAsStep({
input: {
order_id: order.id,
shipping_method_ids: shippingMethodIds,
is_return: isReturn,
},
});
const orderChangeActionInput = (0, workflows_sdk_1.transform)({
order,
orderExchange,
shippingOptions,
createdMethods,
customPrice: input.custom_amount,
orderChange,
input,
}, ({ shippingOptions, orderExchange, order, createdMethods, customPrice, orderChange, input, }) => {
const shippingOption = shippingOptions[0];
const createdMethod = createdMethods[0];
const methodPrice = customPrice ?? shippingOption.calculated_price.calculated_amount;
return {
action: utils_1.ChangeActionType.SHIPPING_ADD,
reference: "order_shipping_method",
order_change_id: orderChange.id,
reference_id: createdMethod.id,
amount: methodPrice,
order_id: order.id,
return_id: input.return_id,
exchange_id: orderExchange.id,
};
});
create_order_change_actions_1.createOrderChangeActionsWorkflow.runAsStep({
input: [orderChangeActionInput],
});
return new workflows_sdk_1.WorkflowResponse((0, steps_1.previewOrderChangeStep)(order.id));
});
//# sourceMappingURL=create-exchange-shipping-method.js.map
;