From 1e3699b049a6ea070844bedafe013673b7faccdd Mon Sep 17 00:00:00 2001 From: waruneeauy Date: Wed, 24 Jul 2024 15:44:34 +0700 Subject: [PATCH] updated permission menu --- src/controllers/PermissionController.ts | 126 ++++++++++++++---------- src/entities/AuthRoleAttr.ts | 2 +- 2 files changed, 77 insertions(+), 51 deletions(-) diff --git a/src/controllers/PermissionController.ts b/src/controllers/PermissionController.ts index 1ff38310..25964ae6 100644 --- a/src/controllers/PermissionController.ts +++ b/src/controllers/PermissionController.ts @@ -9,6 +9,7 @@ import { AuthRole } from "../entities/AuthRole"; import { AuthRoleAttr } from "../entities/AuthRoleAttr"; import { PosMaster } from "../entities/PosMaster"; import { Profile } from "../entities/Profile"; +import { AuthSys } from "../entities/AuthSys"; const REDIS_HOST = process.env.REDIS_HOST; const REDIS_PORT = process.env.REDIS_PORT; @@ -20,6 +21,7 @@ export class PermissionController extends Controller { private posMasterRepository = AppDataSource.getRepository(PosMaster); private authRoleRepo = AppDataSource.getRepository(AuthRole); private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr); + private authSysRepo = AppDataSource.getRepository(AuthSys); private redis = require("redis"); @Get("") @@ -28,61 +30,85 @@ export class PermissionController extends Controller { host: REDIS_HOST, port: REDIS_PORT, }); - const formattedData = null; + // const formattedData = null; // await Promise.all([ - return new HttpSuccess( - redisClient.get(request.user.sub, async (err: any, reply: any) => { - if (reply != null) { - return JSON.parse(reply); - } else { - const profile = await this.profileRepo.findOne({ - select: ["id"], - where: { keycloak: request.user.sub }, - }); - if (!profile) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); - } + // return new HttpSuccess( + // redisClient.get(request.user.sub, async (err: any, reply: any) => { + // if (reply != null) { + // return JSON.parse(reply); + // } else { + const profile = await this.profileRepo.findOne({ + select: ["id"], + where: { keycloak: request.user.sub }, + }); + if (!profile) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + } - const posMaster = await this.posMasterRepository.findOne({ - select: ["authRoleId"], - where: { current_holderId: profile.id }, - }); - if (!posMaster) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์"); - } + const posMaster = await this.posMasterRepository.findOne({ + select: ["authRoleId"], + where: { current_holderId: profile.id }, + }); + if (!posMaster) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์"); + } - const getDetail = await this.authRoleRepo.findOne({ - select: ["id", "roleName", "roleDescription"], - where: { id: posMaster.authRoleId }, - }); - if (!getDetail) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - } + const getDetail = await this.authRoleRepo.findOne({ + select: ["id", "roleName", "roleDescription"], + where: { id: posMaster.authRoleId }, + }); + if (!getDetail) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } - const roleAttrData = await this.authRoleAttrRepo.find({ - select: [ - "authSysId", - "parentNode", - "attrOwnership", - "attrIsCreate", - "attrIsList", - "attrIsGet", - "attrIsUpdate", - "attrIsDelete", - "attrPrivilege", - ], - where: { authRoleId: getDetail.id }, - }); + const roleAttrData = await this.authRoleAttrRepo.find({ + select: [ + "authSysId", + "parentNode", + "attrOwnership", + "attrIsCreate", + "attrIsList", + "attrIsGet", + "attrIsUpdate", + "attrIsDelete", + "attrPrivilege", + ], + where: { authRoleId: getDetail.id }, + }); - const formattedData = { - ...getDetail, - roleAttributes: roleAttrData, - }; - redisClient.setex(request.user.sub, 20, JSON.stringify(formattedData)); - return formattedData; - } - }), - ); + const formattedData = { + ...getDetail, + roles: roleAttrData, + }; + return new HttpSuccess(formattedData); + // redisClient.setex(request.user.sub, 20, JSON.stringify(formattedData)); + // return formattedData; + // } + // }), + // ); // ]); } + + @Get("menu") + public async listAuthSys() { + const getList = await this.authSysRepo.find({ + select: ["id", "parentId", "sysName", "sysDescription", "icon", "path", "order"], + }); + + if (!getList || getList.length === 0) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + } + + const lists = getList + .filter((x) => x.parentId == null) + .map((item) => { + return { + ...item, + children: getList.filter((x) => x.parentId == item.id).sort((a, b) => a.order - b.order), + }; + }) + .sort((a, b) => a.order - b.order); + + return new HttpSuccess(lists); + } } diff --git a/src/entities/AuthRoleAttr.ts b/src/entities/AuthRoleAttr.ts index a6022079..cdd985d5 100644 --- a/src/entities/AuthRoleAttr.ts +++ b/src/entities/AuthRoleAttr.ts @@ -75,7 +75,7 @@ export class AuthRoleAttr extends EntityBase { }) parentNode?: string; - // @ManyToOne(() => AuthSys, (authSys) => authSys.authSys) + // @ManyToOne(() => AuthSys, (authSys) => authSys) // @JoinColumn({ name: "authSysId" }) // authRoleAttrForSys: AuthSys;