feat(medusa,core-flows): update cart adjustments on item updates (#6539)
This commit is contained in:
@@ -14,4 +14,3 @@ export * from "./remove-shipping-method-adjustments"
|
||||
export * from "./retrieve-cart"
|
||||
export * from "./update-carts"
|
||||
export * from "./validate-variants-existence"
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { PromotionActions } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
import { updateCartPromotionsWorkflow } from "../workflows"
|
||||
|
||||
interface StepInput {
|
||||
id: string
|
||||
promo_codes?: string[]
|
||||
action?:
|
||||
| PromotionActions.ADD
|
||||
| PromotionActions.REMOVE
|
||||
| PromotionActions.REPLACE
|
||||
}
|
||||
|
||||
export const refreshCartPromotionsStepId = "refresh-cart-promotions"
|
||||
export const refreshCartPromotionsStep = createStep(
|
||||
refreshCartPromotionsStepId,
|
||||
async (data: StepInput, { container }) => {
|
||||
const { promo_codes = [], id, action = PromotionActions.ADD } = data
|
||||
|
||||
await updateCartPromotionsWorkflow(container).run({
|
||||
input: {
|
||||
action,
|
||||
promoCodes: promo_codes,
|
||||
cartId: id,
|
||||
},
|
||||
})
|
||||
|
||||
return new StepResponse(null)
|
||||
}
|
||||
)
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
getVariantsStep,
|
||||
validateVariantsExistStep,
|
||||
} from "../steps"
|
||||
import { refreshCartPromotionsStep } from "../steps/refresh-cart-promotions"
|
||||
import { prepareLineItemData } from "../utils/prepare-line-item-data"
|
||||
|
||||
// TODO: The AddToCartWorkflow are missing the following steps:
|
||||
@@ -70,6 +71,8 @@ export const addToCartWorkflow = createWorkflow(
|
||||
|
||||
const items = addToCartStep({ items: lineItems })
|
||||
|
||||
refreshCartPromotionsStep({ id: input.cart.id })
|
||||
|
||||
return items
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { CartDTO } from "@medusajs/types"
|
||||
import { PromotionActions } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
@@ -27,7 +26,7 @@ type WorkflowInput = {
|
||||
export const updateCartPromotionsWorkflowId = "update-cart-promotions"
|
||||
export const updateCartPromotionsWorkflow = createWorkflow(
|
||||
updateCartPromotionsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<CartDTO> => {
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
const retrieveCartInput = {
|
||||
id: input.cartId,
|
||||
config: {
|
||||
@@ -65,9 +64,5 @@ export const updateCartPromotionsWorkflow = createWorkflow(
|
||||
createLineItemAdjustmentsStep({ lineItemAdjustmentsToCreate }),
|
||||
createShippingMethodAdjustmentsStep({ shippingMethodAdjustmentsToCreate })
|
||||
)
|
||||
|
||||
return retrieveCartStep(retrieveCartInput).config({
|
||||
name: "retrieve-cart-result-step",
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
@@ -7,35 +7,18 @@ import {
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
createLineItemAdjustmentsStep,
|
||||
createShippingMethodAdjustmentsStep,
|
||||
findOneOrAnyRegionStep,
|
||||
findOrCreateCustomerStep,
|
||||
findSalesChannelStep,
|
||||
getActionsToComputeFromPromotionsStep,
|
||||
prepareAdjustmentsFromPromotionActionsStep,
|
||||
removeLineItemAdjustmentsStep,
|
||||
removeShippingMethodAdjustmentsStep,
|
||||
retrieveCartStep,
|
||||
updateCartsStep,
|
||||
} from "../steps"
|
||||
import { refreshCartPromotionsStep } from "../steps/refresh-cart-promotions"
|
||||
|
||||
export const updateCartWorkflowId = "update-cart"
|
||||
export const updateCartWorkflow = createWorkflow(
|
||||
updateCartWorkflowId,
|
||||
(input: WorkflowData<UpdateCartWorkflowInputDTO>): WorkflowData<CartDTO> => {
|
||||
const retrieveCartInput = {
|
||||
id: input.id,
|
||||
config: {
|
||||
relations: [
|
||||
"items",
|
||||
"items.adjustments",
|
||||
"shipping_methods",
|
||||
"shipping_methods.adjustments",
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
const [salesChannel, region, customerData] = parallelize(
|
||||
findSalesChannelStep({
|
||||
salesChannelId: input.sales_channel_id,
|
||||
@@ -79,34 +62,24 @@ export const updateCartWorkflow = createWorkflow(
|
||||
|
||||
updateCartsStep([cartInput])
|
||||
|
||||
const cart = retrieveCartStep(retrieveCartInput)
|
||||
const actions = getActionsToComputeFromPromotionsStep({
|
||||
cart,
|
||||
promoCodes: input.promo_codes,
|
||||
refreshCartPromotionsStep({
|
||||
id: input.id,
|
||||
promo_codes: input.promo_codes,
|
||||
action: PromotionActions.REPLACE,
|
||||
})
|
||||
|
||||
const {
|
||||
lineItemAdjustmentsToCreate,
|
||||
lineItemAdjustmentIdsToRemove,
|
||||
shippingMethodAdjustmentsToCreate,
|
||||
shippingMethodAdjustmentIdsToRemove,
|
||||
} = prepareAdjustmentsFromPromotionActionsStep({ actions })
|
||||
const retrieveCartInput = {
|
||||
id: input.id,
|
||||
config: {
|
||||
relations: [
|
||||
"items",
|
||||
"items.adjustments",
|
||||
"shipping_methods",
|
||||
"shipping_methods.adjustments",
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
parallelize(
|
||||
removeLineItemAdjustmentsStep({ lineItemAdjustmentIdsToRemove }),
|
||||
removeShippingMethodAdjustmentsStep({
|
||||
shippingMethodAdjustmentIdsToRemove,
|
||||
})
|
||||
)
|
||||
|
||||
parallelize(
|
||||
createLineItemAdjustmentsStep({ lineItemAdjustmentsToCreate }),
|
||||
createShippingMethodAdjustmentsStep({ shippingMethodAdjustmentsToCreate })
|
||||
)
|
||||
|
||||
return retrieveCartStep(retrieveCartInput).config({
|
||||
name: "retrieve-cart-result-step",
|
||||
})
|
||||
return retrieveCartStep(retrieveCartInput)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { getVariantPriceSetsStep } from ".."
|
||||
import { updateLineItemsStep } from "../../line-item/steps"
|
||||
import { refreshCartPromotionsStep } from "../steps/refresh-cart-promotions"
|
||||
|
||||
// TODO: The UpdateLineItemsWorkflow are missing the following steps:
|
||||
// - Confirm inventory exists (inventory module)
|
||||
@@ -55,6 +56,8 @@ export const updateLineItemInCartWorkflow = createWorkflow(
|
||||
selector: lineItemUpdate.selector,
|
||||
})
|
||||
|
||||
refreshCartPromotionsStep({ id: input.cart.id })
|
||||
|
||||
const updatedItem = transform({ result }, (data) => data.result?.[0])
|
||||
|
||||
return updatedItem
|
||||
|
||||
Reference in New Issue
Block a user