diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index c6b66f7e..0fa1829a 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -1472,50 +1472,99 @@ export class ProfileController extends Controller { pageSize: number; }, ) { - if (body.isAct == true) { - const posMaster = await this.posMasterRepo.findOne({ + let posMaster = await this.posMasterRepo.findOne({ + where: { + current_holder: { keycloak: request.user.sub }, + orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, + }, + relations: ["current_holder", "current_holder.posLevel"], + }); + if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลการครองตำแหน่ง"); + if ((posMaster?.current_holder?.posLevel?.posLevelAuthority ?? null) == "DEPUTY") { + posMaster = await this.posMasterRepo.findOne({ where: { - current_holder: { keycloak: request.user.sub }, + orgRoot: { isDeputy: true }, + orgChild1Id: IsNull(), orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, }, }); - if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลการครองตำแหน่ง"); + return new HttpSuccess({ data: [], total: 0 }); + } else if ((posMaster?.current_holder?.posLevel?.posLevelAuthority ?? null) == "GOVERNOR") { + return new HttpSuccess({ data: [], total: 0 }); + } + let condition: any = { + orgRootId: posMaster.orgRootId, + id: Not(posMaster.current_holderId || ""), + }; + let conditionNow: any = { + orgRootId: posMaster.orgRootId ?? IsNull(), + orgChild1Id: posMaster.orgChild1Id ?? IsNull(), + orgChild2Id: posMaster.orgChild2Id ?? IsNull(), + orgChild3Id: posMaster.orgChild3Id ?? IsNull(), + orgChild4Id: posMaster.orgChild4Id ?? IsNull(), + id: Not(posMaster.current_holderId), + }; + if ( + posMaster.orgRootId == null || + posMaster.orgChild1Id == null || + posMaster.orgChild2Id == null + ) { + condition.orgChild1Id = IsNull(); + } else if (posMaster.orgChild3Id == null) { + condition.orgChild2Id = IsNull(); + } else if (posMaster.orgChild4Id == null) { + condition.orgChild3Id = IsNull(); + } else if (posMaster.orgChild4Id != null) { + condition.orgChild4Id = IsNull(); + } + if (body.isDirector == true) { + condition.isDirector = true; + conditionNow.isDirector = true; + } + if (body.isAct == true) { + // const posMaster = await this.posMasterRepo.findOne({ + // where: { + // current_holder: { keycloak: request.user.sub }, + // orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, + // }, + // }); + // if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลการครองตำแหน่ง"); - let condition: any = { - orgRootId: posMaster.orgRootId, - id: Not(posMaster.current_holderId || ""), - }; - // let conditionNow: any = { + // let condition: any = { // orgRootId: posMaster.orgRootId, - // orgChild1Id: posMaster.orgChild1Id, - // orgChild2Id: posMaster.orgChild2Id, - // orgChild3Id: posMaster.orgChild3Id, - // orgChild4Id: posMaster.orgChild4Id, - // id: Not(posMaster.current_holderId), + // id: Not(posMaster.current_holderId || ""), // }; - // if (posMaster.orgChild1Id == null) { - // condition.orgRootId = null; - // } else if (posMaster.orgChild2Id == null) { - // condition.orgChild1Id = null; - // } else if (posMaster.orgChild3Id == null) { - // condition.orgChild2Id = null; - // } else if (posMaster.orgChild4Id == null) { - // condition.orgChild3Id = null; - // } else { - // condition.orgChild4Id = null; + // // let conditionNow: any = { + // // orgRootId: posMaster.orgRootId, + // // orgChild1Id: posMaster.orgChild1Id, + // // orgChild2Id: posMaster.orgChild2Id, + // // orgChild3Id: posMaster.orgChild3Id, + // // orgChild4Id: posMaster.orgChild4Id, + // // id: Not(posMaster.current_holderId), + // // }; + // // if (posMaster.orgChild1Id == null) { + // // condition.orgRootId = null; + // // } else if (posMaster.orgChild2Id == null) { + // // condition.orgChild1Id = null; + // // } else if (posMaster.orgChild3Id == null) { + // // condition.orgChild2Id = null; + // // } else if (posMaster.orgChild4Id == null) { + // // condition.orgChild3Id = null; + // // } else { + // // condition.orgChild4Id = null; + // // } + // if (body.isDirector == true) { + // condition.isDirector = true; + // // conditionNow.isDirector = true; // } - if (body.isDirector == true) { - condition.isDirector = true; - // conditionNow.isDirector = true; - } const [lists, total] = await AppDataSource.getRepository(viewDirectorActing) .createQueryBuilder("viewDirectorActing") - .andWhere(condition) - // .andWhere( - // new Brackets((qb) => { - // qb.orWhere(condition).orWhere(conditionNow); - // }), - // ) + // .andWhere(condition) + .andWhere( + new Brackets((qb) => { + qb.orWhere(condition).orWhere(conditionNow); + }), + ) .andWhere( new Brackets((qb) => { qb.orWhere( @@ -1565,18 +1614,18 @@ export class ProfileController extends Controller { .getManyAndCount(); return new HttpSuccess({ data: lists, total }); } else { - const posMaster = await this.posMasterRepo.findOne({ - where: { - current_holder: { keycloak: request.user.sub }, - orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, - }, - }); - if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลการครองตำแหน่ง"); + // const posMaster = await this.posMasterRepo.findOne({ + // where: { + // current_holder: { keycloak: request.user.sub }, + // orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, + // }, + // }); + // if (!posMaster) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลการครองตำแหน่ง"); - let condition: any = { - orgRootId: posMaster.orgRootId, - id: Not(posMaster.current_holderId || ""), - }; + // let condition: any = { + // orgRootId: posMaster.orgRootId, + // id: Not(posMaster.current_holderId || ""), + // }; // let conditionNow: any = { // orgRootId: posMaster.orgRootId, // orgChild1Id: posMaster.orgChild1Id, @@ -1596,13 +1645,13 @@ export class ProfileController extends Controller { // } else { // condition.orgChild4Id = null; // } - if (body.isDirector == true) { - condition.isDirector = true; - // conditionNow.isDirector = true; - } + // if (body.isDirector == true) { + // condition.isDirector = true; + // // conditionNow.isDirector = true; + // } const [lists, total] = await AppDataSource.getRepository(viewDirector) .createQueryBuilder("viewDirector") - .andWhere(condition) + // .andWhere(condition) // .andWhere( // new Brackets((qb) => { // qb.orWhere(condition).orWhere(conditionNow); @@ -1724,8 +1773,6 @@ export class ProfileController extends Controller { condition.isDirector = true; conditionNow.isDirector = true; } - console.log(condition); - console.log(conditionNow); if (body.isAct == true) { const [lists, total] = await AppDataSource.getRepository(viewDirectorActing) .createQueryBuilder("viewDirectorActing") diff --git a/src/entities/view/viewDirector.ts b/src/entities/view/viewDirector.ts index f6c1ae9a..0f5aad68 100644 --- a/src/entities/view/viewDirector.ts +++ b/src/entities/view/viewDirector.ts @@ -49,7 +49,7 @@ import { ViewColumn, ViewEntity } from "typeorm"; AND ( (\`acting\`.\`orgChild4Id\` IS NULL AND \`posMaster\`.\`orgChild4Id\` IS NULL) OR (\`acting\`.\`orgChild4Id\` = \`posMaster\`.\`orgChild4Id\`) - ) + ) LIMIT 1 ) AS \`actFullNameId\`, ( SELECT \`actFullName\` @@ -71,7 +71,7 @@ import { ViewColumn, ViewEntity } from "typeorm"; AND ( (\`acting\`.\`orgChild4Id\` IS NULL AND \`posMaster\`.\`orgChild4Id\` IS NULL) OR (\`acting\`.\`orgChild4Id\` = \`posMaster\`.\`orgChild4Id\`) - ) + ) LIMIT 1 ) AS \`actFullName\` FROM