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

@@ -2,7 +2,8 @@ import path from "path"
import { Product, ProductCategory } from "@medusajs/medusa"
import { In } from "typeorm"
import startServerWithEnvironment from "../../../helpers/start-server-with-environment"
import startServerWithEnvironment
from "../../../helpers/start-server-with-environment"
import { useApi } from "../../../helpers/use-api"
import { useDb } from "../../../helpers/use-db"
import adminSeeder from "../../helpers/admin-seeder"
@@ -22,11 +23,15 @@ const adminHeaders = {
describe("/admin/product-categories", () => {
let medusaProcess
let dbConnection
let productCategory = null
let productCategory2 = null
let productCategoryChild = null
let productCategoryParent = null
let productCategoryChild2 = null
let productCategory!: ProductCategory
let productCategory1!: ProductCategory
let productCategory2!: ProductCategory
let productCategoryChild!: ProductCategory
let productCategoryParent!: ProductCategory
let productCategoryChild0!: ProductCategory
let productCategoryChild1!: ProductCategory
let productCategoryChild2!: ProductCategory
let productCategoryChild3!: ProductCategory
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
@@ -124,22 +129,44 @@ describe("/admin/product-categories", () => {
productCategoryParent = await simpleProductCategoryFactory(dbConnection, {
name: "Mens",
rank: 0,
})
productCategory = await simpleProductCategoryFactory(dbConnection, {
name: "sweater",
parent_category: productCategoryParent,
is_internal: true,
rank: 0
})
productCategoryChild = await simpleProductCategoryFactory(dbConnection, {
name: "cashmere",
parent_category: productCategory,
rank: 0
})
productCategoryChild0 = await simpleProductCategoryFactory(dbConnection, {
name: "rank 2",
parent_category: productCategoryChild,
rank: 2
})
productCategoryChild1 = await simpleProductCategoryFactory(dbConnection, {
name: "rank 1",
parent_category: productCategoryChild,
rank: 1
})
productCategoryChild2 = await simpleProductCategoryFactory(dbConnection, {
name: "specific cashmere",
name: "rank 0",
parent_category: productCategoryChild,
rank: 0
})
productCategoryChild3 = await simpleProductCategoryFactory(dbConnection, {
name: "rank 3",
parent_category: productCategoryChild,
rank: 3
})
})
@@ -157,39 +184,50 @@ describe("/admin/product-categories", () => {
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(4)
expect(response.data.count).toEqual(7)
expect(response.data.offset).toEqual(0)
expect(response.data.limit).toEqual(100)
expect(response.data.product_categories).toEqual(
expect.arrayContaining([
[
expect.objectContaining({
id: productCategoryChild.id,
parent_category: expect.objectContaining({
id: productCategory.id,
handle: productCategory.handle,
rank: 0
}),
category_children: [
expect.objectContaining({
id: productCategoryChild2.id,
handle: productCategoryChild2.handle,
rank: 0
}),
expect.objectContaining({
id: productCategoryChild1.id,
handle: productCategoryChild1.handle,
rank: 1
}),
expect.objectContaining({
id: productCategoryChild0.id,
handle: productCategoryChild0.handle,
rank: 2
}),
expect.objectContaining({
id: productCategoryChild3.id,
handle: productCategoryChild3.handle,
rank: 3
}),
],
}),
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: [
expect.objectContaining({
id: productCategoryChild2.id,
handle: productCategory.handle,
rank: 0
})
],
}),
@@ -199,8 +237,58 @@ describe("/admin/product-categories", () => {
id: productCategoryChild.id,
}),
category_children: [],
rank: 0,
handle: productCategoryChild2.handle,
}),
])
expect.objectContaining({
id: productCategory.id,
parent_category: expect.objectContaining({
id: productCategoryParent.id,
rank: 0,
handle: productCategoryParent.handle,
}),
category_children: [
expect.objectContaining({
id: productCategoryChild.id,
handle: productCategoryChild.handle,
rank: 0,
})
],
}),
expect.objectContaining({
id: productCategoryChild1.id,
parent_category: expect.objectContaining({
id: productCategoryChild.id,
handle: productCategoryChild.handle,
rank: 0
}),
category_children: [],
handle: productCategoryChild1.handle,
rank: 1
}),
expect.objectContaining({
id: productCategoryChild0.id,
parent_category: expect.objectContaining({
id: productCategoryChild.id,
handle: productCategoryChild.handle,
rank: 0
}),
category_children: [],
handle: productCategoryChild0.handle,
rank: 2
}),
expect.objectContaining({
id: productCategoryChild3.id,
parent_category: expect.objectContaining({
id: productCategoryChild.id,
handle: productCategoryChild.handle,
rank: 0
}),
category_children: [],
handle: productCategoryChild3.handle,
rank: 3
}),
]
)
})
@@ -263,19 +351,43 @@ describe("/admin/product-categories", () => {
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.product_categories).toEqual(
expect.arrayContaining([
[
expect.objectContaining({
id: productCategoryParent.id,
rank: 0,
handle: productCategoryParent.handle,
category_children: [
expect.objectContaining({
id: productCategory.id,
rank: 0,
handle: productCategory.handle,
category_children: [
expect.objectContaining({
id: productCategoryChild.id,
category_children: [
expect.objectContaining({
id: productCategoryChild2.id,
category_children: []
category_children: [],
handle: productCategoryChild2.handle,
rank: 0
}),
expect.objectContaining({
id: productCategoryChild1.id,
category_children: [],
handle: productCategoryChild1.handle,
rank: 1
}),
expect.objectContaining({
id: productCategoryChild0.id,
category_children: [],
handle: productCategoryChild0.handle,
rank: 2
}),
expect.objectContaining({
id: productCategoryChild3.id,
category_children: [],
handle: productCategoryChild3.handle,
rank: 3
})
],
})
@@ -283,7 +395,7 @@ describe("/admin/product-categories", () => {
})
],
}),
])
]
)
})
})
@@ -327,15 +439,16 @@ describe("/admin/product-categories", () => {
it("successfully creates a product category", async () => {
const api = useApi()
const payload = {
name: "test",
handle: "test",
is_internal: true,
parent_category_id: productCategory.id,
}
const response = await api.post(
`/admin/product-categories`,
{
name: "test",
handle: "test",
is_internal: true,
parent_category_id: productCategory.id,
},
payload,
adminHeaders
)
@@ -343,16 +456,52 @@ describe("/admin/product-categories", () => {
expect(response.data).toEqual(
expect.objectContaining({
product_category: expect.objectContaining({
name: "test",
handle: "test",
is_internal: true,
name: payload.name,
handle: payload.handle,
is_internal: payload.is_internal,
is_active: false,
created_at: expect.any(String),
updated_at: expect.any(String),
parent_category: expect.objectContaining({
id: productCategory.id
id: payload.parent_category_id
}),
category_children: []
category_children: [],
rank: 0,
}),
})
)
})
it("successfully creates a product category with a rank", async () => {
const api = useApi()
const payload = {
name: "test",
handle: "test",
is_internal: true,
parent_category_id: productCategoryParent.id,
}
const response = await api.post(
`/admin/product-categories`,
payload,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data).toEqual(
expect.objectContaining({
product_category: expect.objectContaining({
name: payload.name,
handle: payload.handle,
is_internal: payload.is_internal,
is_active: false,
created_at: expect.any(String),
updated_at: expect.any(String),
parent_category: expect.objectContaining({
id: productCategoryParent.id
}),
category_children: [],
rank: 1,
}),
})
)
@@ -409,6 +558,18 @@ describe("/admin/product-categories", () => {
handle: "category",
parent_category: productCategoryParent,
})
productCategory1 = await simpleProductCategoryFactory(dbConnection, {
name: "category-1",
parent_category: productCategoryParent,
rank: 1
})
productCategory2 = await simpleProductCategoryFactory(dbConnection, {
name: "category-2",
parent_category: productCategoryParent,
rank: 2
})
})
afterEach(async () => {
@@ -473,25 +634,54 @@ describe("/admin/product-categories", () => {
productCategoryParent = await simpleProductCategoryFactory(dbConnection, {
name: "category parent",
handle: "category-parent",
})
productCategory = await simpleProductCategoryFactory(dbConnection, {
name: "category",
handle: "category",
name: "category-0",
parent_category: productCategoryParent,
rank: 0
})
productCategory1 = await simpleProductCategoryFactory(dbConnection, {
name: "category-1",
parent_category: productCategoryParent,
rank: 1
})
productCategory2 = await simpleProductCategoryFactory(dbConnection, {
name: "category-2",
parent_category: productCategoryParent,
rank: 2
})
productCategoryChild = await simpleProductCategoryFactory(dbConnection, {
name: "category child",
handle: "category-child",
parent_category: productCategory,
rank: 0,
})
productCategoryChild0 = await simpleProductCategoryFactory(dbConnection, {
name: "category child 0",
parent_category: productCategoryChild,
rank: 0,
})
productCategoryChild1 = await simpleProductCategoryFactory(dbConnection, {
name: "category child 1",
parent_category: productCategoryChild,
rank: 1
})
productCategoryChild2 = await simpleProductCategoryFactory(dbConnection, {
name: "category child 2",
handle: "category-child-2",
parent_category: productCategoryChild,
rank: 2,
})
productCategoryChild3 = await simpleProductCategoryFactory(dbConnection, {
name: "category child 3",
parent_category: productCategoryChild,
rank: 3,
})
})
@@ -518,6 +708,24 @@ describe("/admin/product-categories", () => {
)
})
it("throws an error if rank is negative", async () => {
const api = useApi()
const error = await api.post(
`/admin/product-categories/not-found-id`,
{
rank: -1
},
adminHeaders
).catch(e => e)
expect(error.response.status).toEqual(400)
expect(error.response.data.type).toEqual("invalid_data")
expect(error.response.data.message).toEqual(
"rank must not be less than 0"
)
})
it("throws an error if invalid attribute is sent", async () => {
const api = useApi()
@@ -564,7 +772,8 @@ describe("/admin/product-categories", () => {
parent_category: expect.objectContaining({
id: productCategory.id,
}),
category_children: []
category_children: [],
rank: 1,
}),
})
)
@@ -574,7 +783,7 @@ describe("/admin/product-categories", () => {
const api = useApi()
const response = await api.post(
`/admin/product-categories/${productCategoryChild2.id}`,
`/admin/product-categories/${productCategoryChild.id}`,
{
parent_category_id: productCategory.id,
},
@@ -593,17 +802,155 @@ describe("/admin/product-categories", () => {
category_children: [
expect.objectContaining({
id: productCategory.id,
rank: 0,
category_children: [
expect.objectContaining({
id: productCategoryChild.id,
category_children: []
}),
expect.objectContaining({
id: productCategoryChild2.id,
category_children: []
rank: 0,
})
]
})
],
}),
expect.objectContaining({
id: productCategory1.id,
category_children: [],
rank: 1
}),
expect.objectContaining({
id: productCategory2.id,
category_children: [],
rank: 2
}),
]
})
)
})
it("when parent is updated, rank is updated to elements count + 1", async () => {
const api = useApi()
const response = await api.post(
`/admin/product-categories/${productCategoryChild1.id}`,
{
parent_category_id: productCategoryParent.id,
},
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data).toEqual(
expect.objectContaining({
product_category: expect.objectContaining({
parent_category: expect.objectContaining({
id: productCategoryParent.id,
}),
rank: 3,
}),
})
)
})
it("when parent is updated with rank, rank accurately updated", async () => {
const api = useApi()
const response = await api.post(
`/admin/product-categories/${productCategoryChild1.id}`,
{
parent_category_id: productCategoryParent.id,
rank: 0,
},
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data).toEqual(
expect.objectContaining({
product_category: expect.objectContaining({
parent_category: expect.objectContaining({
id: productCategoryParent.id,
}),
rank: 0,
}),
})
)
})
it("when only rank is updated, rank should be updated", async () => {
const api = useApi()
const response = await api.post(
`/admin/product-categories/${productCategoryChild1.id}`,
{
rank: 0
},
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data).toEqual(
expect.objectContaining({
product_category: expect.objectContaining({
rank: 0,
}),
})
)
})
it("when rank is greater than list count, rank is updated to updated to elements count + 1", async () => {
const api = useApi()
const response = await api.post(
`/admin/product-categories/${productCategoryChild1.id}`,
{
rank: 99
},
adminHeaders
)
expect(response.data).toEqual(
expect.objectContaining({
product_category: expect.objectContaining({
rank: 3,
}),
})
)
})
it("when rank is updated, it accurately updates sibling ranks", async () => {
const api = useApi()
const response = await api.post(
`/admin/product-categories/${productCategoryChild2.id}`,
{
rank: 0
},
adminHeaders
)
const parentResponse = await api.get(
`/admin/product-categories/${productCategoryChild2.parent_category_id}`,
adminHeaders
)
expect(parentResponse.data.product_category).toEqual(
expect.objectContaining({
id: productCategoryChild2.parent_category_id,
category_children: [
expect.objectContaining({
id: productCategoryChild2.id,
rank: 0
}),
expect.objectContaining({
id: productCategoryChild0.id,
rank: 1,
}),
expect.objectContaining({
id: productCategoryChild1.id,
rank: 2
}),
expect.objectContaining({
id: productCategoryChild3.id,
rank: 3
}),
]
})
)

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(