use promiss all

This commit is contained in:
Kittapath 2024-07-24 09:42:34 +07:00
parent 865696dfb5
commit dc48548db3

View file

@ -9,6 +9,8 @@ import { AuthRole } from "../entities/AuthRole";
import { AuthRoleAttr } from "../entities/AuthRoleAttr"; import { AuthRoleAttr } from "../entities/AuthRoleAttr";
import { PosMaster } from "../entities/PosMaster"; import { PosMaster } from "../entities/PosMaster";
import { Profile } from "../entities/Profile"; import { Profile } from "../entities/Profile";
const REDIS_HOST = process.env.REDIS_HOST;
const REDIS_PORT = process.env.REDIS_PORT;
@Route("api/v1/org/permission") @Route("api/v1/org/permission")
@Tags("Permission") @Tags("Permission")
@ -18,53 +20,67 @@ export class PermissionController extends Controller {
private posMasterRepository = AppDataSource.getRepository(PosMaster); private posMasterRepository = AppDataSource.getRepository(PosMaster);
private authRoleRepo = AppDataSource.getRepository(AuthRole); private authRoleRepo = AppDataSource.getRepository(AuthRole);
private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr); private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr);
private redis = require("redis");
@Get("") @Get("")
public async getPermission(@Request() request: { user: Record<string, any> }) { public async getPermission(@Request() request: { user: Record<string, any> }) {
const profile = await this.profileRepo.findOne({ const redisClient = await this.redis.createClient({
select: ["id"], host: REDIS_HOST,
where: { keycloak: request.user.sub }, port: REDIS_PORT,
});
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 getDetail = await this.authRoleRepo.findOne({
select: ["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 formattedData = { await Promise.all([
...getDetail, await redisClient.get(request.user.sub, async (err: any, reply: any) => {
roleAttributes: roleAttrData, if (reply != null) {
}; return new HttpSuccess(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(formattedData); 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: ["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 formattedData = {
...getDetail,
roleAttributes: roleAttrData,
};
redisClient.setex(request.user.sub, 20, JSON.stringify(formattedData));
return new HttpSuccess(formattedData);
}
}),
]);
} }
} }