feat(medusa): add list location levels endpoint in api v2 (#6741)

This commit is contained in:
Philip Korsholm
2024-03-27 18:59:38 +01:00
committed by GitHub
parent cf3c9b13b5
commit ff84f749d5
4 changed files with 122 additions and 0 deletions

View File

@@ -206,6 +206,80 @@ medusaIntegrationTestRunner({
)
})
describe("List inventory levels", () => {
let inventoryItemId
let stockLocation1Id
let stockLocation2Id
beforeEach(async () => {
const inventoryItem = await api.post(
`/admin/inventory-items`,
{ sku: "test-sku" },
adminHeaders
)
inventoryItemId = inventoryItem.data.inventory_item.id
const locationService = appContainer.resolve(
ModuleRegistrationName.STOCK_LOCATION
)
const stockLocation1 = await locationService.create({
name: "loc-1",
})
stockLocation1Id = stockLocation1.id
const stockLocation2 = await locationService.create({
name: "loc-2",
})
stockLocation2Id = stockLocation2.id
await api.post(
`/admin/inventory-items/${inventoryItemId}/location-levels`,
{
location_id: stockLocation1Id,
stocked_quantity: 10,
},
adminHeaders
)
await api.post(
`/admin/inventory-items/${inventoryItemId}/location-levels`,
{
location_id: stockLocation2Id,
stocked_quantity: 15,
},
adminHeaders
)
})
it("should list the inventory levels", async () => {
const response = await api.get(
`/admin/inventory-items/${inventoryItemId}/location-levels`,
adminHeaders
)
expect(response.data).toEqual(
expect.objectContaining({
count: 2,
offset: 0,
limit: 50,
})
)
expect(response.data.inventory_levels).toHaveLength(2)
expect(response.data.inventory_levels).toEqual(
expect.arrayContaining([
expect.objectContaining({
location_id: stockLocation1Id,
stocked_quantity: 10,
}),
expect.objectContaining({
location_id: stockLocation2Id,
stocked_quantity: 15,
}),
])
)
})
})
describe("Update inventory item", () => {
let inventoryItemId
beforeEach(async () => {

View File

@@ -46,3 +46,27 @@ export const POST = async (
res.status(200).json({ inventory_item })
}
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const query = remoteQueryObjectFromString({
entryPoint: "inventory_levels",
variables: {
filters: req.filterableFields,
order: req.listConfig.order,
skip: req.listConfig.skip,
take: req.listConfig.take,
},
fields: req.remoteQueryConfig.fields,
})
const { rows: inventory_levels, metadata } = await remoteQuery(query)
res.status(200).json({
inventory_levels,
count: metadata.count,
offset: metadata.skip,
limit: metadata.take,
})
}

View File

@@ -1,6 +1,7 @@
import * as QueryConfig from "./query-config"
import {
AdminGetInventoryItemsItemLocationLevelsParams,
AdminGetInventoryItemsItemParams,
AdminGetInventoryItemsParams,
AdminPostInventoryItemsInventoryItemParams,
@@ -52,6 +53,21 @@ export const adminInventoryRoutesMiddlewares: MiddlewareRoute[] = [
),
],
},
{
method: ["GET"],
matcher: "/admin/inventory-items/:id/location-levels",
middlewares: [
transformQuery(
AdminGetInventoryItemsItemLocationLevelsParams,
QueryConfig.listLocationLevelsTransformQueryConfig
),
],
},
{
method: ["POST"],
matcher: "/admin/inventory-items/:id/location-levels",
middlewares: [transformBody(AdminPostInventoryItemsItemLocationLevelsReq)],
},
{
method: ["POST"],
matcher: "/admin/inventory-items/:id/location-levels",

View File

@@ -259,6 +259,14 @@ export class AdminPostInventoryItemsReq {
metadata?: Record<string, unknown>
}
export class AdminGetInventoryItemsItemLocationLevelsParams extends FindParams {
/**
* Location IDs to filter location levels.
*/
@IsOptional()
@IsString({ each: true })
location_id?: string[]
}
/**
* @schema AdminPostInventoryItemsItemLocationLevelsLevelReq
* type: object