-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(medusa,core-flows): update cart adjustments on item updates #6539
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@medusajs/core-flows": patch | ||
"@medusajs/medusa": patch | ||
--- | ||
|
||
feat(medusa,core-flows): update cart adjustments on item updates |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }) | ||
|
||
parallelize( | ||
removeLineItemAdjustmentsStep({ lineItemAdjustmentIdsToRemove }), | ||
removeShippingMethodAdjustmentsStep({ | ||
shippingMethodAdjustmentIdsToRemove, | ||
}) | ||
) | ||
|
||
parallelize( | ||
createLineItemAdjustmentsStep({ lineItemAdjustmentsToCreate }), | ||
createShippingMethodAdjustmentsStep({ shippingMethodAdjustmentsToCreate }) | ||
) | ||
const retrieveCartInput = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Can we do this? I didn't know that. Thought everything had to be either a step or transformer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We changed it recently to allow plain objects as well. You only need the transformer if its an unresolved object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's great There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one can't keep up with so many improvements 😆 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha, let's keep it that way 🏎️ |
||
id: input.id, | ||
config: { | ||
relations: [ | ||
"items", | ||
"items.adjustments", | ||
"shipping_methods", | ||
"shipping_methods.adjustments", | ||
], | ||
}, | ||
} | ||
|
||
return retrieveCartStep(retrieveCartInput).config({ | ||
name: "retrieve-cart-result-step", | ||
}) | ||
return retrieveCartStep(retrieveCartInput) | ||
} | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love this! 🎆