diff --git a/src/controllers/PermissionController.ts b/src/controllers/PermissionController.ts index bc95151e..70425dc0 100644 --- a/src/controllers/PermissionController.ts +++ b/src/controllers/PermissionController.ts @@ -12,6 +12,8 @@ import { AuthSys } from "../entities/AuthSys"; import { promisify } from "util"; import { In } from "typeorm"; import permission from "../interfaces/permission"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; +import { EmployeePosMaster } from "../entities/EmployeePosMaster"; const REDIS_HOST = process.env.REDIS_HOST; const REDIS_PORT = process.env.REDIS_PORT; @@ -20,7 +22,9 @@ const REDIS_PORT = process.env.REDIS_PORT; @Security("bearerAuth") export class PermissionController extends Controller { private profileRepo = AppDataSource.getRepository(Profile); + private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); private posMasterRepository = AppDataSource.getRepository(PosMaster); + private posMasterEmpRepository = AppDataSource.getRepository(EmployeePosMaster); private authRoleRepo = AppDataSource.getRepository(AuthRole); private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr); private authSysRepo = AppDataSource.getRepository(AuthSys); @@ -34,20 +38,28 @@ export class PermissionController extends Controller { }); const getAsync = promisify(redisClient.get).bind(redisClient); - const profile = await this.profileRepo.findOne({ + let profileType = "OFFICER"; + let profile:any = await this.profileRepo.findOne({ select: ["id"], where: { keycloak: request.user.sub }, }); if (!profile) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + profileType = "EMPLOYEE"; + profile = await this.profileEmployeeRepo.findOne({ + select: ["id"], + where: { keycloak: request.user.sub }, + }); + if (!profile) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + } } let reply = await getAsync("role_" + profile.id); if (reply != null) { reply = JSON.parse(reply); } else { - const posMaster = await this.posMasterRepository.findOne({ - // select: ["authRoleId"], + let posMaster:any = await this.posMasterRepository.findOne({ + select: ["authRoleId"], where: { current_holderId: profile.id, orgRevision: { @@ -57,9 +69,21 @@ export class PermissionController extends Controller { }, }); if (!posMaster) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์"); + posMaster = await this.posMasterEmpRepository.findOne({ + select: ["authRoleId"], + where: { + current_holderId: profile.id, + orgRevision: { + orgRevisionIsDraft: false, + orgRevisionIsCurrent: true, + }, + }, + }); + if (!posMaster) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์"); + } } - + const getDetail = await this.authRoleRepo.findOne({ select: ["id", "roleName", "roleDescription"], where: { id: posMaster.authRoleId }, @@ -100,20 +124,28 @@ export class PermissionController extends Controller { }); const getAsync = promisify(redisClient.get).bind(redisClient); - const profile = await this.profileRepo.findOne({ - select: ["id"], - where: { keycloak: request.user.sub }, - }); - if (!profile) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); - } + let profileType = "OFFICER"; + let profile:any = await this.profileRepo.findOne({ + select: ["id"], + where: { keycloak: request.user.sub }, + }); + if (!profile) { + profileType = "EMPLOYEE"; + profile = await this.profileEmployeeRepo.findOne({ + select: ["id"], + where: { keycloak: request.user.sub }, + }); + if (!profile) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + } + } let reply = await getAsync("menu_" + profile.id); if (reply != null) { reply = JSON.parse(reply); } else { - const posMaster = await this.posMasterRepository.findOne({ - // select: ["authRoleId"], + let posMaster:any = await this.posMasterRepository.findOne({ + select: ["authRoleId"], where: { current_holderId: profile.id, orgRevision: { @@ -123,7 +155,19 @@ export class PermissionController extends Controller { }, }); if (!posMaster) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งในโครงสร้าง"); + posMaster = await this.posMasterEmpRepository.findOne({ + select: ["authRoleId"], + where: { + current_holderId: profile.id, + orgRevision: { + orgRevisionIsDraft: false, + orgRevisionIsCurrent: true, + }, + }, + }); + if (!posMaster) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์"); + } } if (!posMaster.authRoleId) { @@ -243,18 +287,27 @@ export class PermissionController extends Controller { }); const getAsync = promisify(redisClient.get).bind(redisClient); - const profile = await this.profileRepo.findOne({ - select: ["id"], - where: { keycloak: request.user.sub }, - }); - if (!profile) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); - } + let profileType = "OFFICER"; + let profile:any = await this.profileRepo.findOne({ + select: ["id"], + where: { keycloak: request.user.sub }, + }); + if (!profile) { + profileType = "EMPLOYEE"; + profile = await this.profileEmployeeRepo.findOne({ + select: ["id"], + where: { keycloak: request.user.sub }, + }); + if (!profile) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + } + } let reply = await getAsync("posMaster_" + profile.id); if (reply != null) { reply = JSON.parse(reply); } else { + if(profileType == "OFFICER"){ const posMaster = await this.posMasterRepository.findOne({ where: { current_holderId: profile.id, @@ -281,7 +334,36 @@ export class PermissionController extends Controller { orgChild4Id: posMaster.orgChild4Id, }; } + redisClient.setex("posMaster_" + profile.id, 86400, JSON.stringify(reply));}else{ + + const posMaster = await this.posMasterEmpRepository.findOne({ + where: { + current_holderId: profile.id, + orgRevision: { + orgRevisionIsDraft: false, + orgRevisionIsCurrent: true, + }, + }, + }); + if (!posMaster) { + reply = { + orgRootId: null, + orgChild1Id: null, + orgChild2Id: null, + orgChild3Id: null, + orgChild4Id: null, + }; + } else { + reply = { + orgRootId: posMaster.orgRootId, + orgChild1Id: posMaster.orgChild1Id, + orgChild2Id: posMaster.orgChild2Id, + orgChild3Id: posMaster.orgChild3Id, + orgChild4Id: posMaster.orgChild4Id, + }; + } redisClient.setex("posMaster_" + profile.id, 86400, JSON.stringify(reply)); + } } return new HttpSuccess(reply); @@ -295,18 +377,27 @@ export class PermissionController extends Controller { }); const getAsync = promisify(redisClient.get).bind(redisClient); - const profile = await this.profileRepo.findOne({ + let profileType = "OFFICER"; + let profile:any = await this.profileRepo.findOne({ select: ["id"], where: { id: id }, }); if (!profile) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + profileType = "EMPLOYEE"; + profile = await this.profileEmployeeRepo.findOne({ + select: ["id"], + where: { id: id }, + }); + if (!profile) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + } } let reply = await getAsync("user_" + profile.id); if (reply != null) { reply = JSON.parse(reply); } else { + if(profileType == "OFFICER"){ const posMaster = await this.posMasterRepository.findOne({ where: { current_holderId: profile.id, @@ -333,7 +424,36 @@ export class PermissionController extends Controller { orgChild4Id: posMaster.orgChild4Id, }; } + redisClient.setex("user_" + profile.id, 86400, JSON.stringify(reply));}else{ + + const posMaster = await this.posMasterEmpRepository.findOne({ + where: { + current_holderId: profile.id, + orgRevision: { + orgRevisionIsDraft: false, + orgRevisionIsCurrent: true, + }, + }, + }); + if (!posMaster) { + reply = { + orgRootId: null, + orgChild1Id: null, + orgChild2Id: null, + orgChild3Id: null, + orgChild4Id: null, + }; + } else { + reply = { + orgRootId: posMaster.orgRootId, + orgChild1Id: posMaster.orgChild1Id, + orgChild2Id: posMaster.orgChild2Id, + orgChild3Id: posMaster.orgChild3Id, + orgChild4Id: posMaster.orgChild4Id, + }; + } redisClient.setex("user_" + profile.id, 86400, JSON.stringify(reply)); + } } return new HttpSuccess(reply);