From 29dca1ca481e34cc17678b7b4f0e854d3a404acd Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Thu, 11 Sep 2025 12:12:13 +0200 Subject: [PATCH] fix(): update cart line item route fetching (#13477) **What** Remove cart fetching before workflow which fetch the cart as well --- .changeset/spotty-hotels-speak.md | 6 ++++ .../workflows/update-line-item-in-cart.ts | 7 ++++ .../carts/[id]/line-items/[line_id]/route.ts | 32 ++----------------- 3 files changed, 15 insertions(+), 30 deletions(-) create mode 100644 .changeset/spotty-hotels-speak.md diff --git a/.changeset/spotty-hotels-speak.md b/.changeset/spotty-hotels-speak.md new file mode 100644 index 0000000000..a533576091 --- /dev/null +++ b/.changeset/spotty-hotels-speak.md @@ -0,0 +1,6 @@ +--- +"@medusajs/medusa": patch +"@medusajs/core-flows": patch +--- + +fix(): update cart line item route fetching diff --git a/packages/core/core-flows/src/cart/workflows/update-line-item-in-cart.ts b/packages/core/core-flows/src/cart/workflows/update-line-item-in-cart.ts index 286f89c34e..86e51a1775 100644 --- a/packages/core/core-flows/src/cart/workflows/update-line-item-in-cart.ts +++ b/packages/core/core-flows/src/cart/workflows/update-line-item-in-cart.ts @@ -134,6 +134,13 @@ export const updateLineItemInCartWorkflow = createWorkflow( input: UpdateLineItemInCartWorkflowInputDTO & AdditionalData }) => { const item = data.cart.items.find((i) => i.id === data.input.item_id)! + if (!item) { + throw new MedusaError( + MedusaError.Types.NOT_FOUND, + `Line item with id: ${data.input.item_id} was not found` + ) + } + const variantIds = [item?.variant_id].filter(Boolean) return { item, variantIds } } diff --git a/packages/medusa/src/api/store/carts/[id]/line-items/[line_id]/route.ts b/packages/medusa/src/api/store/carts/[id]/line-items/[line_id]/route.ts index 41d94eeade..852e4081df 100644 --- a/packages/medusa/src/api/store/carts/[id]/line-items/[line_id]/route.ts +++ b/packages/medusa/src/api/store/carts/[id]/line-items/[line_id]/route.ts @@ -2,10 +2,9 @@ import { deleteLineItemsWorkflowId, updateLineItemInCartWorkflowId, } from "@medusajs/core-flows" -import { prepareListQuery } from "@medusajs/framework" import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http" import { HttpTypes } from "@medusajs/framework/types" -import { MedusaError, Modules } from "@medusajs/framework/utils" +import { Modules } from "@medusajs/framework/utils" import { refetchCart } from "../../../helpers" import { StoreUpdateCartLineItemType } from "../../../validators" @@ -13,38 +12,11 @@ export const POST = async ( req: MedusaRequest, res: MedusaResponse ) => { - // TODO: Move this to the workflow when the query to line item is fixed - const cart = await refetchCart( - req.params.id, - req.scope, - prepareListQuery( - {}, - { - defaults: [ - "id", - "region_id", - "customer_id", - "sales_channel_id", - "currency_code", - "*items", - ], - } - ).remoteQueryConfig.fields - ) - - const item = cart.items?.find((i) => i.id === req.params.line_id) - if (!item) { - throw new MedusaError( - MedusaError.Types.NOT_FOUND, - `Line item with id: ${req.params.line_id} was not found` - ) - } - const we = req.scope.resolve(Modules.WORKFLOW_ENGINE) await we.run(updateLineItemInCartWorkflowId, { input: { cart_id: req.params.id, - item_id: item.id, + item_id: req.params.line_id, update: req.validatedBody, }, transactionId: "cart-update-item-" + req.params.id,