chore:(product): renaming vars (#6972)

* feat(product): return parents tree

* query mpath with IN

* more tests

* remove where filters

* revert where

* naming

---------

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
This commit is contained in:
Carlos R. L. Rodrigues
2024-04-05 19:22:55 +02:00
committed by GitHub
parent 09a2220569
commit 01487970b2
3 changed files with 14 additions and 14 deletions

View File

@@ -37,13 +37,13 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
const manager = super.getActiveManager<SqlEntityManager>(context)
const findOptions_ = { ...findOptions }
const { includeDescendantsTree, includeParentsTree } = transformOptions
const { includeDescendantsTree, includeAncestorsTree } = transformOptions
findOptions_.options ??= {}
const fields = (findOptions_.options.fields ??= [])
// Ref: Building descendants
// mpath and parent_category_id needs to be added to the query for the tree building to be done accurately
if (includeDescendantsTree || includeParentsTree) {
if (includeDescendantsTree || includeAncestorsTree) {
fields.indexOf("mpath") === -1 && fields.push("mpath")
fields.indexOf("parent_category_id") === -1 &&
fields.push("parent_category_id")
@@ -59,14 +59,14 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
findOptions_.options as MikroOptions<ProductCategory>
)
if (!includeDescendantsTree && !includeParentsTree) {
if (!includeDescendantsTree && !includeAncestorsTree) {
return productCategories
}
return this.buildProductCategoriesWithTree(
{
descendants: includeDescendantsTree,
parents: includeParentsTree,
ancestors: includeAncestorsTree,
},
productCategories,
findOptions_
@@ -76,7 +76,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
async buildProductCategoriesWithTree(
include: {
descendants?: boolean
parents?: boolean
ancestors?: boolean
},
productCategories: ProductCategory[],
findOptions: DAL.FindOptions<ProductCategory> = { where: {} },
@@ -88,7 +88,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
findOptions.options?.populate ?? ([] as any)
).find((pop) => pop.field === "parent_category")
include.parents = include.parents || hasPopulateParentCategory
include.ancestors = include.ancestors || hasPopulateParentCategory
const mpaths: any[] = []
const parentMpaths = new Set()
@@ -97,7 +97,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
mpaths.push({ mpath: { $like: `${cat.mpath}%` } })
}
if (include.parents) {
if (include.ancestors) {
let parent = ""
cat.mpath?.split(".").forEach((mpath) => {
if (mpath === "") {
@@ -155,7 +155,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
return category
}
if (include.parents) {
if (include.ancestors) {
delete category.category_children
}
if (include.descendants) {
@@ -181,7 +181,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
const manager = super.getActiveManager<SqlEntityManager>(context)
const findOptions_ = { ...findOptions }
const { includeDescendantsTree, includeParentsTree } = transformOptions
const { includeDescendantsTree, includeAncestorsTree } = transformOptions
findOptions_.options ??= {}
const fields = (findOptions_.options.fields ??= [])
@@ -206,7 +206,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
return [productCategories, count]
}
if (!includeDescendantsTree && !includeParentsTree) {
if (!includeDescendantsTree && !includeAncestorsTree) {
return [productCategories, count]
}
@@ -214,7 +214,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito
await this.buildProductCategoriesWithTree(
{
descendants: includeDescendantsTree,
parents: includeParentsTree,
ancestors: includeAncestorsTree,
},
productCategories,
findOptions_

View File

@@ -71,7 +71,7 @@ export default class ProductCategoryService<
): Promise<TEntity[]> {
const transformOptions = {
includeDescendantsTree: filters?.include_descendants_tree || false,
includeParentsTree: filters?.include_ancestors_tree || false,
includeAncestorsTree: filters?.include_ancestors_tree || false,
}
delete filters.include_descendants_tree
delete filters.include_ancestors_tree
@@ -97,7 +97,7 @@ export default class ProductCategoryService<
): Promise<[TEntity[], number]> {
const transformOptions = {
includeDescendantsTree: filters?.include_descendants_tree || false,
includeParentsTree: filters?.include_ancestors_tree || false,
includeAncestorsTree: filters?.include_ancestors_tree || false,
}
delete filters.include_descendants_tree
delete filters.include_ancestors_tree

View File

@@ -3,5 +3,5 @@ import { RepositoryTransformOptions } from "../common"
export interface ProductCategoryTransformOptions
extends RepositoryTransformOptions {
includeDescendantsTree?: boolean
includeParentsTree?: boolean
includeAncestorsTree?: boolean
}