|
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( |
|
6
|
ChangeActionType.SHIPPING_ADJUSTMENTS_REPLACE, |
|
7
|
{ |
|
8
|
operation({ action, currentOrder, options }) { |
|
9
|
let existing = currentOrder.shipping_methods.find( |
|
10
|
(method) => method.id === action.details.reference_id |
|
11
|
) |
|
12
|
|
|
13
|
if (!existing) { |
|
14
|
return |
|
15
|
} |
|
16
|
|
|
17
|
existing.adjustments = action.details.adjustments ?? [] |
|
18
|
|
|
19
|
setActionReference(existing, action, options) |
|
20
|
}, |
|
21
|
validate({ action }) { |
|
22
|
const refId = action.details?.reference_id |
|
23
|
|
|
24
|
if (!action.details.adjustments) { |
|
25
|
throw new MedusaError( |
|
26
|
MedusaError.Types.INVALID_DATA, |
|
27
|
`Adjustments of shipping method ${refId} must exist.` |
|
28
|
) |
|
29
|
} |
|
30
|
}, |
|
31
|
} |
|
32
|
) |