Merge branch 'develop' into develop-Bright

# Conflicts:
#	src/controllers/CommandController.ts
This commit is contained in:
Bright 2024-10-10 13:28:56 +07:00
commit c92a0516c7
4 changed files with 351 additions and 67 deletions

View file

@ -19,7 +19,7 @@ import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { Command } from "../entities/Command"; 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 { CommandType } from "../entities/CommandType";
import { CommandSend } from "../entities/CommandSend"; import { CommandSend } from "../entities/CommandSend";
import { Profile, CreateProfileAllFields } from "../entities/Profile"; import { Profile, CreateProfileAllFields } from "../entities/Profile";
@ -35,10 +35,8 @@ import CallAPI from "../interfaces/call-api";
import { ProfileSalary, CreateProfileSalary } from "../entities/ProfileSalary"; import { ProfileSalary, CreateProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory"; import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { import {
calculateAge,
calculateRetireDate, calculateRetireDate,
calculateRetireLaw, calculateRetireLaw,
calculateRetireYear,
removeProfileInOrganize, removeProfileInOrganize,
setLogDataDiff, setLogDataDiff,
} from "../interfaces/utils"; } from "../interfaces/utils";
@ -51,33 +49,12 @@ import { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory";
import { PosMasterAct } from "../entities/PosMasterAct"; import { PosMasterAct } from "../entities/PosMasterAct";
import { PosLevel } from "../entities/PosLevel"; import { PosLevel } from "../entities/PosLevel";
import { PosType } from "../entities/PosType"; import { PosType } from "../entities/PosType";
import { import { addUserRoles, createUser, getRoles } from "../keycloak";
addUserGroup,
addUserRoles,
createGroup,
createUser,
deleteGroup,
deleteUser,
editUser,
getGroups,
getRoles,
getUser,
getUserGroups,
getUserList,
removeUserGroup,
removeUserRoles,
getRoleMappings,
getUserCount,
enableStatus,
} from "../keycloak";
import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation"; import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation";
import { ProfileEducationHistory } from "../entities/ProfileEducationHistory"; import { ProfileEducationHistory } from "../entities/ProfileEducationHistory";
import { import { CreateProfileCertificate, ProfileCertificate } from "../entities/ProfileCertificate";
CreateProfileCertificate,
ProfileCertificate,
UpdateProfileCertificate,
} from "../entities/ProfileCertificate";
import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory"; import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory";
import permission from "../interfaces/permission";
@Route("api/v1/org/command") @Route("api/v1/org/command")
@Tags("Command") @Tags("Command")
@ -129,11 +106,107 @@ export class CommandController extends Controller {
@Query() year?: number, @Query() year?: number,
@Query() status?: string | null, @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, "COMMAND");
}
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 const [commands, total] = await this.commandRepository
.createQueryBuilder("command") .createQueryBuilder("command")
.andWhere("command.createdUserId = :createdUserId", { .andWhere(
createdUserId: request.user.sub, 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( .andWhere(
status != null && status != undefined && status != "" status != null && status != undefined && status != ""
? "command.status IN (:...status)" ? "command.status IN (:...status)"
@ -2063,18 +2136,32 @@ export class CommandController extends Controller {
const _null: any = null; const _null: any = null;
if (item.bodyProfile.posLevelId === "") item.bodyProfile.posLevelId = null; if (item.bodyProfile.posLevelId === "") item.bodyProfile.posLevelId = null;
if (item.bodyProfile.posTypeId === "") item.bodyProfile.posTypeId = 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, "ไม่พบข้อมูลระดับตำแหน่งนี้"); 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, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
} }
let profile:any = await this.profileRepository.findOneBy({ citizenId: item.bodyProfile.citizenId }); let profile: any = await this.profileRepository.findOneBy({
if(!profile) { citizenId: item.bodyProfile.citizenId,
});
if (!profile) {
profile = Object.assign({ ...item.bodyProfile, ...meta }); profile = Object.assign({ ...item.bodyProfile, ...meta });
profile.dateRetire = item.bodyProfile.birthDate == null ? _null : calculateRetireDate(item.bodyProfile.birthDate); profile.dateRetire =
profile.dateRetireLaw = item.bodyProfile.birthDate == null ? _null : calculateRetireLaw(item.bodyProfile.birthDate); 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, { const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, {
firstName: profile.firstName, firstName: profile.firstName,
lastName: profile.lastName, lastName: profile.lastName,
@ -2127,7 +2214,7 @@ export class CommandController extends Controller {
Object.assign(profileCer, { ...cer, ...meta }); Object.assign(profileCer, { ...cer, ...meta });
const cerHistory = new ProfileCertificateHistory(); const cerHistory = new ProfileCertificateHistory();
Object.assign(cerHistory, { ...profileCer, id: undefined }); Object.assign(cerHistory, { ...profileCer, id: undefined });
profileCer.profileId = profile.id profileCer.profileId = profile.id;
await this.certificateRepo.save(profileCer, { data: req }); await this.certificateRepo.save(profileCer, { data: req });
setLogDataDiff(req, { before, after: profileCer }); setLogDataDiff(req, { before, after: profileCer });
cerHistory.profileCertificateId = profileCer.id; cerHistory.profileCertificateId = profileCer.id;
@ -2135,7 +2222,6 @@ export class CommandController extends Controller {
}), }),
); );
} }
//Salary //Salary
if(item.bodySalarys && item.bodySalarys != null) { if(item.bodySalarys && item.bodySalarys != null) {
const dest_item = await this.salaryRepo.findOne({ const dest_item = await this.salaryRepo.findOne({
@ -2146,14 +2232,13 @@ export class CommandController extends Controller {
Object.assign(profileSal, { ...item.bodySalarys, ...meta }); Object.assign(profileSal, { ...item.bodySalarys, ...meta });
const salaryHistory = new ProfileSalaryHistory(); const salaryHistory = new ProfileSalaryHistory();
Object.assign(salaryHistory, { ...profileSal, id: undefined }); Object.assign(salaryHistory, { ...profileSal, id: undefined });
profileSal.order = dest_item == null ? 1 : dest_item.order + 1, (profileSal.order = dest_item == null ? 1 : dest_item.order + 1),
profileSal.profileId = profile.id (profileSal.profileId = profile.id);
await this.salaryRepo.save(profileSal, { data: req }); await this.salaryRepo.save(profileSal, { data: req });
setLogDataDiff(req, { before, after: profileSal }); setLogDataDiff(req, { before, after: profileSal });
salaryHistory.profileSalaryId = profileSal.id; salaryHistory.profileSalaryId = profileSal.id;
await this.salaryHistoryRepo.save(salaryHistory, { data: req }); await this.salaryHistoryRepo.save(salaryHistory, { data: req });
} }
//Position //Position
if(item.bodyPosition && item.bodyPosition != null) { if(item.bodyPosition && item.bodyPosition != null) {
const posMaster = await this.posMasterRepository.findOne({ const posMaster = await this.posMasterRepository.findOne({

View file

@ -1433,7 +1433,11 @@ export class OrganizationController extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
} }
if (!request.user.role.includes("SUPER_ADMIN")) { 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) const orgRootData = await AppDataSource.getRepository(OrgRoot)
@ -6528,9 +6532,9 @@ export class OrganizationController extends Controller {
return new HttpSuccess(formattedData); return new HttpSuccess(formattedData);
} }
/** /**
* API * API org
* *
* @summary - (ADMIN) * @summary - org (ADMIN)
* *
*/ */
@Get("check/child1/{id}") @Get("check/child1/{id}")
@ -6545,4 +6549,206 @@ export class OrganizationController extends Controller {
const check = orgRevision.orgChild1s.find((x) => x.isOfficer == true); const check = orgRevision.orgChild1s.find((x) => x.isOfficer == true);
return new HttpSuccess(check != null); 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;
}
} }

View file

@ -65,11 +65,10 @@ export class PermissionOrgController extends Controller {
if (!orgRevisionActive) { if (!orgRevisionActive) {
return new HttpSuccess([]); return new HttpSuccess([]);
} }
let _data: any = [null]; let _data: any = null;
if (!request.user.role.includes("SUPER_ADMIN")) { if (!request.user.role.includes("SUPER_ADMIN")) {
_data = await this.listAuthSysOrgFuncByRevisionId(request, "SYS_ORG", orgRevisionActive.id); _data = await this.listAuthSysOrgFuncByRevisionId(request, "SYS_ORG", orgRevisionActive.id);
} }
console.log(_data);
const data = await AppDataSource.getRepository(OrgRoot) const data = await AppDataSource.getRepository(OrgRoot)
.createQueryBuilder("orgRoot") .createQueryBuilder("orgRoot")
.where("orgRoot.orgRevisionId = :id", { id: orgRevisionActive.id }) .where("orgRoot.orgRevisionId = :id", { id: orgRevisionActive.id })
@ -529,13 +528,9 @@ export class PermissionOrgController extends Controller {
let profile = await this.profileRepo.findOne({ let profile = await this.profileRepo.findOne({
where: { where: {
keycloak: request.user.sub, keycloak: request.user.sub,
// current_holders: { orgRevisionId: revisionId },
}, },
relations: ["next_holders", "next_holders.authRole", "next_holders.authRole.authRoles"], relations: ["next_holders", "next_holders.authRole", "next_holders.authRole.authRoles"],
}); });
console.log(request.user.sub);
console.log(revisionId);
console.log(profile);
if (!profile) { if (!profile) {
return [null]; return [null];
} }
@ -551,8 +546,6 @@ export class PermissionOrgController extends Controller {
orgRevisionId: revisionId, orgRevisionId: revisionId,
}, },
}); });
console.log(posMaster);
console.log(attrOwnership);
if (!posMaster) { if (!posMaster) {
return [null]; return [null];
} else if (attrOwnership == "OWNER") { } else if (attrOwnership == "OWNER") {

View file

@ -83,7 +83,16 @@ class CheckAuth {
} else if (x.orgChild4Id == null) { } else if (x.orgChild4Id == null) {
node = 3; 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 = { data = {
root: [x.orgRootId], root: [x.orgRootId],
child1: null, child1: null,
@ -111,15 +120,6 @@ class CheckAuth {
privilege: "NORMAL", privilege: "NORMAL",
}; };
} else if (privilege == "SPECIFIC") { } else if (privilege == "SPECIFIC") {
} else if (privilege == "OWNER") {
data = {
root: null,
child1: null,
child2: null,
child3: null,
child4: null,
privilege: "OWNER",
};
} }
return data; return data;