Skip to content

Commit 23053af

Browse files
chore(medusa,core-flows): update request return items and shipping (medusajs#8151)
1 parent f73ca97 commit 23053af

30 files changed

+619
-110
lines changed

integration-tests/http/__tests__/returns/returns.spec.ts

+84-21
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ medusaIntegrationTestRunner({
2626
items: [
2727
{
2828
title: "Custom Item 2",
29-
quantity: 1,
30-
unit_price: 50,
29+
quantity: 2,
30+
unit_price: 25,
3131
},
3232
],
3333
sales_channel_id: "test",
@@ -152,7 +152,7 @@ medusaIntegrationTestRunner({
152152
items: [
153153
{
154154
id: item.id,
155-
quantity: 1,
155+
quantity: 2,
156156
},
157157
],
158158
},
@@ -208,7 +208,7 @@ medusaIntegrationTestRunner({
208208
items: [
209209
{
210210
id: item.id,
211-
quantity: 1,
211+
quantity: 2,
212212
},
213213
],
214214
},
@@ -222,14 +222,15 @@ medusaIntegrationTestRunner({
222222
expect.objectContaining({
223223
id: expect.any(String),
224224
title: "Custom Item 2",
225-
unit_price: 50,
226-
quantity: 1,
225+
unit_price: 25,
226+
quantity: 2,
227227
subtotal: 50,
228228
total: 50,
229229
fulfilled_total: 50,
230230
return_requested_total: 50,
231231
detail: expect.objectContaining({
232-
return_requested_quantity: 1,
232+
quantity: 2,
233+
return_requested_quantity: 2,
233234
}),
234235
}),
235236
]),
@@ -284,6 +285,32 @@ medusaIntegrationTestRunner({
284285
})
285286
)
286287

288+
// updated the requested quantitty
289+
const updateReturnItemActionId =
290+
result.data.order_preview.items[0].actions[0].id
291+
result = await api.post(
292+
`/admin/returns/${returnId}/request-items/${updateReturnItemActionId}`,
293+
{
294+
quantity: 2,
295+
internal_note: "Test internal note",
296+
},
297+
adminHeaders
298+
)
299+
300+
expect(result.data.order_preview).toEqual(
301+
expect.objectContaining({
302+
id: order.id,
303+
items: expect.arrayContaining([
304+
expect.objectContaining({
305+
detail: expect.objectContaining({
306+
quantity: 2,
307+
return_requested_quantity: 2,
308+
}),
309+
}),
310+
]),
311+
})
312+
)
313+
287314
result = await api.post(
288315
`/admin/returns/${returnId}/shipping-method`,
289316
{
@@ -299,12 +326,20 @@ medusaIntegrationTestRunner({
299326
expect.objectContaining({
300327
id: expect.any(String),
301328
title: "Custom Item 2",
302-
unit_price: 50,
303-
quantity: 1,
329+
unit_price: 25,
330+
quantity: 2,
304331
subtotal: 50,
305332
total: 50,
306333
fulfilled_total: 50,
307334
return_requested_total: 50,
335+
actions: expect.arrayContaining([
336+
expect.objectContaining({
337+
details: expect.objectContaining({
338+
quantity: 2,
339+
}),
340+
internal_note: "Test internal note",
341+
}),
342+
]),
308343
}),
309344
]),
310345
shipping_methods: expect.arrayContaining([
@@ -331,8 +366,8 @@ medusaIntegrationTestRunner({
331366
expect.objectContaining({
332367
id: expect.any(String),
333368
title: "Custom Item 2",
334-
unit_price: 50,
335-
quantity: 1,
369+
unit_price: 25,
370+
quantity: 2,
336371
subtotal: 50,
337372
total: 50,
338373
fulfilled_total: 50,
@@ -372,6 +407,34 @@ medusaIntegrationTestRunner({
372407
adminHeaders
373408
)
374409

410+
// updates the shipping method price
411+
const updateShippingActionId =
412+
result.data.order_preview.shipping_methods[1].actions[0].id
413+
result = await api.post(
414+
`/admin/returns/${returnId}/shipping-method/${updateShippingActionId}`,
415+
{
416+
custom_price: 1002,
417+
internal_note: "cx agent note",
418+
},
419+
adminHeaders
420+
)
421+
422+
expect(result.data.order_preview.shipping_methods).toHaveLength(2)
423+
expect(result.data.order_preview.shipping_methods[1]).toEqual(
424+
expect.objectContaining({
425+
id: expect.any(String),
426+
name: "Return shipping",
427+
amount: 1002,
428+
subtotal: 1002,
429+
total: 1002,
430+
actions: [
431+
expect.objectContaining({
432+
internal_note: "cx agent note",
433+
}),
434+
],
435+
})
436+
)
437+
375438
result = await api.post(
376439
`/admin/returns/${returnId}/request`,
377440
{},
@@ -385,8 +448,8 @@ medusaIntegrationTestRunner({
385448
expect.objectContaining({
386449
id: expect.any(String),
387450
title: "Custom Item 2",
388-
unit_price: 50,
389-
quantity: 1,
451+
unit_price: 25,
452+
quantity: 2,
390453
subtotal: 50,
391454
total: 50,
392455
fulfilled_total: 50,
@@ -397,9 +460,9 @@ medusaIntegrationTestRunner({
397460
expect.objectContaining({
398461
id: expect.any(String),
399462
name: "Return shipping",
400-
amount: 1000,
401-
subtotal: 1000,
402-
total: 1000,
463+
amount: 1002,
464+
subtotal: 1002,
465+
total: 1002,
403466
}),
404467
]),
405468
})
@@ -418,8 +481,8 @@ medusaIntegrationTestRunner({
418481
expect.objectContaining({
419482
id: expect.any(String),
420483
title: "Custom Item 2",
421-
unit_price: 50,
422-
quantity: 1,
484+
unit_price: 25,
485+
quantity: 2,
423486
subtotal: 50,
424487
total: 50,
425488
fulfilled_total: 50,
@@ -430,9 +493,9 @@ medusaIntegrationTestRunner({
430493
expect.objectContaining({
431494
id: expect.any(String),
432495
name: "Return shipping",
433-
amount: 1000,
434-
subtotal: 1000,
435-
total: 1000,
496+
amount: 1002,
497+
subtotal: 1002,
498+
total: 1002,
436499
}),
437500
]),
438501
})

packages/core/core-flows/src/order/steps/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ export * from "./register-shipment"
2525
export * from "./set-tax-lines-for-items"
2626
export * from "./update-order-change-actions"
2727
export * from "./update-order-exchanges"
28+
export * from "./update-shipping-methods"
2829
export * from "./update-tax-lines"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
IOrderModuleService,
3+
UpdateOrderShippingMethodDTO,
4+
} from "@medusajs/types"
5+
import {
6+
ModuleRegistrationName,
7+
getSelectsAndRelationsFromObjectArray,
8+
} from "@medusajs/utils"
9+
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
10+
11+
export const updateOrderShippingMethodsStepId = "update-order-shopping-methods"
12+
export const updateOrderShippingMethodsStep = createStep(
13+
updateOrderShippingMethodsStepId,
14+
async (data: UpdateOrderShippingMethodDTO[], { container }) => {
15+
const service = container.resolve<IOrderModuleService>(
16+
ModuleRegistrationName.ORDER
17+
)
18+
19+
const { selects, relations } = getSelectsAndRelationsFromObjectArray(data, {
20+
objectFields: ["metadata"],
21+
})
22+
const dataBeforeUpdate = await service.listOrderClaims(
23+
{ id: data.map((d) => d.id) },
24+
{ relations, select: selects }
25+
)
26+
27+
const updated = await service.updateShippingMethods(data)
28+
29+
return new StepResponse(updated, dataBeforeUpdate)
30+
},
31+
async (dataBeforeUpdate, { container }) => {
32+
if (!dataBeforeUpdate?.length) {
33+
return
34+
}
35+
36+
const service = container.resolve<IOrderModuleService>(
37+
ModuleRegistrationName.ORDER
38+
)
39+
40+
await service.updateShippingMethods(dataBeforeUpdate)
41+
}
42+
)

packages/core/core-flows/src/order/workflows/begin-order-claim.ts packages/core/core-flows/src/order/workflows/claim/begin-order-claim.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {
55
createWorkflow,
66
transform,
77
} from "@medusajs/workflows-sdk"
8-
import { useRemoteQueryStep } from "../../common"
9-
import { createOrderClaimsStep } from "../steps/create-claims"
10-
import { createOrderChangeStep } from "../steps/create-order-change"
11-
import { throwIfOrderIsCancelled } from "../utils/order-validation"
8+
import { useRemoteQueryStep } from "../../../common"
9+
import { createOrderClaimsStep } from "../../steps/create-claims"
10+
import { createOrderChangeStep } from "../../steps/create-order-change"
11+
import { throwIfOrderIsCancelled } from "../../utils/order-validation"
1212

1313
const validationStep = createStep(
1414
"begin-claim-order-validation",

packages/core/core-flows/src/order/workflows/claim-add-new-item.ts packages/core/core-flows/src/order/workflows/claim/claim-add-new-item.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import {
1111
createWorkflow,
1212
transform,
1313
} from "@medusajs/workflows-sdk"
14-
import { useRemoteQueryStep } from "../../common"
15-
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions"
16-
import { previewOrderChangeStep } from "../steps/preview-order-change"
14+
import { useRemoteQueryStep } from "../../../common"
15+
import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions"
16+
import { previewOrderChangeStep } from "../../steps/preview-order-change"
1717
import {
1818
throwIfIsCancelled,
1919
throwIfOrderChangeIsNotActive,
2020
throwIfOrderIsCancelled,
21-
} from "../utils/order-validation"
22-
import { addOrderLineItemsWorkflow } from "./add-line-items"
21+
} from "../../utils/order-validation"
22+
import { addOrderLineItemsWorkflow } from "../add-line-items"
2323

2424
const validationStep = createStep(
2525
"claim-add-new-item-validation",

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ import {
1313
transform,
1414
when,
1515
} from "@medusajs/workflows-sdk"
16-
import { useRemoteQueryStep } from "../../common"
17-
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions"
18-
import { createReturnsStep } from "../steps/create-returns"
19-
import { previewOrderChangeStep } from "../steps/preview-order-change"
20-
import { updateOrderClaimsStep } from "../steps/update-order-claims"
16+
import { useRemoteQueryStep } from "../../../common"
17+
import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions"
18+
import { createReturnsStep } from "../../steps/create-returns"
19+
import { previewOrderChangeStep } from "../../steps/preview-order-change"
20+
import { updateOrderClaimsStep } from "../../steps/update-order-claims"
2121
import {
2222
throwIfIsCancelled,
2323
throwIfItemsDoesNotExistsInOrder,
2424
throwIfOrderChangeIsNotActive,
2525
throwIfOrderIsCancelled,
26-
} from "../utils/order-validation"
26+
} from "../../utils/order-validation"
2727

2828
const validationStep = createStep(
2929
"claim-request-item-return-validation",

packages/core/core-flows/src/order/workflows/begin-order-exchange.ts packages/core/core-flows/src/order/workflows/exchange/begin-order-exchange.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {
55
createWorkflow,
66
transform,
77
} from "@medusajs/workflows-sdk"
8-
import { useRemoteQueryStep } from "../../common"
9-
import { createOrderExchangesStep } from "../steps/create-exchanges"
10-
import { createOrderChangeStep } from "../steps/create-order-change"
11-
import { throwIfOrderIsCancelled } from "../utils/order-validation"
8+
import { useRemoteQueryStep } from "../../../common"
9+
import { createOrderExchangesStep } from "../../steps/create-exchanges"
10+
import { createOrderChangeStep } from "../../steps/create-order-change"
11+
import { throwIfOrderIsCancelled } from "../../utils/order-validation"
1212

1313
const validationStep = createStep(
1414
"begin-exchange-order-validation",

packages/core/core-flows/src/order/workflows/create-exchange-return-shipping-method.ts packages/core/core-flows/src/order/workflows/exchange/create-exchange-return-shipping-method.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import {
66
} from "@medusajs/types"
77
import { ChangeActionType } from "@medusajs/utils"
88
import {
9+
WorkflowData,
910
createStep,
1011
createWorkflow,
1112
transform,
12-
WorkflowData,
1313
} from "@medusajs/workflows-sdk"
14-
import { useRemoteQueryStep } from "../../common"
15-
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions"
16-
import { createOrderShippingMethods } from "../steps/create-order-shipping-methods"
14+
import { useRemoteQueryStep } from "../../../common"
15+
import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions"
16+
import { createOrderShippingMethods } from "../../steps/create-order-shipping-methods"
1717
import {
1818
throwIfOrderChangeIsNotActive,
1919
throwIfOrderIsCancelled,
20-
} from "../utils/order-validation"
20+
} from "../../utils/order-validation"
2121

2222
const validationStep = createStep(
2323
"validate-create-exchange-return-shipping-method",

packages/core/core-flows/src/order/workflows/exchange-add-new-item.ts packages/core/core-flows/src/order/workflows/exchange/exchange-add-new-item.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import {
1111
createWorkflow,
1212
transform,
1313
} from "@medusajs/workflows-sdk"
14-
import { useRemoteQueryStep } from "../../common"
15-
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions"
16-
import { previewOrderChangeStep } from "../steps/preview-order-change"
14+
import { useRemoteQueryStep } from "../../../common"
15+
import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions"
16+
import { previewOrderChangeStep } from "../../steps/preview-order-change"
1717
import {
1818
throwIfIsCancelled,
1919
throwIfOrderChangeIsNotActive,
2020
throwIfOrderIsCancelled,
21-
} from "../utils/order-validation"
22-
import { addOrderLineItemsWorkflow } from "./add-line-items"
21+
} from "../../utils/order-validation"
22+
import { addOrderLineItemsWorkflow } from "../add-line-items"
2323

2424
const validationStep = createStep(
2525
"exchange-add-new-item-validation",

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ import {
1313
transform,
1414
when,
1515
} from "@medusajs/workflows-sdk"
16-
import { useRemoteQueryStep } from "../../common"
17-
import { createOrderChangeActionsStep } from "../steps/create-order-change-actions"
18-
import { createReturnsStep } from "../steps/create-returns"
19-
import { previewOrderChangeStep } from "../steps/preview-order-change"
20-
import { updateOrderExchangesStep } from "../steps/update-order-exchanges"
16+
import { useRemoteQueryStep } from "../../../common"
17+
import { createOrderChangeActionsStep } from "../../steps/create-order-change-actions"
18+
import { createReturnsStep } from "../../steps/create-returns"
19+
import { previewOrderChangeStep } from "../../steps/preview-order-change"
20+
import { updateOrderExchangesStep } from "../../steps/update-order-exchanges"
2121
import {
2222
throwIfIsCancelled,
2323
throwIfItemsDoesNotExistsInOrder,
2424
throwIfOrderChangeIsNotActive,
2525
throwIfOrderIsCancelled,
26-
} from "../utils/order-validation"
26+
} from "../../utils/order-validation"
2727

2828
const validationStep = createStep(
2929
"exchange-request-item-return-validation",

0 commit comments

Comments
 (0)