diff --git a/integration-tests/http/__tests__/inventory/admin/inventory.spec.ts b/integration-tests/http/__tests__/inventory/admin/inventory.spec.ts index 4064a52bcbf48..408f15571b1c9 100644 --- a/integration-tests/http/__tests__/inventory/admin/inventory.spec.ts +++ b/integration-tests/http/__tests__/inventory/admin/inventory.spec.ts @@ -175,6 +175,38 @@ medusaIntegrationTestRunner({ message: `Cannot remove Inventory Levels for ${stockLocation1.id} because there are stocked or reserved items at the locations`, }) }) + + it("should successfully add an inventory location", async () => { + await api.post( + `/admin/inventory-items/${inventoryItem1.id}/location-levels/${stockLocation1.id}`, + { stocked_quantity: 10 }, + adminHeaders + ) + + await api.post( + `/admin/inventory-items/${inventoryItem1.id}/location-levels/batch`, + { create: [{ location_id: stockLocation2.id }] }, + adminHeaders + ) + + const { + data: { inventory_levels: inventoryLevels }, + } = await api.get( + `/admin/inventory-items/${inventoryItem1.id}/location-levels`, + adminHeaders + ) + + expect(inventoryLevels).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + location_id: stockLocation1.id, + }), + expect.objectContaining({ + location_id: stockLocation2.id, + }), + ]) + ) + }) }) describe("DELETE /admin/inventory-items/:id/location-levels/:id", () => { diff --git a/packages/core/core-flows/src/inventory/workflows/bulk-create-delete-levels.ts b/packages/core/core-flows/src/inventory/workflows/bulk-create-delete-levels.ts index 989e5102ab83b..13ef8eab29c57 100644 --- a/packages/core/core-flows/src/inventory/workflows/bulk-create-delete-levels.ts +++ b/packages/core/core-flows/src/inventory/workflows/bulk-create-delete-levels.ts @@ -1,6 +1,7 @@ import { InventoryLevelDTO, InventoryTypes } from "@medusajs/types" import { createWorkflow, + when, WorkflowData, WorkflowResponse, } from "@medusajs/workflows-sdk" @@ -22,12 +23,22 @@ export const bulkCreateDeleteLevelsWorkflow = createWorkflow( ( input: WorkflowData ): WorkflowResponse => { - deleteInventoryLevelsWorkflow.runAsStep({ - input: { - $or: input.deletes, - }, + when({ input }, ({ input }) => { + return !!input.deletes?.length + }).then(() => { + deleteInventoryLevelsWorkflow.runAsStep({ + input: { + $or: input.deletes, + }, + }) }) - return new WorkflowResponse(createInventoryLevelsStep(input.creates)) + const created = when({ input }, ({ input }) => { + return !!input.creates?.length + }).then(() => { + return createInventoryLevelsStep(input.creates) + }) + + return new WorkflowResponse(created || []) } )