@medusajs/core-flows
Version:
Set of workflow definitions for Medusa
202 lines • 8.56 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateExchangeShippingMethodWorkflow = exports.updateExchangeShippingMethodWorkflowId = exports.updateExchangeShippingMethodValidationStep = 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 preview_order_change_1 = require("../../steps/preview-order-change");
const order_validation_1 = require("../../utils/order-validation");
const prepare_shipping_method_1 = require("../../utils/prepare-shipping-method");
const schemas_1 = require("../../../cart/utils/schemas");
/**
* This step validates that an exchange's shipping method can be updated.
* If the exchange is canceled, the order change is not active, the shipping method
* doesn't exist in the exchange, or the action isn't adding a shipping method,
* the step will throw an error.
*
* :::note
*
* You can retrieve an 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 = updateExchangeShippingMethodValidationStep({
* orderChange: {
* id: "orch_123",
* // other order change details...
* },
* orderExchange: {
* id: "exchange_123",
* // other order exchange details...
* },
* input: {
* exchange_id: "exchange_123",
* action_id: "orchact_123",
* data: {
* custom_amount: 10,
* }
* }
* })
*/
exports.updateExchangeShippingMethodValidationStep = (0, workflows_sdk_1.createStep)("validate-update-exchange-shipping-method", async function ({ orderChange, orderExchange, input, }) {
(0, order_validation_1.throwIfIsCancelled)(orderExchange, "Exchange");
(0, order_validation_1.throwIfOrderChangeIsNotActive)({ orderChange });
const associatedAction = (orderChange.actions ?? []).find((a) => a.id === input.action_id);
if (!associatedAction) {
throw new Error(`No shipping method found for exchange ${input.exchange_id} in order change ${orderChange.id}`);
}
else if (associatedAction.action !== utils_1.ChangeActionType.SHIPPING_ADD) {
throw new Error(`Action ${associatedAction.id} is not adding a shipping method`);
}
});
exports.updateExchangeShippingMethodWorkflowId = "update-exchange-shipping-method";
/**
* This workflow updates an exchange's inbound or outbound shipping method. It's used by the
* [Update Inbound Shipping Admin API Route](https://docs.medusajs.com/api/admin#exchanges_postexchangesidinboundshippingmethodaction_id)
* or the [Outbound Inbound Shipping Admin API Route](https://docs.medusajs.com/api/admin#exchanges_postexchangesidoutboundshippingmethodaction_id).
*
* You can use this workflow within your customizations or your own custom workflows, allowing you to update an exchange's
* inbound or outbound shipping method in your custom flow.
*
* @example
* const { result } = await updateExchangeShippingMethodWorkflow(container)
* .run({
* input: {
* exchange_id: "exchange_123",
* action_id: "orchact_123",
* data: {
* custom_amount: 10,
* }
* }
* })
*
* @summary
*
* Update an exchange's inbound or outbound shipping method.
*
* @property hooks.setPricingContext - This hook is executed before the shipping method is updated. You can consume this hook to return any custom context useful for the prices retrieval of the shipping method's option.
*
* For example, assuming you have the following custom pricing rule:
*
* ```json
* {
* "attribute": "location_id",
* "operator": "eq",
* "value": "sloc_123",
* }
* ```
*
* You can consume the `setPricingContext` hook to add the `location_id` context to the prices calculation:
*
* ```ts
* import { updateExchangeShippingMethodWorkflow } from "@medusajs/medusa/core-flows";
* import { StepResponse } from "@medusajs/workflows-sdk";
*
* updateExchangeShippingMethodWorkflow.hooks.setPricingContext((
* { order_exchange, order_change, additional_data }, { container }
* ) => {
* return new StepResponse({
* location_id: "sloc_123", // Special price for in-store purchases
* });
* });
* ```
*
* The price of the shipping method's option will now be retrieved using the context you return.
*
* :::note
*
* Learn more about prices calculation context in the [Prices Calculation](https://docs.medusajs.com/resources/commerce-modules/pricing/price-calculation) documentation.
*
* :::
*/
exports.updateExchangeShippingMethodWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.updateExchangeShippingMethodWorkflowId, function (input) {
const orderExchange = (0, common_1.useRemoteQueryStep)({
entry_point: "order_exchange",
fields: [
"id",
"status",
"order_id",
"canceled_at",
"order.currency_code",
],
variables: { id: input.exchange_id },
list: false,
throw_if_key_not_found: true,
});
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 setPricingContext = (0, workflows_sdk_1.createHook)("setPricingContext", {
order_exchange: orderExchange,
order_change: orderChange,
additional_data: input.additional_data,
}, {
resultValidator: schemas_1.pricingContextResult,
});
const setPricingContextResult = setPricingContext.getResult();
const shippingOptions = (0, workflows_sdk_1.when)({ input }, ({ input }) => {
return input.data?.custom_amount === null;
}).then(() => {
const action = (0, workflows_sdk_1.transform)({ orderChange, input, orderExchange }, ({ orderChange, input, orderExchange }) => {
const originalAction = (orderChange.actions ?? []).find((a) => a.id === input.action_id);
return {
shipping_method_id: originalAction.reference_id,
currency_code: orderExchange.order.currency_code,
};
});
const pricingContext = (0, workflows_sdk_1.transform)({ action, setPricingContextResult }, (data) => {
return {
...(data.setPricingContextResult
? data.setPricingContextResult
: {}),
currency_code: data.action.currency_code,
};
});
const shippingMethod = (0, common_1.useRemoteQueryStep)({
entry_point: "order_shipping_method",
fields: ["id", "shipping_option_id"],
variables: {
id: action.shipping_method_id,
},
list: false,
}).config({ name: "fetch-shipping-method" });
return (0, common_1.useRemoteQueryStep)({
entry_point: "shipping_option",
fields: [
"id",
"name",
"calculated_price.calculated_amount",
"calculated_price.is_calculated_price_tax_inclusive",
],
variables: {
id: shippingMethod.shipping_option_id,
calculated_price: {
context: pricingContext,
},
},
}).config({ name: "fetch-shipping-option" });
});
(0, exports.updateExchangeShippingMethodValidationStep)({
orderExchange,
orderChange,
input,
});
const updateData = (0, workflows_sdk_1.transform)({ orderChange, input, shippingOptions }, prepare_shipping_method_1.prepareShippingMethodUpdate);
(0, workflows_sdk_1.parallelize)((0, steps_1.updateOrderChangeActionsStep)([updateData.action]), (0, steps_1.updateOrderShippingMethodsStep)([updateData.shippingMethod]));
return new workflows_sdk_1.WorkflowResponse((0, preview_order_change_1.previewOrderChangeStep)(orderExchange.order_id), {
hooks: [setPricingContext],
});
});
//# sourceMappingURL=update-exchange-shipping-method.js.map
;