Skip to content

Commit

Permalink
fix(core-flow): invalid update quantity in update line item in cart w…
Browse files Browse the repository at this point in the history
…orkflow (#10405)

* fix: invalid update quantity in update line item in cart workflow

* test: update cart workflow test

* fix: rm shallow copy in transform
  • Loading branch information
daykrm authored Jan 5, 2025
1 parent 7d8f6cf commit 79c87c0
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 3 deletions.
116 changes: 115 additions & 1 deletion integration-tests/modules/__tests__/cart/store/cart.workflows.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2036,6 +2036,121 @@ medusaIntegrationTestRunner({
)
})

it("should throw insufficient inventory", async () => {
const salesChannel = await scModuleService.createSalesChannels({
name: "Webshop",
})

const location = await stockLocationModule.createStockLocations({
name: "Warehouse",
})

const [product] = await productModule.createProducts([
{
title: "Test product",
variants: [
{
title: "Test variant",
},
],
},
])

const inventoryItem = await inventoryModule.createInventoryItems({
sku: "inv-1234",
})

await inventoryModule.createInventoryLevels([
{
inventory_item_id: inventoryItem.id,
location_id: location.id,
stocked_quantity: 2,
},
])

const priceSet = await pricingModule.createPriceSets({
prices: [
{
amount: 3000,
currency_code: "usd",
},
],
})

await remoteLink.create([
{
[Modules.PRODUCT]: {
variant_id: product.variants[0].id,
},
[Modules.PRICING]: {
price_set_id: priceSet.id,
},
},
{
[Modules.SALES_CHANNEL]: {
sales_channel_id: salesChannel.id,
},
[Modules.STOCK_LOCATION]: {
stock_location_id: location.id,
},
},
{
[Modules.PRODUCT]: {
variant_id: product.variants[0].id,
},
[Modules.INVENTORY]: {
inventory_item_id: inventoryItem.id,
},
},
])

let cart = await cartModuleService.createCarts({
currency_code: "usd",
sales_channel_id: salesChannel.id,
items: [
{
variant_id: product.variants[0].id,
quantity: 1,
unit_price: 5000,
title: "Test item",
},
],
})

cart = await cartModuleService.retrieveCart(cart.id, {
select: ["id", "region_id", "currency_code"],
relations: ["items", "items.variant_id", "items.metadata"],
})

const item = cart.items?.[0]!

const { errors } = await updateLineItemInCartWorkflow(
appContainer
).run({
input: {
cart_id: cart.id,
item_id: item.id,
update: {
metadata: {
foo: "bar",
},
quantity: 3,
},
},
throwOnError: false,
})

expect(errors).toEqual([
{
action: "confirm-item-inventory-as-step",
handlerType: "invoke",
error: expect.objectContaining({
message: `Some variant does not have the required inventory`,
}),
},
])
})

describe("compensation", () => {
it("should revert line item update to original state", async () => {
expect.assertions(2)
Expand Down Expand Up @@ -2075,7 +2190,6 @@ medusaIntegrationTestRunner({
inventory_item_id: inventoryItem.id,
location_id: location.id,
stocked_quantity: 2,
reserved_quantity: 0,
},
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export const updateLineItemInCartWorkflow = createWorkflow(

validateVariantPricesStep({ variants })

const items = transform({ item }, ({ item }) => {
return [item]
const items = transform({ input, item }, (data) => {
return [Object.assign(data.item, { quantity: data.input.update.quantity })]
})

confirmVariantInventoryWorkflow.runAsStep({
Expand Down

0 comments on commit 79c87c0

Please sign in to comment.