diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index f0ba1287..d83831a8 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -19,7 +19,7 @@ import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import HttpError from "../interfaces/http-error"; import { Command } from "../entities/Command"; -import { Brackets, LessThan, MoreThan, Double, In } from "typeorm"; +import { Brackets, LessThan, MoreThan, Double, In, Not } from "typeorm"; import { CommandType } from "../entities/CommandType"; import { CommandSend } from "../entities/CommandSend"; import { Profile, CreateProfileAllFields } from "../entities/Profile"; @@ -35,10 +35,8 @@ import CallAPI from "../interfaces/call-api"; import { ProfileSalary, CreateProfileSalary } from "../entities/ProfileSalary"; import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory"; import { - calculateAge, calculateRetireDate, calculateRetireLaw, - calculateRetireYear, removeProfileInOrganize, setLogDataDiff, } from "../interfaces/utils"; @@ -51,33 +49,12 @@ import { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory"; import { PosMasterAct } from "../entities/PosMasterAct"; import { PosLevel } from "../entities/PosLevel"; import { PosType } from "../entities/PosType"; -import { - addUserGroup, - addUserRoles, - createGroup, - createUser, - deleteGroup, - deleteUser, - editUser, - getGroups, - getRoles, - getUser, - getUserGroups, - getUserList, - removeUserGroup, - removeUserRoles, - getRoleMappings, - getUserCount, - enableStatus, -} from "../keycloak"; +import { addUserRoles, createUser, getRoles } from "../keycloak"; import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation"; import { ProfileEducationHistory } from "../entities/ProfileEducationHistory"; -import { - CreateProfileCertificate, - ProfileCertificate, - UpdateProfileCertificate, -} from "../entities/ProfileCertificate"; +import { CreateProfileCertificate, ProfileCertificate } from "../entities/ProfileCertificate"; import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory"; +import permission from "../interfaces/permission"; @Route("api/v1/org/command") @Tags("Command") @@ -129,11 +106,107 @@ export class CommandController extends Controller { @Query() year?: number, @Query() status?: string | null, ) { + let profilekArray: any = []; + + let _profile = await this.profileRepository.findOne({ + where: { keycloak: request.user.sub }, + relations: ["current_holders", "current_holders.orgRevision"], + }); + let isDirector = + _profile?.current_holders?.filter( + (x) => + x.orgRevision?.orgRevisionIsCurrent == true && x.orgRevision?.orgRevisionIsDraft == false, + )[0]?.isDirector || false; + if (isDirector) { + let _data: any = { + root: null, + child1: null, + child2: null, + child3: null, + child4: null, + }; + if (!request.user.role.includes("SUPER_ADMIN")) { + _data = await new permission().PermissionOrgList(request, "SYS_ORG"); + } + const profiles = await this.profileRepository + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.current_holders", "current_holders") + .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") + .andWhere( + _data.root != undefined && _data.root != null + ? _data.root[0] != null + ? `current_holders.orgRootId IN (:...root)` + : `current_holders.orgRootId is null` + : "1=1", + { + root: _data.root, + }, + ) + .andWhere( + _data.child1 != undefined && _data.child1 != null + ? _data.child1[0] != null + ? `current_holders.orgChild1Id IN (:...child1)` + : `current_holders.orgChild1Id is null` + : "1=1", + { + child1: _data.child1, + }, + ) + .andWhere( + _data.child2 != undefined && _data.child2 != null + ? _data.child2[0] != null + ? `current_holders.orgChild2Id IN (:...child2)` + : `current_holders.orgChild2Id is null` + : "1=1", + { + child2: _data.child2, + }, + ) + .andWhere( + _data.child3 != undefined && _data.child3 != null + ? _data.child3[0] != null + ? `current_holders.orgChild3Id IN (:...child3)` + : `current_holders.orgChild3Id is null` + : "1=1", + { + child3: _data.child3, + }, + ) + .andWhere( + _data.child4 != undefined && _data.child4 != null + ? _data.child4[0] != null + ? `current_holders.orgChild4Id IN (:...child4)` + : `current_holders.orgChild4Id is null` + : "1=1", + { + child4: _data.child4, + }, + ) + .select("profile.keycloak", "keycloak") + .getRawMany(); + profilekArray = profiles.map((p) => p.keycloak); + } + const [commands, total] = await this.commandRepository .createQueryBuilder("command") - .andWhere("command.createdUserId = :createdUserId", { - createdUserId: request.user.sub, - }) + .andWhere( + new Brackets((qb) => { + qb.orWhere( + profilekArray.length > 0 + ? "command.createdUserId IN (:...profilekArray)" + : "command.createdUserId='1'", + { + profilekArray: profilekArray, + }, + ).orWhere("command.createdUserId = :createdUserId", { + createdUserId: request.user.sub, + }); + }), + ) .andWhere( status != null && status != undefined && status != "" ? "command.status IN (:...status)" @@ -2063,18 +2136,32 @@ export class CommandController extends Controller { const _null: any = null; if (item.bodyProfile.posLevelId === "") item.bodyProfile.posLevelId = null; if (item.bodyProfile.posTypeId === "") item.bodyProfile.posTypeId = null; - if (item.bodyProfile.posLevelId && !(await this.posLevelRepo.findOneBy({ id: item.bodyProfile.posLevelId }))) { + if ( + item.bodyProfile.posLevelId && + !(await this.posLevelRepo.findOneBy({ id: item.bodyProfile.posLevelId })) + ) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้"); } - if (item.bodyProfile.posTypeId && !(await this.posTypeRepo.findOneBy({ id: item.bodyProfile.posTypeId }))) { + if ( + item.bodyProfile.posTypeId && + !(await this.posTypeRepo.findOneBy({ id: item.bodyProfile.posTypeId })) + ) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } - let profile:any = await this.profileRepository.findOneBy({ citizenId: item.bodyProfile.citizenId }); - if(!profile) { + let profile: any = await this.profileRepository.findOneBy({ + citizenId: item.bodyProfile.citizenId, + }); + if (!profile) { profile = Object.assign({ ...item.bodyProfile, ...meta }); - profile.dateRetire = item.bodyProfile.birthDate == null ? _null : calculateRetireDate(item.bodyProfile.birthDate); - profile.dateRetireLaw = item.bodyProfile.birthDate == null ? _null : calculateRetireLaw(item.bodyProfile.birthDate); + profile.dateRetire = + item.bodyProfile.birthDate == null + ? _null + : calculateRetireDate(item.bodyProfile.birthDate); + profile.dateRetireLaw = + item.bodyProfile.birthDate == null + ? _null + : calculateRetireLaw(item.bodyProfile.birthDate); const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, { firstName: profile.firstName, lastName: profile.lastName, @@ -2101,8 +2188,8 @@ export class CommandController extends Controller { await this.profileRepository.save(profile); setLogDataDiff(req, { before, after: profile }); } - - if(profile && profile.id) { + + if (profile && profile.id) { //Educations await Promise.all( item.bodyEducations.map(async (education) => { @@ -2124,7 +2211,7 @@ export class CommandController extends Controller { Object.assign(profileCer, { ...cer, ...meta }); const cerHistory = new ProfileCertificateHistory(); Object.assign(cerHistory, { ...profileCer, id: undefined }); - profileCer.profileId = profile.id + profileCer.profileId = profile.id; await this.certificateRepo.save(profileCer, { data: req }); setLogDataDiff(req, { before, after: profileCer }); cerHistory.profileCertificateId = profileCer.id; @@ -2140,8 +2227,8 @@ export class CommandController extends Controller { Object.assign(profileSal, { ...item.bodySalarys, ...meta }); const salaryHistory = new ProfileSalaryHistory(); Object.assign(salaryHistory, { ...profileSal, id: undefined }); - profileSal.order = dest_item == null ? 1 : dest_item.order + 1, - profileSal.profileId = profile.id + (profileSal.order = dest_item == null ? 1 : dest_item.order + 1), + (profileSal.profileId = profile.id); await this.salaryRepo.save(profileSal, { data: req }); setLogDataDiff(req, { before, after: profileSal }); salaryHistory.profileSalaryId = profileSal.id; @@ -2150,7 +2237,7 @@ export class CommandController extends Controller { const posMaster = await this.posMasterRepository.findOne({ where: { id: item.bodyPosition.posmasterId }, }); - if (posMaster == null) + if (posMaster == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); const posMasterOld = await this.posMasterRepository.findOne({ @@ -2201,7 +2288,7 @@ export class CommandController extends Controller { profile.posLevelId = positionNew.posLevelId; profile.posTypeId = positionNew.posTypeId; profile.position = positionNew.positionName; - await this.profileRepository.save(profile, { data: req }); + await this.profileRepository.save(profile, { data: req }); setLogDataDiff(req, { before, after: profile }); await this.positionRepository.save(positionNew, { data: req }); } diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index b4005ffc..31ac3043 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -1433,7 +1433,11 @@ export class OrganizationController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } if (!request.user.role.includes("SUPER_ADMIN")) { - _data = await new permission().PermissionOrgList(request, "SYS_ORG"); + if (orgRevision.orgRevisionIsDraft == true && orgRevision.orgRevisionIsCurrent == false) { + _data = await this.listAuthSysOrgFuncByRevisionIdN(request, "SYS_ORG", orgRevision.id); + } else { + _data = await this.listAuthSysOrgFuncByRevisionIdC(request, "SYS_ORG", orgRevision.id); + } } const orgRootData = await AppDataSource.getRepository(OrgRoot) @@ -6528,9 +6532,9 @@ export class OrganizationController extends Controller { return new HttpSuccess(formattedData); } /** - * API เช็คสกจในระบบ + * API เช็ค org ในระบบ * - * @summary - เช็คสกจในระบบ (ADMIN) + * @summary - เช็ค org ในระบบ (ADMIN) * */ @Get("check/child1/{id}") @@ -6545,4 +6549,206 @@ export class OrganizationController extends Controller { const check = orgRevision.orgChild1s.find((x) => x.isOfficer == true); return new HttpSuccess(check != null); } + public async listAuthSysOrgFuncByRevisionIdN( + 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"], + }); + let data: any = { + root: [null], + child1: [null], + child2: [null], + child3: [null], + child4: [null], + }; + if (!profile) { + return { + root: null, + child1: null, + child2: null, + child3: null, + child4: null, + }; + } + + let attrOwnership = + profile?.next_holders + .filter((x) => x.orgRevisionId == revisionId)[0] + ?.authRole?.authRoles?.filter((x) => x.authSysId == system)[0]?.attrOwnership || null; + + let attrPrivilege = + profile?.next_holders + .filter((x) => x.orgRevisionId == revisionId)[0] + ?.authRole?.authRoles?.filter((x) => x.authSysId == system)[0]?.attrPrivilege || null; + + const posMaster = await this.posMasterRepository.findOne({ + where: { + next_holderId: profile.id, + orgRevisionId: revisionId, + }, + }); + if (!posMaster) { + data = { + root: [null], + child1: [null], + child2: [null], + child3: [null], + child4: [null], + }; + } else if (attrOwnership == "OWNER") { + data = { + root: null, + child1: null, + child2: null, + child3: null, + child4: null, + }; + } else if (attrPrivilege == "ROOT") { + data = { + root: [posMaster.orgRootId], + child1: null, + child2: null, + child3: null, + child4: null, + privilege: "ROOT", + }; + } else if (attrPrivilege == "CHILD") { + let node = 4; + if (posMaster.orgChild1Id == null) { + node = 0; + } else if (posMaster.orgChild2Id == null) { + node = 1; + } else if (posMaster.orgChild3Id == null) { + node = 2; + } else if (posMaster.orgChild4Id == null) { + node = 3; + } + data = { + root: node >= 0 ? [posMaster.orgRootId] : null, + child1: node >= 1 ? [posMaster.orgChild1Id] : null, + child2: node >= 2 ? [posMaster.orgChild2Id] : null, + child3: node >= 3 ? [posMaster.orgChild3Id] : null, + child4: node >= 4 ? [posMaster.orgChild4Id] : null, + }; + } else if (attrPrivilege == "NORMAL") { + data = { + root: [posMaster.orgRootId], + child1: [posMaster.orgChild1Id], + child2: [posMaster.orgChild2Id], + child3: [posMaster.orgChild3Id], + child4: [posMaster.orgChild4Id], + }; + } else if (attrPrivilege == "SPECIFIC") { + } + return data; + } + public async listAuthSysOrgFuncByRevisionIdC( + request: RequestWithUser, + system: string, + revisionId: string, + ) { + let profile = await this.profileRepo.findOne({ + where: { + keycloak: request.user.sub, + }, + relations: [ + "current_holders", + "current_holders.authRole", + "current_holders.authRole.authRoles", + ], + }); + let data: any = { + root: [null], + child1: [null], + child2: [null], + child3: [null], + child4: [null], + }; + if (!profile) { + return { + root: null, + child1: null, + child2: null, + child3: null, + child4: null, + }; + } + + let attrOwnership = + profile?.current_holders + .filter((x) => x.orgRevisionId == revisionId)[0] + ?.authRole?.authRoles?.filter((x) => x.authSysId == system)[0]?.attrOwnership || null; + + let attrPrivilege = + profile?.current_holders + .filter((x) => x.orgRevisionId == revisionId)[0] + ?.authRole?.authRoles?.filter((x) => x.authSysId == system)[0]?.attrPrivilege || null; + + const posMaster = await this.posMasterRepository.findOne({ + where: { + next_holderId: profile.id, + orgRevisionId: revisionId, + }, + }); + if (!posMaster) { + data = { + root: [null], + child1: [null], + child2: [null], + child3: [null], + child4: [null], + }; + } else if (attrOwnership == "OWNER") { + data = { + root: null, + child1: null, + child2: null, + child3: null, + child4: null, + }; + } else if (attrPrivilege == "ROOT") { + data = { + root: [posMaster.orgRootId], + child1: null, + child2: null, + child3: null, + child4: null, + privilege: "ROOT", + }; + } else if (attrPrivilege == "CHILD") { + let node = 4; + if (posMaster.orgChild1Id == null) { + node = 0; + } else if (posMaster.orgChild2Id == null) { + node = 1; + } else if (posMaster.orgChild3Id == null) { + node = 2; + } else if (posMaster.orgChild4Id == null) { + node = 3; + } + data = { + root: node >= 0 ? [posMaster.orgRootId] : null, + child1: node >= 1 ? [posMaster.orgChild1Id] : null, + child2: node >= 2 ? [posMaster.orgChild2Id] : null, + child3: node >= 3 ? [posMaster.orgChild3Id] : null, + child4: node >= 4 ? [posMaster.orgChild4Id] : null, + }; + } else if (attrPrivilege == "NORMAL") { + data = { + root: [posMaster.orgRootId], + child1: [posMaster.orgChild1Id], + child2: [posMaster.orgChild2Id], + child3: [posMaster.orgChild3Id], + child4: [posMaster.orgChild4Id], + }; + } else if (attrPrivilege == "SPECIFIC") { + } + return data; + } } diff --git a/src/controllers/PermissionOrgController.ts b/src/controllers/PermissionOrgController.ts index 3fffe3b6..0644a19a 100644 --- a/src/controllers/PermissionOrgController.ts +++ b/src/controllers/PermissionOrgController.ts @@ -65,11 +65,10 @@ export class PermissionOrgController extends Controller { if (!orgRevisionActive) { return new HttpSuccess([]); } - let _data: any = [null]; + let _data: any = null; if (!request.user.role.includes("SUPER_ADMIN")) { _data = await this.listAuthSysOrgFuncByRevisionId(request, "SYS_ORG", orgRevisionActive.id); } - console.log(_data); const data = await AppDataSource.getRepository(OrgRoot) .createQueryBuilder("orgRoot") .where("orgRoot.orgRevisionId = :id", { id: orgRevisionActive.id }) @@ -529,13 +528,9 @@ export class PermissionOrgController extends Controller { let profile = await this.profileRepo.findOne({ where: { keycloak: request.user.sub, - // current_holders: { orgRevisionId: revisionId }, }, relations: ["next_holders", "next_holders.authRole", "next_holders.authRole.authRoles"], }); - console.log(request.user.sub); - console.log(revisionId); - console.log(profile); if (!profile) { return [null]; } @@ -551,8 +546,6 @@ export class PermissionOrgController extends Controller { orgRevisionId: revisionId, }, }); - console.log(posMaster); - console.log(attrOwnership); if (!posMaster) { return [null]; } else if (attrOwnership == "OWNER") { diff --git a/src/interfaces/permission.ts b/src/interfaces/permission.ts index b69afce3..e063de6b 100644 --- a/src/interfaces/permission.ts +++ b/src/interfaces/permission.ts @@ -83,7 +83,16 @@ class CheckAuth { } else if (x.orgChild4Id == null) { node = 3; } - if (privilege == "ROOT") { + if (privilege == "OWNER") { + data = { + root: null, + child1: null, + child2: null, + child3: null, + child4: null, + privilege: "OWNER", + }; + } else if (privilege == "ROOT") { data = { root: [x.orgRootId], child1: null, @@ -111,15 +120,6 @@ class CheckAuth { privilege: "NORMAL", }; } else if (privilege == "SPECIFIC") { - } else if (privilege == "OWNER") { - data = { - root: null, - child1: null, - child2: null, - child3: null, - child4: null, - privilege: "OWNER", - }; } return data;