|
1
|
import { ChangeActionType, MedusaError } from "@medusajs/framework/utils" |
|
2
|
import { OrderChangeProcessing } from "../calculate-order-change" |
|
3
|
import { setActionReference } from "../set-action-reference" |
|
4
|
|
|
5
|
OrderChangeProcessing.registerActionType(ChangeActionType.SHIPPING_ADD, { |
|
6
|
operation({ action, currentOrder, options }) { |
|
7
|
const shipping = Array.isArray(currentOrder.shipping_methods) |
|
8
|
? currentOrder.shipping_methods |
|
9
|
: [currentOrder.shipping_methods] |
|
10
|
|
|
11
|
let existing = shipping.find((sh) => sh.id === action.reference_id) |
|
12
|
|
|
13
|
if (!existing) { |
|
14
|
existing = { |
|
15
|
id: action.reference_id!, |
|
16
|
order_id: currentOrder.id, |
|
17
|
return_id: action.return_id, |
|
18
|
claim_id: action.claim_id, |
|
19
|
exchange_id: action.exchange_id, |
|
20
|
amount: action.amount as number, |
|
21
|
} |
|
22
|
shipping.push(existing) |
|
23
|
} |
|
24
|
|
|
25
|
setActionReference(existing, action, options) |
|
26
|
currentOrder.shipping_methods = shipping |
|
27
|
}, |
|
28
|
validate({ action }) { |
|
29
|
if (!action.reference_id) { |
|
30
|
throw new MedusaError( |
|
31
|
MedusaError.Types.INVALID_DATA, |
|
32
|
"Reference ID is required." |
|
33
|
) |
|
34
|
} |
|
35
|
|
|
36
|
if (action.amount == null) { |
|
37
|
throw new MedusaError( |
|
38
|
MedusaError.Types.INVALID_DATA, |
|
39
|
"Amount is required." |
|
40
|
) |
|
41
|
} |
|
42
|
}, |
|
43
|
}) |