feat(medusa): add list location levels endpoint in api v2 (#6741)
This commit is contained in:
1 parent
cf3c9b13b5
commit
ff84f749d5
4 files changed
+122
No files matched your search
@@ -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", () => {
|
describe("Update inventory item", () => {
|
||||||
let inventoryItemId
|
let inventoryItemId
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
|||||||
@@ -46,3 +46,27 @@ export const POST = async (
|
|||||||
|
|
||||||
res.status(200).json({ inventory_item })
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import * as QueryConfig from "./query-config"
|
import * as QueryConfig from "./query-config"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
AdminGetInventoryItemsItemLocationLevelsParams,
|
||||||
AdminGetInventoryItemsItemParams,
|
AdminGetInventoryItemsItemParams,
|
||||||
AdminGetInventoryItemsParams,
|
AdminGetInventoryItemsParams,
|
||||||
AdminPostInventoryItemsInventoryItemParams,
|
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"],
|
method: ["POST"],
|
||||||
matcher: "/admin/inventory-items/:id/location-levels",
|
matcher: "/admin/inventory-items/:id/location-levels",
|
||||||
|
|||||||
@@ -259,6 +259,14 @@ export class AdminPostInventoryItemsReq {
|
|||||||
metadata?: Record<string, unknown>
|
metadata?: Record<string, unknown>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class AdminGetInventoryItemsItemLocationLevelsParams extends FindParams {
|
||||||
|
/**
|
||||||
|
* Location IDs to filter location levels.
|
||||||
|
*/
|
||||||
|
@IsOptional()
|
||||||
|
@IsString({ each: true })
|
||||||
|
location_id?: string[]
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @schema AdminPostInventoryItemsItemLocationLevelsLevelReq
|
* @schema AdminPostInventoryItemsItemLocationLevelsLevelReq
|
||||||
* type: object
|
* type: object
|
||||||
|
|||||||
Reference in new issue
Block a user