|
1 |
| -import { OrderChangeDTO, OrderDTO, ReturnDTO } from "@medusajs/types" |
2 |
| -import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils" |
| 1 | +import { |
| 2 | + FulfillmentWorkflow, |
| 3 | + OrderChangeDTO, |
| 4 | + OrderDTO, |
| 5 | + ReturnDTO, |
| 6 | +} from "@medusajs/types" |
| 7 | +import { |
| 8 | + ChangeActionType, |
| 9 | + MedusaError, |
| 10 | + Modules, |
| 11 | + OrderChangeStatus, |
| 12 | +} from "@medusajs/utils" |
3 | 13 | import {
|
4 | 14 | WorkflowData,
|
5 | 15 | createStep,
|
6 | 16 | createWorkflow,
|
7 | 17 | transform,
|
| 18 | + when, |
8 | 19 | } from "@medusajs/workflows-sdk"
|
9 |
| -import { useRemoteQueryStep } from "../../../common" |
| 20 | +import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common" |
| 21 | +import { createReturnFulfillmentWorkflow } from "../../../fulfillment/workflows/create-return-fulfillment" |
10 | 22 | import { previewOrderChangeStep } from "../../steps"
|
11 | 23 | import { confirmOrderChanges } from "../../steps/confirm-order-changes"
|
12 | 24 | import { createReturnItemsStep } from "../../steps/create-return-items"
|
@@ -36,21 +48,93 @@ const validationStep = createStep(
|
36 | 48 | }
|
37 | 49 | )
|
38 | 50 |
|
| 51 | +function prepareFulfillmentData({ |
| 52 | + order, |
| 53 | + items, |
| 54 | + returnShippingOption, |
| 55 | +}: { |
| 56 | + order: OrderDTO |
| 57 | + items: any[] |
| 58 | + returnShippingOption: { |
| 59 | + id: string |
| 60 | + provider_id: string |
| 61 | + service_zone: { |
| 62 | + fulfillment_set: { |
| 63 | + location?: { |
| 64 | + id: string |
| 65 | + address: Record<string, any> |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +}) { |
| 71 | + const orderItemsMap = new Map<string, Required<OrderDTO>["items"][0]>( |
| 72 | + order.items!.map((i) => [i.id, i]) |
| 73 | + ) |
| 74 | + const fulfillmentItems = items.map((i) => { |
| 75 | + const orderItem = orderItemsMap.get(i.item_id)! |
| 76 | + return { |
| 77 | + line_item_id: i.item_id, |
| 78 | + quantity: i.quantity, |
| 79 | + return_quantity: i.quantity, |
| 80 | + title: orderItem.variant_title ?? orderItem.title, |
| 81 | + sku: orderItem.variant_sku || "", |
| 82 | + barcode: orderItem.variant_barcode || "", |
| 83 | + } as FulfillmentWorkflow.CreateFulfillmentItemWorkflowDTO |
| 84 | + }) |
| 85 | + |
| 86 | + const locationId = |
| 87 | + returnShippingOption.service_zone.fulfillment_set.location?.id |
| 88 | + |
| 89 | + // delivery address is the stock location address |
| 90 | + const address = |
| 91 | + returnShippingOption.service_zone.fulfillment_set.location?.address ?? {} |
| 92 | + |
| 93 | + delete address.id |
| 94 | + |
| 95 | + if (!locationId) { |
| 96 | + throw new MedusaError( |
| 97 | + MedusaError.Types.INVALID_DATA, |
| 98 | + `Cannot create return without stock location, either provide a location or you should link the shipping option ${returnShippingOption.id} to a stock location.` |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + return { |
| 103 | + input: { |
| 104 | + location_id: locationId, |
| 105 | + provider_id: returnShippingOption.provider_id, |
| 106 | + shipping_option_id: returnShippingOption.id, |
| 107 | + items: fulfillmentItems, |
| 108 | + delivery_address: address, |
| 109 | + order: order, |
| 110 | + }, |
| 111 | + } |
| 112 | +} |
| 113 | + |
39 | 114 | export const confirmReturnRequestWorkflowId = "confirm-return-request"
|
40 | 115 | export const confirmReturnRequestWorkflow = createWorkflow(
|
41 | 116 | confirmReturnRequestWorkflowId,
|
42 | 117 | function (input: WorkflowInput): WorkflowData<OrderDTO> {
|
43 | 118 | const orderReturn: ReturnDTO = useRemoteQueryStep({
|
44 | 119 | entry_point: "return",
|
45 |
| - fields: ["id", "status", "order_id", "canceled_at"], |
| 120 | + fields: ["id", "status", "order_id", "location_id", "canceled_at"], |
46 | 121 | variables: { id: input.return_id },
|
47 | 122 | list: false,
|
48 | 123 | throw_if_key_not_found: true,
|
49 | 124 | })
|
50 | 125 |
|
51 | 126 | const order: OrderDTO = useRemoteQueryStep({
|
52 | 127 | entry_point: "orders",
|
53 |
| - fields: ["id", "version", "canceled_at"], |
| 128 | + fields: [ |
| 129 | + "id", |
| 130 | + "version", |
| 131 | + "canceled_at", |
| 132 | + "items.id", |
| 133 | + "items.title", |
| 134 | + "items.variant_title", |
| 135 | + "items.variant_sku", |
| 136 | + "items.variant_barcode", |
| 137 | + ], |
54 | 138 | variables: { id: orderReturn.order_id },
|
55 | 139 | list: false,
|
56 | 140 | throw_if_key_not_found: true,
|
@@ -85,13 +169,79 @@ export const confirmReturnRequestWorkflow = createWorkflow(
|
85 | 169 |
|
86 | 170 | validationStep({ order, orderReturn, orderChange })
|
87 | 171 |
|
88 |
| - createReturnItemsStep({ |
| 172 | + const createdReturnItems = createReturnItemsStep({ |
89 | 173 | returnId: orderReturn.id,
|
90 | 174 | changes: returnItemActions,
|
91 | 175 | })
|
92 | 176 |
|
93 | 177 | confirmOrderChanges({ changes: [orderChange], orderId: order.id })
|
94 | 178 |
|
| 179 | + const returnModified = useRemoteQueryStep({ |
| 180 | + entry_point: "return", |
| 181 | + fields: [ |
| 182 | + "id", |
| 183 | + "status", |
| 184 | + "order_id", |
| 185 | + "canceled_at", |
| 186 | + "shipping_methods.shipping_option_id", |
| 187 | + ], |
| 188 | + variables: { id: input.return_id }, |
| 189 | + list: false, |
| 190 | + throw_if_key_not_found: true, |
| 191 | + }).config({ name: "return-query" }) |
| 192 | + |
| 193 | + const returnShippingOptionId = transform( |
| 194 | + { returnModified }, |
| 195 | + ({ returnModified }) => { |
| 196 | + if (!returnModified.shipping_methods?.length) { |
| 197 | + return |
| 198 | + } |
| 199 | + |
| 200 | + return returnModified.shipping_methods[0].shipping_option_id |
| 201 | + } |
| 202 | + ) |
| 203 | + |
| 204 | + when({ returnShippingOptionId }, ({ returnShippingOptionId }) => { |
| 205 | + return !!returnShippingOptionId |
| 206 | + }).then(() => { |
| 207 | + const returnShippingOption = useRemoteQueryStep({ |
| 208 | + entry_point: "shipping_options", |
| 209 | + fields: [ |
| 210 | + "id", |
| 211 | + "provider_id", |
| 212 | + "service_zone.fulfillment_set.location.id", |
| 213 | + "service_zone.fulfillment_set.location.address.*", |
| 214 | + ], |
| 215 | + variables: { |
| 216 | + id: returnShippingOptionId, |
| 217 | + }, |
| 218 | + list: false, |
| 219 | + throw_if_key_not_found: true, |
| 220 | + }).config({ name: "return-shipping-option" }) |
| 221 | + |
| 222 | + const fulfillmentData = transform( |
| 223 | + { order, items: createdReturnItems, returnShippingOption }, |
| 224 | + prepareFulfillmentData |
| 225 | + ) |
| 226 | + |
| 227 | + const returnFulfillment = |
| 228 | + createReturnFulfillmentWorkflow.runAsStep(fulfillmentData) |
| 229 | + |
| 230 | + const link = transform( |
| 231 | + { orderReturn, fulfillment: returnFulfillment }, |
| 232 | + (data) => { |
| 233 | + return [ |
| 234 | + { |
| 235 | + [Modules.ORDER]: { return_id: data.orderReturn.id }, |
| 236 | + [Modules.FULFILLMENT]: { fulfillment_id: data.fulfillment.id }, |
| 237 | + }, |
| 238 | + ] |
| 239 | + } |
| 240 | + ) |
| 241 | + |
| 242 | + createRemoteLinkStep(link) |
| 243 | + }) |
| 244 | + |
95 | 245 | return previewOrderChangeStep(order.id)
|
96 | 246 | }
|
97 | 247 | )
|
0 commit comments