From 4fb443c0ea38bde3148bce059c0ee3b91dfff3d4 Mon Sep 17 00:00:00 2001 From: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com> Date: Tue, 16 May 2023 14:21:53 +0200 Subject: [PATCH] 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 --- .changeset/chilled-dolphins-prove.md | 6 ++ .../inventory/inventory-items/index.js | 35 +++++++++++ ...tInventoryItemsItemLocationLevelsParams.ts | 4 ++ .../api/routes/admin/inventory-items/index.ts | 58 ++++++++++++------- .../inventory-items/list-location-levels.ts | 21 ++++++- 5 files changed, 100 insertions(+), 24 deletions(-) create mode 100644 .changeset/chilled-dolphins-prove.md diff --git a/.changeset/chilled-dolphins-prove.md b/.changeset/chilled-dolphins-prove.md new file mode 100644 index 0000000000..2b9098c8fa --- /dev/null +++ b/.changeset/chilled-dolphins-prove.md @@ -0,0 +1,6 @@ +--- +"@medusajs/client-types": patch +"@medusajs/medusa": patch +--- + +feat(medusa,client-types): add location_id filtering to list-location levels diff --git a/integration-tests/plugins/__tests__/inventory/inventory-items/index.js b/integration-tests/plugins/__tests__/inventory/inventory-items/index.js index b9ace5f7b2..2fd790bbee 100644 --- a/integration-tests/plugins/__tests__/inventory/inventory-items/index.js +++ b/integration-tests/plugins/__tests__/inventory/inventory-items/index.js @@ -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() diff --git a/packages/generated/client-types/src/lib/models/AdminGetInventoryItemsItemLocationLevelsParams.ts b/packages/generated/client-types/src/lib/models/AdminGetInventoryItemsItemLocationLevelsParams.ts index 72078082ec..012ec18862 100644 --- a/packages/generated/client-types/src/lib/models/AdminGetInventoryItemsItemLocationLevelsParams.ts +++ b/packages/generated/client-types/src/lib/models/AdminGetInventoryItemsItemLocationLevelsParams.ts @@ -4,6 +4,10 @@ import { SetRelation, Merge } from "../core/ModelUtils" export interface AdminGetInventoryItemsItemLocationLevelsParams { + /** + * Locations ids to search for. + */ + location_id?: Array /** * Comma separated list of relations to include in the results. */ diff --git a/packages/medusa/src/api/routes/admin/inventory-items/index.ts b/packages/medusa/src/api/routes/admin/inventory-items/index.ts index 4f0007d863..dd008c868d 100644 --- a/packages/medusa/src/api/routes/admin/inventory-items/index.ts +++ b/packages/medusa/src/api/routes/admin/inventory-items/index.ts @@ -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 = [] /** diff --git a/packages/medusa/src/api/routes/admin/inventory-items/list-location-levels.ts b/packages/medusa/src/api/routes/admin/inventory-items/list-location-levels.ts index 2037eb3ad7..2478b7d524 100644 --- a/packages/medusa/src/api/routes/admin/inventory-items/list-location-levels.ts +++ b/packages/medusa/src/api/routes/admin/inventory-items/list-location-levels.ts @@ -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[] +}