Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Mar 2, 2023
1 parent de5b5d9 commit 7d7b709
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 86 deletions.
31 changes: 16 additions & 15 deletions integration-tests/api/__tests__/admin/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -11,7 +12,7 @@ import {
simpleProductFactory,
} from "../../factories"

jest.setTimeout(30000)
jest.setTimeout(3000000)

const adminHeaders = {
headers: {
Expand All @@ -22,22 +23,22 @@ const adminHeaders = {
describe("/admin/product-categories", () => {
let medusaProcess
let dbConnection
let productCategory = null
let productCategory1 = null
let productCategory2 = null
let productCategoryChild = null
let productCategoryParent = null
let productCategoryChild0 = null
let productCategoryChild1 = null
let productCategoryChild2 = null
let productCategoryChild3 = 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, "..", ".."))
const [process, connection] = await startServerWithEnvironment({
cwd,
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true },
})
} as any)
dbConnection = connection
medusaProcess = process
})
Expand Down Expand Up @@ -83,7 +84,7 @@ describe("/admin/product-categories", () => {
})

it("gets product category with children tree and parent", async () => {
const api = useApi()
const api = useApi() as any

const response = await api.get(
`/admin/product-categories/${productCategory.id}`,
Expand Down Expand Up @@ -174,8 +175,8 @@ describe("/admin/product-categories", () => {
return await db.teardown()
})

it("gets list of product category with immediate children and parents", async () => {
const api = useApi()
it.only("gets list of product category with immediate children and parents", async () => {
const api = useApi() as any

const response = await api.get(
`/admin/product-categories`,
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"private": true,
"scripts": {
"test": "jest --silent=false --runInBand --bail --detectOpenHandles --forceExit",
"test": "jest --silent=false --runInBand --bail --detectOpenHandles --forceExit -- __tests__/admin/product-category.ts",
"build": "babel src -d dist --extensions \".ts,.js\""
},
"dependencies": {
Expand Down
113 changes: 43 additions & 70 deletions packages/medusa/src/repositories/product-category.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import { Brackets, FindOptionsWhere, ILike, DeleteResult, In } from "typeorm"
import { ProductCategory } from "../models/product-category"
import { DeleteResult, FindOptionsWhere, ILike, In } from "typeorm"
import { ProductCategory } from "../models"
import { ExtendedFindConfig, QuerySelector } from "../types/common"
import { dataSource } from "../loaders/database"
import { buildLegacyFieldsListFrom } from "../utils"

const sortChildren = (category: ProductCategory): ProductCategory => {
if (category.category_children) {
category.category_children =
category?.category_children.map((child) => sortChildren(child)) || []
category.category_children = category?.category_children
.map((child) => sortChildren(child))
.sort((a, b) => a.position - b.position)
}

category.category_children = category.category_children.sort((a, b) => {
if (a.position < b.position) {
return -1
}
if (a.position > b.position) {
return 1
}
return 0
})
}

return category
}

Expand All @@ -30,81 +20,64 @@ export const ProductCategoryRepository = dataSource
options: ExtendedFindConfig<ProductCategory> = {
where: {},
},
q: string | undefined,
q?: string,
treeScope: QuerySelector<ProductCategory> = {},
includeTree = false
): Promise<[ProductCategory[], number]> {
const entityName = "product_category"
const options_ = { ...options }
options_.where = options_.where as FindOptionsWhere<ProductCategory>

const legacySelect = buildLegacyFieldsListFrom(options_.select)
const legacyRelations = buildLegacyFieldsListFrom(options_.relations)

const selectStatements = (relationName: string): string[] => {
const modelColumns = this.metadata.ownColumns.map(
(column) => column.propertyName
)
const selectColumns = legacySelect.length ? legacySelect : modelColumns
const queryBuilder = this.createQueryBuilder(entityName)
/* queryBuilder.addOrderBy(`${entityName}.position`, "ASC")
queryBuilder.addOrderBy(`${entityName}.name`, "ASC")*/
options_.order = options_.order ?? {}
options_.order = {
...options_.order,
position: "ASC",
name: "ASC",
}

return selectColumns.map((column) => {
return `${relationName}.${column}`
})
if (options_.relations?.category_children) {
options_.order = options_.order ?? {}
options_.order.category_children = { position: "ASC", name: "ASC" }
options_.where.category_children = {
...treeScope,
...((options_.where.category_children as any) ?? {}),
}
}

const queryBuilder = this.createQueryBuilder(entityName)
.select(selectStatements(entityName))
.skip(options_.skip)
.take(options_.take)
.addOrderBy(`${entityName}.position`, "ASC")
if (options_.relations?.parent_category) {
options_.order = options_.order ?? {}
options_.order.parent_category = { position: "ASC", name: "ASC" }
options_.where.parent_category = {
...treeScope,
...((options_.where.parent_category as any) ?? {}),
} as any
}

if (q) {
delete options_.where?.name
delete options_.where?.handle

queryBuilder.where(
new Brackets((bracket) => {
bracket
.where({ name: ILike(`%${q}%`) })
.orWhere({ handle: ILike(`%${q}%`) })
})
)
options_.where = [
{
...options_.where,
name: ILike(`%${q}%`),
},
{
...options_.where,
handle: ILike(`%${q}%`),
},
]
}

queryBuilder.andWhere(options_.where)

const includedTreeRelations: string[] = legacyRelations.filter((rel) =>
ProductCategory.treeRelations.includes(rel)
)

includedTreeRelations.forEach((treeRelation) => {
const treeWhere = Object.entries(treeScope)
.map((entry) => `${treeRelation}.${entry[0]} = :${entry[0]}`)
.join(" AND ")

queryBuilder
.leftJoin(
`${entityName}.${treeRelation}`,
treeRelation,
treeWhere,
treeScope
)
.addSelect(selectStatements(treeRelation))
.addOrderBy(`${treeRelation}.position`, "ASC")
})

const nonTreeRelations: string[] = legacyRelations.filter(
(rel) => !ProductCategory.treeRelations.includes(rel)
)

nonTreeRelations.forEach((relation) => {
queryBuilder.leftJoinAndSelect(`${entityName}.${relation}`, relation)
})

if (options_.withDeleted) {
queryBuilder.withDeleted()
}

queryBuilder.setFindOptions(options_)

let [categories, count] = await queryBuilder.getManyAndCount()

if (includeTree) {
Expand Down

0 comments on commit 7d7b709

Please sign in to comment.