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 12:21:53 +00:00
committed by GitHub
parent 2d9cb9b17f
commit 4fb443c0ea
5 changed files with 100 additions and 24 deletions
@@ -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 = []
/**
@@ -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[]
}