feat(medusa): categories can be ranked based on position (#3341)

* chore: categories can be ranked based on position

* chore: fix tests

* chore: sort categories by order

* chore: fix bug where mpath relationship is messed up

* chore: enable linting - lint changes

* Update packages/medusa/src/repositories/product-category.ts

Co-authored-by: Frane Polić <16856471+fPolic@users.noreply.github.com>

* chore: fixed specs

* chore: cleanup repository to new typeorm interfaces + cleanup

* chore: revert repository changes due to incorrect sql

* chore: addressed pr reviews

---------

Co-authored-by: Frane Polić <16856471+fPolic@users.noreply.github.com>
Co-authored-by: adrien2p <adrien.deperetti@gmail.com>
This commit is contained in:
Riqwan Thamir
2023-03-06 15:49:16 +01:00
committed by GitHub
parent 67ba8be02b
commit 0a6aa0e624
16 changed files with 986 additions and 167 deletions

View File

@@ -10,12 +10,13 @@ jest.setTimeout(30000)
describe("/store/product-categories", () => {
let medusaProcess
let dbConnection
let productCategory = null
let productCategory2 = null
let productCategoryChild = null
let productCategoryParent = null
let productCategoryChild2 = null
let productCategoryChild3 = null
let productCategory!: ProductCategory
let productCategory2!: ProductCategory
let productCategoryChild!: ProductCategory
let productCategoryParent!: ProductCategory
let productCategoryChild2!: ProductCategory
let productCategoryChild3!: ProductCategory
let productCategoryChild4!: ProductCategory
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
@@ -39,12 +40,14 @@ describe("/store/product-categories", () => {
name: "category parent",
is_active: true,
is_internal: false,
rank: 0,
})
productCategory = await simpleProductCategoryFactory(dbConnection, {
name: "category",
parent_category: productCategoryParent,
is_active: true,
rank: 0,
})
productCategoryChild = await simpleProductCategoryFactory(dbConnection, {
@@ -52,6 +55,7 @@ describe("/store/product-categories", () => {
parent_category: productCategory,
is_active: true,
is_internal: false,
rank: 3
})
productCategoryChild2 = await simpleProductCategoryFactory(dbConnection, {
@@ -59,6 +63,7 @@ describe("/store/product-categories", () => {
parent_category: productCategory,
is_internal: true,
is_active: true,
rank: 0,
})
productCategoryChild3 = await simpleProductCategoryFactory(dbConnection, {
@@ -66,6 +71,15 @@ describe("/store/product-categories", () => {
parent_category: productCategory,
is_active: false,
is_internal: false,
rank: 1,
})
productCategoryChild4 = await simpleProductCategoryFactory(dbConnection, {
name: "category child 4",
parent_category: productCategory,
is_active: true,
is_internal: false,
rank: 2
})
})
@@ -93,6 +107,11 @@ describe("/store/product-categories", () => {
name: productCategoryParent.name,
}),
category_children: [
expect.objectContaining({
id: productCategoryChild4.id,
handle: productCategoryChild4.handle,
name: productCategoryChild4.name,
}),
expect.objectContaining({
id: productCategoryChild.id,
handle: productCategoryChild.handle,
@@ -160,14 +179,33 @@ describe("/store/product-categories", () => {
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(3)
expect(response.data.count).toEqual(4)
expect(response.data.offset).toEqual(0)
expect(response.data.limit).toEqual(100)
expect(response.data.product_categories).toEqual(
expect.arrayContaining([
[
expect.objectContaining({
id: productCategory.id,
rank: 0,
parent_category: expect.objectContaining({
id: productCategoryParent.id,
}),
category_children: [
expect.objectContaining({
id: productCategoryChild4.id,
rank: 2,
}),
expect.objectContaining({
id: productCategoryChild.id,
rank: 3,
}),
],
}),
expect.objectContaining({
id: productCategoryParent.id,
parent_category: null,
rank: 0,
category_children: [
expect.objectContaining({
id: productCategory.id,
@@ -175,24 +213,22 @@ describe("/store/product-categories", () => {
],
}),
expect.objectContaining({
id: productCategory.id,
parent_category: expect.objectContaining({
id: productCategoryParent.id,
}),
category_children: [
expect.objectContaining({
id: productCategoryChild.id,
}),
],
}),
expect.objectContaining({
id: productCategoryChild.id,
id: productCategoryChild4.id,
rank: 2,
parent_category: expect.objectContaining({
id: productCategory.id,
}),
category_children: [],
}),
])
expect.objectContaining({
id: productCategoryChild.id,
rank: 3,
parent_category: expect.objectContaining({
id: productCategory.id,
}),
category_children: [],
}),
]
)
})
@@ -228,17 +264,26 @@ describe("/store/product-categories", () => {
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.count).toEqual(2)
expect(response.data.product_categories).toEqual(
expect.arrayContaining([
[
expect.objectContaining({
id: productCategoryChild4.id,
category_children: [],
parent_category: expect.objectContaining({
id: productCategory.id,
}),
rank: 2
}),
expect.objectContaining({
id: productCategoryChild.id,
category_children: [],
parent_category: expect.objectContaining({
id: productCategory.id,
}),
rank: 3
}),
])
]
)
const nullCategoryResponse = await api.get(