fix(cart): add shipping method to cart (#7945)

This commit is contained in:
Carlos R. L. Rodrigues authored and GitHub committed 2024-07-04 12:47:39 +00:00
1 parent f4225c051f
commit d036130604
4 files changed
+64 -4

No files matched your search

@@ -1500,6 +1500,21 @@ medusaIntegrationTestRunner({
}, },
}) })
await addShippingMethodToWorkflow(appContainer).run({
input: {
options: [{ id: shippingOption.id }],
cart_id: cart.id,
},
})
// should remove the previous shipping method
await addShippingMethodToWorkflow(appContainer).run({
input: {
options: [{ id: shippingOption.id }],
cart_id: cart.id,
},
})
cart = await cartModuleService.retrieveCart(cart.id, { cart = await cartModuleService.retrieveCart(cart.id, {
relations: ["shipping_methods"], relations: ["shipping_methods"],
}) })
@@ -1508,12 +1523,12 @@ medusaIntegrationTestRunner({
expect.objectContaining({ expect.objectContaining({
id: cart.id, id: cart.id,
currency_code: "usd", currency_code: "usd",
shipping_methods: expect.arrayContaining([ shipping_methods: [
expect.objectContaining({ expect.objectContaining({
amount: 3000, amount: 3000,
name: "Test shipping option", name: "Test shipping option",
}), }),
]), ],
}) })
) )
}) })
@@ -18,6 +18,7 @@ export * from "./prepare-adjustments-from-promotion-actions"
export * from "./refresh-cart-shipping-methods" export * from "./refresh-cart-shipping-methods"
export * from "./remove-line-item-adjustments" export * from "./remove-line-item-adjustments"
export * from "./remove-shipping-method-adjustments" export * from "./remove-shipping-method-adjustments"
export * from "./remove-shipping-method-from-cart"
export * from "./retrieve-cart" export * from "./retrieve-cart"
export * from "./retrieve-cart-with-links" export * from "./retrieve-cart-with-links"
export * from "./set-tax-lines-for-items" export * from "./set-tax-lines-for-items"
@@ -0,0 +1,35 @@
import { ICartModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
interface StepInput {
shipping_method_ids: string[]
}
export const removeShippingMethodFromCartStepId =
"remove-shipping-method-to-cart-step"
export const removeShippingMethodFromCartStep = createStep(
removeShippingMethodFromCartStepId,
async (data: StepInput, { container }) => {
const cartService = container.resolve<ICartModuleService>(
ModuleRegistrationName.CART
)
const methods = await cartService.softDeleteShippingMethods(
data.shipping_method_ids
)
return new StepResponse(methods, data.shipping_method_ids)
},
async (ids, { container }) => {
if (!ids) {
return
}
const cartService: ICartModuleService = container.resolve(
ModuleRegistrationName.CART
)
await cartService.restoreShippingMethods(ids)
}
)
@@ -7,6 +7,7 @@ import {
import { useRemoteQueryStep } from "../../../common/steps/use-remote-query" import { useRemoteQueryStep } from "../../../common/steps/use-remote-query"
import { import {
addShippingMethodToCartStep, addShippingMethodToCartStep,
removeShippingMethodFromCartStep,
validateCartShippingOptionsStep, validateCartShippingOptionsStep,
} from "../steps" } from "../steps"
import { refreshCartPromotionsStep } from "../steps/refresh-cart-promotions" import { refreshCartPromotionsStep } from "../steps/refresh-cart-promotions"
@@ -76,7 +77,15 @@ export const addShippingMethodToWorkflow = createWorkflow(
} }
) )
const shippingMethods = addShippingMethodToCartStep({ const currentShippingMethods = transform({ cart }, ({ cart }) => {
return cart.shipping_methods.map((sm) => sm.id)
})
removeShippingMethodFromCartStep({
shipping_method_ids: currentShippingMethods,
})
const shippingMethodsToAdd = addShippingMethodToCartStep({
shipping_methods: shippingMethodInput, shipping_methods: shippingMethodInput,
}) })
@@ -84,7 +93,7 @@ export const addShippingMethodToWorkflow = createWorkflow(
refreshCartPromotionsStep({ id: input.cart_id }), refreshCartPromotionsStep({ id: input.cart_id }),
updateTaxLinesStep({ updateTaxLinesStep({
cart_or_cart_id: input.cart_id, cart_or_cart_id: input.cart_id,
shipping_methods: shippingMethods, shipping_methods: shippingMethodsToAdd,
}) })
) )
} }