|
| 1 | +import { |
| 2 | + BigNumberInput, |
| 3 | + CartLineItemDTO, |
| 4 | + ICartModuleService, |
| 5 | +} from "@medusajs/framework/types" |
| 6 | +import { MathBN, Modules, isDefined } from "@medusajs/framework/utils" |
| 7 | +import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk" |
| 8 | +import { |
| 9 | + findMatchingLineItem, |
| 10 | + lineItemFieldsForMerging, |
| 11 | +} from "../utils/find-matching-line-item" |
| 12 | + |
| 13 | +/** |
| 14 | + * The details of the items to resolve pricing quantities for. |
| 15 | + */ |
| 16 | +export interface GetLineItemPricingQuantitiesStepInput { |
| 17 | + /** |
| 18 | + * The ID of the cart the items are added to. |
| 19 | + */ |
| 20 | + id: string |
| 21 | + /** |
| 22 | + * The items being added to the cart. |
| 23 | + */ |
| 24 | + items?: { |
| 25 | + variant_id?: string |
| 26 | + quantity?: BigNumberInput |
| 27 | + unit_price?: BigNumberInput |
| 28 | + metadata?: Record<string, unknown> | null |
| 29 | + }[] |
| 30 | +} |
| 31 | + |
| 32 | +export const getLineItemPricingQuantitiesStepId = |
| 33 | + "get-line-item-pricing-quantities" |
| 34 | +/** |
| 35 | + * This step resolves the quantity that each item added to a cart must be priced |
| 36 | + * for. When an item is merged into an existing line item of the cart, its price |
| 37 | + * has to be selected for the resulting total quantity, not only for the added |
| 38 | + * quantity. Otherwise, quantity-based prices (such as price list tiers) are |
| 39 | + * resolved against the wrong quantity. |
| 40 | + * |
| 41 | + * The returned quantities are aligned, by index, with the input items. |
| 42 | + * |
| 43 | + * @example |
| 44 | + * const quantities = getLineItemPricingQuantitiesStep({ |
| 45 | + * "id": "cart_123", |
| 46 | + * "items": [{ |
| 47 | + * "variant_id": "variant_123", |
| 48 | + * "quantity": 1, |
| 49 | + * }] |
| 50 | + * }) |
| 51 | + */ |
| 52 | +export const getLineItemPricingQuantitiesStep = createStep( |
| 53 | + getLineItemPricingQuantitiesStepId, |
| 54 | + async (data: GetLineItemPricingQuantitiesStepInput, { container }) => { |
| 55 | + const items = data.items ?? [] |
| 56 | + const quantities = items.map((item) => |
| 57 | + MathBN.convert(item.quantity ?? 1).toNumber() |
| 58 | + ) |
| 59 | + |
| 60 | + const variantIds = items |
| 61 | + .map((item) => item.variant_id) |
| 62 | + .filter((id): id is string => !!id) |
| 63 | + |
| 64 | + if (!variantIds.length) { |
| 65 | + return new StepResponse(quantities) |
| 66 | + } |
| 67 | + |
| 68 | + const cartModule = container.resolve<ICartModuleService>(Modules.CART) |
| 69 | + |
| 70 | + const existingVariantItems = await cartModule.listLineItems( |
| 71 | + { |
| 72 | + cart_id: data.id, |
| 73 | + variant_id: variantIds, |
| 74 | + }, |
| 75 | + { |
| 76 | + select: lineItemFieldsForMerging, |
| 77 | + } |
| 78 | + ) |
| 79 | + |
| 80 | + const variantItemsMap = existingVariantItems.reduce( |
| 81 | + (result, variantItem) => { |
| 82 | + if (!result.has(variantItem.variant_id!)) { |
| 83 | + result.set(variantItem.variant_id!, []) |
| 84 | + } |
| 85 | + result.get(variantItem.variant_id!)!.push(variantItem) |
| 86 | + return result |
| 87 | + }, |
| 88 | + new Map<string, CartLineItemDTO[]>() |
| 89 | + ) |
| 90 | + |
| 91 | + items.forEach((item, index) => { |
| 92 | + if (!item.variant_id) { |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + const existingItem = findMatchingLineItem( |
| 97 | + variantItemsMap.get(item.variant_id), |
| 98 | + { |
| 99 | + metadata: item.metadata, |
| 100 | + unit_price: item.unit_price, |
| 101 | + is_custom_price: isDefined(item.unit_price), |
| 102 | + } |
| 103 | + ) |
| 104 | + |
| 105 | + if (!existingItem) { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + const quantity = MathBN.sum( |
| 110 | + existingItem.quantity as number, |
| 111 | + item.quantity ?? 1 |
| 112 | + ) |
| 113 | + |
| 114 | + // In case of multiple items merging into the same line item, the |
| 115 | + // quantities accumulate, the same way they do when the items are updated. |
| 116 | + existingItem.quantity = quantity |
| 117 | + |
| 118 | + quantities[index] = quantity.toNumber() |
| 119 | + }) |
| 120 | + |
| 121 | + return new StepResponse(quantities) |
| 122 | + } |
| 123 | +) |
0 commit comments