fixing permission & menu

This commit is contained in:
Warunee Tamkoo 2024-08-14 17:37:20 +07:00
parent 53787597fd
commit 24bbcc6f7f
2 changed files with 44 additions and 29 deletions

View file

@ -34,18 +34,18 @@ export class PermissionController extends Controller {
});
const getAsync = promisify(redisClient.get).bind(redisClient);
let reply = await getAsync("role_" + request.user.sub);
const profile = await this.profileRepo.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 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: {
@ -87,7 +87,7 @@ export class PermissionController extends Controller {
...getDetail,
roles: roleAttrData,
};
redisClient.setex("role_" + request.user.sub, 86400, JSON.stringify(reply));
redisClient.setex("role_" + profile.id, 86400, JSON.stringify(reply));
}
return new HttpSuccess(reply);
}
@ -100,17 +100,18 @@ export class PermissionController extends Controller {
});
const getAsync = promisify(redisClient.get).bind(redisClient);
let reply = await getAsync("menu_" + request.user.sub);
const profile = await this.profileRepo.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 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: {
@ -125,10 +126,15 @@ export class PermissionController extends Controller {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งในโครงสร้าง");
}
if (!posMaster.authRoleId) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
}
const authRole = await this.authRoleRepo.findOne({
select: ["id"],
where: { id: posMaster.authRoleId },
});
if (!authRole) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
}
@ -142,14 +148,18 @@ export class PermissionController extends Controller {
const getList = await this.authSysRepo.find({
select: ["id", "parentId", "sysName", "sysDescription", "icon", "path", "order"],
where: [
{
id: In(sysId),
},
{
parentId: In(sysId),
},
],
where: {
id: In(sysId),
},
order: { order: "ASC" },
});
const getListChild = await this.authSysRepo.find({
select: ["id", "parentId", "sysName", "sysDescription", "icon", "path", "order"],
where: {
parentId: In(sysId),
},
order: { order: "ASC" },
});
reply = await getList
@ -164,7 +174,7 @@ export class PermissionController extends Controller {
.map((item2) => {
return {
...item2,
children: getList
children: getListChild
.filter((x) => x.parentId == item2.id)
.sort((a, b) => a.order - b.order),
};
@ -172,7 +182,7 @@ export class PermissionController extends Controller {
};
});
redisClient.setex("menu_" + request.user.sub, 86400, JSON.stringify(reply));
redisClient.setex("menu_" + profile.id, 86400, JSON.stringify(reply));
}
return new HttpSuccess(reply);