From 211720f24cbcb1f01c36aa35660e1ff0c4518ebd Mon Sep 17 00:00:00 2001 From: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com> Date: Wed, 12 Oct 2022 20:56:56 +0200 Subject: [PATCH] fix(medusa): List products by `type_id` (#2427) **What** - Fixes `GET /products` in both admin and store API so they no longer accept the param `type?: string`, but instead accept `type_id?: string[]` **Why** - Filtering by type would never return any products as `ptyp_:id` !== `ProductType`. **Testing** - Added an integration test for each endpoint. Closes CORE-695 --- .changeset/afraid-cups-turn.md | 5 ++++ .../api/__tests__/admin/product.js | 24 +++++++++++++++++++ .../api/__tests__/store/products.js | 18 ++++++++++++++ .../routes/admin/products/list-products.ts | 10 +++++++- .../routes/store/products/list-products.ts | 23 +++++++++++------- packages/medusa/src/types/product.ts | 4 ++-- 6 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 .changeset/afraid-cups-turn.md diff --git a/.changeset/afraid-cups-turn.md b/.changeset/afraid-cups-turn.md new file mode 100644 index 0000000000..ba2cc0b4d7 --- /dev/null +++ b/.changeset/afraid-cups-turn.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +Changes type to type_id for the list products endpoints in both the Store and Admin API. diff --git a/integration-tests/api/__tests__/admin/product.js b/integration-tests/api/__tests__/admin/product.js index 99091a6685..eee503ae84 100644 --- a/integration-tests/api/__tests__/admin/product.js +++ b/integration-tests/api/__tests__/admin/product.js @@ -168,6 +168,30 @@ describe("/admin/products", () => { } }) + it("returns a list of products where type_id is test-type", async () => { + const api = useApi() + + const response = await api + .get("/admin/products?type_id[]=test-type", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err) + }) + + expect(response.status).toEqual(200) + expect(response.data.products).toHaveLength(5) + expect(response.data.products).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + type_id: "test-type", + }), + ]) + ) + }) + it("doesn't expand collection and types", async () => { const api = useApi() diff --git a/integration-tests/api/__tests__/store/products.js b/integration-tests/api/__tests__/store/products.js index ef053b75c2..f7b60a4eb8 100644 --- a/integration-tests/api/__tests__/store/products.js +++ b/integration-tests/api/__tests__/store/products.js @@ -195,6 +195,24 @@ describe("/store/products", () => { } }) + it("works when filtering by type_id", async () => { + const api = useApi() + + const response = await api.get( + `/store/products?type_id[]=test-type&fields=id,title,type_id` + ) + + expect(response.status).toEqual(200) + expect(response.data.products).toHaveLength(5) + expect(response.data.products).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + type_id: "test-type", + }), + ]) + ) + }) + it("returns only published products", async () => { const api = useApi() diff --git a/packages/medusa/src/api/routes/admin/products/list-products.ts b/packages/medusa/src/api/routes/admin/products/list-products.ts index 5e0fa2fab3..002be0b63e 100644 --- a/packages/medusa/src/api/routes/admin/products/list-products.ts +++ b/packages/medusa/src/api/routes/admin/products/list-products.ts @@ -73,11 +73,19 @@ import { FilterableProductProps } from "../../../../types/product" * type: array * items: * type: string + * - in: query + * name: type_id + * style: form + * explode: false + * description: Type IDs to filter products by + * schema: + * type: array + * items: + * type: string * - (query) title {string} title to search for. * - (query) description {string} description to search for. * - (query) handle {string} handle to search for. * - (query) is_giftcard {boolean} Search for giftcards using is_giftcard=true. - * - (query) type {string} type ID to search for. * - in: query * name: created_at * description: Date comparison for when resulting products were created. diff --git a/packages/medusa/src/api/routes/store/products/list-products.ts b/packages/medusa/src/api/routes/store/products/list-products.ts index 20d22253f4..f51eacc632 100644 --- a/packages/medusa/src/api/routes/store/products/list-products.ts +++ b/packages/medusa/src/api/routes/store/products/list-products.ts @@ -5,13 +5,13 @@ import { IsNumber, IsOptional, IsString, - ValidateNested + ValidateNested, } from "class-validator" import { omit, pickBy } from "lodash" import { CartService, ProductService, - RegionService + RegionService, } from "../../../../services" import { defaultStoreProductsRelations } from "." @@ -54,6 +54,15 @@ import { IsType } from "../../../../utils/validators/is-type" * items: * type: string * - in: query + * name: type_id + * style: form + * explode: false + * description: Type IDs to search for + * schema: + * type: array + * items: + * type: string + * - in: query * name: tags * style: form * explode: false @@ -66,7 +75,6 @@ import { IsType } from "../../../../utils/validators/is-type" * - (query) description {string} description to search for. * - (query) handle {string} handle to search for. * - (query) is_giftcard {boolean} Search for giftcards using is_giftcard=true. - * - (query) type {string} type to search for. * - in: query * name: created_at * description: Date comparison for when resulting products were created. @@ -301,14 +309,11 @@ export class StoreGetProductsParams extends StoreGetProductsPaginationParams { @Transform(({ value }) => optionalBooleanMapper.get(value.toLowerCase())) is_giftcard?: boolean - @IsString() + @IsArray() @IsOptional() - type?: string + type_id?: string[] - @FeatureFlagDecorators(SalesChannelFeatureFlag.key, [ - IsOptional(), - IsArray(), - ]) + @FeatureFlagDecorators(SalesChannelFeatureFlag.key, [IsOptional(), IsArray()]) sales_channel_id?: string[] @IsOptional() diff --git a/packages/medusa/src/types/product.ts b/packages/medusa/src/types/product.ts index ebc184bf92..2c19255e60 100644 --- a/packages/medusa/src/types/product.ts +++ b/packages/medusa/src/types/product.ts @@ -60,9 +60,9 @@ export class FilterableProductProps { @Transform(({ value }) => optionalBooleanMapper.get(value.toLowerCase())) is_giftcard?: boolean - @IsString() + @IsArray() @IsOptional() - type?: string + type_id?: string[] @FeatureFlagDecorators(SalesChannelFeatureFlag.key, [IsOptional(), IsArray()]) sales_channel_id?: string[]