@medusajs/core-flows
Version:
Set of workflow definitions for Medusa
138 lines • 5.56 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateExchangeAddItemWorkflow = exports.updateExchangeAddItemWorkflowId = exports.updateExchangeAddItemValidationStep = 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 order_validation_1 = require("../../utils/order-validation");
const refresh_shipping_1 = require("./refresh-shipping");
/**
* This step validates that an outbound or new item can be removed from an exchange.
* If the order or exchange is canceled, the item is not found in the exchange,
* 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 = updateExchangeAddItemValidationStep({
* order: {
* id: "order_123",
* // other order details...
* },
* 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: {
* quantity: 1
* }
* }
* })
*/
exports.updateExchangeAddItemValidationStep = (0, workflows_sdk_1.createStep)("update-exchange-add-item-validation", async function ({ order, orderChange, orderExchange, input, }, context) {
(0, order_validation_1.throwIfIsCancelled)(order, "Order");
(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 request to add item for exchange ${input.exchange_id} in order change ${orderChange.id}`);
}
else if (associatedAction.action !== utils_1.ChangeActionType.ITEM_ADD) {
throw new Error(`Action ${associatedAction.id} is not adding an item`);
}
});
exports.updateExchangeAddItemWorkflowId = "update-exchange-add-item";
/**
* This workflow updates an outbound or new item in the exchange. It's used by the
* [Update Outbound Item Admin API Route](https://docs.medusajs.com/api/admin#exchanges_postexchangesidoutbounditemsaction_id).
*
* You can use this workflow within your customizations or your own custom workflows, allowing you to update an outbound or new item
* in an exchange in your custom flow.
*
* @example
* const { result } = await updateExchangeAddItemWorkflow(container)
* .run({
* input: {
* exchange_id: "exchange_123",
* action_id: "orchact_123",
* data: {
* quantity: 1
* }
* }
* })
*
* @summary
*
* Update an outbound or new item in an exchange.
*/
exports.updateExchangeAddItemWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.updateExchangeAddItemWorkflowId, 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", "canceled_at", "items.*"],
variables: { id: orderExchange.order_id },
list: false,
throw_if_key_not_found: true,
}).config({ name: "order-query" });
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" });
(0, exports.updateExchangeAddItemValidationStep)({
order,
input,
orderExchange,
orderChange,
});
const updateData = (0, workflows_sdk_1.transform)({ orderChange, input }, ({ input, orderChange }) => {
const originalAction = (orderChange.actions ?? []).find((a) => a.id === input.action_id);
const data = input.data;
return {
id: input.action_id,
details: {
quantity: data.quantity ?? originalAction.details?.quantity,
},
internal_note: data.internal_note,
};
});
(0, steps_1.updateOrderChangeActionsStep)([updateData]);
const refreshArgs = (0, workflows_sdk_1.transform)({ orderChange, orderExchange }, ({ orderChange, orderExchange }) => {
return {
order_change_id: orderChange.id,
exchange_id: orderExchange.id,
order_id: orderExchange.order_id,
};
});
refresh_shipping_1.refreshExchangeShippingWorkflow.runAsStep({
input: refreshArgs,
});
return new workflows_sdk_1.WorkflowResponse((0, steps_1.previewOrderChangeStep)(order.id));
});
//# sourceMappingURL=update-exchange-add-item.js.map
;