1 import {
2 BigNumberInput,
3 InferEntityType,
4 PromotionTypes,
5 } from "@medusajs/framework/types"
6 import {
7 ApplicationMethodAllocation,
8 ApplicationMethodTargetType,
9 ApplicationMethodType,
10 ComputedActions,
11 MathBN,
12 MedusaError,
13 } from "@medusajs/framework/utils"
14 import { Promotion } from "@models"
15 import { areRulesValidForContext } from "../validations"
16 import { sortShippingLineByPriceAscending } from "./sort-by-price"
17 import { computeActionForBudgetExceeded } from "./usage"
18
19 export function getComputedActionsForShippingMethods(
20 promotion: PromotionTypes.PromotionDTO | InferEntityType<typeof Promotion>,
21 shippingMethodApplicationContext: PromotionTypes.ComputeActionContext[ApplicationMethodTargetType.SHIPPING_METHODS],
22 methodIdPromoValueMap: Map<string, number>
23 ): PromotionTypes.ComputeActions[] {
24 let applicableShippingItems: PromotionTypes.ComputeActionContext[ApplicationMethodTargetType.SHIPPING_METHODS] =
25 []
26
27 if (!shippingMethodApplicationContext) {
28 throw new MedusaError(
29 MedusaError.Types.INVALID_DATA,
30 `"shipping_methods" should be present as an array in the context for computeActions`
31 )
32 }
33
34 for (const shippingMethodContext of shippingMethodApplicationContext) {
35 const isPromotionApplicableToItem = areRulesValidForContext(
36 promotion.application_method?.target_rules!,
37 shippingMethodContext,
38 ApplicationMethodTargetType.SHIPPING_METHODS
39 )
40
41 if (!isPromotionApplicableToItem) {
42 continue
43 }
44
45 applicableShippingItems.push(shippingMethodContext)
46 }
47
48 const allocation = promotion.application_method?.allocation!
49 if (allocation === ApplicationMethodAllocation.ONCE) {
50 applicableShippingItems = applicableShippingItems.sort(
51 sortShippingLineByPriceAscending
52 )
53 }
54
55 return applyPromotionToShippingMethods(
56 promotion,
57 applicableShippingItems,
58 methodIdPromoValueMap
59 )
60 }
61
62 export function applyPromotionToShippingMethods(
63 promotion: PromotionTypes.PromotionDTO | InferEntityType<typeof Promotion>,
64 shippingMethods: PromotionTypes.ComputeActionContext[ApplicationMethodTargetType.SHIPPING_METHODS],
65 methodIdPromoValueMap: Map<string, BigNumberInput>
66 ): PromotionTypes.ComputeActions[] {
67 const { application_method: applicationMethod } = promotion
68 const allocation = applicationMethod?.allocation!
69 const computedActions: PromotionTypes.ComputeActions[] = []
70 const maxQuantity = applicationMethod?.max_quantity ?? 0
71 let remainingQuota = maxQuantity
72
73 if (
74 allocation === ApplicationMethodAllocation.EACH ||
75 allocation === ApplicationMethodAllocation.ONCE
76 ) {
77 for (const method of shippingMethods!) {
78 if (
79 allocation === ApplicationMethodAllocation.ONCE &&
80 remainingQuota <= 0
81 ) {
82 break
83 }
84 if (!method.subtotal) {
85 continue
86 }
87
88 const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0
89 let promotionValue = MathBN.convert(applicationMethod?.value ?? 0)
90 const applicableTotal = MathBN.sub(method.subtotal, appliedPromoValue)
91
92 if (applicationMethod?.type === ApplicationMethodType.PERCENTAGE) {
93 promotionValue = MathBN.mult(
94 MathBN.div(promotionValue, 100),
95 applicableTotal
96 )
97 }
98
99 const amount = MathBN.min(promotionValue, applicableTotal)
100
101 if (MathBN.lte(amount, 0)) {
102 continue
103 }
104
105 const budgetExceededAction = computeActionForBudgetExceeded(
106 promotion,
107 amount
108 )
109
110 if (budgetExceededAction) {
111 computedActions.push(budgetExceededAction)
112
113 continue
114 }
115
116 methodIdPromoValueMap.set(
117 method.id,
118 MathBN.add(appliedPromoValue, amount)
119 )
120
121 if (allocation === ApplicationMethodAllocation.ONCE) {
122 remainingQuota -= 1
123 }
124
125 computedActions.push({
126 action: ComputedActions.ADD_SHIPPING_METHOD_ADJUSTMENT,
127 shipping_method_id: method.id,
128 amount,
129 code: promotion.code!,
130 })
131 }
132 }
133
134 if (allocation === ApplicationMethodAllocation.ACROSS) {
135 const totalApplicableValue = shippingMethods!.reduce((acc, method) => {
136 const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0
137
138 return MathBN.add(
139 acc,
140 MathBN.sub(method.subtotal ?? 0, appliedPromoValue)
141 )
142 }, MathBN.convert(0))
143
144 if (MathBN.lte(totalApplicableValue, 0)) {
145 return computedActions
146 }
147
148 for (const method of shippingMethods!) {
149 if (!method.subtotal) {
150 continue
151 }
152
153 const promotionValue = applicationMethod?.value ?? 0
154 const appliedPromoValue = methodIdPromoValueMap.get(method.id) ?? 0
155 const applicableTotal = MathBN.sub(method.subtotal, appliedPromoValue)
156
157 let applicablePromotionValue = MathBN.mult(
158 MathBN.div(applicableTotal, totalApplicableValue),
159 promotionValue
160 )
161
162 if (applicationMethod?.type === ApplicationMethodType.PERCENTAGE) {
163 applicablePromotionValue = MathBN.mult(
164 MathBN.div(promotionValue, 100),
165 applicableTotal
166 )
167 }
168
169 const amount = MathBN.min(applicablePromotionValue, applicableTotal)
170
171 if (MathBN.lte(amount, 0)) {
172 continue
173 }
174
175 const budgetExceededAction = computeActionForBudgetExceeded(
176 promotion,
177 amount
178 )
179
180 if (budgetExceededAction) {
181 computedActions.push(budgetExceededAction)
182
183 continue
184 }
185
186 methodIdPromoValueMap.set(
187 method.id,
188 MathBN.add(appliedPromoValue, amount)
189 )
190
191 computedActions.push({
192 action: ComputedActions.ADD_SHIPPING_METHOD_ADJUSTMENT,
193 shipping_method_id: method.id,
194 amount,
195 code: promotion.code!,
196 })
197 }
198 }
199
200 return computedActions
201 }