diff --git a/integration-tests/api/__tests__/admin/product-category.ts b/integration-tests/api/__tests__/admin/product-category.ts index 253676b4d631b..24aa82973d9f0 100644 --- a/integration-tests/api/__tests__/admin/product-category.ts +++ b/integration-tests/api/__tests__/admin/product-category.ts @@ -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" @@ -11,7 +12,7 @@ import { simpleProductFactory, } from "../../factories" -jest.setTimeout(30000) +jest.setTimeout(3000000) const adminHeaders = { headers: { @@ -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 }) @@ -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}`, @@ -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`, diff --git a/integration-tests/api/package.json b/integration-tests/api/package.json index f2f39f27f467a..be7fc09cb8da0 100644 --- a/integration-tests/api/package.json +++ b/integration-tests/api/package.json @@ -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": { diff --git a/packages/medusa/src/repositories/product-category.ts b/packages/medusa/src/repositories/product-category.ts index f044ea23e0425..3e2ba8b65d5b7 100644 --- a/packages/medusa/src/repositories/product-category.ts +++ b/packages/medusa/src/repositories/product-category.ts @@ -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 } @@ -30,7 +20,7 @@ export const ProductCategoryRepository = dataSource options: ExtendedFindConfig = { where: {}, }, - q: string | undefined, + q?: string, treeScope: QuerySelector = {}, includeTree = false ): Promise<[ProductCategory[], number]> { @@ -38,73 +28,56 @@ export const ProductCategoryRepository = dataSource const options_ = { ...options } options_.where = options_.where as FindOptionsWhere - 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) {