Skip to content

Commit d63ca00

Browse files
chore(core-flows): return fulfillment link (#8304)
1 parent feabe0e commit d63ca00

File tree

10 files changed

+261
-53
lines changed

10 files changed

+261
-53
lines changed

integration-tests/modules/__tests__/order/workflows/create-complete-return.spec.ts

+1-20
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,7 @@ medusaIntegrationTestRunner({
428428
variables: {
429429
id: order.id,
430430
},
431-
fields: [
432-
"*",
433-
"items.*",
434-
"shipping_methods.*",
435-
"total",
436-
"item_total",
437-
"fulfillments.*",
438-
],
431+
fields: ["*", "items.*", "shipping_methods.*", "total", "item_total"],
439432
})
440433

441434
const [returnOrder] = await remoteQuery(remoteQueryObject)
@@ -501,18 +494,6 @@ medusaIntegrationTestRunner({
501494
order_id: expect.any(String),
502495
}),
503496
]),
504-
fulfillments: [
505-
expect.objectContaining({
506-
id: expect.any(String),
507-
location_id: location.id,
508-
provider_id: providerId,
509-
shipping_option_id: shippingOption.id,
510-
// TODO: Validate the address once we are fixed on it
511-
/*delivery_address: {
512-
id: "fuladdr_01HY0RTAP0P1EEAFK7BXJ0BKBN",
513-
},*/
514-
}),
515-
],
516497
})
517498
)
518499
})

packages/core/core-flows/src/order/workflows/return/confirm-return-request.ts

+156-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
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"
313
import {
414
WorkflowData,
515
createStep,
616
createWorkflow,
717
transform,
18+
when,
819
} from "@medusajs/workflows-sdk"
9-
import { useRemoteQueryStep } from "../../../common"
20+
import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common"
21+
import { createReturnFulfillmentWorkflow } from "../../../fulfillment/workflows/create-return-fulfillment"
1022
import { previewOrderChangeStep } from "../../steps"
1123
import { confirmOrderChanges } from "../../steps/confirm-order-changes"
1224
import { createReturnItemsStep } from "../../steps/create-return-items"
@@ -36,21 +48,93 @@ const validationStep = createStep(
3648
}
3749
)
3850

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+
39114
export const confirmReturnRequestWorkflowId = "confirm-return-request"
40115
export const confirmReturnRequestWorkflow = createWorkflow(
41116
confirmReturnRequestWorkflowId,
42117
function (input: WorkflowInput): WorkflowData<OrderDTO> {
43118
const orderReturn: ReturnDTO = useRemoteQueryStep({
44119
entry_point: "return",
45-
fields: ["id", "status", "order_id", "canceled_at"],
120+
fields: ["id", "status", "order_id", "location_id", "canceled_at"],
46121
variables: { id: input.return_id },
47122
list: false,
48123
throw_if_key_not_found: true,
49124
})
50125

51126
const order: OrderDTO = useRemoteQueryStep({
52127
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+
],
54138
variables: { id: orderReturn.order_id },
55139
list: false,
56140
throw_if_key_not_found: true,
@@ -85,13 +169,79 @@ export const confirmReturnRequestWorkflow = createWorkflow(
85169

86170
validationStep({ order, orderReturn, orderChange })
87171

88-
createReturnItemsStep({
172+
const createdReturnItems = createReturnItemsStep({
89173
returnId: orderReturn.id,
90174
changes: returnItemActions,
91175
})
92176

93177
confirmOrderChanges({ changes: [orderChange], orderId: order.id })
94178

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+
95245
return previewOrderChangeStep(order.id)
96246
}
97247
)

packages/core/core-flows/src/order/workflows/return/create-complete-return.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
WorkflowData,
1313
createStep,
1414
createWorkflow,
15-
parallelize,
1615
transform,
1716
} from "@medusajs/workflows-sdk"
1817
import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common"
@@ -50,7 +49,10 @@ function prepareShippingMethodData({
5049
adjustments: [],
5150
}
5251

53-
if (isDefined(inputShippingOption.price) && inputShippingOption.price >= 0) {
52+
if (
53+
isDefined(inputShippingOption.price) &&
54+
MathBN.gte(inputShippingOption.price, 0)
55+
) {
5456
obj.amount = inputShippingOption.price
5557
} else {
5658
if (returnShippingOption.price_type === "calculated") {
@@ -288,27 +290,26 @@ export const createAndCompleteReturnOrderWorkflow = createWorkflow(
288290
const returnFulfillment =
289291
createReturnFulfillmentWorkflow.runAsStep(fulfillmentData)
290292

293+
const returnCreated = createCompleteReturnStep({
294+
order_id: input.order_id,
295+
items: input.items,
296+
shipping_method: shippingMethodData,
297+
created_by: input.created_by,
298+
})
299+
291300
const link = transform(
292-
{ order_id: input.order_id, fulfillment: returnFulfillment },
301+
{ returnCreated, fulfillment: returnFulfillment },
293302
(data) => {
294303
return [
295304
{
296-
[Modules.ORDER]: { order_id: data.order_id },
305+
[Modules.ORDER]: { return_id: data.returnCreated.id },
297306
[Modules.FULFILLMENT]: { fulfillment_id: data.fulfillment.id },
298307
},
299308
]
300309
}
301310
)
302311

303-
const [returnCreated] = parallelize(
304-
createCompleteReturnStep({
305-
order_id: input.order_id,
306-
items: input.items,
307-
shipping_method: shippingMethodData,
308-
created_by: input.created_by,
309-
}),
310-
createRemoteLinkStep(link)
311-
)
312+
createRemoteLinkStep(link)
312313

313314
const receiveItems = transform(
314315
{

packages/core/types/src/fulfillment/mutations/fulfillment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export interface CreateFulfillmentDTO {
6565
/**
6666
* The labels associated with the fulfillment.
6767
*/
68-
labels: Omit<CreateFulfillmentLabelDTO, "fulfillment_id">[]
68+
labels?: Omit<CreateFulfillmentLabelDTO, "fulfillment_id">[]
6969

7070
/**
7171
* The associated order to be sent to the provider.

packages/core/types/src/workflow/fulfillment/create-fulfillment.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export type CreateFulfillmentWorkflowInput = {
174174
/**
175175
* The labels associated with the fulfillment.
176176
*/
177-
labels: CreateFulfillmentLabelWorkflowDTO[]
177+
labels?: CreateFulfillmentLabelWorkflowDTO[]
178178

179179
/**
180180
* The associated fulfillment order to be sent to the provider.

packages/core/types/src/workflow/order/request-item-return.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface DeleteRequestItemReturnWorkflowInput {
1414

1515
export interface UpdateRequestItemReturnWorkflowInput {
1616
return_id: string
17+
claim_id?: string
18+
exchange_id?: string
1719
action_id: string
1820
data: {
1921
quantity?: BigNumberInput

packages/core/utils/src/link/links.ts

+6
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,10 @@ export const LINKS = {
110110
Modules.FULFILLMENT,
111111
"fulfillment_id"
112112
),
113+
ReturnFulfillment: composeLinkName(
114+
Modules.ORDER,
115+
"return_id",
116+
Modules.FULFILLMENT,
117+
"fulfillment_id"
118+
),
113119
}

0 commit comments

Comments
 (0)