|
| 1 | +import { OrderChangeDTO, OrderWorkflow, ReturnDTO } from "@medusajs/types" |
| 2 | +import { OrderChangeStatus } from "@medusajs/utils" |
| 3 | +import { |
| 4 | + WorkflowData, |
| 5 | + createStep, |
| 6 | + createWorkflow, |
| 7 | + transform, |
| 8 | +} from "@medusajs/workflows-sdk" |
| 9 | +import { useRemoteQueryStep } from "../../../common" |
| 10 | +import { updateReturnsStep } from "../../steps" |
| 11 | +import { previewOrderChangeStep } from "../../steps/preview-order-change" |
| 12 | +import { |
| 13 | + throwIfIsCancelled, |
| 14 | + throwIfOrderChangeIsNotActive, |
| 15 | +} from "../../utils/order-validation" |
| 16 | + |
| 17 | +const validationStep = createStep( |
| 18 | + "validate-update-return", |
| 19 | + async function ({ |
| 20 | + orderChange, |
| 21 | + orderReturn, |
| 22 | + }: { |
| 23 | + orderReturn: ReturnDTO |
| 24 | + orderChange: OrderChangeDTO |
| 25 | + }) { |
| 26 | + throwIfIsCancelled(orderReturn, "Return") |
| 27 | + throwIfOrderChangeIsNotActive({ orderChange }) |
| 28 | + } |
| 29 | +) |
| 30 | + |
| 31 | +export const updateReturnWorkflowId = "update-return" |
| 32 | +export const updateReturnWorkflow = createWorkflow( |
| 33 | + updateReturnWorkflowId, |
| 34 | + function ( |
| 35 | + input: WorkflowData<OrderWorkflow.UpdateReturnWorkflowInput> |
| 36 | + ): WorkflowData { |
| 37 | + const orderReturn: ReturnDTO = useRemoteQueryStep({ |
| 38 | + entry_point: "return", |
| 39 | + fields: ["id", "status", "order_id", "canceled_at"], |
| 40 | + variables: { id: input.return_id }, |
| 41 | + list: false, |
| 42 | + throw_if_key_not_found: true, |
| 43 | + }) |
| 44 | + |
| 45 | + const orderChange: OrderChangeDTO = useRemoteQueryStep({ |
| 46 | + entry_point: "order_change", |
| 47 | + fields: ["id", "status", "version", "actions.*"], |
| 48 | + variables: { |
| 49 | + filters: { |
| 50 | + order_id: orderReturn.order_id, |
| 51 | + return_id: orderReturn.id, |
| 52 | + status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED], |
| 53 | + }, |
| 54 | + }, |
| 55 | + list: false, |
| 56 | + }).config({ name: "order-change-query" }) |
| 57 | + |
| 58 | + validationStep({ orderReturn, orderChange }) |
| 59 | + |
| 60 | + const updateData = transform({ input }, ({ input }) => { |
| 61 | + return { |
| 62 | + id: input.return_id, |
| 63 | + location_id: input.location_id, |
| 64 | + no_notification: input.no_notification, |
| 65 | + metadata: input.metadata, |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + updateReturnsStep([updateData]) |
| 70 | + |
| 71 | + return previewOrderChangeStep(orderReturn.order_id) |
| 72 | + } |
| 73 | +) |
0 commit comments