From 6f50e376a9a1ebe7d755d0a14743030ddd933adb Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Tue, 13 Aug 2024 12:40:46 +0200 Subject: [PATCH] fix(core-flows): fixes duplicate fulfillment issue (#8581) what: - fixes a bug where fulfilling an outbound item was throwing an error due to the entire order being processed for fulfillment RESOLVES CC-298 --- .../http/__tests__/claims/claims.spec.ts | 44 ++++++++++++++----- .../src/order/workflows/create-fulfillment.ts | 8 +++- .../src/order/workflows/create-shipment.ts | 2 +- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/integration-tests/http/__tests__/claims/claims.spec.ts b/integration-tests/http/__tests__/claims/claims.spec.ts index d370dc9910..e856a48076 100644 --- a/integration-tests/http/__tests__/claims/claims.spec.ts +++ b/integration-tests/http/__tests__/claims/claims.spec.ts @@ -247,18 +247,14 @@ medusaIntegrationTestRunner({ ).data.fulfillment_set inventoryItem = ( - await api.post( - `/admin/inventory-items`, - { sku: "inv-1234" }, - adminHeaders - ) - ).data.inventory_item + await api.get(`/admin/inventory-items?sku=test-variant`, adminHeaders) + ).data.inventory_items[0] await api.post( `/admin/inventory-items/${inventoryItem.id}/location-levels`, { location_id: location.id, - stocked_quantity: 2, + stocked_quantity: 10, }, adminHeaders ) @@ -271,7 +267,7 @@ medusaIntegrationTestRunner({ `/admin/inventory-items/${inventoryItemExtra.id}/location-levels`, { location_id: location.id, - stocked_quantity: 4, + stocked_quantity: 10, }, adminHeaders ) @@ -438,7 +434,9 @@ medusaIntegrationTestRunner({ }) describe("Claims lifecycle", () => { - it("Full flow with 2 orders", async () => { + let claimId + + beforeEach(async () => { let r2 = await api.post( "/admin/claims", { @@ -513,7 +511,7 @@ medusaIntegrationTestRunner({ await api.post(`/admin/claims/${claimId2}/request`, {}, adminHeaders) - const claimId = baseClaim.id + claimId = baseClaim.id const item = order.items[0] let result = await api.post( @@ -629,16 +627,38 @@ medusaIntegrationTestRunner({ ).data expect(reservationsResponse.reservations).toHaveLength(1) + }) + it("should complete flow with fulfilled items successfully", async () => { + const fulfillOrder = ( + await api.get(`/admin/orders/${order.id}`, adminHeaders) + ).data.order + + const fulfillableItem = fulfillOrder.items.find( + (item) => item.detail.fulfilled_quantity === 0 + ) + + await api.post( + `/admin/orders/${order.id}/fulfillments`, + { + location_id: location.id, + items: [{ id: fulfillableItem.id, quantity: 1 }], + }, + adminHeaders + ) + }) + + it("should go through cancel flow successfully", async () => { await api.post(`/admin/claims/${claimId}/cancel`, {}, adminHeaders) - result = ( + const [claim] = ( await api.get( `/admin/claims?fields=*claim_items,*additional_items`, adminHeaders ) ).data.claims - expect(result[0].canceled_at).toBeDefined() + + expect(claim.canceled_at).toBeDefined() const reservationsResponseAfterCanceling = ( await api.get( diff --git a/packages/core/core-flows/src/order/workflows/create-fulfillment.ts b/packages/core/core-flows/src/order/workflows/create-fulfillment.ts index 6796bc209e..c3b1fa9d02 100644 --- a/packages/core/core-flows/src/order/workflows/create-fulfillment.ts +++ b/packages/core/core-flows/src/order/workflows/create-fulfillment.ts @@ -297,7 +297,13 @@ export const createOrderFulfillmentWorkflow = createWorkflow( const fulfillment = createFulfillmentWorkflow.runAsStep(fulfillmentData) const registerOrderFulfillmentData = transform( - { order, fulfillment, input, inputItemsMap, itemsList: input.items_list }, + { + order, + fulfillment, + input, + inputItemsMap, + itemsList: input.items ?? input.items_list, + }, prepareRegisterOrderFulfillmentData ) diff --git a/packages/core/core-flows/src/order/workflows/create-shipment.ts b/packages/core/core-flows/src/order/workflows/create-shipment.ts index ac26c5ac19..8d22eb0d9e 100644 --- a/packages/core/core-flows/src/order/workflows/create-shipment.ts +++ b/packages/core/core-flows/src/order/workflows/create-shipment.ts @@ -67,7 +67,7 @@ function prepareRegisterShipmentData({ reference: Modules.FULFILLMENT, reference_id: fulfillment.id, created_by: input.created_by, - items: order.items!.map((i) => { + items: (input.items ?? order.items)!.map((i) => { return { id: i.id, quantity: i.quantity,