เช็คprofileemp

This commit is contained in:
kittapath 2024-08-26 22:44:22 +07:00
parent d1cd27287c
commit fde5f8d90a

View file

@ -12,6 +12,8 @@ import { AuthSys } from "../entities/AuthSys";
import { promisify } from "util"; import { promisify } from "util";
import { In } from "typeorm"; import { In } from "typeorm";
import permission from "../interfaces/permission"; import permission from "../interfaces/permission";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { EmployeePosMaster } from "../entities/EmployeePosMaster";
const REDIS_HOST = process.env.REDIS_HOST; const REDIS_HOST = process.env.REDIS_HOST;
const REDIS_PORT = process.env.REDIS_PORT; const REDIS_PORT = process.env.REDIS_PORT;
@ -20,7 +22,9 @@ const REDIS_PORT = process.env.REDIS_PORT;
@Security("bearerAuth") @Security("bearerAuth")
export class PermissionController extends Controller { export class PermissionController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile); private profileRepo = AppDataSource.getRepository(Profile);
private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee);
private posMasterRepository = AppDataSource.getRepository(PosMaster); private posMasterRepository = AppDataSource.getRepository(PosMaster);
private posMasterEmpRepository = AppDataSource.getRepository(EmployeePosMaster);
private authRoleRepo = AppDataSource.getRepository(AuthRole); private authRoleRepo = AppDataSource.getRepository(AuthRole);
private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr); private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr);
private authSysRepo = AppDataSource.getRepository(AuthSys); private authSysRepo = AppDataSource.getRepository(AuthSys);
@ -34,20 +38,28 @@ export class PermissionController extends Controller {
}); });
const getAsync = promisify(redisClient.get).bind(redisClient); 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"], select: ["id"],
where: { keycloak: request.user.sub }, where: { keycloak: request.user.sub },
}); });
if (!profile) { 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); let reply = await getAsync("role_" + profile.id);
if (reply != null) { if (reply != null) {
reply = JSON.parse(reply); reply = JSON.parse(reply);
} else { } else {
const posMaster = await this.posMasterRepository.findOne({ let posMaster:any = await this.posMasterRepository.findOne({
// select: ["authRoleId"], select: ["authRoleId"],
where: { where: {
current_holderId: profile.id, current_holderId: profile.id,
orgRevision: { orgRevision: {
@ -57,7 +69,19 @@ export class PermissionController extends Controller {
}, },
}); });
if (!posMaster) { 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({ const getDetail = await this.authRoleRepo.findOne({
@ -100,20 +124,28 @@ export class PermissionController extends Controller {
}); });
const getAsync = promisify(redisClient.get).bind(redisClient); const getAsync = promisify(redisClient.get).bind(redisClient);
const profile = await this.profileRepo.findOne({ let profileType = "OFFICER";
select: ["id"], let profile:any = await this.profileRepo.findOne({
where: { keycloak: request.user.sub }, select: ["id"],
}); where: { keycloak: request.user.sub },
if (!profile) { });
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); 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); let reply = await getAsync("menu_" + profile.id);
if (reply != null) { if (reply != null) {
reply = JSON.parse(reply); reply = JSON.parse(reply);
} else { } else {
const posMaster = await this.posMasterRepository.findOne({ let posMaster:any = await this.posMasterRepository.findOne({
// select: ["authRoleId"], select: ["authRoleId"],
where: { where: {
current_holderId: profile.id, current_holderId: profile.id,
orgRevision: { orgRevision: {
@ -123,7 +155,19 @@ export class PermissionController extends Controller {
}, },
}); });
if (!posMaster) { 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) { if (!posMaster.authRoleId) {
@ -243,18 +287,27 @@ export class PermissionController extends Controller {
}); });
const getAsync = promisify(redisClient.get).bind(redisClient); const getAsync = promisify(redisClient.get).bind(redisClient);
const profile = await this.profileRepo.findOne({ let profileType = "OFFICER";
select: ["id"], let profile:any = await this.profileRepo.findOne({
where: { keycloak: request.user.sub }, select: ["id"],
}); where: { keycloak: request.user.sub },
if (!profile) { });
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); 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); let reply = await getAsync("posMaster_" + profile.id);
if (reply != null) { if (reply != null) {
reply = JSON.parse(reply); reply = JSON.parse(reply);
} else { } else {
if(profileType == "OFFICER"){
const posMaster = await this.posMasterRepository.findOne({ const posMaster = await this.posMasterRepository.findOne({
where: { where: {
current_holderId: profile.id, current_holderId: profile.id,
@ -281,7 +334,36 @@ export class PermissionController extends Controller {
orgChild4Id: posMaster.orgChild4Id, 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)); redisClient.setex("posMaster_" + profile.id, 86400, JSON.stringify(reply));
}
} }
return new HttpSuccess(reply); return new HttpSuccess(reply);
@ -295,18 +377,27 @@ export class PermissionController extends Controller {
}); });
const getAsync = promisify(redisClient.get).bind(redisClient); 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"], select: ["id"],
where: { id: id }, where: { id: id },
}); });
if (!profile) { 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); let reply = await getAsync("user_" + profile.id);
if (reply != null) { if (reply != null) {
reply = JSON.parse(reply); reply = JSON.parse(reply);
} else { } else {
if(profileType == "OFFICER"){
const posMaster = await this.posMasterRepository.findOne({ const posMaster = await this.posMasterRepository.findOne({
where: { where: {
current_holderId: profile.id, current_holderId: profile.id,
@ -333,7 +424,36 @@ export class PermissionController extends Controller {
orgChild4Id: posMaster.orgChild4Id, 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)); redisClient.setex("user_" + profile.id, 86400, JSON.stringify(reply));
}
} }
return new HttpSuccess(reply); return new HttpSuccess(reply);