api ข้อมูลทะเบียนประวัติตาม keycloak + ผู้บังคับบัญชา

This commit is contained in:
Bright 2024-09-13 11:14:10 +07:00
parent 4641362b95
commit 83abbeedec

View file

@ -3233,6 +3233,408 @@ export class ProfileController extends Controller {
return new HttpSuccess(_profile);
}
/**
* API keycloak +
* @summary keycloak +
* @param id keycloak Id
*/
@Get("keycloak/commander/{id}")
async getProfileAndCommander(@Path() id: string) {
const profile = await this.profileRepo.findOne({
where: { keycloak: id },
relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"],
});
if (!profile) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
}
const orgRevisionPublish = await this.orgRevisionRepo.findOne({
where: {
orgRevisionIsDraft: false,
orgRevisionIsCurrent: true
}
})
if (!orgRevisionPublish) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง");
}
const posMaster =
profile.current_holders == null ||
profile.current_holders.length == 0 ||
profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) == null
? null
: profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id);
const root =
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot == null
? null
: profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot;
const child1 =
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild1 ==
null
? null
: profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild1;
const child2 =
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild2 ==
null
? null
: profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild2;
const child3 =
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild3 ==
null
? null
: profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild3;
const child4 =
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild4 ==
null
? null
: profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild4;
const position = await this.positionRepository.findOne({
relations: ["posExecutive"],
where: {
posMasterId: posMaster?.id,
},
});
//find org(สังกัด)
const posMaster_ = await this.posMasterRepo.findOne({
where: {
orgRevisionId: orgRevisionPublish?.id,
current_holderId: profile.id,
},
order: { createdAt: "DESC" },
relations: {
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
const fullNameParts = [
posMaster_ == null || posMaster_.orgChild4 == null ? null : posMaster_.orgChild4.orgChild4Name,
posMaster_ == null || posMaster_.orgChild3 == null ? null : posMaster_.orgChild3.orgChild3Name,
posMaster_ == null || posMaster_.orgChild2 == null ? null : posMaster_.orgChild2.orgChild2Name,
posMaster_ == null || posMaster_.orgChild1 == null ? null : posMaster_.orgChild1.orgChild1Name,
posMaster_ == null || posMaster_.orgRoot == null ? null : posMaster_.orgRoot.orgRootName,
];
const org = fullNameParts.filter((part) => part !== undefined && part !== null).join("/");
//find commander(ผู้บังคับบัญชา)
let node = 4;
let childId = posMaster?.orgChild4Id;
let condition: any = { orgChild4Id: childId };
if (posMaster?.orgChild4Id == null && posMaster?.orgChild3Id != null) {
node = 3;
childId = posMaster?.orgChild3Id;
condition = { orgChild3Id: childId, orgChild4Id: IsNull() };
} else if (posMaster?.orgChild3Id == null && posMaster?.orgChild2Id != null) {
node = 2;
childId = posMaster?.orgChild2Id;
condition = { orgChild2Id: childId, orgChild3Id: IsNull() };
} else if (posMaster?.orgChild2Id == null && posMaster?.orgChild1Id != null) {
node = 1;
childId = posMaster?.orgChild1Id;
condition = { orgChild1Id: childId, orgChild2Id: IsNull() };
} else if (posMaster?.orgChild1Id == null) {
node = 0;
childId = posMaster?.orgRootId;
condition = { orgRootId: childId, orgChild1Id: IsNull() };
}
const findCmd = await this.posMasterRepo.findOne({
where: {
current_holderId: Not(IsNull()) || Not(""),
orgRevisionId: orgRevisionPublish?.id,
...condition,
},
relations: ["current_holder"],
order: { posMasterOrder: "ASC" },
});
let findOSAB: PosMaster | null = null;
let findTSAB: PosMaster | null = null;
//หาผู้บังคับบัญชาที่เหนือขึ้นไปอีก 1 ขั้น
if (node !== 0) {
findOSAB = await AppDataSource.getRepository(PosMaster)
.createQueryBuilder("posMaster")
.leftJoinAndSelect("posMaster.current_holder", "current_holder")
.where("posMaster.current_holderId IS NOT NULL")
.andWhere("posMaster.orgRevisionId = :revisionId", { revisionId: orgRevisionPublish?.id })
.andWhere(
new Brackets((qb) => {
if (node === 4) {
qb.andWhere("posMaster.orgChild4Id IS NULL");
qb.andWhere("posMaster.orgChild3Id = :childId", {
childId: posMaster?.orgChild3Id,
});
} else if (node === 3) {
qb.andWhere("posMaster.orgChild3Id IS NULL");
qb.andWhere("posMaster.orgChild2Id = :childId", {
childId: posMaster?.orgChild2Id,
});
} else if (node === 2) {
qb.andWhere("posMaster.orgChild2Id IS NULL");
qb.andWhere("posMaster.orgChild1Id = :childId", {
childId: posMaster?.orgChild1Id,
});
} else if (node === 1) {
qb.andWhere("posMaster.orgChild1Id IS NULL");
qb.andWhere("posMaster.orgRootId = :childId", { childId: posMaster?.orgRootId });
}
}),
)
.orderBy("posMaster.posMasterOrder", "ASC")
.getOne();
}
//หาผู้บังคับบัญชาที่เหนือขึ้นไปอีก 2 ขั้น
if (node !== 0 && node !== 1) {
findTSAB = await AppDataSource.getRepository(PosMaster)
.createQueryBuilder("posMaster")
.leftJoinAndSelect("posMaster.current_holder", "current_holder")
.where("posMaster.current_holderId IS NOT NULL")
.andWhere("posMaster.orgRevisionId = :revisionId", { revisionId: orgRevisionPublish?.id })
.andWhere(
new Brackets((qb) => {
if (node === 4) {
qb.andWhere("posMaster.orgChild3Id IS NULL");
qb.andWhere("posMaster.orgChild2Id = :childId", {
childId: posMaster?.orgChild2Id,
});
} else if (node === 3) {
qb.andWhere("posMaster.orgChild2Id IS NULL");
qb.andWhere("posMaster.orgChild1Id = :childId", {
childId: posMaster?.orgChild1Id,
});
} else if (node === 2) {
qb.andWhere("posMaster.orgChild1Id IS NULL");
qb.andWhere("posMaster.orgRootId = :childId", {
childId: posMaster?.orgRootId,
});
}
}),
)
.orderBy("posMaster.posMasterOrder", "ASC")
.getOne();
}
let commandProfileId;
let commanderFullname_: any = {};
let commanderPosition_: any = {};
let commanderRootName_: any = {};
let commanderOrg_: any;
let commandAboveProfileId;
let commanderAboveFullname_: any = {};
let commanderAbovePosition_: any = {};
let commanderAboveRootName_: any = {};
let commanderAboveOrg_: any;
let dataCommander: any
commanderFullname_ =
(findCmd?.current_holder?.prefix ?? "") +
(findCmd?.current_holder?.firstName ?? "") +
(findCmd?.current_holder?.firstName ? " " : "") +
(findCmd?.current_holder?.lastName ?? "");
commanderPosition_ = findCmd?.current_holder?.position ?? "";
commanderAboveFullname_ =
(findOSAB?.current_holder?.prefix ?? "") +
(findOSAB?.current_holder?.firstName ?? "") +
(findOSAB?.current_holder?.firstName ? " " : "") +
(findOSAB?.current_holder?.lastName ?? "");
commanderAbovePosition_ = findOSAB?.current_holder?.position ?? "";
if (findCmd?.current_holderId == profile?.id) {
commandProfileId = findOSAB?.current_holderId
commanderFullname_ =
(findOSAB?.current_holder?.prefix ?? "") +
(findOSAB?.current_holder?.firstName ?? "") +
(findOSAB?.current_holder?.firstName ? " " : "") +
(findOSAB?.current_holder?.lastName ?? "");
commanderPosition_ = findOSAB?.current_holder?.position ?? "";
commandAboveProfileId = findTSAB?.current_holderId
commanderAboveFullname_ =
(findTSAB?.current_holder?.prefix ?? "") +
(findTSAB?.current_holder?.firstName ?? "") +
(findTSAB?.current_holder?.firstName ? " " : "") +
(findTSAB?.current_holder?.lastName ?? "");
commanderAbovePosition_ = findTSAB?.current_holder?.position ?? "";
dataCommander = {
commanderFullname: commanderFullname_,
commanderPosition: commanderPosition_,
commanderAboveFullname: commanderAboveFullname_,
commanderAbovePosition: commanderAbovePosition_,
};
}
else {
commandProfileId = findCmd?.current_holderId
commandAboveProfileId = findOSAB?.current_holderId
dataCommander = {
commanderFullname: commanderFullname_,
commanderPosition: commanderPosition_,
commanderAboveFullname: commanderAboveFullname_,
commanderAbovePosition: commanderAbovePosition_,
};
}
const commandProfile = await this.profileRepo.findOne({
where: { id: String(commandProfileId) },
relations: ["current_holders", "current_holders.orgRoot"],
});
commanderRootName_ =
commandProfile?.current_holders == null ||
commandProfile?.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot == null
? null
: commandProfile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot;
//find สังกัดผู้บังคับบัญชา
const commanderPosMaster_ = await this.posMasterRepo.findOne({
where: {
orgRevisionId: orgRevisionPublish?.id,
current_holderId: String(commandProfileId),
},
order: { createdAt: "DESC" },
relations: {
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
const commanderFullNameParts = [
commanderPosMaster_ == null || commanderPosMaster_.orgChild4 == null ? null : commanderPosMaster_.orgChild4.orgChild4Name,
commanderPosMaster_ == null || commanderPosMaster_.orgChild3 == null ? null : commanderPosMaster_.orgChild3.orgChild3Name,
commanderPosMaster_ == null || commanderPosMaster_.orgChild2 == null ? null : commanderPosMaster_.orgChild2.orgChild2Name,
commanderPosMaster_ == null || commanderPosMaster_.orgChild1 == null ? null : commanderPosMaster_.orgChild1.orgChild1Name,
commanderPosMaster_ == null || commanderPosMaster_.orgRoot == null ? null : commanderPosMaster_.orgRoot.orgRootName,
];
commanderOrg_ = commanderFullNameParts.filter((part) => part !== undefined && part !== null).join("/");
const commandAboveProfile = await this.profileRepo.findOne({
where: { id: String(commandAboveProfileId) },
relations: ["current_holders", "current_holders.orgRoot"],
});
commanderAboveRootName_ =
commandAboveProfile?.current_holders == null ||
commandAboveProfile?.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot == null
? null
: commandAboveProfile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot;
//find สังกัดผู้บังคับบัญชาเหนือไป1ขั้น
const commanderAbovePosMaster_ = await this.posMasterRepo.findOne({
where: {
orgRevisionId: orgRevisionPublish?.id,
current_holderId: String(commandAboveProfileId),
},
order: { createdAt: "DESC" },
relations: {
orgRoot: true,
orgChild1: true,
orgChild2: true,
orgChild3: true,
orgChild4: true,
},
});
const commanderAboveFullNameParts = [
commanderAbovePosMaster_ == null || commanderAbovePosMaster_.orgChild4 == null ? null : commanderAbovePosMaster_.orgChild4.orgChild4Name,
commanderAbovePosMaster_ == null || commanderAbovePosMaster_.orgChild3 == null ? null : commanderAbovePosMaster_.orgChild3.orgChild3Name,
commanderAbovePosMaster_ == null || commanderAbovePosMaster_.orgChild2 == null ? null : commanderAbovePosMaster_.orgChild2.orgChild2Name,
commanderAbovePosMaster_ == null || commanderAbovePosMaster_.orgChild1 == null ? null : commanderAbovePosMaster_.orgChild1.orgChild1Name,
commanderAbovePosMaster_ == null || commanderAbovePosMaster_.orgRoot == null ? null : commanderAbovePosMaster_.orgRoot.orgRootName,
];
commanderAboveOrg_ = commanderAboveFullNameParts.filter((part) => part !== undefined && part !== null).join("/");
const _profile: any = {
profileId: profile.id,
prefix: profile.prefix,
rank: profile.rank,
avatar: profile.avatar,
isProbation: profile.isProbation,
avatarName: profile.avatarName,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
birthDate: profile.birthDate,
position: profile.position,
leaveDate: profile.dateLeave,
dateStart: profile.dateStart,
dateRetireLaw: profile.dateRetireLaw,
posMaster: posMaster == null ? null : posMaster.posMasterNo,
posMasterNo: posMaster == null ? null : posMaster.posMasterNo,
posLevelName: profile.posLevel == null ? null : profile.posLevel.posLevelName,
posLevelRank: profile.posLevel == null ? null : profile.posLevel.posLevelRank,
posLevelId: profile.posLevel == null ? null : profile.posLevel.id,
posTypeName: profile.posType == null ? null : profile.posType.posTypeName,
posTypeRank: profile.posType == null ? null : profile.posType.posTypeRank,
posTypeId: profile.posType == null ? null : profile.posType.id,
posExecutiveName:
position == null || position.posExecutive == null
? null
: position.posExecutive.posExecutiveName,
posExecutivePriority:
position == null || position.posExecutive == null
? null
: position.posExecutive.posExecutivePriority,
posExecutiveId:
position == null || position.posExecutive == null ? null : position.posExecutive.id,
rootId: root == null ? null : root.id,
root: root == null ? null : root.orgRootName,
rootShortName: root == null ? null : root.orgRootShortName,
child1Id: child1 == null ? null : child1.id,
child1: child1 == null ? null : child1.orgChild1Name,
child1ShortName: child1 == null ? null : child1.orgChild1ShortName,
child2Id: child2 == null ? null : child2.id,
child2: child2 == null ? null : child2.orgChild2Name,
child2ShortName: child2 == null ? null : child2.orgChild2ShortName,
child3Id: child3 == null ? null : child3.id,
child3: child3 == null ? null : child3.orgChild3Name,
child3ShortName: child3 == null ? null : child3.orgChild3ShortName,
child4Id: child4 == null ? null : child4.id,
child4: child4 == null ? null : child4.orgChild4Name,
child4ShortName: child4 == null ? null : child4.orgChild4ShortName,
node: null,
nodeId: null,
org: org ? org : null,
// commander: dataCommander,
commanderFullname: dataCommander && dataCommander.commanderFullname
? dataCommander.commanderFullname : null,
commanderPosition: dataCommander && dataCommander.commanderPosition
? dataCommander.commanderPosition : null,
commanderRootName: commanderRootName_ && commanderRootName_.orgRootName
? commanderRootName_.orgRootName : null,
commanderOrg: commanderOrg_ ? commanderOrg_ : null,
commanderAboveFullname: dataCommander && dataCommander.commanderAboveFullname
? dataCommander.commanderAboveFullname : null,
commanderAbovePosition: dataCommander && dataCommander.commanderAbovePosition
? dataCommander.commanderAbovePosition : null,
commanderAboveRootName: commanderAboveRootName_ && commanderAboveRootName_.orgRootName
? commanderAboveRootName_.orgRootName : null,
commanderAboveOrg: commanderAboveOrg_ ? commanderAboveOrg_ : null,
};
if (_profile.child4Id != null) {
_profile.node = 4;
_profile.nodeId = _profile.child4Id;
} else if (_profile.child3Id != null) {
_profile.node = 3;
_profile.nodeId = _profile.child3Id;
} else if (_profile.child2Id != null) {
_profile.node = 2;
_profile.nodeId = _profile.child2Id;
} else if (_profile.child1Id != null) {
_profile.node = 1;
_profile.nodeId = _profile.child1Id;
} else if (_profile.rootId != null) {
_profile.node = 0;
_profile.nodeId = _profile.rootId;
}
return new HttpSuccess(_profile);
}
/**
* API profileid
*