feat(medusa): Add location id filtering to list location levels (#4066)

**What**
- add capabilities for filtering locations by id when listing locations for an inventory item
This commit is contained in:
Philip Korsholm
2023-05-16 14:21:53 +02:00
committed by GitHub
parent 2d9cb9b17f
commit 4fb443c0ea
5 changed files with 100 additions and 24 deletions

View File

@@ -0,0 +1,6 @@
---
"@medusajs/client-types": patch
"@medusajs/medusa": patch
---
feat(medusa,client-types): add location_id filtering to list-location levels

View File

@@ -362,6 +362,41 @@ describe("Inventory Items endpoints", () => {
expect(variantInventoryRes.status).toEqual(200)
})
it("lists location levels based on id param constraint", async () => {
const api = useApi()
const inventoryItemId = inventoryItems[0].id
await api.post(
`/admin/inventory-items/${inventoryItemId}/location-levels`,
{
location_id: location2Id,
stocked_quantity: 10,
},
adminHeaders
)
await api.post(
`/admin/inventory-items/${inventoryItemId}/location-levels`,
{
location_id: location3Id,
stocked_quantity: 5,
},
adminHeaders
)
const result = await api.get(
`/admin/inventory-items/${inventoryItemId}/location-levels?location_id[]=${location2Id}`,
adminHeaders
)
expect(result.status).toEqual(200)
expect(result.data.inventory_item.location_levels).toHaveLength(1)
expect(result.data.inventory_item.location_levels[0]).toEqual(
expect.objectContaining({
stocked_quantity: 10,
})
)
})
describe("List inventory items", () => {
it("Lists inventory items with location", async () => {
const api = useApi()

View File

@@ -4,6 +4,10 @@
import { SetRelation, Merge } from "../core/ModelUtils"
export interface AdminGetInventoryItemsItemLocationLevelsParams {
/**
* Locations ids to search for.
*/
location_id?: Array<string>
/**
* Comma separated list of relations to include in the results.
*/

View File

@@ -1,24 +1,5 @@
import { InventoryItemDTO, InventoryLevelDTO } from "@medusajs/types"
import { Router } from "express"
import "reflect-metadata"
import { ProductVariant } from "../../../../models"
import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
import middlewares, {
transformBody,
transformQuery,
} from "../../../middlewares"
import { checkRegisteredModules } from "../../../middlewares/check-registered-modules"
import {
AdminPostInventoryItemsParams,
AdminPostInventoryItemsReq,
} from "./create-inventory-item"
import {
AdminPostInventoryItemsItemLocationLevelsParams,
AdminPostInventoryItemsItemLocationLevelsReq,
} from "./create-location-level"
import { AdminGetInventoryItemsItemParams } from "./get-inventory-item"
import { AdminGetInventoryItemsParams } from "./list-inventory-items"
import { AdminGetInventoryItemsItemLocationLevelsParams } from "./list-location-levels"
import {
AdminPostInventoryItemsInventoryItemParams,
AdminPostInventoryItemsInventoryItemReq,
@@ -27,6 +8,27 @@ import {
AdminPostInventoryItemsItemLocationLevelsLevelParams,
AdminPostInventoryItemsItemLocationLevelsLevelReq,
} from "./update-location-level"
import {
AdminPostInventoryItemsItemLocationLevelsParams,
AdminPostInventoryItemsItemLocationLevelsReq,
} from "./create-location-level"
import {
AdminPostInventoryItemsParams,
AdminPostInventoryItemsReq,
} from "./create-inventory-item"
import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
import { InventoryItemDTO, InventoryLevelDTO } from "@medusajs/types"
import middlewares, {
transformBody,
transformQuery,
} from "../../../middlewares"
import { AdminGetInventoryItemsItemLocationLevelsParams } from "./list-location-levels"
import { AdminGetInventoryItemsItemParams } from "./get-inventory-item"
import { AdminGetInventoryItemsParams } from "./list-inventory-items"
import { ProductVariant } from "../../../../models"
import { Router } from "express"
import { checkRegisteredModules } from "../../../middlewares/check-registered-modules"
const route = Router()
@@ -91,8 +93,8 @@ export default (app) => {
route.get(
"/:id/location-levels",
transformQuery(AdminGetInventoryItemsItemLocationLevelsParams, {
defaultFields: defaultAdminInventoryItemFields,
defaultRelations: defaultAdminInventoryItemRelations,
defaultFields: defaultAdminLocationLevelFields,
defaultRelations: [],
isList: false,
}),
middlewares.wrap(require("./list-location-levels").default)
@@ -144,6 +146,18 @@ export const defaultAdminInventoryItemFields: (keyof InventoryItemDTO)[] = [
"updated_at",
]
export const defaultAdminLocationLevelFields: (keyof InventoryLevelDTO)[] = [
"id",
"inventory_item_id",
"location_id",
"stocked_quantity",
"reserved_quantity",
"incoming_quantity",
"metadata",
"created_at",
"updated_at",
]
export const defaultAdminInventoryItemRelations = []
/**

View File

@@ -1,6 +1,9 @@
import { IInventoryService } from "@medusajs/types"
import { IsOptional, IsString } from "class-validator"
import { Request, Response } from "express"
import { FindParams } from "../../../../types/common"
import { IInventoryService } from "@medusajs/types"
import { IsType } from "../../../../utils/validators/is-type"
/**
* @oas [get] /admin/inventory-items/{id}/location-levels
@@ -10,6 +13,15 @@ import { FindParams } from "../../../../types/common"
* x-authenticated: true
* parameters:
* - (path) id=* {string} The ID of the Inventory Item.
* - in: query
* name: location_id
* style: form
* explode: false
* description: Locations ids to search for.
* schema:
* type: array
* items:
* type: string
* - (query) expand {string} Comma separated list of relations to include in the results.
* - (query) fields {string} Comma separated list of fields to include in the results.
* x-codegen:
@@ -65,6 +77,7 @@ export default async (req: Request, res: Response) => {
const [levels] = await inventoryService.listInventoryLevels(
{
...req.filterableFields,
inventory_item_id: id,
},
req.retrieveConfig
@@ -79,4 +92,8 @@ export default async (req: Request, res: Response) => {
}
// eslint-disable-next-line max-len
export class AdminGetInventoryItemsItemLocationLevelsParams extends FindParams {}
export class AdminGetInventoryItemsItemLocationLevelsParams extends FindParams {
@IsOptional()
@IsString({ each: true })
location_id?: string[]
}