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. RodriguesandRiqwan Thamir authored and GitHub committed 2024-04-05 19:22:55 +02:00
1 parent 09a2220569
commit 01487970b2
3 files changed
+14 -14

No files matched your search

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