From 25a61874851160e1a1c0ef1e078277553205d888 Mon Sep 17 00:00:00 2001 From: Abdul Rahman Reza Date: Sat, 30 Nov 2024 23:35:22 +0700 Subject: [PATCH] update menu children --- src/controllers/menu.ts | 51 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/controllers/menu.ts b/src/controllers/menu.ts index f5204f3..73dc0f4 100644 --- a/src/controllers/menu.ts +++ b/src/controllers/menu.ts @@ -85,6 +85,7 @@ export class MenuController { .where(whereAttr, whereVal); const res_list = repo.createQueryBuilder() .where(whereAttr, whereVal) + .andWhere('id_parent is null') .orderBy(param.order_field, param.order_direction) .offset(offset) .limit(param.limit); @@ -94,9 +95,57 @@ export class MenuController { res_list.withDeleted(); } + const res_list_all = await repo.createQueryBuilder().andWhere('id_parent is not null').getMany(); + let list_data_final = await res_list.getMany(); + + let list_child: any = {}; + for (let p of res_list_all) { + if (!list_child[p.id_parent]) { + list_child[p.id_parent] = []; + } + + list_child[p.id_parent].push(p); + } + + for (let f of list_data_final) { + if (list_child[f.id]) { + f.children = list_child[f.id]; + + for (let g of f.children) { + if (list_child[g.id]) { + g.children = list_child[g.id]; + + for (let h of g.children) { + if (list_child[h.id]) { + h.children = list_child[h.id]; + + for (let i of h.children) { + if (list_child[i.id]) { + i.children = list_child[i.id]; + + for (let j of i.children) { + if (list_child[j.id]) { + j.children = list_child[j.id]; + + for (let k of j.children) { + if (list_child[k.id]) { + k.children = list_child[k.id]; + } + } + } + } + } + } + } + } + } + } + } + } + const current_page = param.page; const total_count_data = await res_count.getCount(); - const list_data = await res_list.getMany(); + const list_data = list_data_final; const count_data = CommonHelper.countObject(list_data); return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, list_data);