From 95d338262b63e3daa6697bb23806980a8b5e5cdc Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 13 Apr 2023 19:57:30 +0200 Subject: [PATCH] feat(medusa): allow category list api to be filtered by handle (#3825) what: - Introduce a filter to the list endpoint to query by handle why: - Storefronts mostly interact through handles and not IDs for readability and seo purposes - Take an example of a url `site.com/category/mens-wear` RESOLVES CORE-1325 --- .changeset/little-panthers-tell.md | 5 +++++ .../api/__tests__/admin/product-category.ts | 13 +++++++++++++ .../api/__tests__/store/product-category.ts | 12 ++++++++++++ .../lib/models/AdminGetProductCategoriesParams.ts | 6 +++++- .../lib/models/StoreGetProductCategoriesParams.ts | 4 ++++ .../product-categories/list-product-categories.ts | 7 ++++++- .../product-categories/list-product-categories.ts | 5 +++++ 7 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 .changeset/little-panthers-tell.md diff --git a/.changeset/little-panthers-tell.md b/.changeset/little-panthers-tell.md new file mode 100644 index 0000000000..27154b5df5 --- /dev/null +++ b/.changeset/little-panthers-tell.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +feat(medusa): allow category api to be filtered by handle diff --git a/integration-tests/api/__tests__/admin/product-category.ts b/integration-tests/api/__tests__/admin/product-category.ts index 19cd58a77c..95c73a2784 100644 --- a/integration-tests/api/__tests__/admin/product-category.ts +++ b/integration-tests/api/__tests__/admin/product-category.ts @@ -305,6 +305,19 @@ describe("/admin/product-categories", () => { expect(response.data.product_categories[0].id).toEqual(productCategory.id) }) + it("filters based on handle attribute of the data model", async () => { + const api = useApi() + + const response = await api.get( + `/admin/product-categories?handle=${productCategory.handle}`, + adminHeaders, + ) + + expect(response.status).toEqual(200) + expect(response.data.count).toEqual(1) + expect(response.data.product_categories[0].id).toEqual(productCategory.id) + }) + it("filters based on free text on name and handle columns", async () => { const api = useApi() diff --git a/integration-tests/api/__tests__/store/product-category.ts b/integration-tests/api/__tests__/store/product-category.ts index 73ab07f518..019c85d48b 100644 --- a/integration-tests/api/__tests__/store/product-category.ts +++ b/integration-tests/api/__tests__/store/product-category.ts @@ -300,6 +300,18 @@ describe("/store/product-categories", () => { expect(response.data.product_categories[0].id).toEqual(productCategoryParent.id) }) + it("filters based on handle attribute of the data model", async () => { + const api = useApi() + + const response = await api.get( + `/store/product-categories?handle=${productCategory.handle}`, + ) + + expect(response.status).toEqual(200) + expect(response.data.count).toEqual(1) + expect(response.data.product_categories[0].id).toEqual(productCategory.id) + }) + it("filters based on parent category", async () => { const api = useApi() diff --git a/packages/generated/client-types/src/lib/models/AdminGetProductCategoriesParams.ts b/packages/generated/client-types/src/lib/models/AdminGetProductCategoriesParams.ts index dbea731398..059d8b1ca2 100644 --- a/packages/generated/client-types/src/lib/models/AdminGetProductCategoriesParams.ts +++ b/packages/generated/client-types/src/lib/models/AdminGetProductCategoriesParams.ts @@ -5,9 +5,13 @@ import { SetRelation, Merge } from "../core/ModelUtils" export interface AdminGetProductCategoriesParams { /** - * Query used for searching product category names orhandles. + * Query used for searching product category names or handles. */ q?: string + /** + * Query used for searching product category by handle. + */ + handle?: string /** * Search for only internal categories. */ diff --git a/packages/generated/client-types/src/lib/models/StoreGetProductCategoriesParams.ts b/packages/generated/client-types/src/lib/models/StoreGetProductCategoriesParams.ts index f07495a5fc..ebce16ed83 100644 --- a/packages/generated/client-types/src/lib/models/StoreGetProductCategoriesParams.ts +++ b/packages/generated/client-types/src/lib/models/StoreGetProductCategoriesParams.ts @@ -8,6 +8,10 @@ export interface StoreGetProductCategoriesParams { * Query used for searching product category names or handles. */ q?: string + /** + * Query used for searching product category by handle. + */ + handle?: string /** * Returns categories scoped by parent */ diff --git a/packages/medusa/src/api/routes/admin/product-categories/list-product-categories.ts b/packages/medusa/src/api/routes/admin/product-categories/list-product-categories.ts index 5e4a05e3a1..7bfbcf5356 100644 --- a/packages/medusa/src/api/routes/admin/product-categories/list-product-categories.ts +++ b/packages/medusa/src/api/routes/admin/product-categories/list-product-categories.ts @@ -13,7 +13,8 @@ import { optionalBooleanMapper } from "../../../../utils/validators/is-boolean" * description: "Retrieve a list of product categories." * x-authenticated: true * parameters: - * - (query) q {string} Query used for searching product category names orhandles. + * - (query) q {string} Query used for searching product category names or handles. + * - (query) handle {string} Query used for searching product category by handle. * - (query) is_internal {boolean} Search for only internal categories. * - (query) is_active {boolean} Search for only active categories * - (query) include_descendants_tree {boolean} Include all nested descendants of category @@ -94,6 +95,10 @@ export class AdminGetProductCategoriesParams extends extendedFindParamsMixin({ @IsOptional() q?: string + @IsString() + @IsOptional() + handle?: string + @IsBoolean() @IsOptional() @Transform(({ value }) => optionalBooleanMapper.get(value)) diff --git a/packages/medusa/src/api/routes/store/product-categories/list-product-categories.ts b/packages/medusa/src/api/routes/store/product-categories/list-product-categories.ts index 7459647d2e..3c0dcabc81 100644 --- a/packages/medusa/src/api/routes/store/product-categories/list-product-categories.ts +++ b/packages/medusa/src/api/routes/store/product-categories/list-product-categories.ts @@ -15,6 +15,7 @@ import { defaultStoreCategoryScope } from "." * x-authenticated: false * parameters: * - (query) q {string} Query used for searching product category names or handles. + * - (query) handle {string} Query used for searching product category by handle. * - (query) parent_category_id {string} Returns categories scoped by parent * - (query) include_descendants_tree {boolean} Include all nested descendants of category * - (query) offset=0 {integer} How many product categories to skip in the result. @@ -96,6 +97,10 @@ export class StoreGetProductCategoriesParams extends extendedFindParamsMixin({ @IsOptional() q?: string + @IsString() + @IsOptional() + handle?: string + @IsString() @IsOptional() @Transform(({ value }) => {