fix(medusa): category_id and q params for list products endpoint ld work (#4889)

Looks like during an earlier refactor, some of the categories logic wasn't ported over to the handler that works with q params. 

what:

- adds a fix that allows queries to be made on category_id when q param is passed.

Fixes https://github.com/medusajs/medusa/issues/4885
This commit is contained in:
Riqwan Thamir
2023-08-28 11:46:55 +00:00
committed by GitHub
parent 962c907dfa
commit a1110b3438
4 changed files with 162 additions and 46 deletions
@@ -27,6 +27,8 @@ describe("/admin/products [MEDUSA_FF_PRODUCT_CATEGORIES=true]", () => {
let categoryWithoutProduct
let nestedCategoryWithProduct
let nested2CategoryWithProduct
let categoryWithMultipleProducts
const categoryWithMultipleProductsId = "category-with-multiple-products-id"
const nestedCategoryWithProductId = "nested-category-with-product-id"
const nested2CategoryWithProductId = "nested2-category-with-product-id"
const categoryWithProductId = "category-with-product-id"
@@ -68,6 +70,17 @@ describe("/admin/products [MEDUSA_FF_PRODUCT_CATEGORIES=true]", () => {
is_internal: false,
})
categoryWithMultipleProducts = await simpleProductCategoryFactory(
dbConnection,
{
id: categoryWithMultipleProductsId,
name: "category with multiple Products",
products: [{ id: testProductId }, { id: testProduct1Id }],
is_active: true,
is_internal: false,
}
)
nestedCategoryWithProduct = await simpleProductCategoryFactory(
dbConnection,
{
@@ -122,6 +135,33 @@ describe("/admin/products [MEDUSA_FF_PRODUCT_CATEGORIES=true]", () => {
])
})
it("should return a list of products queried by category_id and q", async () => {
const api = useApi()
const productName = "Test product1"
// The other product under this category is with the title "Test product"
// By querying for "Test product1", the "Test product" should not be shown
const params = `category_id[]=${categoryWithMultipleProductsId}&q=${productName}&expand=categories`
const response = await api.get(`/admin/products?${params}`, adminHeaders)
expect(response.status).toEqual(200)
expect(response.data.products).toHaveLength(1)
expect(response.data.products).toEqual([
expect.objectContaining({
id: testProduct1Id,
title: productName,
categories: [
expect.objectContaining({
id: categoryWithMultipleProductsId,
}),
expect.objectContaining({
id: nestedCategoryWithProductId,
}),
],
}),
])
})
it("returns a list of products in product category without category children explicitly set to false", async () => {
const api = useApi()
const params = `category_id[]=${categoryWithProductId}&include_category_children=false`