1 import {
2 ChangeActionType,
3 isDefined,
4 MathBN,
5 MedusaError,
6 mergeMetadata,
7 } from "@medusajs/framework/utils"
8 import { OrderChangeProcessing } from "../calculate-order-change"
9 import { setActionReference } from "../set-action-reference"
10
11 OrderChangeProcessing.registerActionType(ChangeActionType.ITEM_UPDATE, {
12 operation({ action, currentOrder, options }) {
13 const existingIndex = currentOrder.items.findIndex(
14 (item) => item.id === action.details.reference_id
15 )
16
17 const existing = currentOrder.items[existingIndex]
18
19 const originalQuantity = MathBN.convert(
20 existing.detail.quantity ?? existing.quantity
21 )
22 const originalUnitPrice = MathBN.convert(
23 existing.detail.unit_price ?? existing.unit_price
24 )
25
26 const currentQuantity = MathBN.convert(action.details.quantity)
27 const quantityDiff = MathBN.sub(currentQuantity, originalQuantity)
28
29 existing.quantity = currentQuantity
30 existing.detail.quantity = currentQuantity
31
32 if (isDefined(action.details.metadata)) {
33 const isUnset = action.details.metadata === null
34
35 if (isUnset) {
36 existing.detail.metadata = null
37 existing.metadata = null
38 } else {
39 existing.detail.metadata = mergeMetadata(
40 existing.detail.metadata ?? existing.metadata ?? {},
41 action.details.metadata
42 )
43 existing.metadata = mergeMetadata(
44 existing.metadata ?? {},
45 action.details.metadata
46 )
47 }
48 }
49
50 if (action.details.adjustments) {
51 existing.adjustments = action.details.adjustments
52 }
53
54 if (isDefined(action.details.unit_price)) {
55 const currentUnitPrice = MathBN.convert(action.details.unit_price)
56 const originalTotal = MathBN.mult(originalUnitPrice, originalQuantity)
57 const currentTotal = MathBN.mult(currentUnitPrice, currentQuantity)
58
59 existing.unit_price = currentUnitPrice
60 existing.detail.unit_price = currentUnitPrice
61
62 setActionReference(existing, action, options)
63
64 return MathBN.sub(currentTotal, originalTotal)
65 }
66
67 setActionReference(existing, action, options)
68
69 return MathBN.mult(existing.unit_price, quantityDiff)
70 },
71 validate({ action, currentOrder }) {
72 const refId = action.details?.reference_id
73 if (refId == null) {
74 throw new MedusaError(
75 MedusaError.Types.INVALID_DATA,
76 "Reference ID is required."
77 )
78 }
79
80 const existing = currentOrder.items.find((item) => item.id === refId)
81 if (!existing) {
82 throw new MedusaError(
83 MedusaError.Types.INVALID_DATA,
84 `Item ID "${refId}" not found.`
85 )
86 }
87
88 if (action.details?.quantity == null) {
89 throw new MedusaError(
90 MedusaError.Types.INVALID_DATA,
91 `Quantity of item ${refId} is required.`
92 )
93 }
94
95 action.details.quantity ??= 0
96
97 const lower = MathBN.lt(
98 action.details.quantity,
99 existing.detail?.fulfilled_quantity
100 )
101
102 if (lower) {
103 throw new MedusaError(
104 MedusaError.Types.INVALID_DATA,
105 `Item ${refId} has already been fulfilled and quantity cannot be lower than ${existing.detail?.fulfilled_quantity}.`
106 )
107 }
108 },
109 })