From 564d7d96c3de5ff82e5b3e95de87aaf88cf7fac7 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang <153157069+AdisakKanthawilang@users.noreply.github.com> Date: Tue, 7 Oct 2025 14:59:11 +0700 Subject: [PATCH] #176 (#185) * #176 * delete log --- src/controllers/UserController.ts | 104 +++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 30 deletions(-) diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 00af5b10..38daaeae 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -470,15 +470,21 @@ export class KeycloakController extends Controller { throw new Error("Failed. Cannot get user list."); }*/ - @Get("user") + @Post("user/admin") async listUserKeycloak( - @Query("page") page: number = 1, - @Query("pageSize") pageSize: number = 10, - @Query() keyword: string = "", - @Query() type: string = "", - @Request() req: RequestWithUser, + @Request() req: RequestWithUser, + @Body() + body:{ + page: number, + pageSize: number, + keyword: string | null, + type: string, + isAll: boolean, + node: number | null, + nodeId: string | null, + } ) { - let condition: any = {}; + let checkChildFromRole: any = {}; if (req.user.role.includes("ADMIN") && !req.user.role.includes("SUPER_ADMIN")) { const profile = await this.profileRepo.findOne({ @@ -498,7 +504,7 @@ export class KeycloakController extends Controller { profile?.current_holders[0]?.orgRootId && profile?.current_holders[0]?.orgChild1Id == null ) { - condition = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' + checkChildFromRole = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' and current_holders.orgChild1Id IS NULL and current_holders.orgChild2Id IS NULL and current_holders.orgChild3Id IS NULL @@ -507,7 +513,7 @@ export class KeycloakController extends Controller { profile?.current_holders[0]?.orgChild1Id && profile?.current_holders[0]?.orgChild2Id == null ) { - condition = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' + checkChildFromRole = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' and current_holders.orgChild1Id = '${profile?.current_holders[0]?.orgChild1Id}' and current_holders.orgChild2Id IS NULL and current_holders.orgChild3Id IS NULL @@ -516,7 +522,7 @@ export class KeycloakController extends Controller { profile?.current_holders[0]?.orgChild2Id && profile?.current_holders[0]?.orgChild3Id == null ) { - condition = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' + checkChildFromRole = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' and current_holders.orgChild1Id = '${profile?.current_holders[0]?.orgChild1Id}' and current_holders.orgChild2Id = '${profile?.current_holders[0]?.orgChild2Id}' and current_holders.orgChild3Id IS NULL @@ -525,77 +531,115 @@ export class KeycloakController extends Controller { profile?.current_holders[0]?.orgChild3Id && profile?.current_holders[0]?.orgChild4Id == null ) { - condition = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' + checkChildFromRole = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' and current_holders.orgChild1Id = '${profile?.current_holders[0]?.orgChild1Id}' and current_holders.orgChild2Id = '${profile?.current_holders[0]?.orgChild2Id}' and current_holders.orgChild3Id = '${profile?.current_holders[0]?.orgChild3Id}' and current_holders.orgChild4Id IS NULL`; } else if (profile?.current_holders[0]?.orgChild4Id) { - condition = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' + checkChildFromRole = `current_holders.orgRootId = '${profile?.current_holders[0]?.orgRootId}' and current_holders.orgChild1Id = '${profile?.current_holders[0]?.orgChild1Id}' and current_holders.orgChild2Id = '${profile?.current_holders[0]?.orgChild2Id}' and current_holders.orgChild3Id = '${profile?.current_holders[0]?.orgChild3Id}' and current_holders.orgChild4Id = '${profile?.current_holders[0]?.orgChild4Id}'`; } } + + let typeCondition = ""; + let checkChildConditions = ""; + let conditions:any = {}; + + if(body.nodeId !== null && body.nodeId !== "" && body.nodeId !== undefined){ + if (body.node === 0) { + typeCondition = `current_holders.orgRootId = '${body.nodeId}'`; + if (!body.isAll) { + checkChildConditions = `and current_holders.orgChild1Id IS NULL`; + } + } else if (body.node === 1) { + typeCondition = `current_holders.orgChild1Id = '${body.nodeId}'`; + if (!body.isAll) { + checkChildConditions = `and current_holders.orgChild2Id IS NULL`; + } + } else if (body.node === 2) { + typeCondition = `current_holders.orgChild2Id = '${body.nodeId}'`; + if (!body.isAll) { + checkChildConditions = `and current_holders.orgChild3Id IS NULL`; + } + } else if (body.node === 3) { + typeCondition = `current_holders.orgChild3Id = '${body.nodeId}'`; + if (!body.isAll) { + checkChildConditions = `and current_holders.orgChild4Id IS NULL`; + } + } else if (body.node === 4) { + typeCondition = `current_holders.orgChild4Id = '${body.nodeId}'`; + } + + conditions = typeCondition; + if(checkChildConditions){ + conditions += checkChildConditions; + } + } + let profiles: any = []; let total: any; - if (type.trim().toUpperCase() == "OFFICER") { + if (body.type.trim().toUpperCase() == "OFFICER") { [profiles, total] = await this.profileRepo .createQueryBuilder("profile") .leftJoinAndSelect("profile.roleKeycloaks", "roleKeycloaks") .leftJoinAndSelect("profile.current_holders", "current_holders") .where("profile.keycloak IS NOT NULL AND profile.keycloak != ''") - .andWhere(condition) + .andWhere(checkChildFromRole) + .andWhere(conditions) .andWhere( new Brackets((qb) => { qb.orWhere( - keyword != null && keyword != "" ? `profile.citizenId like '%${keyword}%'` : "1=1", + body.keyword != null && body.keyword != "" ? `profile.citizenId like '%${body.keyword}%'` : "1=1", ) .orWhere( - keyword != null && keyword != "" ? `profile.email like '%${keyword}%'` : "1=1", + body.keyword != null && body.keyword != "" ? `profile.email like '%${body.keyword}%'` : "1=1", ) .orWhere( - keyword != null && keyword != "" - ? `CONCAT(profile.prefix, profile.firstName," ",profile.lastName) like '%${keyword}%'` + body.keyword != null && body.keyword != "" + ? `CONCAT(profile.prefix, profile.firstName," ",profile.lastName) like '%${body.keyword}%'` : "1=1", ); }), ) .orderBy("profile.citizenId", "ASC") - .skip((page - 1) * pageSize) - .take(pageSize) + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) .getManyAndCount(); - } else if (type.trim().toUpperCase() == "EMPLOYEE") { + } else if (body.type.trim().toUpperCase() == "EMPLOYEE") { [profiles, total] = await this.profileEmpRepo .createQueryBuilder("profileEmployee") .leftJoinAndSelect("profileEmployee.roleKeycloaks", "roleKeycloaks") .leftJoinAndSelect("profileEmployee.current_holders", "current_holders") .where("profileEmployee.keycloak IS NOT NULL AND profileEmployee.keycloak != ''") - .andWhere(condition) + .andWhere(checkChildFromRole) + .andWhere(conditions) .andWhere({ employeeClass: "PERM" }) .andWhere( new Brackets((qb) => { qb.orWhere( - keyword != null && keyword != "" - ? `profileEmployee.citizenId like '%${keyword}%'` + body.keyword != null && body.keyword != "" + ? `profileEmployee.citizenId like '%${body.keyword}%'` : "1=1", ) .orWhere( - keyword != null && keyword != "" - ? `profileEmployee.email like '%${keyword}%'` + body.keyword != null && body.keyword != "" + ? `profileEmployee.email like '%${body.keyword}%'` : "1=1", ) .orWhere( - keyword != null && keyword != "" - ? `CONCAT(profileEmployee.prefix, profileEmployee.firstName," ",profileEmployee.lastName) like '%${keyword}%'` + body.keyword != null && body.keyword != "" + ? `CONCAT(profileEmployee.prefix, profileEmployee.firstName," ",profileEmployee.lastName) like '%${body.keyword}%'` : "1=1", ); }), ) .orderBy("profileEmployee.citizenId", "ASC") - .skip((page - 1) * pageSize) - .take(pageSize) + .skip((body.page - 1) * body.pageSize) + .take(body.pageSize) .getManyAndCount(); }