add permission brother

This commit is contained in:
kittapath 2025-12-12 01:39:07 +07:00
parent 5c274cf916
commit 84dbbf67c3
2 changed files with 426 additions and 278 deletions

View file

@ -28,7 +28,7 @@ import CallAPI from "../interfaces/call-api";
@Security("bearerAuth") @Security("bearerAuth")
@Response( @Response(
HttpStatusCode.INTERNAL_SERVER_ERROR, HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง"
) )
export class PersonalController extends Controller { export class PersonalController extends Controller {
private personalRepository = AppDataSource.getRepository(Personal); private personalRepository = AppDataSource.getRepository(Personal);
@ -41,7 +41,10 @@ export class PersonalController extends Controller {
* *
*/ */
@Post("add") @Post("add")
async AddPersonal(@Body() requestBody: PostPersonal, @Request() request: RequestWithUser) { async AddPersonal(
@Body() requestBody: PostPersonal,
@Request() request: RequestWithUser
) {
try { try {
await new permission().PermissionCreate(request, "SYS_PROBATION"); await new permission().PermissionCreate(request, "SYS_PROBATION");
@ -51,19 +54,31 @@ export class PersonalController extends Controller {
if (checkPersonal > 0) { if (checkPersonal > 0) {
throw new HttpError( throw new HttpError(
HttpStatusCode.BAD_REQUEST, HttpStatusCode.BAD_REQUEST,
"ผู้ทดลองปฏิบัติหน้าที่ราชการนี้มีอยู่แล้ว", "ผู้ทดลองปฏิบัติหน้าที่ราชการนี้มีอยู่แล้ว"
); );
} }
let organization = await (requestBody.orgChild4Name ? requestBody.orgChild4Name + "\n" : ""); let organization = await (requestBody.orgChild4Name
organization += await (requestBody.orgChild3Name ? requestBody.orgChild3Name + "\n" : ""); ? requestBody.orgChild4Name + "\n"
organization += await (requestBody.orgChild2Name ? requestBody.orgChild2Name + "\n" : ""); : "");
organization += await (requestBody.orgChild1Name ? requestBody.orgChild1Name + "\n" : ""); organization += await (requestBody.orgChild3Name
organization += await (requestBody.orgRootName ? requestBody.orgRootName : ""); ? requestBody.orgChild3Name + "\n"
: "");
organization += await (requestBody.orgChild2Name
? requestBody.orgChild2Name + "\n"
: "");
organization += await (requestBody.orgChild1Name
? requestBody.orgChild1Name + "\n"
: "");
organization += await (requestBody.orgRootName
? requestBody.orgRootName
: "");
const personalData = Object.assign(new Personal()); const personalData = Object.assign(new Personal());
personalData.personal_id = requestBody.id; personalData.personal_id = requestBody.id;
personalData.order_number = requestBody.order_number ? requestBody.order_number : ""; personalData.order_number = requestBody.order_number
? requestBody.order_number
: "";
personalData.probation_status = 1; personalData.probation_status = 1;
personalData.createdUserId = request.user.sub; personalData.createdUserId = request.user.sub;
personalData.createdFullName = request.user.name; personalData.createdFullName = request.user.name;
@ -75,8 +90,12 @@ export class PersonalController extends Controller {
personalData.firstName = requestBody.firstName; personalData.firstName = requestBody.firstName;
personalData.lastName = requestBody.lastName; personalData.lastName = requestBody.lastName;
personalData.isProbation = requestBody.isProbation ? 1 : 0; personalData.isProbation = requestBody.isProbation ? 1 : 0;
personalData.positionLevelName = requestBody.posLevelName ? requestBody.posLevelName : ""; personalData.positionLevelName = requestBody.posLevelName
personalData.positionName = requestBody.position ? requestBody.position : ""; ? requestBody.posLevelName
: "";
personalData.positionName = requestBody.position
? requestBody.position
: "";
personalData.positionLineName = requestBody.posLineName; personalData.positionLineName = requestBody.posLineName;
personalData.positionTypeName = requestBody.posTypeName; personalData.positionTypeName = requestBody.posTypeName;
personalData.posNo = requestBody.posNo ? requestBody.posNo : ""; personalData.posNo = requestBody.posNo ? requestBody.posNo : "";
@ -95,7 +114,8 @@ export class PersonalController extends Controller {
personalData.child3Dna = requestBody.orgChild3Dna; personalData.child3Dna = requestBody.orgChild3Dna;
personalData.child4Dna = requestBody.orgChild4Dna; personalData.child4Dna = requestBody.orgChild4Dna;
const getFieldValue = (field: string | null | undefined): string => field ?? ""; const getFieldValue = (field: string | null | undefined): string =>
field ?? "";
personalData.orgChild1Name = getFieldValue(requestBody.orgChild1Name); personalData.orgChild1Name = getFieldValue(requestBody.orgChild1Name);
personalData.orgChild2Name = getFieldValue(requestBody.orgChild2Name); personalData.orgChild2Name = getFieldValue(requestBody.orgChild2Name);
personalData.orgChild3Name = getFieldValue(requestBody.orgChild3Name); personalData.orgChild3Name = getFieldValue(requestBody.orgChild3Name);
@ -111,7 +131,11 @@ export class PersonalController extends Controller {
} catch (error: any) { } catch (error: any) {
if (error instanceof HttpError) { if (error instanceof HttpError) {
throw error; throw error;
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message); } else
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
error.message
);
} }
} }
@ -129,13 +153,16 @@ export class PersonalController extends Controller {
@Query("page") page: number = 1, @Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10, @Query("pageSize") pageSize: number = 10,
@Query("sortBy") sortBy?: string, @Query("sortBy") sortBy?: string,
@Query("descending") descending?: boolean, @Query("descending") descending?: boolean
) { ) {
try { try {
// await new permission().PermissionList(request, "SYS_PROBATION"); // await new permission().PermissionList(request, "SYS_PROBATION");
// const _data = await new permission().PermissionOrgList(request, "SYS_PROBATION"); // const _data = await new permission().PermissionOrgList(request, "SYS_PROBATION");
let _data = await new permission().PermissionOrgList(request, "SYS_PROBATION"); let _data = await new permission().PermissionOrgList(
request,
"SYS_PROBATION"
);
await new CallAPI() await new CallAPI()
.PostData(request, "/org/finddna", _data) .PostData(request, "/org/finddna", _data)
.then((x) => { .then((x) => {
@ -208,17 +235,17 @@ export class PersonalController extends Controller {
: "1=1", : "1=1",
{ {
root: _data.root, root: _data.root,
}, }
) )
.andWhere( .andWhere(
_data.child1 != undefined && _data.child1 != null _data.child1 != undefined && _data.child1 != null
? _data.child1[0] != null ? _data.child1[0] != null
? `personal.child1Dna IN (:...child1)` ? `personal.child1Dna IN (:...child1)`
: `personal.child1Dna is null` : `personal.child1Dna is ${_data.privilege == "PARENT" ? "not null" : "null"}`
: "1=1", : "1=1",
{ {
child1: _data.child1, child1: _data.child1,
}, }
) )
.andWhere( .andWhere(
_data.child2 != undefined && _data.child2 != null _data.child2 != undefined && _data.child2 != null
@ -228,7 +255,7 @@ export class PersonalController extends Controller {
: "1=1", : "1=1",
{ {
child2: _data.child2, child2: _data.child2,
}, }
) )
.andWhere( .andWhere(
_data.child3 != undefined && _data.child3 != null _data.child3 != undefined && _data.child3 != null
@ -238,7 +265,7 @@ export class PersonalController extends Controller {
: "1=1", : "1=1",
{ {
child3: _data.child3, child3: _data.child3,
}, }
) )
.andWhere( .andWhere(
_data.child4 != undefined && _data.child4 != null _data.child4 != undefined && _data.child4 != null
@ -248,7 +275,7 @@ export class PersonalController extends Controller {
: "1=1", : "1=1",
{ {
child4: _data.child4, child4: _data.child4,
}, }
) )
.andWhere( .andWhere(
new Brackets((qb) => { new Brackets((qb) => {
@ -258,36 +285,60 @@ export class PersonalController extends Controller {
: "1=1", : "1=1",
{ {
keyword: `%${searchKeyword}%`, keyword: `%${searchKeyword}%`,
}, }
); );
qb.orWhere(searchKeyword ? `positionName like '%${keyword}%'` : "1=1", { qb.orWhere(
searchKeyword ? `positionName like '%${keyword}%'` : "1=1",
{
keyword: `%${searchKeyword}%`, keyword: `%${searchKeyword}%`,
}); }
qb.orWhere(searchKeyword ? `positionLevelName like '%${keyword}%'` : "1=1", { );
qb.orWhere(
searchKeyword ? `positionLevelName like '%${keyword}%'` : "1=1",
{
keyword: `%${searchKeyword}%`, keyword: `%${searchKeyword}%`,
}); }
qb.orWhere(searchKeyword ? `organization like '%${keyword}%'` : "1=1", { );
qb.orWhere(
searchKeyword ? `organization like '%${keyword}%'` : "1=1",
{
keyword: `%${searchKeyword}%`, keyword: `%${searchKeyword}%`,
}); }
qb.orWhere(searchKeyword ? `order_number like '%${keyword}%'` : "1=1", { );
qb.orWhere(
searchKeyword ? `order_number like '%${keyword}%'` : "1=1",
{
keyword: `%${searchKeyword}%`, keyword: `%${searchKeyword}%`,
}); }
}), );
})
); );
if (sortBy) { if (sortBy) {
if (sortBy === "position_line") { if (sortBy === "position_line") {
query = query.orderBy(`personal.positionName`, descending ? "DESC" : "ASC"); query = query.orderBy(
`personal.positionName`,
descending ? "DESC" : "ASC"
);
} else if (sortBy === "position_level") { } else if (sortBy === "position_level") {
query = query.orderBy(`personal.positionLevelName`, descending ? "DESC" : "ASC"); query = query.orderBy(
`personal.positionLevelName`,
descending ? "DESC" : "ASC"
);
} else if (sortBy === "position_type") { } else if (sortBy === "position_type") {
query = query.orderBy(`personal.positionTypeName`, descending ? "DESC" : "ASC"); query = query.orderBy(
`personal.positionTypeName`,
descending ? "DESC" : "ASC"
);
} else if (sortBy === "name") { } else if (sortBy === "name") {
query = query query = query
.orderBy(`personal.prefixName`, descending ? "DESC" : "ASC") .orderBy(`personal.prefixName`, descending ? "DESC" : "ASC")
.addOrderBy(`personal.firstName`, descending ? "DESC" : "ASC") .addOrderBy(`personal.firstName`, descending ? "DESC" : "ASC")
.addOrderBy(`personal.lastName`, descending ? "DESC" : "ASC"); .addOrderBy(`personal.lastName`, descending ? "DESC" : "ASC");
} else { } else {
query = query.orderBy(`personal.${sortBy}`, descending ? "DESC" : "ASC"); query = query.orderBy(
`personal.${sortBy}`,
descending ? "DESC" : "ASC"
);
} }
} else { } else {
query = query.orderBy("updatedAt", "DESC"); query = query.orderBy("updatedAt", "DESC");
@ -299,7 +350,10 @@ export class PersonalController extends Controller {
.getManyAndCount(); .getManyAndCount();
if (!lists) { if (!lists) {
throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "ไม่สามารถแสดงข้อมูลได้"); throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถแสดงข้อมูลได้"
);
} }
let result: any = []; let result: any = [];
@ -312,7 +366,8 @@ export class PersonalController extends Controller {
await result.push({ await result.push({
personal_id: lists[i].personal_id, personal_id: lists[i].personal_id,
ordering: i + 1, ordering: i + 1,
name: lists[i].prefixName + lists[i].firstName + " " + lists[i].lastName, name:
lists[i].prefixName + lists[i].firstName + " " + lists[i].lastName,
prefixName: lists[i].prefixName, prefixName: lists[i].prefixName,
firstName: lists[i].firstName, firstName: lists[i].firstName,
lastName: lists[i].lastName, lastName: lists[i].lastName,
@ -357,7 +412,11 @@ export class PersonalController extends Controller {
} catch (error: any) { } catch (error: any) {
if (error instanceof HttpError) { if (error instanceof HttpError) {
throw error; throw error;
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message); } else
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
error.message
);
} }
} }
@ -368,10 +427,18 @@ export class PersonalController extends Controller {
* *
*/ */
@Get("") @Get("")
async GetPersonal(@Request() request: RequestWithUser, @Query() personal_id: string) { async GetPersonal(
@Request() request: RequestWithUser,
@Query() personal_id: string
) {
try { try {
let _workflow = await new permission().Workflow(request, personal_id, "SYS_PROBATION"); let _workflow = await new permission().Workflow(
if (_workflow == false) await new permission().PermissionGet(request, "SYS_PROBATION"); request,
personal_id,
"SYS_PROBATION"
);
if (_workflow == false)
await new permission().PermissionGet(request, "SYS_PROBATION");
const person = await this.personalRepository.findOne({ const person = await this.personalRepository.findOne({
where: { personal_id: personal_id }, where: { personal_id: personal_id },
}); });
@ -400,7 +467,11 @@ export class PersonalController extends Controller {
} catch (error: any) { } catch (error: any) {
if (error instanceof HttpError) { if (error instanceof HttpError) {
throw error; throw error;
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message); } else
throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR,
error.message
);
} }
} }
} }

View file

@ -1,54 +1,75 @@
import axios from "axios" import axios from "axios";
import { RequestWithUser } from "../middlewares/user" import { RequestWithUser } from "../middlewares/user";
import CallAPI from "./call-api" import CallAPI from "./call-api";
import HttpError from "./http-error" import HttpError from "./http-error";
import HttpStatus from "./http-status" import HttpStatus from "./http-status";
import { promisify } from "util" import { promisify } from "util";
class CheckAuth { class CheckAuth {
private redis = require("redis") private redis = require("redis");
public async Permission(req: RequestWithUser, system: string, action: string) { public async Permission(
if (req.headers.hasOwnProperty("api_key") && req.headers["api_key"] && req.headers["api_key"] == process.env.API_KEY) { req: RequestWithUser,
return null system: string,
action: string
) {
if (
req.headers.hasOwnProperty("api_key") &&
req.headers["api_key"] &&
req.headers["api_key"] == process.env.API_KEY
) {
return null;
} }
return await new CallAPI() return await new CallAPI()
.GetData(req, "/org/permission") .GetData(req, "/org/permission")
.then(x => { .then((x) => {
let permission = false let permission = false;
let role = x.roles.find((x: any) => x.authSysId == system) let role = x.roles.find((x: any) => x.authSysId == system);
if (!role) throw "ไม่มีสิทธิ์เข้าระบบ" if (!role) throw "ไม่มีสิทธิ์เข้าระบบ";
if (role.attrOwnership == "OWNER") return "OWNER" if (role.attrOwnership == "OWNER") return "OWNER";
if (action.trim().toLocaleUpperCase() == "CREATE") permission = role.attrIsCreate if (action.trim().toLocaleUpperCase() == "CREATE")
if (action.trim().toLocaleUpperCase() == "DELETE") permission = role.attrIsDelete permission = role.attrIsCreate;
if (action.trim().toLocaleUpperCase() == "GET") permission = role.attrIsGet if (action.trim().toLocaleUpperCase() == "DELETE")
if (action.trim().toLocaleUpperCase() == "LIST") permission = role.attrIsList permission = role.attrIsDelete;
if (action.trim().toLocaleUpperCase() == "UPDATE") permission = role.attrIsUpdate if (action.trim().toLocaleUpperCase() == "GET")
if (permission == false) throw "ไม่มีสิทธิ์ใช้งานระบบนี้" permission = role.attrIsGet;
return role.attrPrivilege if (action.trim().toLocaleUpperCase() == "LIST")
permission = role.attrIsList;
if (action.trim().toLocaleUpperCase() == "UPDATE")
permission = role.attrIsUpdate;
if (permission == false) throw "ไม่มีสิทธิ์ใช้งานระบบนี้";
return role.attrPrivilege;
}) })
.catch(x => { .catch((x) => {
if (x.status != undefined) { if (x.status != undefined) {
throw new HttpError(x.status, x.message) throw new HttpError(x.status, x.message);
} else { } else {
throw new HttpError(HttpStatus.FORBIDDEN, x) throw new HttpError(HttpStatus.FORBIDDEN, x);
} }
}) });
} }
public async PermissionOrg(req: RequestWithUser, system: string, action: string) { public async PermissionOrg(
if (req.headers.hasOwnProperty("api_key") && req.headers["api_key"] && req.headers["api_key"] == process.env.API_KEY) { req: RequestWithUser,
system: string,
action: string
) {
if (
req.headers.hasOwnProperty("api_key") &&
req.headers["api_key"] &&
req.headers["api_key"] == process.env.API_KEY
) {
return { return {
root: null, root: null,
child1: null, child1: null,
child2: null, child2: null,
child3: null, child3: null,
child4: null, child4: null,
} };
} }
return await new CallAPI() return await new CallAPI()
.GetData(req, `/org/permission/org/${system}/${action}`) .GetData(req, `/org/permission/org/${system}/${action}`)
.then(async x => { .then(async (x) => {
let privilege = x.privilege let privilege = x.privilege;
let data: any = { let data: any = {
root: [null], root: [null],
@ -57,16 +78,16 @@ class CheckAuth {
child3: [null], child3: [null],
child4: [null], child4: [null],
privilege: [null], privilege: [null],
} };
let node = 4 let node = 4;
if (x.orgChild1Id == null) { if (x.orgChild1Id == null) {
node = 0 node = 0;
} else if (x.orgChild2Id == null) { } else if (x.orgChild2Id == null) {
node = 1 node = 1;
} else if (x.orgChild3Id == null) { } else if (x.orgChild3Id == null) {
node = 2 node = 2;
} else if (x.orgChild4Id == null) { } else if (x.orgChild4Id == null) {
node = 3 node = 3;
} }
if (privilege == "OWNER") { if (privilege == "OWNER") {
data = { data = {
@ -76,7 +97,7 @@ class CheckAuth {
child3: null, child3: null,
child4: null, child4: null,
privilege: "OWNER", privilege: "OWNER",
} };
} else if (privilege == "ROOT") { } else if (privilege == "ROOT") {
data = { data = {
root: [x.orgRootId], root: [x.orgRootId],
@ -85,7 +106,16 @@ class CheckAuth {
child3: null, child3: null,
child4: null, child4: null,
privilege: "ROOT", privilege: "ROOT",
} };
} else if (privilege == "PARENT") {
data = {
root: [x.orgRootId],
child1: [null],
child2: null,
child3: null,
child4: null,
privilege: "PARENT",
};
} else if (privilege == "CHILD") { } else if (privilege == "CHILD") {
data = { data = {
root: node >= 0 ? [x.orgRootId] : null, root: node >= 0 ? [x.orgRootId] : null,
@ -94,7 +124,16 @@ class CheckAuth {
child3: node >= 3 ? [x.orgChild3Id] : null, child3: node >= 3 ? [x.orgChild3Id] : null,
child4: node >= 4 ? [x.orgChild4Id] : null, child4: node >= 4 ? [x.orgChild4Id] : null,
privilege: "CHILD", privilege: "CHILD",
} };
} else if (privilege == "BROTHER") {
data = {
// root: node >= 0 ? [x.orgRootId] : null,
root: node >= 0 ? [x.orgRootId] : null,
child1: node >= 2 ? [x.orgChild1Id] : null,
child2: node >= 3 ? [x.orgChild2Id] : null,
child3: node >= 4 ? [x.orgChild3Id] : null,
privilege: "BROTHER",
};
} else if (privilege == "NORMAL") { } else if (privilege == "NORMAL") {
data = { data = {
root: [x.orgRootId], root: [x.orgRootId],
@ -103,80 +142,98 @@ class CheckAuth {
child3: [x.orgChild3Id], child3: [x.orgChild3Id],
child4: [x.orgChild4Id], child4: [x.orgChild4Id],
privilege: "NORMAL", privilege: "NORMAL",
} };
} else if (privilege == "SPECIFIC") { } else if (privilege == "SPECIFIC") {
} }
return data return data;
}) })
.catch(x => { .catch((x) => {
if (x.status != undefined) { if (x.status != undefined) {
throw new HttpError(x.status, x.message) throw new HttpError(x.status, x.message);
} else { } else {
throw new HttpError(HttpStatus.FORBIDDEN, x) throw new HttpError(HttpStatus.FORBIDDEN, x);
} }
}) });
} }
public async PermissionOrgByUser(req: RequestWithUser, system: string, action: string, profileId: string) { public async PermissionOrgByUser(
if (req.headers.hasOwnProperty("api_key") && req.headers["api_key"] && req.headers["api_key"] == process.env.API_KEY) { req: RequestWithUser,
return true system: string,
action: string,
profileId: string
) {
if (
req.headers.hasOwnProperty("api_key") &&
req.headers["api_key"] &&
req.headers["api_key"] == process.env.API_KEY
) {
return true;
} }
return await new CallAPI() return await new CallAPI()
.GetData(req, `/org/permission/user/${system}/${action}/${profileId}`) .GetData(req, `/org/permission/user/${system}/${action}/${profileId}`)
.then(async x => { .then(async (x) => {
let org = x.org let org = x.org;
if (org.root != null) if (x.orgRootId != org.root[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล" if (org.root != null)
if (org.child1 != null) if (x.orgChild1Id != org.child1[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล" if (x.orgRootId != org.root[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล";
if (org.child2 != null) if (x.orgChild2Id != org.child2[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล" if (org.child1 != null)
if (org.child3 != null) if (x.orgChild3Id != org.child3[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล" if (x.orgChild1Id != org.child1[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล";
if (org.child4 != null) if (x.orgChild4Id != org.child4[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล" if (org.child2 != null)
if (x.orgChild2Id != org.child2[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล";
if (org.child3 != null)
if (x.orgChild3Id != org.child3[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล";
if (org.child4 != null)
if (x.orgChild4Id != org.child4[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล";
return true return true;
}) })
.catch(x => { .catch((x) => {
if (x.status != undefined) { if (x.status != undefined) {
throw new HttpError(x.status, x.message) throw new HttpError(x.status, x.message);
} else { } else {
throw new HttpError(HttpStatus.FORBIDDEN, x) throw new HttpError(HttpStatus.FORBIDDEN, x);
} }
}) });
} }
public async Workflow(req: RequestWithUser, id: string, sysName: string) { public async Workflow(req: RequestWithUser, id: string, sysName: string) {
if (req.headers.hasOwnProperty("api_key") && req.headers["api_key"] && req.headers["api_key"] == process.env.API_KEY) { if (
return null req.headers.hasOwnProperty("api_key") &&
req.headers["api_key"] &&
req.headers["api_key"] == process.env.API_KEY
) {
return null;
} }
return await new CallAPI() return await new CallAPI()
.PostData(req, "/org/workflow/keycloak/isofficer", { .PostData(req, "/org/workflow/keycloak/isofficer", {
refId: id, refId: id,
sysName: sysName, sysName: sysName,
}) })
.then(x => { .then((x) => {
return true return true;
})
.catch(x => {
return false
}) })
.catch((x) => {
return false;
});
} }
public async checkOrg(token: any, keycloakId: string) { public async checkOrg(token: any, keycloakId: string) {
const redisClient = await this.redis.createClient({ const redisClient = await this.redis.createClient({
host: process.env.REDIS_HOST, host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT, port: process.env.REDIS_PORT,
}) });
const getAsync = promisify(redisClient.get).bind(redisClient) const getAsync = promisify(redisClient.get).bind(redisClient);
try { try {
let reply = await getAsync("org_" + keycloakId) let reply = await getAsync("org_" + keycloakId);
if (reply != null) { if (reply != null) {
reply = JSON.parse(reply) reply = JSON.parse(reply);
} else { } else {
if (!keycloakId) throw new Error("No KeycloakId provided") if (!keycloakId) throw new Error("No KeycloakId provided");
const x = await new CallAPI().GetData( const x = await new CallAPI().GetData(
{ {
headers: { authorization: token }, headers: { authorization: token },
}, },
`/org/permission/checkOrg/${keycloakId}`, `/org/permission/checkOrg/${keycloakId}`,
false false
) );
const data = { const data = {
orgRootId: x.orgRootId, orgRootId: x.orgRootId,
@ -184,62 +241,82 @@ class CheckAuth {
orgChild2Id: x.orgChild2Id, orgChild2Id: x.orgChild2Id,
orgChild3Id: x.orgChild3Id, orgChild3Id: x.orgChild3Id,
orgChild4Id: x.orgChild4Id, orgChild4Id: x.orgChild4Id,
} };
return data return data;
} }
} catch (error) { } catch (error) {
console.error("Error calling API:", error) console.error("Error calling API:", error);
throw error throw error;
} }
} }
public async PermissionCreate(req: RequestWithUser, system: string) { public async PermissionCreate(req: RequestWithUser, system: string) {
return await this.Permission(req, system, "CREATE") return await this.Permission(req, system, "CREATE");
} }
public async PermissionDelete(req: RequestWithUser, system: string) { public async PermissionDelete(req: RequestWithUser, system: string) {
return await this.Permission(req, system, "DELETE") return await this.Permission(req, system, "DELETE");
} }
public async PermissionGet(req: RequestWithUser, system: string) { public async PermissionGet(req: RequestWithUser, system: string) {
return await this.Permission(req, system, "GET") return await this.Permission(req, system, "GET");
} }
public async PermissionList(req: RequestWithUser, system: string) { public async PermissionList(req: RequestWithUser, system: string) {
return await this.Permission(req, system, "LIST") return await this.Permission(req, system, "LIST");
} }
public async PermissionUpdate(req: RequestWithUser, system: string) { public async PermissionUpdate(req: RequestWithUser, system: string) {
return await this.Permission(req, system, "UPDATE") return await this.Permission(req, system, "UPDATE");
} }
public async PermissionOrgCreate(req: RequestWithUser, system: string) { public async PermissionOrgCreate(req: RequestWithUser, system: string) {
return await this.PermissionOrg(req, system, "CREATE") return await this.PermissionOrg(req, system, "CREATE");
} }
public async PermissionOrgDelete(req: RequestWithUser, system: string) { public async PermissionOrgDelete(req: RequestWithUser, system: string) {
return await this.PermissionOrg(req, system, "DELETE") return await this.PermissionOrg(req, system, "DELETE");
} }
public async PermissionOrgGet(req: RequestWithUser, system: string) { public async PermissionOrgGet(req: RequestWithUser, system: string) {
return await this.PermissionOrg(req, system, "GET") return await this.PermissionOrg(req, system, "GET");
} }
public async PermissionOrgList(req: RequestWithUser, system: string) { public async PermissionOrgList(req: RequestWithUser, system: string) {
return await this.PermissionOrg(req, system, "LIST") return await this.PermissionOrg(req, system, "LIST");
} }
public async PermissionOrgUpdate(req: RequestWithUser, system: string) { public async PermissionOrgUpdate(req: RequestWithUser, system: string) {
return await this.PermissionOrg(req, system, "UPDATE") return await this.PermissionOrg(req, system, "UPDATE");
} }
public async PermissionOrgUserCreate(req: RequestWithUser, system: string, profileId: string) { public async PermissionOrgUserCreate(
return await this.PermissionOrgByUser(req, system, "CREATE", profileId) req: RequestWithUser,
system: string,
profileId: string
) {
return await this.PermissionOrgByUser(req, system, "CREATE", profileId);
} }
public async PermissionOrgUserDelete(req: RequestWithUser, system: string, profileId: string) { public async PermissionOrgUserDelete(
return await this.PermissionOrgByUser(req, system, "DELETE", profileId) req: RequestWithUser,
system: string,
profileId: string
) {
return await this.PermissionOrgByUser(req, system, "DELETE", profileId);
} }
public async PermissionOrgUserGet(req: RequestWithUser, system: string, profileId: string) { public async PermissionOrgUserGet(
return await this.PermissionOrgByUser(req, system, "GET", profileId) req: RequestWithUser,
system: string,
profileId: string
) {
return await this.PermissionOrgByUser(req, system, "GET", profileId);
} }
public async PermissionOrgUserList(req: RequestWithUser, system: string, profileId: string) { public async PermissionOrgUserList(
return await this.PermissionOrgByUser(req, system, "LIST", profileId) req: RequestWithUser,
system: string,
profileId: string
) {
return await this.PermissionOrgByUser(req, system, "LIST", profileId);
} }
public async PermissionOrgUserUpdate(req: RequestWithUser, system: string, profileId: string) { public async PermissionOrgUserUpdate(
return await this.PermissionOrgByUser(req, system, "UPDATE", profileId) req: RequestWithUser,
system: string,
profileId: string
) {
return await this.PermissionOrgByUser(req, system, "UPDATE", profileId);
} }
} }
export default CheckAuth export default CheckAuth;