feat: move create inventory to @medusajs/workflows (#5301)

**Why**
- We have some workflow-like flows in @medusajs/medusa. These should be moved over to the workflows package.
- Inventory Items <> Variant currently assume a 1-1 mapping. There should be support for a many-to-many mapping.

**What**
- PR introduces a feature flag for supporting many-to-many mappings for inventory and variants.
- Deletes legacy transaction handler in @medusajs/medusa.
- Adjusts existing createInventoryItems handler to remove dependency on variant data.

**Unkowns**
~~1. Couldn't find an existing test for the CreateProduct workflow. It should be tested that this still works as expected.~~
2. Have removed transaction managers as we should move to handling consistency through orchestration tooling. Are we ready for that?
This commit is contained in:
Sebastian Rindom
2023-10-11 11:01:56 -07:00
committed by GitHub
parent bbd9dd408f
commit 66413d094e
25 changed files with 480 additions and 281 deletions

View File

@@ -0,0 +1,96 @@
const path = require("path")
const { ProductVariantInventoryService } = require("@medusajs/medusa")
const {
bootstrapApp,
} = require("../../../../environment-helpers/bootstrap-app")
const { initDb, useDb } = require("../../../../environment-helpers/use-db")
const { setPort, useApi } = require("../../../../environment-helpers/use-api")
const adminSeeder = require("../../../../helpers/admin-seeder")
jest.setTimeout(30000)
const {
simpleProductFactory,
simpleOrderFactory,
} = require("../../../../factories")
const adminHeaders = { headers: { "x-medusa-access-token": "test_token" } }
describe("Inventory Items endpoints", () => {
let appContainer
let dbConnection
let express
let variantId
let inventoryItems
let locationId
let location2Id
let location3Id
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd })
const { container, app, port } = await bootstrapApp({ cwd, verbose: true })
appContainer = container
// Set feature flag
const flagRouter = appContainer.resolve("featureFlagRouter")
flagRouter.setFlag("many_to_many_inventory", true)
setPort(port)
express = app.listen(port, (err) => {
process.send(port)
})
})
beforeEach(async () => {
await adminSeeder(dbConnection)
})
afterAll(async () => {
const flagRouter = appContainer.resolve("featureFlagRouter")
flagRouter.setFlag("many_to_many_inventory", false)
const db = useDb()
await db.shutdown()
express.close()
})
afterEach(async () => {
jest.clearAllMocks()
const db = useDb()
return await db.teardown()
})
describe("Inventory Items", () => {
it("should create inventory item without variant id", async () => {
const api = useApi()
await api.post(
`/admin/inventory-items`,
{
sku: "TABLE_LEG",
description: "Table Leg",
},
adminHeaders
)
/** @type {ProductVariantInventoryService} */
const productVariantInventoryService = appContainer.resolve(
"productVariantInventoryService"
)
const inventoryService = appContainer.resolve("inventoryService")
const inventoryItems = await inventoryService.list()
expect(inventoryItems.length).toEqual(1)
const variants = await productVariantInventoryService.listByItem([
inventoryItems[0].id,
])
expect(variants.length).toEqual(0)
})
})
})

View File

@@ -345,7 +345,7 @@ describe("Inventory Items endpoints", () => {
const inventoryItemCreateRes = await api.post(
`/admin/inventory-items`,
{ variant_id: variantId },
{ variant_id: variantId, sku: "attach_this_to_variant" },
adminHeaders
)