Skip to content

Commit

Permalink
feat(utils): consolidate promotion utils + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan committed Feb 28, 2024
1 parent 0d46abf commit 3d0fe73
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 249 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-starfishes-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/utils": patch
---

feat(utils): consolidate promotion utils + refactor
Original file line number Diff line number Diff line change
Expand Up @@ -3424,13 +3424,13 @@ describe("Promotion Service: computeActions", () => {
{
action: "addShippingMethodAdjustment",
shipping_method_id: "shipping_method_express",
amount: 83.33333333333331,
amount: 166.66666666666666,
code: "PROMOTION_TEST_2",
},
{
action: "addShippingMethodAdjustment",
shipping_method_id: "shipping_method_standard",
amount: 16.66666666666667,
amount: 33.333333333333336,
code: "PROMOTION_TEST_2",
},
])
Expand Down
86 changes: 39 additions & 47 deletions packages/promotion/src/services/promotion-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
PromotionTypes,
} from "@medusajs/types"
import {
ApplicationMethodAllocation,
ApplicationMethodTargetType,
CampaignBudgetType,
ComputedActions,
InjectManager,
InjectTransactionManager,
MedusaContext,
Expand Down Expand Up @@ -251,6 +253,8 @@ export default class PromotionModuleService<
sharedContext
)

// Promotions we need to apply includes all the codes thats passed as an argument
// of this method, along with any automatic promotions that can be applied to the context
const automaticPromotionCodes = automaticPromotions.map((p) => p.code!)
const promotionCodesToApply = [
...promotionCodes,
Expand Down Expand Up @@ -307,17 +311,21 @@ export default class PromotionModuleService<
}
)

const appliedCodes = [...appliedShippingCodes, ...appliedItemCodes]
const sortedPermissionsToApply = promotions
.filter((p) => promotionCodesToApply.includes(p.code!))
.sort(ComputeActionUtils.sortByBuyGetType)

const existingPromotionsMap = new Map<string, PromotionTypes.PromotionDTO>(
promotions.map((promotion) => [promotion.code!, promotion])
)

// We look at any existing promo codes applied in the context and recommend
// them to be removed to start calculations from the beginning and refresh
// the adjustments if they are requested to be applied again
const appliedCodes = [...appliedShippingCodes, ...appliedItemCodes]

for (const appliedCode of appliedCodes) {
const promotion = existingPromotionsMap.get(appliedCode)
const adjustments = codeAdjustmentMap.get(appliedCode) || []
const action = appliedShippingCodes.includes(appliedCode)
? ComputedActions.REMOVE_SHIPPING_METHOD_ADJUSTMENT
: ComputedActions.REMOVE_ITEM_ADJUSTMENT

if (!promotion) {
throw new MedusaError(
Expand All @@ -326,31 +334,21 @@ export default class PromotionModuleService<
)
}

if (appliedItemCodes.includes(appliedCode)) {
const adjustments = codeAdjustmentMap.get(appliedCode) || []

adjustments.forEach((adjustment) =>
computedActions.push({
action: "removeItemAdjustment",
adjustment_id: adjustment.id,
code: appliedCode,
})
)
}

if (appliedShippingCodes.includes(appliedCode)) {
const adjustments = codeAdjustmentMap.get(appliedCode) || []

adjustments.forEach((adjustment) =>
computedActions.push({
action: "removeShippingMethodAdjustment",
adjustment_id: adjustment.id,
code: appliedCode,
})
)
}
adjustments.forEach((adjustment) =>
computedActions.push({
action,
adjustment_id: adjustment.id,
code: appliedCode,
})
)
}

// We sort the promo codes to apply with buy get type first as they
// are likely to be most valuable.
const sortedPermissionsToApply = promotions
.filter((p) => promotionCodesToApply.includes(p.code!))
.sort(ComputeActionUtils.sortByBuyGetType)

for (const promotionToApply of sortedPermissionsToApply) {
const promotion = existingPromotionsMap.get(promotionToApply.code!)!

Expand Down Expand Up @@ -384,36 +382,30 @@ export default class PromotionModuleService<
}

if (promotion.type === PromotionType.STANDARD) {
if (
const isTargetOrder =
applicationMethod.target_type === ApplicationMethodTargetType.ORDER
) {
const computedActionsForItems =
ComputeActionUtils.getComputedActionsForOrder(
promotion,
applicationContext,
methodIdPromoValueMap
)

computedActions.push(...computedActionsForItems)
}

if (
const isTargetItems =
applicationMethod.target_type === ApplicationMethodTargetType.ITEMS
) {
const isTargetShipping =
applicationMethod.target_type ===
ApplicationMethodTargetType.SHIPPING_METHODS
const allocationOverride = isTargetOrder
? ApplicationMethodAllocation.ACROSS
: undefined

if (isTargetOrder || isTargetItems) {
const computedActionsForItems =
ComputeActionUtils.getComputedActionsForItems(
promotion,
applicationContext[ApplicationMethodTargetType.ITEMS],
methodIdPromoValueMap
methodIdPromoValueMap,
allocationOverride
)

computedActions.push(...computedActionsForItems)
}

if (
applicationMethod.target_type ===
ApplicationMethodTargetType.SHIPPING_METHODS
) {
if (isTargetShipping) {
const computedActionsForShippingMethods =
ComputeActionUtils.getComputedActionsForShippingMethods(
promotion,
Expand Down
4 changes: 1 addition & 3 deletions packages/promotion/src/utils/compute-actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from "./buy-get"
export * from "./items"
export * from "./order"
export * from "./shipping-methods"
export * from "./line-items"
export * from "./usage"
178 changes: 0 additions & 178 deletions packages/promotion/src/utils/compute-actions/items.ts

This file was deleted.

Loading

0 comments on commit 3d0fe73

Please sign in to comment.