feat(medusa): remove created reservations on subsequent failure for cart completion (#3554)

**What**
- If cart completion fails after creating reservations, remove those reservations

**Why**
- To avoid hanging reservations if something fails at a later point
This commit is contained in:
Philip Korsholm
2023-03-29 19:03:53 +02:00
committed by GitHub
parent a7e3f2d343
commit 5fd74b38ae
7 changed files with 160 additions and 43 deletions

View File

@@ -191,6 +191,47 @@ describe("/store/carts", () => {
expect(stockLevel.stocked_quantity).toEqual(5)
})
it("removes reserved quantity when failing to complete the cart", async () => {
const api = useApi()
const cartRes = await api.post(
`/store/carts`,
{
region_id: "test-region",
items: [
{
variant_id: variantId,
quantity: 3,
},
],
},
{ withCredentials: true }
)
const cartId = cartRes.data.cart.id
await api.post(`/store/carts/${cartId}/payment-sessions`)
await api.post(`/store/carts/${cartId}/payment-session`, {
provider_id: "test-pay",
})
const getRes = await api
.post(`/store/carts/${cartId}/complete`)
.catch((err) => err)
expect(getRes.response.status).toEqual(400)
expect(getRes.response.data).toEqual({
type: "invalid_data",
message:
"Can't insert null value in field customer_id on insert in table order",
})
const inventoryService = appContainer.resolve("inventoryService")
const [, count] = await inventoryService.listReservationItems({
line_item_id: cartRes.data.cart.items.map((i) => i.id),
})
expect(count).toEqual(0)
})
it("fails to add a item on the cart if the inventory isn't enough", async () => {
const api = useApi()
@@ -249,10 +290,10 @@ describe("/store/carts", () => {
.catch((e) => e)
expect(completeCartRes.response.status).toEqual(409)
expect(completeCartRes.response.data.code).toEqual(
expect(completeCartRes.response.data.errors[0].code).toEqual(
"insufficient_inventory"
)
expect(completeCartRes.response.data.message).toEqual(
expect(completeCartRes.response.data.errors[0].message).toEqual(
`Variant with id: ${variantId} does not have the required inventory`
)