From af3e0abd2a7aaef8fe99da5e8bd0ee35d169e56a Mon Sep 17 00:00:00 2001 From: mamoss <> Date: Mon, 24 Mar 2025 21:57:50 +0700 Subject: [PATCH] add permission profile --- src/controllers/ImportDataController.ts | 105 ++- .../PermissionProfileController.ts | 599 ++++++++++++++++++ src/entities/OFFICER.ts | 26 +- src/entities/OrgRoot.ts | 6 +- src/entities/PermissionProfile.ts | 27 + 5 files changed, 725 insertions(+), 38 deletions(-) create mode 100644 src/controllers/PermissionProfileController.ts create mode 100644 src/entities/PermissionProfile.ts diff --git a/src/controllers/ImportDataController.ts b/src/controllers/ImportDataController.ts index bc66ea3d..cd55cca6 100644 --- a/src/controllers/ImportDataController.ts +++ b/src/controllers/ImportDataController.ts @@ -340,24 +340,24 @@ export class ImportDataController extends Controller { .select(["profile.citizenId", "profile.id"]) .orderBy("profile.citizenId", "ASC") // .where("profile.citizenId = '3101702379675'") - .where({ - citizenId: In([ - // "1100600109451", - // "1209900075508", - // "1739900231556", - // "1809900305214", - // "1920600228762", - // "3101600963742", - // "3102401171243", - // "3120100454406", - // "3180100306172", - // "3700100094722", - // "3809900116957", - "3940900213929", - ]), - }) - // .skip(10000) - // .take(20000) + // .where({ + // citizenId: In([ + // // "1100600109451", + // // "1209900075508", + // // "1739900231556", + // // "1809900305214", + // // "1920600228762", + // // "3101600963742", + // // "3102401171243", + // // "3120100454406", + // // "3180100306172", + // // "3700100094722", + // // "3809900116957", + // "3940900213929", + // ]), + // }) + .skip(0) + .take(10000) .getManyAndCount(); var _profiles: ProfileSalary[] = []; const filePath = path.join(__dirname, "salaryProfile1.csv"); @@ -1956,7 +1956,8 @@ export class ImportDataController extends Controller { @Post("ImportOrg") async ImportOrg(@Request() request: { user: Record }) { const orgRevision = await this.orgRevisionRepo.findOne({ - where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + // where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + where: { id: "30e594c9-a65b-485e-a4c5-71aa497e6b8a" }, }); if (orgRevision == null) return new HttpSuccess(); //create root @@ -2005,7 +2006,7 @@ export class ImportDataController extends Controller { for (const item of IMPORT_CHILD1) { const orgChild1 = new OrgChild1(); let orgRoot = await this.orgRootRepo.findOne({ - where: { orgRootName: item.orgRoot }, + where: { orgRootName: item.orgRoot, orgRevisionId: orgRevision.id }, relations: ["orgChild1s"], }); if (orgRoot == null) { @@ -2067,7 +2068,7 @@ export class ImportDataController extends Controller { for (const item of IMPORT_CHILD2) { const orgChild2 = new OrgChild2(); let orgRoot = await this.orgRootRepo.findOne({ - where: { orgRootName: item.orgRoot }, + where: { orgRootName: item.orgRoot, orgRevisionId: orgRevision.id }, relations: ["orgChild1s"], }); if (orgRoot == null) { @@ -2093,7 +2094,11 @@ export class ImportDataController extends Controller { await this.orgRootRepo.save(orgRoot); } let orgChild1 = await this.orgChild1Repo.findOne({ - where: { orgChild1Name: item.orgChild1 }, + where: { + orgChild1Name: item.orgChild1, + orgRootId: orgRoot.id, + orgRevisionId: orgRevision.id, + }, relations: ["orgChild2s"], }); if (orgChild1 == null) { @@ -2159,7 +2164,7 @@ export class ImportDataController extends Controller { for (const item of IMPORT_CHILD3) { const orgChild3 = new OrgChild3(); let orgRoot = await this.orgRootRepo.findOne({ - where: { orgRootName: item.orgRoot }, + where: { orgRootName: item.orgRoot, orgRevisionId: orgRevision.id }, relations: ["orgChild1s"], }); if (orgRoot == null) { @@ -2185,7 +2190,11 @@ export class ImportDataController extends Controller { await this.orgRootRepo.save(orgRoot); } let orgChild1 = await this.orgChild1Repo.findOne({ - where: { orgChild1Name: item.orgChild1 }, + where: { + orgChild1Name: item.orgChild1, + orgRootId: orgRoot.id, + orgRevisionId: orgRevision.id, + }, relations: ["orgChild2s"], }); if (orgChild1 == null) { @@ -2214,7 +2223,12 @@ export class ImportDataController extends Controller { await this.orgChild1Repo.save(orgChild1); } let orgChild2 = await this.orgChild2Repo.findOne({ - where: { orgChild2Name: item.orgChild2 }, + where: { + orgChild2Name: item.orgChild2, + orgChild1Id: orgChild1.id, + orgRootId: orgRoot.id, + orgRevisionId: orgRevision.id, + }, relations: ["orgChild3s"], }); if (orgChild2 == null) { @@ -2277,7 +2291,7 @@ export class ImportDataController extends Controller { async MapPositionOfficer(@Request() request: { user: Record }) { const officer = await AppDataSource.getRepository(Profile) .createQueryBuilder("profile") - .leftJoinAndSelect("profile.current_holders", "posMaster") + .leftJoinAndSelect("profile.next_holders", "posMaster") // .where({citizenId: "1720700018869"}) .where("posMaster.id IS NULL") .getMany(); @@ -2286,7 +2300,8 @@ export class ImportDataController extends Controller { let type_: any = null; let level_: any = null; const orgRevision = await this.orgRevisionRepo.findOne({ - where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + // where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + where: { id: "30e594c9-a65b-485e-a4c5-71aa497e6b8a" }, }); if (orgRevision == null) return new HttpSuccess(); for await (const _item of officer) { @@ -2380,8 +2395,9 @@ export class ImportDataController extends Controller { posMaster.isStaff = false; posMaster.isDirector = false; posMaster.isSit = false; - posMaster.current_holderId = _item.id; - posMaster.posMasterNo = item.POS_NUM_CODE == "" || item.POS_NUM_CODE == null ? _null : Number(item.POS_NUM_CODE); + posMaster.next_holderId = _item.id; + posMaster.posMasterNo = + item.POS_NUM_CODE == "" || item.POS_NUM_CODE == null ? _null : Number(item.POS_NUM_CODE); posMaster.orgRevisionId = orgRevision.id; posMaster.posMasterCreatedAt = new Date(); posMaster.createdUserId = request.user.sub; @@ -2590,10 +2606,13 @@ export class ImportDataController extends Controller { @Post("update-amount") async UpdateAmount(@Request() request: { user: Record }) { let rowCount = 0; - const profile:any = await this.profileRepo.find({where: { amount: LessThanOrEqual(0) }}); + const profile: any = await this.profileRepo.find({ where: { amount: LessThanOrEqual(0) } }); for await (const _item of profile) { - const OFFICER = await this.OFFICERRepo.findOne({ select: ["CIT", "SALARY"], where: { CIT: _item.citizenId } }); + const OFFICER = await this.OFFICERRepo.findOne({ + select: ["CIT", "SALARY"], + where: { CIT: _item.citizenId }, + }); if (OFFICER) { rowCount++; _item.amount = Number(OFFICER.SALARY); @@ -2603,4 +2622,28 @@ export class ImportDataController extends Controller { } return new HttpSuccess(); } + + /** + * @summary เงินเดือน ข้าราชการ + */ + @Post("update-profileSalary") + async UpdateProfileSalary(@Request() request: { user: Record }) { + let rowCount = 0; + const profileSalarys: any = await this.salaryRepo.find(); + + for await (const _item of profileSalarys) { + const OFFICER = await this.OFFICERRepo.findOne({ + select: ["ID", "POS_NUM_CODE_SIT", "POS_NUM_CODE_SIT_ABB"], + where: { ID: _item.refId }, + }); + if (OFFICER) { + rowCount++; + _item.posNumCodeSit = OFFICER.POS_NUM_CODE_SIT; + _item.posNumCodeSitAbb = OFFICER.POS_NUM_CODE_SIT_ABB; + await this.profileRepo.save(_item); + console.log(rowCount); + } + } + return new HttpSuccess(); + } } diff --git a/src/controllers/PermissionProfileController.ts b/src/controllers/PermissionProfileController.ts new file mode 100644 index 00000000..3dfa263b --- /dev/null +++ b/src/controllers/PermissionProfileController.ts @@ -0,0 +1,599 @@ +import { + Controller, + Post, + Delete, + Route, + Security, + Tags, + Body, + Path, + Request, + Response, + Get, + Query, +} from "tsoa"; +import { AppDataSource } from "../database/data-source"; +import HttpSuccess from "../interfaces/http-success"; +import HttpStatusCode from "../interfaces/http-status"; +import HttpError from "../interfaces/http-error"; +import { OrgRoot } from "../entities/OrgRoot"; +import { OrgRevision } from "../entities/OrgRevision"; +import { RequestWithUser } from "../middlewares/user"; +import { PermissionProfile } from "../entities/PermissionProfile"; +import { Profile } from "../entities/Profile"; +import HttpStatus from "../interfaces/http-status"; +import { PosMaster } from "../entities/PosMaster"; +import { setLogDataDiff } from "../interfaces/utils"; +import { Brackets, In } from "typeorm"; + +@Route("api/v1/org/permission-profile") +@Tags("PermissionProfile") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +export class PermissionProfileController extends Controller { + private orgRootRepository = AppDataSource.getRepository(OrgRoot); + private profileRepository = AppDataSource.getRepository(Profile); + private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); + private permissionProfileRepository = AppDataSource.getRepository(PermissionProfile); + private posMasterRepository = AppDataSource.getRepository(PosMaster); + private profileRepo = AppDataSource.getRepository(Profile); + + /** + * API หาสำนักทั้งหมดแบบร่าง + * + * @summary หาสำนักทั้งหมดแบบร่าง + * + */ + @Get() + async GetActiveRootIdAdmin(@Request() request: RequestWithUser) { + const orgRevisionActive = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent: false, orgRevisionIsDraft: true }, + relations: ["posMasters"], + }); + if (!orgRevisionActive) return new HttpSuccess([]); + let rootId: any = null; + if (!request.user.role.includes("SUPER_ADMIN")) { + const profile = await this.profileRepo.findOne({ + where: { + keycloak: request.user.sub, + }, + }); + if (profile == null) return new HttpSuccess([]); + + if (!request.user.role.includes("SUPER_ADMIN")) { + rootId = + orgRevisionActive?.posMasters?.filter((x) => x.next_holderId == profile.id)[0] + ?.orgRootId || null; + if (!rootId) return new HttpSuccess([]); + } + } + const data = await AppDataSource.getRepository(OrgRoot) + .createQueryBuilder("orgRoot") + .where("orgRoot.orgRevisionId = :id", { id: orgRevisionActive.id }) + .andWhere(rootId != null ? `orgRoot.id = :rootId` : "1=1", { + rootId: rootId, + }) + .orderBy("orgRoot.orgRootOrder", "ASC") + .getMany(); + const _data = data.map((x) => ({ + ...x, + labelName: `${x.orgRootName} ${x.orgRootCode} ${x.orgRootShortName}`, + })); + return new HttpSuccess(_data); + } + + @Get("profile") + async listProfile( + @Request() request: RequestWithUser, + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Query() + searchField?: "fullName" | "position" | "posNo" | "postype" | "poslevel", + @Query() searchKeyword: string = "", + ) { + // if (!request.user.role.includes("SUPER_ADMIN")) { + // throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์ใช้งานระบบนี้"); + // } + let queryLike = + "CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword"; + if (searchField == "postype") { + queryLike = "posType.posTypeName LIKE :keyword"; + } else if (searchField == "poslevel") { + queryLike = "posLevel.posLevelName LIKE :keyword"; + } else if (searchField == "position") { + queryLike = "profile.position LIKE :keyword"; + } else if (searchField == "posNo") { + queryLike = ` + CASE + WHEN current_holders.orgChild4Id IS NOT NULL THEN CONCAT(orgChild4.orgChild4ShortName, current_holders.posMasterNo) + WHEN current_holders.orgChild3Id IS NOT NULL THEN CONCAT(orgChild3.orgChild3ShortName, current_holders.posMasterNo) + WHEN current_holders.orgChild2Id IS NOT NULL THEN CONCAT(orgChild2.orgChild2ShortName, current_holders.posMasterNo) + WHEN current_holders.orgChild1Id IS NOT NULL THEN CONCAT(orgChild1.orgChild1ShortName, current_holders.posMasterNo) + ELSE CONCAT(orgRoot.orgRootShortName, current_holders.posMasterNo) + END LIKE :keyword + `; + } + const findRevision = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + }); + const orgRevisionId = findRevision?.id; + if (!findRevision) { + throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); + } + // console.log(findRevision.id); + + const [record, total] = await this.profileRepository + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.positions", "positions") + .leftJoinAndSelect("positions.posExecutive", "posExecutive") + .leftJoinAndSelect("current_holders.orgRevision", "orgRevision") + .leftJoinAndSelect("current_holders.orgRoot", "orgRoot") + .leftJoinAndSelect("current_holders.orgChild1", "orgChild1") + .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") + .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") + .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") + .where("current_holders.orgRevisionId = :orgRevisionId", { orgRevisionId }) + .andWhere( + new Brackets((qb) => { + // qb.andWhere(`current_holders.orgRevision = :orgRevisionId`, { + // orgRevisionId: findRevision.id, + // }) + qb.andWhere( + searchKeyword != undefined && searchKeyword != null && searchKeyword != "" + ? queryLike + : "1=1", + { + keyword: `%${searchKeyword}%`, + }, + ); + }), + ) + .orderBy("current_holders.posMasterNo", "ASC") + .skip((page - 1) * pageSize) + .take(pageSize) + .getManyAndCount(); + + const data = await Promise.all( + record.map((_data) => { + const shortName = + _data.current_holders.length == 0 + ? null + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 != + null + ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild3 != null + ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild2 != null + ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild1 != null + ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != + null && + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgRoot != null + ? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : null; + const root = + _data.current_holders.length == 0 || + (_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null) + ? null + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; + + const child1 = + _data.current_holders == null || + _data.current_holders.length == 0 || + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1; + + const child2 = + _data.current_holders == null || + _data.current_holders.length == 0 || + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2; + + const child3 = + _data.current_holders == null || + _data.current_holders.length == 0 || + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3; + + const child4 = + _data.current_holders == null || + _data.current_holders.length == 0 || + _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4; + + let _child1 = child1 == null ? "" : `${child1.orgChild1Name}/`; + let _child2 = child2 == null ? "" : `${child2.orgChild2Name}/`; + let _child3 = child3 == null ? "" : `${child3.orgChild3Name}/`; + let _child4 = child4 == null ? "" : `${child4.orgChild4Name}/`; + + return { + id: _data.id, + avatar: _data.avatar, + avatarName: _data.avatarName, + prefix: _data.prefix, + rank: _data.rank, + firstName: _data.firstName, + lastName: _data.lastName, + org: `${_child4}${_child3}${_child2}${_child1}${root?.orgRootName ?? ""}`, + posNo: shortName, + position: _data.position, + posType: _data.posType == null ? null : _data.posType.posTypeName, + posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName, + }; + }), + ); + + return new HttpSuccess({ data: data, total }); + } + + /** + * API รายละเอียดรายการสิทธิ์โครงสร้าง + * + * @summary - CRUD สิทธิ์โครงสร้าง (ADMIN) + * + * @param {string} id Id โครงสร้าง + */ + @Post("profile") + async GetById( + @Request() request: RequestWithUser, + @Body() + requestBody: { + id: string | null; + page: number; + pageSize: number; + searchField?: "fullName" | "position" | "posNo" | "postype" | "poslevel"; + searchKeyword: string; + }, + ) { + // if (!request.user.role.includes("SUPER_ADMIN")) { + // throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์ใช้งานระบบนี้"); + // } + let profiles: any = []; + if (requestBody.id != null) { + const _permissionProfile = await this.orgRootRepository.findOne({ + where: { id: requestBody.id }, + relations: ["permissionProfileRoots", "permissionProfileRoots.profileTree"], + }); + if (!_permissionProfile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์โครงสร้างนี้"); + } + profiles = await _permissionProfile.permissionProfileRoots.map((x) => x.profileId); + } else { + const _permissionProfile = await this.permissionProfileRepository.find(); + profiles = await _permissionProfile.map((x) => x.profileId); + } + + let queryLike = + "CONCAT(profileTree.prefix, profileTree.firstName, ' ', profileTree.lastName) LIKE :keyword"; + if (requestBody.searchField == "postype") { + queryLike = "posLevel.posLevelName LIKE :keyword"; + } else if (requestBody.searchField == "poslevel") { + queryLike = "posType.posTypeName LIKE :keyword"; + } else if (requestBody.searchField == "position") { + queryLike = "profileTree.position LIKE :keyword"; + } else if (requestBody.searchField == "posNo") { + queryLike = `CONCAT( + IFNULL(orgChild4.orgChild4ShortName, ''), + IFNULL(current_holders.posMasterNo , '') + ) LIKE :keyword OR CONCAT( + IFNULL(orgChild3.orgChild3ShortName, ''), + IFNULL(current_holders.posMasterNo , '') + ) LIKE :keyword OR CONCAT( + IFNULL(orgChild2.orgChild2ShortName, ''), + IFNULL(current_holders.posMasterNo , '') + ) LIKE :keyword OR CONCAT( + IFNULL(orgChild1.orgChild1ShortName, ''), + IFNULL(current_holders.posMasterNo , '') + ) LIKE :keyword OR CONCAT( + IFNULL(orgRoot.orgRootShortName, ''), + IFNULL(current_holders.posMasterNo , '') + ) LIKE :keyword`; + } + const findRevision = await this.orgRevisionRepository.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, + }); + if (!findRevision) { + throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); + } + const [record, total] = await this.permissionProfileRepository + .createQueryBuilder("permissionProfile") + .leftJoinAndSelect("permissionProfile.orgRootTree", "orgRootTree") + .leftJoinAndSelect("permissionProfile.profileTree", "profileTree") + .leftJoinAndSelect("profileTree.posLevel", "posLevel") + .leftJoinAndSelect("profileTree.posType", "posType") + .leftJoinAndSelect("profileTree.current_holders", "current_holders") + .leftJoinAndSelect("current_holders.positions", "positions") + .leftJoinAndSelect("current_holders.orgRevision", "orgRevision") + .leftJoinAndSelect("current_holders.orgRoot", "orgRoot") + .leftJoinAndSelect("current_holders.orgChild1", "orgChild1") + .leftJoinAndSelect("current_holders.orgChild2", "orgChild2") + .leftJoinAndSelect("current_holders.orgChild3", "orgChild3") + .leftJoinAndSelect("current_holders.orgChild4", "orgChild4") + .where("current_holders.orgRevisionId = :orgRevisionId", { orgRevisionId: findRevision.id }) + .andWhere(requestBody.id == null ? "1=1" : `permissionProfile.orgRootId LIKE :rootId`, { + rootId: requestBody.id, + }) + .andWhere( + requestBody.searchKeyword != undefined && + requestBody.searchKeyword != null && + requestBody.searchKeyword != "" + ? queryLike + : "1=1", + { + keyword: `%${requestBody.searchKeyword}%`, + }, + ) + // .andWhere(`current_holders.orgRevisionId LIKE :orgRevisionId`, { + // orgRevisionId: findRevision.id, + // }) + .orderBy("current_holders.posMasterNo", "ASC") + .skip((requestBody.page - 1) * requestBody.pageSize) + .take(requestBody.pageSize) + .getManyAndCount(); + + const data = await Promise.all( + record.map((_data) => { + const shortName = + _data.profileTree.current_holders.length == 0 + ? null + : _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) != + null && + _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild4 != null + ? `${_data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName}${_data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) != + null && + _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild3 != null + ? `${_data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName}${_data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : _data.profileTree.current_holders.find( + (x) => x.orgRevisionId == findRevision.id, + ) != null && + _data.profileTree.current_holders.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgChild2 != null + ? `${_data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName}${_data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : _data.profileTree.current_holders.find( + (x) => x.orgRevisionId == findRevision.id, + ) != null && + _data.profileTree.current_holders.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgChild1 != null + ? `${_data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName}${_data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : _data.profileTree.current_holders.find( + (x) => x.orgRevisionId == findRevision.id, + ) != null && + _data.profileTree.current_holders.find( + (x) => x.orgRevisionId == findRevision.id, + )?.orgRoot != null + ? `${_data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName}${_data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : null; + const root = + _data.profileTree.current_holders.length == 0 || + (_data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) != + null && + _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgRoot == null) + ? null + : _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgRoot; + + const child1 = + _data.profileTree.current_holders == null || + _data.profileTree.current_holders.length == 0 || + _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild1; + + const child2 = + _data.profileTree.current_holders == null || + _data.profileTree.current_holders.length == 0 || + _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild2; + + const child3 = + _data.profileTree.current_holders == null || + _data.profileTree.current_holders.length == 0 || + _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild3; + + const child4 = + _data.profileTree.current_holders == null || + _data.profileTree.current_holders.length == 0 || + _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null + ? null + : _data.profileTree.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild4; + + let _child1 = child1 == null ? "" : `${child1.orgChild1Name}/`; + let _child2 = child2 == null ? "" : `${child2.orgChild2Name}/`; + let _child3 = child3 == null ? "" : `${child3.orgChild3Name}/`; + let _child4 = child4 == null ? "" : `${child4.orgChild4Name}/`; + + return { + id: _data.id, + profileId: _data.profileId, + orgRootId: _data.orgRootId, + orgNew: _data.orgRootTree.orgRootName, + avatar: _data.profileTree.avatar, + avatarName: _data.profileTree.avatarName, + prefix: _data.profileTree.prefix, + rank: _data.profileTree.rank, + firstName: _data.profileTree.firstName, + lastName: _data.profileTree.lastName, + org: `${_child4}${_child3}${_child2}${_child1}${root?.orgRootName ?? ""}`, + posNo: shortName, + position: _data.profileTree.position, + posType: _data.profileTree.posType == null ? null : _data.profileTree.posType.posTypeName, + posLevel: + _data.profileTree.posLevel == null ? null : _data.profileTree.posLevel.posLevelName, + }; + }), + ); + return new HttpSuccess({ data, total }); + } + + /** + * API สร้างรายการ body สิทธิ์โครงสร้าง + * + * @summary - CRUD สิทธิ์โครงสร้าง (ADMIN) + * + */ + @Post() + async Post( + @Request() request: RequestWithUser, + @Body() requestBody: { nodeId: string; personId: string[] }, + ) { + // if (!request.user.role.includes("SUPER_ADMIN")) { + // throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์ใช้งานระบบนี้"); + // } + const orgRoot = await this.orgRootRepository.findOne({ + where: { id: requestBody.nodeId }, + }); + if (!orgRoot) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสำนัก"); + } + const profile = await this.profileRepository.find({ + where: { id: In(requestBody.personId) }, + }); + if (profile.length == 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัติ"); + } + + const checkDup = await this.permissionProfileRepository.findOne({ + where: { + orgRootTree: { id: requestBody.nodeId }, + profileTree: { id: In(requestBody.personId) }, + }, + }); + + if (checkDup) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "มีสิทธิ์นี้อยู่ในระบบแล้ว"); + } + const before = null; + // const _permissionProfile = new PermissionProfile(); + // _permissionProfile.orgRootTree = orgRoot; + // _permissionProfile.profileTree = profile; + // _permissionProfile.createdUserId = request.user.sub; + // _permissionProfile.createdFullName = request.user.name; + // _permissionProfile.lastUpdateUserId = request.user.sub; + // _permissionProfile.lastUpdateFullName = request.user.name; + // _permissionProfile.createdAt = new Date(); + // _permissionProfile.lastUpdatedAt = new Date(); + const _permissionProfile = profile.map((profile) => { + const permission = new PermissionProfile(); + permission.orgRootId = requestBody.nodeId; + permission.profileId = profile.id; + permission.createdUserId = request.user.sub; + permission.createdFullName = request.user.name; + permission.lastUpdateUserId = request.user.sub; + permission.lastUpdateFullName = request.user.name; + permission.createdAt = new Date(); + permission.lastUpdatedAt = new Date(); + return permission; + }); + + await this.permissionProfileRepository.save(_permissionProfile, { data: request }); + setLogDataDiff(request, { before, after: _permissionProfile }); + return new HttpSuccess(); + } + + /** + * API ลบรายการสิทธิ์โครงสร้าง + * + * @summary - CRUD สิทธิ์โครงสร้าง (ADMIN) + * + * @param {string} id Id สิทธิ์โครงสร้าง + */ + @Delete("{id}") + async Delete(@Request() req: RequestWithUser, @Path() id: string) { + // if (!request.user.role.includes("SUPER_ADMIN")) { + // throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์ใช้งานระบบนี้"); + // } + // const orgRoot = await this.orgRootRepository.findOne({ + // where: { id: nodeId }, + // relations: ["permissionProfileRoots"], + // }); + // if (!orgRoot) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสำนัก"); + // } + // const profile = await this.profileRepository.findOne({ + // where: { id: personId }, + // relations: ["permissionProfileRoots"], + // }); + // if (!profile) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัติ"); + // } + + const _delPermissionProfile = await this.permissionProfileRepository.findOne({ + where: { id: id }, + }); + + if (!_delPermissionProfile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบสิทธิ์นี้อยู่ในระบบแล้ว"); + } + + await this.permissionProfileRepository.remove(_delPermissionProfile, { data: req }); + return new HttpSuccess(); + } + + public async listAuthSysOrgFuncByRevisionId( + request: RequestWithUser, + system: string, + revisionId: string, + ) { + let profile = await this.profileRepo.findOne({ + where: { + keycloak: request.user.sub, + }, + relations: ["next_holders", "next_holders.authRole", "next_holders.authRole.authRoles"], + }); + if (!profile) { + return [null]; + } + + let attrOwnership = + profile?.next_holders + .filter((x) => x.orgRevisionId == revisionId)[0] + ?.authRole?.authRoles?.filter((x) => x.authSysId == system)[0]?.attrOwnership || null; + + const posMaster = await this.posMasterRepository.findOne({ + where: { + next_holderId: profile.id, + orgRevisionId: revisionId, + }, + }); + if (!posMaster) { + return [null]; + } else if (attrOwnership == "OWNER") { + return null; + } else { + return [posMaster.orgRootId]; + } + } +} diff --git a/src/entities/OFFICER.ts b/src/entities/OFFICER.ts index e0ffe3c7..2adac143 100644 --- a/src/entities/OFFICER.ts +++ b/src/entities/OFFICER.ts @@ -12,12 +12,12 @@ export class OFFICER { // }) // RET_YEAR: string; - // @Column({ - // nullable: true, - // type: "text", - // default: null, - // }) - // ID: string; + @Column({ + nullable: true, + type: "text", + default: null, + }) + ID: string; @Column({ nullable: true, @@ -172,4 +172,18 @@ export class OFFICER { default: null, }) ADMIN_NAME: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + POS_NUM_CODE_SIT: string; + + @Column({ + nullable: true, + type: "text", + default: null, + }) + POS_NUM_CODE_SIT_ABB: string; } diff --git a/src/entities/OrgRoot.ts b/src/entities/OrgRoot.ts index 7e32246a..3aefad6e 100644 --- a/src/entities/OrgRoot.ts +++ b/src/entities/OrgRoot.ts @@ -9,6 +9,7 @@ import { PosMaster } from "./PosMaster"; import { PermissionOrg } from "./PermissionOrg"; import { EmployeePosMaster } from "./EmployeePosMaster"; import { EmployeeTempPosMaster } from "./EmployeeTempPosMaster"; +import { PermissionProfile } from "./PermissionProfile"; enum OrgRootRank { DEPARTMENT = "DEPARTMENT", @@ -192,6 +193,9 @@ export class OrgRoot extends EntityBase { @OneToMany(() => PermissionOrg, (permissionOrg) => permissionOrg.orgRootTree) permissionOrgRoots: PermissionOrg[]; + + @OneToMany(() => PermissionProfile, (permissionProfile) => permissionProfile.orgRootTree) + permissionProfileRoots: PermissionProfile[]; } export class CreateOrgRoot { @@ -300,4 +304,4 @@ export class UpdateOrgRoot { @Column() misId?: string; -} \ No newline at end of file +} diff --git a/src/entities/PermissionProfile.ts b/src/entities/PermissionProfile.ts new file mode 100644 index 00000000..7f78412b --- /dev/null +++ b/src/entities/PermissionProfile.ts @@ -0,0 +1,27 @@ +import { Entity, Column, ManyToOne, JoinColumn } from "typeorm"; +import { EntityBase } from "./base/Base"; +import { OrgRoot } from "./OrgRoot"; +import { Profile } from "./Profile"; + +@Entity("permissionProfile") +export class PermissionProfile extends EntityBase { + @Column({ + length: 40, + comment: "คีย์นอก(FK)ของตาราง orgRoot", + }) + orgRootId: string; + + @Column({ + length: 40, + comment: "คีย์นอก(FK)ของตาราง profile", + }) + profileId: string; + + @ManyToOne(() => OrgRoot, (orgRoot) => orgRoot.permissionProfileRoots) + @JoinColumn({ name: "orgRootId" }) + orgRootTree: OrgRoot; + + @ManyToOne(() => Profile, (profile) => profile.permissionProfiles) + @JoinColumn({ name: "profileId" }) + profileTree: Profile; +}