feat(medusa): List (service + controller) product categories #3004 (#3023)

**What:**

Introduces a store endpoint to retrieve a list of product categories

**Why:**

This is part of a greater goal of allowing products to be added to multiple categories.

**How:**

- Creates an endpoint in store routes

RESOLVES CORE-968
This commit is contained in:
Riqwan Thamir
2023-01-16 20:20:42 +01:00
committed by GitHub
parent c49747b3ad
commit 7d4b8b9cc5
10 changed files with 358 additions and 57 deletions

View File

@@ -34,39 +34,42 @@ describe("/store/product-categories", () => {
medusaProcess.kill()
})
describe("GET /store/product-categories/:id", () => {
beforeEach(async () => {
productCategoryParent = await simpleProductCategoryFactory(dbConnection, {
name: "category parent",
is_active: true,
})
productCategory = await simpleProductCategoryFactory(dbConnection, {
name: "category",
parent_category: productCategoryParent,
is_active: true,
})
productCategoryChild = await simpleProductCategoryFactory(dbConnection, {
name: "category child",
parent_category: productCategory,
is_active: true,
})
productCategoryChild2 = await simpleProductCategoryFactory(dbConnection, {
name: "category child 2",
parent_category: productCategory,
is_internal: true,
is_active: true,
})
productCategoryChild3 = await simpleProductCategoryFactory(dbConnection, {
name: "category child 3",
parent_category: productCategory,
is_active: false,
})
beforeEach(async () => {
productCategoryParent = await simpleProductCategoryFactory(dbConnection, {
name: "category parent",
is_active: true,
is_internal: false,
})
productCategory = await simpleProductCategoryFactory(dbConnection, {
name: "category",
parent_category: productCategoryParent,
is_active: true,
})
productCategoryChild = await simpleProductCategoryFactory(dbConnection, {
name: "category child",
parent_category: productCategory,
is_active: true,
is_internal: false,
})
productCategoryChild2 = await simpleProductCategoryFactory(dbConnection, {
name: "category child 2",
parent_category: productCategory,
is_internal: true,
is_active: true,
})
productCategoryChild3 = await simpleProductCategoryFactory(dbConnection, {
name: "category child 3",
parent_category: productCategory,
is_active: false,
is_internal: false,
})
})
describe("GET /store/product-categories/:id", () => {
afterEach(async () => {
const db = useDb()
return await db.teardown()
@@ -142,4 +145,109 @@ describe("/store/product-categories", () => {
)
})
})
describe("GET /store/product-categories", () => {
afterEach(async () => {
const db = useDb()
return await db.teardown()
})
it("gets list of product category with immediate children and parents", async () => {
const api = useApi()
const response = await api.get(
`/store/product-categories`,
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(3)
expect(response.data.offset).toEqual(0)
expect(response.data.limit).toEqual(100)
expect(response.data.product_categories).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: productCategoryParent.id,
parent_category: null,
category_children: [
expect.objectContaining({
id: productCategory.id,
})
],
}),
expect.objectContaining({
id: productCategory.id,
parent_category: expect.objectContaining({
id: productCategoryParent.id,
}),
category_children: [
expect.objectContaining({
id: productCategoryChild.id,
}),
],
}),
expect.objectContaining({
id: productCategoryChild.id,
parent_category: expect.objectContaining({
id: productCategory.id,
}),
category_children: [],
}),
])
)
})
it("throws error when querying not allowed fields", async () => {
const api = useApi()
const error = await api.get(
`/store/product-categories?is_internal=true`,
).catch(e => e)
expect(error.response.status).toEqual(400)
expect(error.response.data.type).toEqual('invalid_data')
expect(error.response.data.message).toEqual('property is_internal should not exist')
})
it("filters based on free text on name and handle columns", async () => {
const api = useApi()
const response = await api.get(
`/store/product-categories?q=category-parent`,
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.product_categories[0].id).toEqual(productCategoryParent.id)
})
it("filters based on parent category", async () => {
const api = useApi()
const response = await api.get(
`/store/product-categories?parent_category_id=${productCategory.id}`,
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.product_categories).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: productCategoryChild.id,
category_children: [],
parent_category: expect.objectContaining({
id: productCategory.id,
}),
}),
])
)
const nullCategoryResponse = await api.get(
`/store/product-categories?parent_category_id=null`,
).catch(e => e)
expect(nullCategoryResponse.status).toEqual(200)
expect(nullCategoryResponse.data.count).toEqual(1)
expect(nullCategoryResponse.data.product_categories[0].id).toEqual(productCategoryParent.id)
})
})
})