|
1
|
import { ChangeActionType, MedusaError } from "@medusajs/framework/utils" |
|
2
|
import { OrderChangeProcessing } from "../calculate-order-change" |
|
3
|
|
|
4
|
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_REMOVE, { |
|
5
|
operation({ action, currentOrder, options }) { |
|
6
|
const shipping = Array.isArray(currentOrder.shipping_methods) |
|
7
|
? currentOrder.shipping_methods |
|
8
|
: [currentOrder.shipping_methods] |
|
9
|
|
|
10
|
const existingIndex = shipping.findIndex( |
|
11
|
(item) => item.id === action.reference_id |
|
12
|
) |
|
13
|
|
|
14
|
if (existingIndex > -1) { |
|
15
|
shipping.splice(existingIndex, 1) |
|
16
|
} |
|
17
|
|
|
18
|
currentOrder.shipping_methods = shipping |
|
19
|
}, |
|
20
|
validate({ action }) { |
|
21
|
if (!action.reference_id) { |
|
22
|
throw new MedusaError( |
|
23
|
MedusaError.Types.INVALID_DATA, |
|
24
|
"Reference ID is required." |
|
25
|
) |
|
26
|
} |
|
27
|
}, |
|
28
|
}) |