hrms-api-org/src/controllers/OrganizationDotnetController.ts

4052 lines
156 KiB
TypeScript

import {
Controller,
Post,
Put,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
Get,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
import { Brackets, In, IsNull, Not } from "typeorm";
import { OrgRevision } from "../entities/OrgRevision";
import { OrgRoot } from "../entities/OrgRoot";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { Position } from "../entities/Position";
import { Insignia } from "../entities/Insignia";
import { CreateProfileInsignia, ProfileInsignia } from "../entities/ProfileInsignia";
import { PosMaster } from "../entities/PosMaster";
import { EmployeePosDict } from "../entities/EmployeePosDict";
@Route("api/v1/org/dotnet")
@Tags("Dotnet")
@Security("bearerAuth")
@Response(
HttpStatus.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatus.OK, "สำเร็จ")
export class OrganizationDotnetController extends Controller {
private orgRootRepo = AppDataSource.getRepository(OrgRoot);
private orgRevisionRepo = AppDataSource.getRepository(OrgRevision);
private profileRepo = AppDataSource.getRepository(Profile);
private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee);
private positionRepository = AppDataSource.getRepository(Position);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private insigniaRepo = AppDataSource.getRepository(ProfileInsignia);
private employeePosDictRepository = AppDataSource.getRepository(EmployeePosDict);
/**
* 1. API Search Profile
*
* @summary 1. API Search Profile
*
*/
@Post("search")
public async SearchProfile(
@Body()
body: {
citizenId?: string | null;
firstName?: string | null;
lastName?: string | null;
},
) {
const profileRepository = AppDataSource.getRepository(Profile);
const queryBuilder = profileRepository
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType");
if (body.citizenId || body.firstName || body.lastName) {
queryBuilder.where(
new Brackets((qb) => {
if (body.citizenId) {
qb.orWhere("profile.citizenId LIKE :citizenId", { citizenId: `%${body.citizenId}%` });
}
if (body.firstName) {
qb.orWhere("profile.firstName LIKE :firstName", { firstName: `%${body.firstName}%` });
}
if (body.lastName) {
qb.orWhere("profile.lastName LIKE :lastName", { lastName: `%${body.lastName}%` });
}
}),
);
}
const profiles = await queryBuilder.getMany();
return new HttpSuccess(profiles);
}
/**
* 6. Search Employee
*
* @summary 6. Search Employee
*
*/
@Post("search-employee")
public async SearchProfileEmployee(
@Body()
body: {
citizenId?: string | null;
firstName?: string | null;
lastName?: string | null;
},
) {
const profileRepository = AppDataSource.getRepository(ProfileEmployee);
const queryBuilder = profileRepository
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType")
.leftJoinAndSelect("profile.profileSalary", "profileSalary")
.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")
.orderBy("profileSalary.order", "DESC");
if (body.citizenId || body.firstName || body.lastName) {
queryBuilder.where(
new Brackets((qb) => {
if (body.citizenId) {
qb.orWhere("profile.citizenId LIKE :citizenId", { citizenId: `%${body.citizenId}%` });
}
if (body.firstName) {
qb.orWhere("profile.firstName LIKE :firstName", { firstName: `%${body.firstName}%` });
}
if (body.lastName) {
qb.orWhere("profile.lastName LIKE :lastName", { lastName: `%${body.lastName}%` });
}
}),
);
}
const profileEmp = await queryBuilder.getMany();
const findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true },
});
const profileEmp_ = await Promise.all(
profileEmp.map((item: ProfileEmployee) => {
const rootName =
item.current_holders.length == 0
? null
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot
?.orgRootName;
const shortName =
item.current_holders.length == 0
? null
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4 !=
null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4.orgChild4ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild3 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild3.orgChild3ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild2 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild2.orgChild2ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild1 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild1.orgChild1ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgRoot != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot.orgRootShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: null;
return {
oc: rootName,
id: item.id,
createdAt: item.createdAt,
createdUserId: item.createdUserId,
lastUpdatedAt: item.lastUpdatedAt,
lastUpdateUserId: item.lastUpdateUserId,
createdFullName: item.createdFullName,
lastUpdateFullName: item.lastUpdateFullName,
avatar: item.avatar,
avatarName: item.avatarName,
rank: item.rank,
prefix: item.prefix,
firstName: item.firstName,
lastName: item.lastName,
citizenId: item.citizenId,
position: item.position,
posLevelId: item.posLevelId,
posTypeId: item.posTypeId,
email: item.email,
phone: item.phone,
keycloak: item.keycloak,
isProbation: item.isProbation,
isLeave: item.isLeave,
leaveReason: item.leaveReason,
dateLeave: item.dateLeave,
dateRetire: item.dateRetire,
dateAppoint: item.dateAppoint,
dateRetireLaw: item.dateRetireLaw,
dateStart: item.dateStart,
govAgeAbsent: item.govAgeAbsent,
govAgePlus: item.govAgePlus,
birthDate: item.birthDate ?? new Date(),
reasonSameDate: item.reasonSameDate,
ethnicity: item.ethnicity,
telephoneNumber: item.telephoneNumber,
nationality: item.nationality,
gender: item.gender,
relationship: item.relationship,
religion: item.religion,
bloodGroup: item.bloodGroup,
registrationAddress: item.registrationAddress,
registrationProvinceId: item.registrationProvinceId,
registrationDistrictId: item.registrationDistrictId,
registrationSubDistrictId: item.registrationSubDistrictId,
registrationZipCode: item.registrationZipCode,
currentAddress: item.currentAddress,
currentProvinceId: item.currentProvinceId,
currentDistrictId: item.currentDistrictId,
currentSubDistrictId: item.currentSubDistrictId,
currentZipCode: item.currentZipCode,
posLevel: item.posLevel,
posType: item.posType,
posNo: shortName,
};
}),
);
return new HttpSuccess(profileEmp_);
}
/**
* 2. API ข้อมูลหน่วยงานตามโครงสร้าง
*
* @summary 2. API ข้อมูลหน่วยงานตามโครงสร้าง
*
* @param {string} id Id หน่วยงาน
*/
@Get("org/{id}")
async GetOrganizationById(@Path() id: string) {
const orgRoot = await this.orgRootRepo.findOne({
where: { id: id },
});
if (!orgRoot) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess(orgRoot);
}
@Get("agency/{id}")
async GetOrgAgencyById(@Path() id: string) {
const orgRoot = await this.orgRootRepo.findOne({
where: { id: id },
});
if (!orgRoot) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess(orgRoot);
}
@Get("go-agency/{id}")
async GetOrgGoAgencyById(@Path() id: string) {
const orgRoot = await this.orgRootRepo.findOne({
where: { id: id },
});
if (!orgRoot) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess(orgRoot);
}
/**
* 3. API Get Profile จาก keycloak id
*
* @summary 3. API Get Profile จาก keycloak id
*
* @param {string} keycloakId Id keycloak
*/
@Get("keycloak/{keycloakId}")
async GetProfileByKeycloakIdAsync(@Path() keycloakId: string) {
const profile = await this.profileRepo.findOne({
relations: [
"registrationProvince",
"registrationDistrict",
"registrationSubDistrict",
"currentProvince",
"currentDistrict",
"currentSubDistrict",
"posLevel",
"posType",
"profileSalary",
"profileInsignias",
"current_holders",
"current_holders.orgRevision",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
where: { keycloak: keycloakId },
order: {
profileSalary: {
commandDateAffect: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
let fullname = "";
let commanderId = "";
let commanderKeycloak = "";
if (!profile) {
const profile = await this.profileEmpRepo.findOne({
relations: [
"registrationProvince",
"registrationDistrict",
"registrationSubDistrict",
"currentProvince",
"currentDistrict",
"currentSubDistrict",
"posLevel",
"posType",
"profileSalary",
"profileInsignias",
"current_holders",
"current_holders.orgRevision",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
where: { keycloak: keycloakId },
order: {
profileSalary: {
commandDateAffect: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const org = {
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.id ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.id ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.id ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.id ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.id ?? null,
};
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: org?.child3 ?? "",
orgChild4Id: org?.child4 ?? "",
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
commanderId = pos.current_holder.id;
commanderKeycloak = pos.current_holder.keycloak;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: org?.child3 ?? "",
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
commanderId = pos.current_holder.id;
commanderKeycloak = pos.current_holder.keycloak;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
commanderId = pos.current_holder.id;
commanderKeycloak = pos.current_holder.keycloak;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: IsNull(),
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
commanderId = pos.current_holder.id;
commanderKeycloak = pos.current_holder.keycloak;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: IsNull(),
orgChild2Id: IsNull(),
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
commanderId = pos.current_holder.id;
commanderKeycloak = pos.current_holder.keycloak;
} else {
fullname = "";
commanderId = "";
commanderKeycloak = "";
}
}
}
}
}
let positionLeaveName = profile.posLevel?.posLevelName ?? null
const _profileCurrent = profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft === false &&
x.orgRevision?.orgRevisionIsCurrent === true
);
let oc = "";
if (_profileCurrent != null) {
if (_profileCurrent.orgChild1Id === null) {
oc = _profileCurrent.orgRoot?.orgRootName;
} else if (_profileCurrent.orgChild2Id === null) {
oc = `${_profileCurrent.orgChild1?.orgChild1Name} ${_profileCurrent.orgRoot?.orgRootName}`;
} else if (_profileCurrent.orgChild3Id === null) {
oc = `${_profileCurrent.orgChild2?.orgChild2Name} ${_profileCurrent.orgChild1?.orgChild1Name} ${_profileCurrent.orgRoot?.orgRootName}`;
} else if (_profileCurrent.orgChild4Id === null) {
oc = `${_profileCurrent.orgChild3?.orgChild3Name} ${_profileCurrent.orgChild2?.orgChild2Name} ${_profileCurrent.orgChild1?.orgChild1Name} ${_profileCurrent.orgRoot?.orgRootName}`;;
} else {
oc = `${_profileCurrent.orgChild4?.orgChild4Name}`
}
}
const mapProfile = {
id: profile.id,
avatar: profile.avatar,
avatarName: profile.avatarName,
rank: profile.rank,
prefix: profile.prefix,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
position: profile.position,
posLevelId: profile.posLevelId,
email: profile.email,
phone: profile.phone,
keycloak: profile.keycloak,
isProbation: profile.isProbation,
isLeave: profile.isLeave,
leaveReason: profile.leaveReason,
dateRetire: profile.dateRetire,
dateAppoint: profile.dateAppoint,
dateRetireLaw: profile.dateRetireLaw,
dateStart: profile.dateStart,
govAgeAbsent: profile.govAgeAbsent,
govAgePlus: profile.govAgePlus,
birthDate: profile.birthDate ?? new Date(),
reasonSameDate: profile.reasonSameDate,
telephoneNumber: profile.telephoneNumber,
nationality: profile.nationality,
gender: profile.gender,
relationship: profile.relationship,
religion: profile.religion,
bloodGroup: profile.bloodGroup,
registrationAddress: profile.registrationAddress,
registrationProvinceId: profile.registrationProvinceId,
registrationDistrictId: profile.registrationDistrictId,
registrationSubDistrictId: profile.registrationSubDistrictId,
registrationProvince: profile.registrationProvince.name,
registrationDistrict: profile.registrationDistrict.name,
registrationSubDistrict: profile.registrationSubDistrict.name,
registrationZipCode: profile.registrationZipCode,
currentAddress: profile.currentAddress,
currentProvinceId: profile.currentProvinceId,
currentDistrictId: profile.currentDistrictId,
currentSubDistrictId: profile.currentSubDistrictId,
currentProvince: profile.currentProvince.name,
currentDistrict: profile.currentDistrict.name,
currentSubDistrict: profile.currentSubDistrict.name,
currentZipCode: profile.currentZipCode,
dutyTimeId: profile.dutyTimeId,
dutyTimeEffectiveDate: profile.dutyTimeEffectiveDate,
amount: profile.amount,
positionSalaryAmount: profile.positionSalaryAmount,
mouthSalaryAmount: profile.mouthSalaryAmount,
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.orgRootName ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.orgChild1Name ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.orgChild2Name ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.orgChild3Name ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.orgChild4Name ?? null,
rootId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRootId ?? null,
child1Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1Id ?? null,
child2Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2Id ?? null,
child3Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3Id ?? null,
child4Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4Id ?? null,
commander: fullname,
commanderId,
commanderKeycloak,
posLevel: profile.posLevel?.posLevelName ?? null,
posType: profile.posType?.posTypeName ?? null,
profileSalary: profile.profileSalary.length > 0 ? profile.profileSalary[0] : null,
profileInsignia: profile.profileInsignias.length > 0 ? profile.profileInsignias[0] : null,
profileType: "EMPLOYEE",
positionLeaveName: positionLeaveName,
oc: oc,
};
return new HttpSuccess(mapProfile);
}
const org = {
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.id ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.id ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.id ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.id ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.id ?? null,
};
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: org?.child3 ?? "",
orgChild4Id: org?.child4 ?? "",
current_holderId: Not(IsNull()),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
commanderId = pos.current_holder.id;
commanderKeycloak = pos.current_holder.keycloak;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: org?.child3 ?? "",
orgChild4Id: IsNull(),
current_holderId: Not(IsNull()),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
commanderId = pos.current_holder.id;
commanderKeycloak = pos.current_holder.keycloak;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
current_holderId: Not(IsNull()),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
commanderId = pos.current_holder.id;
commanderKeycloak = pos.current_holder.keycloak;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: IsNull(),
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
current_holderId: Not(IsNull()),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
commanderId = pos.current_holder.id;
commanderKeycloak = pos.current_holder.keycloak;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: IsNull(),
orgChild2Id: IsNull(),
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
current_holderId: Not(IsNull()),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
commanderId = pos.current_holder.id;
commanderKeycloak = pos.current_holder.keycloak;
} else {
fullname = "";
commanderId = "";
commanderKeycloak = "";
}
}
}
}
}
let positionLeaveName = profile.posType != null && profile.posLevel != null
&& (profile.posType.posTypeName == "บริหาร" || profile.posType.posTypeName == "อำนวยการ")
? `${profile.posType?.posTypeName ?? ""}${profile.posLevel?.posLevelName ?? ""}`
: profile.posLevel?.posLevelName ?? null
const _profileCurrent = profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft === false &&
x.orgRevision?.orgRevisionIsCurrent === true
);
let oc = "";
if (_profileCurrent != null) {
if (_profileCurrent.orgChild1Id === null) {
oc = _profileCurrent.orgRoot?.orgRootName;
} else if (_profileCurrent.orgChild2Id === null) {
oc = `${_profileCurrent.orgChild1?.orgChild1Name} ${_profileCurrent.orgRoot?.orgRootName}`;
} else if (_profileCurrent.orgChild3Id === null) {
oc = `${_profileCurrent.orgChild2?.orgChild2Name} ${_profileCurrent.orgChild1?.orgChild1Name} ${_profileCurrent.orgRoot?.orgRootName}`;
} else if (_profileCurrent.orgChild4Id === null) {
oc = `${_profileCurrent.orgChild3?.orgChild3Name} ${_profileCurrent.orgChild2?.orgChild2Name} ${_profileCurrent.orgChild1?.orgChild1Name} ${_profileCurrent.orgRoot?.orgRootName}`;;
} else {
oc = `${_profileCurrent.orgChild4?.orgChild4Name}`
}
}
const mapProfile = {
id: profile.id,
avatar: profile.avatar,
avatarName: profile.avatarName,
rank: profile.rank,
prefix: profile.prefix,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
position: profile.position,
posLevelId: profile.posLevelId,
email: profile.email,
phone: profile.phone,
keycloak: profile.keycloak,
isProbation: profile.isProbation,
isLeave: profile.isLeave,
leaveReason: profile.leaveReason,
dateRetire: profile.dateRetire,
dateAppoint: profile.dateAppoint,
dateRetireLaw: profile.dateRetireLaw,
dateStart: profile.dateStart,
govAgeAbsent: profile.govAgeAbsent,
govAgePlus: profile.govAgePlus,
birthDate: profile.birthDate ?? new Date(),
reasonSameDate: profile.reasonSameDate,
telephoneNumber: profile.telephoneNumber,
nationality: profile.nationality,
gender: profile.gender,
relationship: profile.relationship,
religion: profile.religion,
bloodGroup: profile.bloodGroup,
registrationAddress: profile.registrationAddress,
registrationProvinceId: profile.registrationProvinceId,
registrationDistrictId: profile.registrationDistrictId,
registrationSubDistrictId: profile.registrationSubDistrictId,
registrationProvince: profile.registrationProvince.name,
registrationDistrict: profile.registrationDistrict.name,
registrationSubDistrict: profile.registrationSubDistrict.name,
registrationZipCode: profile.registrationZipCode,
currentAddress: profile.currentAddress,
currentProvinceId: profile.currentProvinceId,
currentDistrictId: profile.currentDistrictId,
currentSubDistrictId: profile.currentSubDistrictId,
currentProvince: profile.currentProvince.name,
currentDistrict: profile.currentDistrict.name,
currentSubDistrict: profile.currentSubDistrict.name,
currentZipCode: profile.currentZipCode,
dutyTimeId: profile.dutyTimeId,
dutyTimeEffectiveDate: profile.dutyTimeEffectiveDate,
amount: profile.amount,
positionSalaryAmount: profile.positionSalaryAmount,
mouthSalaryAmount: profile.mouthSalaryAmount,
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.orgRootName ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.orgChild1Name ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.orgChild2Name ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.orgChild3Name ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.orgChild4Name ?? null,
rootId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRootId ?? null,
child1Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1Id ?? null,
child2Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2Id ?? null,
child3Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3Id ?? null,
child4Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4Id ?? null,
commander: fullname,
commanderId,
commanderKeycloak,
posLevel: profile.posLevel?.posLevelName ?? null,
posType: profile.posType?.posTypeName ?? null,
profileSalary: profile.profileSalary.length > 0 ? profile.profileSalary[0] : null,
profileInsignia: profile.profileInsignias.length > 0 ? profile.profileInsignias[0] : null,
profileType: "OFFICER",
positionLeaveName: positionLeaveName,
oc: oc,
};
return new HttpSuccess(mapProfile);
}
/**
* 3. API Get Profile จาก profile id
*
* @summary 3. API Get Profile จาก profile id
*
* @param {string} profileId Id profile
*/
@Get("profile/{profileId}")
async GetProfileByProfileIdAsync(@Path() profileId: string) {
const profile = await this.profileRepo.findOne({
relations: [
"posLevel",
"posType",
"profileSalary",
"profileInsignias",
"current_holders",
"current_holders.orgRevision",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
where: { id: profileId },
order: {
profileSalary: {
commandDateAffect: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
if (!profile) {
const profile = await this.profileEmpRepo.findOne({
relations: [
"posLevel",
"posType",
"profileSalary",
"profileInsignias",
"current_holders",
"current_holders.orgRevision",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
where: { id: profileId },
order: {
profileSalary: {
commandDateAffect: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const org = {
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.id ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.id ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.id ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.id ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.id ?? null,
};
let fullname = "";
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: org?.child3 ?? "",
orgChild4Id: org?.child4 ?? "",
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: org?.child3 ?? "",
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: IsNull(),
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
orgRootId: org?.root ?? "",
orgChild1Id: IsNull(),
orgChild2Id: IsNull(),
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
fullname = "";
}
}
}
}
}
const mapProfile = {
id: profile.id,
avatar: profile.avatar,
avatarName: profile.avatarName,
rank: profile.rank,
prefix: profile.prefix,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
position: profile.position,
posLevelId: profile.posLevelId,
email: profile.email,
phone: profile.phone,
keycloak: profile.keycloak,
isProbation: profile.isProbation,
isLeave: profile.isLeave,
leaveReason: profile.leaveReason,
dateRetire: profile.dateRetire,
dateAppoint: profile.dateAppoint,
dateRetireLaw: profile.dateRetireLaw,
dateStart: profile.dateStart,
govAgeAbsent: profile.govAgeAbsent,
govAgePlus: profile.govAgePlus,
birthDate: profile.birthDate ?? new Date(),
reasonSameDate: profile.reasonSameDate,
telephoneNumber: profile.telephoneNumber,
nationality: profile.nationality,
gender: profile.gender,
relationship: profile.relationship,
religion: profile.religion,
bloodGroup: profile.bloodGroup,
registrationAddress: profile.registrationAddress,
registrationProvinceId: profile.registrationProvinceId,
registrationDistrictId: profile.registrationDistrictId,
registrationSubDistrictId: profile.registrationSubDistrictId,
registrationZipCode: profile.registrationZipCode,
currentAddress: profile.currentAddress,
currentProvinceId: profile.currentProvinceId,
currentSubDistrictId: profile.currentSubDistrictId,
currentZipCode: profile.currentZipCode,
dutyTimeId: profile.dutyTimeId,
dutyTimeEffectiveDate: profile.dutyTimeEffectiveDate,
amount: profile.amount,
positionSalaryAmount: profile.positionSalaryAmount,
mouthSalaryAmount: profile.mouthSalaryAmount,
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.orgRootName ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.orgChild1Name ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.orgChild2Name ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.orgChild3Name ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.orgChild4Name ?? null,
rootId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRootId ?? null,
child1Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1Id ?? null,
child2Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2Id ?? null,
child3Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3Id ?? null,
child4Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4Id ?? null,
rootDnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.ancestorDNA ?? null,
child1DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.ancestorDNA ?? null,
child2DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.ancestorDNA ?? null,
child3DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.ancestorDNA ?? null,
child4DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.ancestorDNA ?? null,
commander: fullname,
posLevel: profile.posLevel?.posLevelName ?? null,
posType: profile.posType?.posTypeName ?? null,
profileSalary: profile.profileSalary.length > 0 ? profile.profileSalary[0] : null,
profileInsignia: profile.profileInsignias.length > 0 ? profile.profileInsignias[0] : null,
profileType: "EMPLOYEE",
};
return new HttpSuccess(mapProfile);
}
const org = {
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.id ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.id ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.id ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.id ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.id ?? null,
};
let fullname = "";
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: org?.child3 ?? "",
orgChild4Id: org?.child4 ?? "",
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: org?.child3 ?? "",
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: IsNull(),
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
orgRootId: org?.root ?? "",
orgChild1Id: IsNull(),
orgChild2Id: IsNull(),
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
fullname = "";
}
}
}
}
}
const mapProfile = {
id: profile.id,
avatar: profile.avatar,
avatarName: profile.avatarName,
rank: profile.rank,
prefix: profile.prefix,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
position: profile.position,
posLevelId: profile.posLevelId,
email: profile.email,
phone: profile.phone,
keycloak: profile.keycloak,
isProbation: profile.isProbation,
isLeave: profile.isLeave,
leaveReason: profile.leaveReason,
dateRetire: profile.dateRetire,
dateAppoint: profile.dateAppoint,
dateRetireLaw: profile.dateRetireLaw,
dateStart: profile.dateStart,
govAgeAbsent: profile.govAgeAbsent,
govAgePlus: profile.govAgePlus,
birthDate: profile.birthDate ?? new Date(),
reasonSameDate: profile.reasonSameDate,
telephoneNumber: profile.telephoneNumber,
nationality: profile.nationality,
gender: profile.gender,
relationship: profile.relationship,
religion: profile.religion,
bloodGroup: profile.bloodGroup,
registrationAddress: profile.registrationAddress,
registrationProvinceId: profile.registrationProvinceId,
registrationDistrictId: profile.registrationDistrictId,
registrationSubDistrictId: profile.registrationSubDistrictId,
registrationZipCode: profile.registrationZipCode,
currentAddress: profile.currentAddress,
currentProvinceId: profile.currentProvinceId,
currentSubDistrictId: profile.currentSubDistrictId,
currentZipCode: profile.currentZipCode,
dutyTimeId: profile.dutyTimeId,
dutyTimeEffectiveDate: profile.dutyTimeEffectiveDate,
amount: profile.amount,
positionSalaryAmount: profile.positionSalaryAmount,
mouthSalaryAmount: profile.mouthSalaryAmount,
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.orgRootName ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.orgChild1Name ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.orgChild2Name ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.orgChild3Name ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.orgChild4Name ?? null,
rootId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRootId ?? null,
child1Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1Id ?? null,
child2Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2Id ?? null,
child3Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3Id ?? null,
child4Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4Id ?? null,
rootDnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.ancestorDNA ?? null,
child1DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.ancestorDNA ?? null,
child2DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.ancestorDNA ?? null,
child3DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.ancestorDNA ?? null,
child4DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.ancestorDNA ?? null,
commander: fullname,
posLevel: profile.posLevel?.posLevelName ?? null,
posType: profile.posType?.posTypeName ?? null,
profileSalary: profile.profileSalary.length > 0 ? profile.profileSalary[0] : null,
profileInsignia: profile.profileInsignias.length > 0 ? profile.profileInsignias[0] : null,
profileType: "OFFICER",
};
return new HttpSuccess(mapProfile);
}
/**
* 3. API Get Profile จาก keycloak id
*
* @summary 3. API Get Profile จาก keycloak id
*
* @param {string} citizenId Id keycloak
*/
@Get("citizenId/{citizenId}")
async GetProfileByCitizenIdAsync(@Path() citizenId: string) {
const profile = await this.profileRepo.findOne({
relations: [
"posLevel",
"posType",
"profileSalary",
"profileInsignias",
"current_holders",
"current_holders.orgRevision",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
where: { citizenId: citizenId },
order: {
profileSalary: {
commandDateAffect: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
if (!profile) {
const profile = await this.profileEmpRepo.findOne({
relations: [
"posLevel",
"posType",
"profileSalary",
"profileInsignias",
"current_holders",
"current_holders.orgRevision",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
where: { citizenId: citizenId },
order: {
profileSalary: {
commandDateAffect: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const org = {
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.id ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.id ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.id ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.id ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.id ?? null,
};
let fullname = "";
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: org?.child3 ?? "",
orgChild4Id: org?.child4 ?? "",
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: org?.child3 ?? "",
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: IsNull(),
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
orgRootId: org?.root ?? "",
orgChild1Id: IsNull(),
orgChild2Id: IsNull(),
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
fullname = "";
}
}
}
}
}
const mapProfile = {
id: profile.id,
avatar: profile.avatar,
avatarName: profile.avatarName,
rank: profile.rank,
prefix: profile.prefix,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
position: profile.position,
posLevelId: profile.posLevelId,
email: profile.email,
phone: profile.phone,
keycloak: profile.keycloak,
isProbation: profile.isProbation,
isLeave: profile.isLeave,
leaveReason: profile.leaveReason,
dateRetire: profile.dateRetire,
dateAppoint: profile.dateAppoint,
dateRetireLaw: profile.dateRetireLaw,
dateStart: profile.dateStart,
govAgeAbsent: profile.govAgeAbsent,
govAgePlus: profile.govAgePlus,
birthDate: profile.birthDate ?? new Date(),
reasonSameDate: profile.reasonSameDate,
telephoneNumber: profile.telephoneNumber,
nationality: profile.nationality,
gender: profile.gender,
relationship: profile.relationship,
religion: profile.religion,
bloodGroup: profile.bloodGroup,
registrationAddress: profile.registrationAddress,
registrationProvinceId: profile.registrationProvinceId,
registrationDistrictId: profile.registrationDistrictId,
registrationSubDistrictId: profile.registrationSubDistrictId,
registrationZipCode: profile.registrationZipCode,
currentAddress: profile.currentAddress,
currentProvinceId: profile.currentProvinceId,
currentSubDistrictId: profile.currentSubDistrictId,
currentZipCode: profile.currentZipCode,
dutyTimeId: profile.dutyTimeId,
dutyTimeEffectiveDate: profile.dutyTimeEffectiveDate,
amount: profile.amount,
positionSalaryAmount: profile.positionSalaryAmount,
mouthSalaryAmount: profile.mouthSalaryAmount,
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.orgRootName ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.orgChild1Name ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.orgChild2Name ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.orgChild3Name ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.orgChild4Name ?? null,
rootId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRootId ?? null,
child1Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1Id ?? null,
child2Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2Id ?? null,
child3Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3Id ?? null,
child4Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4Id ?? null,
commander: fullname,
posLevel: profile.posLevel?.posLevelName ?? null,
posType: profile.posType?.posTypeName ?? null,
profileSalary: profile.profileSalary.length > 0 ? profile.profileSalary[0] : null,
profileInsignia: profile.profileInsignias.length > 0 ? profile.profileInsignias[0] : null,
profileType: "EMPLOYEE",
};
return new HttpSuccess(mapProfile);
}
const org = {
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.id ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.id ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.id ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.id ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.id ?? null,
};
let fullname = "";
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: org?.child3 ?? "",
orgChild4Id: org?.child4 ?? "",
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: org?.child3 ?? "",
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: org?.child2 ?? "",
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
isDirector: true,
orgRootId: org?.root ?? "",
orgChild1Id: org?.child1 ?? "",
orgChild2Id: IsNull(),
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
let pos = await this.posMasterRepository.findOne({
relations: ["current_holder"],
where: {
orgRevision: {
orgRevisionIsCurrent: true,
orgRevisionIsDraft: false,
},
orgRootId: org?.root ?? "",
orgChild1Id: IsNull(),
orgChild2Id: IsNull(),
orgChild3Id: IsNull(),
orgChild4Id: IsNull(),
},
});
if (pos) {
fullname =
pos.current_holder.prefix +
pos.current_holder.firstName +
" " +
pos.current_holder.lastName;
} else {
fullname = "";
}
}
}
}
}
const mapProfile = {
id: profile.id,
avatar: profile.avatar,
avatarName: profile.avatarName,
rank: profile.rank,
prefix: profile.prefix,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
position: profile.position,
posLevelId: profile.posLevelId,
email: profile.email,
phone: profile.phone,
keycloak: profile.keycloak,
isProbation: profile.isProbation,
isLeave: profile.isLeave,
leaveReason: profile.leaveReason,
dateRetire: profile.dateRetire,
dateAppoint: profile.dateAppoint,
dateRetireLaw: profile.dateRetireLaw,
dateStart: profile.dateStart,
govAgeAbsent: profile.govAgeAbsent,
govAgePlus: profile.govAgePlus,
birthDate: profile.birthDate ?? new Date(),
reasonSameDate: profile.reasonSameDate,
telephoneNumber: profile.telephoneNumber,
nationality: profile.nationality,
gender: profile.gender,
relationship: profile.relationship,
religion: profile.religion,
bloodGroup: profile.bloodGroup,
registrationAddress: profile.registrationAddress,
registrationProvinceId: profile.registrationProvinceId,
registrationDistrictId: profile.registrationDistrictId,
registrationSubDistrictId: profile.registrationSubDistrictId,
registrationZipCode: profile.registrationZipCode,
currentAddress: profile.currentAddress,
currentProvinceId: profile.currentProvinceId,
currentSubDistrictId: profile.currentSubDistrictId,
currentZipCode: profile.currentZipCode,
dutyTimeId: profile.dutyTimeId,
dutyTimeEffectiveDate: profile.dutyTimeEffectiveDate,
amount: profile.amount,
positionSalaryAmount: profile.positionSalaryAmount,
mouthSalaryAmount: profile.mouthSalaryAmount,
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.orgRootName ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.orgChild1Name ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.orgChild2Name ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.orgChild3Name ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.orgChild4Name ?? null,
rootId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRootId ?? null,
child1Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1Id ?? null,
child2Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2Id ?? null,
child3Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3Id ?? null,
child4Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4Id ?? null,
commander: fullname,
posLevel: profile.posLevel?.posLevelName ?? null,
posType: profile.posType?.posTypeName ?? null,
profileSalary: profile.profileSalary.length > 0 ? profile.profileSalary[0] : null,
profileInsignia: profile.profileInsignias.length > 0 ? profile.profileInsignias[0] : null,
profileType: "OFFICER",
};
return new HttpSuccess(mapProfile);
}
/**
* 3. API Get Profile จาก keycloak id
*
* @summary 3. API Get Profile จาก keycloak id
*
* @param {string} keycloakId Id keycloak
*/
@Get("root/officer/{rootId}")
async GetProfileByRootIdAsync(@Path() rootId: string) {
const profiles = await this.profileRepo.find({
relations: [
"posLevel",
"posType",
"profileSalary",
"profileInsignias",
"profileInsignias.insignia",
"current_holders",
"current_holders.orgRevision",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
where: { current_holders: { orgRootId: rootId } },
order: {
profileSalary: {
commandDateAffect: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
const findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true },
});
const mapProfile = profiles.map((profile) => {
const shortName =
profile.current_holders.length == 0
? null
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4 !=
null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4.orgChild4ShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild3 != null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild3.orgChild3ShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild2 != null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild2.orgChild2ShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild1 != null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild1.orgChild1ShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgRoot != null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot.orgRootShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: null;
return {
id: profile.id,
avatar: profile.avatar,
avatarName: profile.avatarName,
rank: profile.rank ?? "",
prefix: profile.prefix ?? "",
firstName: profile.firstName ?? "",
lastName: profile.lastName ?? "",
citizenId: profile.citizenId ?? "",
position: profile.position ?? "",
posLevelId: profile.posLevelId,
posTypeId: profile.posTypeId,
email: profile.email,
phone: profile.phone,
keycloak: profile.keycloak,
isProbation: profile.isProbation,
isLeave: profile.isLeave,
leaveReason: profile.leaveReason,
dateRetire: profile.dateRetire,
dateAppoint: profile.dateAppoint,
dateRetireLaw: profile.dateRetireLaw,
dateStart: profile.dateStart,
govAgeAbsent: profile.govAgeAbsent,
govAgePlus: profile.govAgePlus,
birthDate: profile.birthDate ?? new Date(),
reasonSameDate: profile.reasonSameDate,
telephoneNumber: profile.telephoneNumber,
nationality: profile.nationality,
gender: profile.gender ?? "",
relationship: profile.relationship ?? "",
religion: profile.religion ?? "",
bloodGroup: profile.bloodGroup ?? "",
registrationAddress: profile.registrationAddress,
registrationProvinceId: profile.registrationProvinceId,
registrationDistrictId: profile.registrationDistrictId,
registrationSubDistrictId: profile.registrationSubDistrictId,
registrationZipCode: profile.registrationZipCode,
currentAddress: profile.currentAddress,
currentProvinceId: profile.currentProvinceId,
currentSubDistrictId: profile.currentSubDistrictId,
currentZipCode: profile.currentZipCode,
dutyTimeId: profile.dutyTimeId,
dutyTimeEffectiveDate: profile.dutyTimeEffectiveDate,
posLevel: profile.posLevel?.posLevelName ?? "",
posType: profile.posType?.posTypeName ?? "",
profileSalary: profile.profileSalary,
profileInsignia: profile.profileInsignias.map((x) => {
return { ...x, insignia: x.insignia.name };
}),
amount: profile.amount,
positionSalaryAmount: profile.positionSalaryAmount,
mouthSalaryAmount: profile.mouthSalaryAmount,
profileType: "OFFICER",
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.orgRootName ?? null,
rootId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRootId ?? null,
rootDnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.ancestorDNA ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.orgChild1Name ?? null,
child1Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1Id ?? null,
child1DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.ancestorDNA ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.orgChild2Name ?? null,
child2Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2Id ?? null,
child2DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.ancestorDNA ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.orgChild3Name ?? null,
child3Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3Id ?? null,
child3DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.ancestorDNA ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.orgChild4Name ?? null,
child4Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4Id ?? null,
child4DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.ancestorDNA ?? null,
posNo: shortName ?? "",
};
});
return new HttpSuccess(mapProfile);
}
/**
* 3. API Get Profile จาก keycloak id
*
* @summary 3. API Get Profile จาก keycloak id
*
* @param {string} keycloakId Id keycloak
*/
@Get("root/employee/{rootId}")
async GetProfileByRootIdEmpAsync(@Path() rootId: string) {
const profiles = await this.profileEmpRepo.find({
relations: [
"posLevel",
"posType",
"profileSalary",
"profileInsignias",
"profileInsignias.insignia",
"current_holders",
"current_holders.orgRevision",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
where: { current_holders: { orgRootId: rootId } },
order: {
profileSalary: {
commandDateAffect: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
const findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true },
});
const mapProfile = profiles.map((profile) => {
const shortName =
profile.current_holders.length == 0
? null
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4 !=
null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4.orgChild4ShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild3 != null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild3.orgChild3ShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild2 != null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild2.orgChild2ShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild1 != null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild1.orgChild1ShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgRoot != null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot.orgRootShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: null;
return {
id: profile.id,
avatar: profile.avatar,
avatarName: profile.avatarName,
rank: profile.rank ?? "",
prefix: profile.prefix ?? "",
firstName: profile.firstName ?? "",
lastName: profile.lastName ?? "",
citizenId: profile.citizenId ?? "",
position: profile.position ?? "",
posLevelId: profile.posLevelId,
email: profile.email,
phone: profile.phone,
keycloak: profile.keycloak,
isProbation: profile.isProbation,
isLeave: profile.isLeave,
leaveReason: profile.leaveReason,
dateRetire: profile.dateRetire,
dateAppoint: profile.dateAppoint,
dateRetireLaw: profile.dateRetireLaw,
dateStart: profile.dateStart,
govAgeAbsent: profile.govAgeAbsent,
govAgePlus: profile.govAgePlus,
birthDate: profile.birthDate ?? new Date(),
reasonSameDate: profile.reasonSameDate,
telephoneNumber: profile.telephoneNumber,
nationality: profile.nationality,
gender: profile.gender ?? "",
relationship: profile.relationship ?? "",
religion: profile.religion ?? "",
bloodGroup: profile.bloodGroup ?? "",
registrationAddress: profile.registrationAddress,
registrationProvinceId: profile.registrationProvinceId,
registrationDistrictId: profile.registrationDistrictId,
registrationSubDistrictId: profile.registrationSubDistrictId,
registrationZipCode: profile.registrationZipCode,
currentAddress: profile.currentAddress,
currentProvinceId: profile.currentProvinceId,
currentSubDistrictId: profile.currentSubDistrictId,
currentZipCode: profile.currentZipCode,
// dutyTimeId: profile.dutyTimeId,
// dutyTimeEffectiveDate: profile.dutyTimeEffectiveDate,
posLevel: profile.posLevel?.posLevelName ?? "",
posType: profile.posType?.posTypeName ?? "",
profileSalary: profile.profileSalary,
profileInsignia: profile.profileInsignias.map((x) => {
return { ...x, insignia: x.insignia.name };
}),
amount: profile.amount,
positionSalaryAmount: profile.positionSalaryAmount,
mouthSalaryAmount: profile.mouthSalaryAmount,
profileType: "EMPLOYEE",
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.orgRootName ?? null,
rootId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRootId ?? null,
posNo: shortName ?? "",
};
});
return new HttpSuccess(mapProfile);
}
/**
* 3. API Get Profile จาก keycloak id
*
* @summary 3. API Get Profile จาก keycloak id
*
* @param {string} keycloakId Id keycloak
*/
@Post("find/employee/position")
async GetProfileByPositionEmpAsync(
@Request() req: RequestWithUser,
@Body()
body: {
empPosId: string[];
rootId: string;
},
) {
const findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true },
});
const employeePosDict = await this.employeePosDictRepository.find({
where: { id: In(body.empPosId) },
});
const profiles = await this.profileEmpRepo.find({
relations: [
"posLevel",
"posType",
"profileSalary",
"profileInsignias",
"profileInsignias.insignia",
"current_holders",
"current_holders.orgRevision",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
where: employeePosDict.map((entry) => ({
posLevelId: entry.posLevelId,
posTypeId: entry.posTypeId,
position: entry.posDictName,
current_holders: { orgRootId: body.rootId },
})),
order: {
profileSalary: {
commandDateAffect: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
const mapProfile = profiles.map((profile) => {
const shortName =
profile.current_holders.length == 0
? null
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4 !=
null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4.orgChild4ShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild3 != null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild3.orgChild3ShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild2 != null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild2.orgChild2ShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild1 != null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild1.orgChild1ShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgRoot != null
? `${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot.orgRootShortName}${profile.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: null;
return {
id: profile.id,
avatar: profile.avatar,
avatarName: profile.avatarName,
rank: profile.rank ?? "",
prefix: profile.prefix ?? "",
firstName: profile.firstName ?? "",
lastName: profile.lastName ?? "",
citizenId: profile.citizenId ?? "",
position: profile.position ?? "",
posLevelId: profile.posLevelId,
email: profile.email,
phone: profile.phone,
keycloak: profile.keycloak,
isProbation: profile.isProbation,
isLeave: profile.isLeave,
leaveReason: profile.leaveReason,
dateRetire: profile.dateRetire,
dateAppoint: profile.dateAppoint,
dateRetireLaw: profile.dateRetireLaw,
dateStart: profile.dateStart,
govAgeAbsent: profile.govAgeAbsent,
govAgePlus: profile.govAgePlus,
birthDate: profile.birthDate ?? new Date(),
reasonSameDate: profile.reasonSameDate,
telephoneNumber: profile.telephoneNumber,
nationality: profile.nationality,
gender: profile.gender ?? "",
relationship: profile.relationship ?? "",
religion: profile.religion ?? "",
bloodGroup: profile.bloodGroup ?? "",
registrationAddress: profile.registrationAddress,
registrationProvinceId: profile.registrationProvinceId,
registrationDistrictId: profile.registrationDistrictId,
registrationSubDistrictId: profile.registrationSubDistrictId,
registrationZipCode: profile.registrationZipCode,
currentAddress: profile.currentAddress,
currentProvinceId: profile.currentProvinceId,
currentSubDistrictId: profile.currentSubDistrictId,
currentZipCode: profile.currentZipCode,
// dutyTimeId: profile.dutyTimeId,
// dutyTimeEffectiveDate: profile.dutyTimeEffectiveDate,
posLevel: profile.posLevel?.posLevelName ?? "",
posType: profile.posType?.posTypeName ?? "",
profileSalary: profile.profileSalary,
profileInsignia: profile.profileInsignias.map((x) => {
return { ...x, insignia: x.insignia.name };
}),
amount: profile.amount,
positionSalaryAmount: profile.positionSalaryAmount,
mouthSalaryAmount: profile.mouthSalaryAmount,
profileType: "EMPLOYEE",
root:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.orgRootName ?? null,
rootId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRootId ?? null,
rootDnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgRoot?.ancestorDNA ?? null,
child1:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.orgChild1Name ?? null,
child1Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1Id ?? null,
child1DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild1?.ancestorDNA ?? null,
child2:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.orgChild2Name ?? null,
child2Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2Id ?? null,
child2DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild2?.ancestorDNA ?? null,
child3:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.orgChild3Name ?? null,
child3Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3Id ?? null,
child3DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild3?.ancestorDNA ?? null,
child4:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.orgChild4Name ?? null,
child4Id:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4Id ?? null,
child4DnaId:
profile?.current_holders?.find(
(x) =>
x.orgRevision?.orgRevisionIsDraft == false &&
x.orgRevision?.orgRevisionIsCurrent == true,
)?.orgChild4?.ancestorDNA ?? null,
posNo: shortName ?? "",
};
});
return new HttpSuccess(mapProfile);
}
/**
* 7.1 Get ข้อมูล GetUserFullName
*
* @summary 7.1 Get ข้อมูล GetUserFullName
*
* @param {string} keycloakId Id keycloak
*/
@Get("user-fullname/{keycloakId}")
async GetUserFullName(@Path() keycloakId: string) {
const profile = await this.profileRepo.findOne({
where: { keycloak: keycloakId },
select: ["prefix", "firstName", "lastName"],
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const fullName = profile ? `${profile.prefix}${profile.firstName} ${profile.lastName}` : "-";
return new HttpSuccess(fullName);
}
/**
* 7.2 Get ข้อมูล GetUserOCId
*
* @summary 7.2 Get ข้อมูล GetUserOCId
*
* @param {string} keycloakId Id keycloak
*/
@Get("user-oc/{keycloakId}")
async getProfileByKeycloak(@Path() keycloakId: string) {
const profile = await this.profileRepo.findOne({
where: { keycloak: keycloakId },
relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"],
});
if (!profile) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
}
const orgRevisionPublish = await this.orgRevisionRepo
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne();
if (!orgRevisionPublish) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง");
}
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 _profile: any = {
profileId: profile.id,
prefix: profile.prefix,
rank: profile.rank,
avatar: profile.avatar,
avatarName: profile.avatarName,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
birthDate: profile.birthDate ?? new Date(),
position: profile.position,
rootId: root == null ? null : root.id,
root: root == null ? null : root.orgRootName,
rootShortName: root == null ? null : root.orgRootShortName,
};
return new HttpSuccess(_profile);
}
/**
* 7.3 Get ข้อมูล GetUserOCId (all node)
*
* @summary 7.3 Get ข้อมูล GetUserOCId (all node)
*
* @param {string} keycloakId Id keycloak
*/
@Get("user-oc-all/{keycloakId}")
async getAllProfileByKeycloak(@Path() keycloakId: string) {
const profile = await this.profileRepo.findOne({
where: { keycloak: keycloakId },
relations: [
"profileSalary",
"profileEducations",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
});
if (!profile) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
}
const orgRevisionPublish = await this.orgRevisionRepo
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne();
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,
},
});
const _profile: any = {
profileId: profile.id,
prefix: profile.prefix,
rank: profile.rank,
avatar: profile.avatar,
avatarName: profile.avatarName,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
birthDate: profile.birthDate ?? new Date(),
position: profile.position,
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,
};
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);
}
/**
* 8. หา root OC Id
*
* @summary 8. หา root OC Id
*
* @param {string} ocId Id หน่วยงาน
*/
@Get("root-oc/{ocId}")
async GetRootOcId(@Path() ocId: string) {
const orgRoot = await this.orgRootRepo.findOne({
where: { id: ocId },
});
if (!orgRoot) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const root = orgRoot ? orgRoot.id : "";
return new HttpSuccess(root);
}
/**
* 5. เอารายชื่อคนที่มีการ ,map keycloak id แล้ว
*
* @summary 5. เอารายชื่อคนที่มีการ ,map keycloak id แล้ว
*
*/
@Get("keycloak")
async GetProfileWithKeycloak() {
const profile = await this.profileRepo.find({
where: { keycloak: Not(IsNull()) || Not("") },
relations: [
"posType",
"posLevel",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
"profileSalary",
],
order: {
profileSalary: {
order: "DESC",
},
},
});
const findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true },
});
const profile_ = await Promise.all(
profile.map((item: Profile) => {
const rootName =
item.current_holders.length == 0
? null
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot
?.orgRootName;
const shortName =
item.current_holders.length == 0
? null
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4 !=
null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4.orgChild4ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild3 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild3.orgChild3ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild2 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild2.orgChild2ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild1 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild1.orgChild1ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgRoot != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot.orgRootShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: null;
return {
oc: rootName,
id: item.id,
createdAt: item.createdAt,
createdUserId: item.createdUserId,
lastUpdatedAt: item.lastUpdatedAt,
lastUpdateUserId: item.lastUpdateUserId,
createdFullName: item.createdFullName,
lastUpdateFullName: item.lastUpdateFullName,
avatar: item.avatar,
avatarName: item.avatarName,
rank: item.rank,
prefix: item.prefix,
firstName: item.firstName,
lastName: item.lastName,
citizenId: item.citizenId,
position: item.position,
posLevelId: item.posLevelId,
posTypeId: item.posTypeId,
email: item.email,
phone: item.phone,
keycloak: item.keycloak,
isProbation: item.isProbation,
isLeave: item.isLeave,
leaveReason: item.leaveReason,
dateLeave: item.dateLeave,
dateRetire: item.dateRetire,
dateAppoint: item.dateAppoint,
dateRetireLaw: item.dateRetireLaw,
dateStart: item.dateStart,
govAgeAbsent: item.govAgeAbsent,
govAgePlus: item.govAgePlus,
birthDate: item.birthDate ?? new Date(),
reasonSameDate: item.reasonSameDate,
ethnicity: item.ethnicity,
telephoneNumber: item.telephoneNumber,
nationality: item.nationality,
gender: item.gender,
relationship: item.relationship,
religion: item.religion,
bloodGroup: item.bloodGroup,
registrationAddress: item.registrationAddress,
registrationProvinceId: item.registrationProvinceId,
registrationDistrictId: item.registrationDistrictId,
registrationSubDistrictId: item.registrationSubDistrictId,
registrationZipCode: item.registrationZipCode,
currentAddress: item.currentAddress,
currentProvinceId: item.currentProvinceId,
currentDistrictId: item.currentDistrictId,
currentSubDistrictId: item.currentSubDistrictId,
currentZipCode: item.currentZipCode,
dutyTimeId: item.dutyTimeId,
dutyTimeEffectiveDate: item.dutyTimeEffectiveDate,
positionLevel: item.posLevel?.posLevelName ?? null,
positionType: item.posType?.posTypeName ?? null,
posNo: shortName,
};
}),
);
return new HttpSuccess(profile_);
}
/**
* 5. เอารายชื่อคนที่มีการ ,map keycloak id แล้ว
*
* @summary 5. เอารายชื่อคนที่มีการ ,map keycloak id แล้ว
*
*/
@Get("keycloak-employee")
async GetProfileWithKeycloakEmployee() {
const profile = await this.profileEmpRepo.find({
where: { keycloak: Not(IsNull()) || Not("") },
relations: [
"posType",
"posLevel",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
"profileSalary",
],
order: {
profileSalary: {
order: "DESC",
},
},
});
const findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true },
});
const profile_ = await Promise.all(
profile.map((item: ProfileEmployee) => {
const rootName =
item.current_holders.length == 0
? null
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot
?.orgRootName;
const shortName =
item.current_holders.length == 0
? null
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4 !=
null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4.orgChild4ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild3 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild3.orgChild3ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild2 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild2.orgChild2ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild1 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild1.orgChild1ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgRoot != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot.orgRootShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: null;
return {
oc: rootName,
id: item.id,
createdAt: item.createdAt,
createdUserId: item.createdUserId,
lastUpdatedAt: item.lastUpdatedAt,
lastUpdateUserId: item.lastUpdateUserId,
createdFullName: item.createdFullName,
lastUpdateFullName: item.lastUpdateFullName,
avatar: item.avatar,
avatarName: item.avatarName,
rank: item.rank,
prefix: item.prefix,
firstName: item.firstName,
lastName: item.lastName,
citizenId: item.citizenId,
position: item.position,
posLevelId: item.posLevelId,
posTypeId: item.posTypeId,
email: item.email,
phone: item.phone,
keycloak: item.keycloak,
isProbation: item.isProbation,
isLeave: item.isLeave,
leaveReason: item.leaveReason,
dateLeave: item.dateLeave,
dateRetire: item.dateRetire,
dateAppoint: item.dateAppoint,
dateRetireLaw: item.dateRetireLaw,
dateStart: item.dateStart,
govAgeAbsent: item.govAgeAbsent,
govAgePlus: item.govAgePlus,
birthDate: item.birthDate ?? new Date(),
reasonSameDate: item.reasonSameDate,
ethnicity: item.ethnicity,
telephoneNumber: item.telephoneNumber,
nationality: item.nationality,
gender: item.gender,
relationship: item.relationship,
religion: item.religion,
bloodGroup: item.bloodGroup,
registrationAddress: item.registrationAddress,
registrationProvinceId: item.registrationProvinceId,
registrationDistrictId: item.registrationDistrictId,
registrationSubDistrictId: item.registrationSubDistrictId,
registrationZipCode: item.registrationZipCode,
currentAddress: item.currentAddress,
currentProvinceId: item.currentProvinceId,
currentDistrictId: item.currentDistrictId,
currentSubDistrictId: item.currentSubDistrictId,
currentZipCode: item.currentZipCode,
dutyTimeId: item.dutyTimeId,
dutyTimeEffectiveDate: item.dutyTimeEffectiveDate,
positionLevel: item.posLevel?.posLevelName ?? null,
positionType: item.posType?.posTypeName ?? null,
posNo: shortName,
};
}),
);
return new HttpSuccess(profile_);
}
/**
* 5. เอารายชื่อคนที่มีการ ,map keycloak id แล้ว
*
* @summary 5. เอารายชื่อคนที่มีการ ,map keycloak id แล้ว
*
*/
@Post("keycloak-all-officer")
async PostProfileWithKeycloakAllOfficer(
@Request() req: RequestWithUser,
@Body()
body: {
node: number;
nodeId: string;
isAll: boolean;
isRetirement?: boolean;
revisionId?: string;
},
) {
let typeCondition: any = {};
if (body.isAll == false) {
if (body.node === 0) {
typeCondition = {
orgRootId: body.nodeId,
orgChild1Id: IsNull(),
};
} else if (body.node === 1) {
typeCondition = {
orgChild1Id: body.nodeId,
orgChild2Id: IsNull(),
};
} else if (body.node === 2) {
typeCondition = {
orgChild2Id: body.nodeId,
orgChild3Id: IsNull(),
};
} else if (body.node === 3) {
typeCondition = {
orgChild3Id: body.nodeId,
orgChild4Id: IsNull(),
};
} else if (body.node === 4) {
typeCondition = {
orgChild4Id: body.nodeId,
};
}
} else {
if (body.node === 0) {
typeCondition = {
orgRootId: body.nodeId,
};
} else if (body.node === 1) {
typeCondition = {
orgChild1Id: body.nodeId,
};
} else if (body.node === 2) {
typeCondition = {
orgChild2Id: body.nodeId,
};
} else if (body.node === 3) {
typeCondition = {
orgChild3Id: body.nodeId,
};
} else if (body.node === 4) {
typeCondition = {
orgChild4Id: body.nodeId,
};
}
}
let profile = await this.profileRepo.find({
where: { keycloak: Not(IsNull()) || Not(""), isLeave: false, current_holders: typeCondition },
relations: [
"posType",
"posLevel",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
});
if (body.isRetirement) {
profile = await this.profileRepo.find({
where: {
keycloak: Not(IsNull()) || Not(""),
isLeave: false,
current_holders: typeCondition,
isRetirement: true,
},
relations: [
"posType",
"posLevel",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
});
}
let findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true },
});
if (body.revisionId) {
findRevision = await this.orgRevisionRepo.findOne({
where: { id: body.revisionId },
});
}
const profile_ = await Promise.all(
profile.map((item: Profile) => {
const shortName =
item.current_holders.length == 0
? null
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4 !=
null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4.orgChild4ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild3 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild3.orgChild3ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild2 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild2.orgChild2ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild1 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild1.orgChild1ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgRoot != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot.orgRootShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: null;
const Oc =
item.current_holders.length == 0
? null
: body.node == 4 && item.current_holders[0].orgChild4 != null
? `${item.current_holders[0].orgChild4.orgChild4Name}/${item.current_holders[0].orgChild3.orgChild3Name}/${item.current_holders[0].orgChild2.orgChild2Name}/${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 3 && item.current_holders[0].orgChild3 != null
? `${item.current_holders[0].orgChild3.orgChild3Name}/${item.current_holders[0].orgChild2.orgChild2Name}/${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 2 && item.current_holders[0].orgChild2 != null
? `${item.current_holders[0].orgChild2.orgChild2Name}/${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 1 && item.current_holders[0].orgChild1 != null
? `${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 0 && item.current_holders[0].orgRoot != null
? `${item.current_holders[0].orgRoot.orgRootName}`
: null;
return {
id: item.id,
prefix: item.prefix,
firstName: item.firstName,
lastName: item.lastName,
citizenId: item.citizenId,
keycloak: item.keycloak,
posNo: shortName,
position: item.position,
positionLevel: item.posLevel?.posLevelName ?? null,
positionType: item.posType?.posTypeName ?? null,
oc: Oc,
};
}),
);
return new HttpSuccess(profile_);
}
/**
* 5. เอารายชื่อคน ขรก. (none-validate-keycloak)
*
* @summary 5. เอารายชื่อคน ขรก. (none-validate-keycloak)
*
*/
@Post("none-validate-keycloak-all-officer")
async PostProfileWithNoneValidateKeycloakAllOfficer(
@Request() req: RequestWithUser,
@Body()
body: {
node: number;
nodeId: string;
isAll: boolean;
isRetirement?: boolean;
revisionId?: string;
},
) {
let typeCondition: any = {};
if (body.isAll == false) {
if (body.node === 0) {
typeCondition = {
orgRootId: body.nodeId,
orgChild1Id: IsNull(),
};
} else if (body.node === 1) {
typeCondition = {
orgChild1Id: body.nodeId,
orgChild2Id: IsNull(),
};
} else if (body.node === 2) {
typeCondition = {
orgChild2Id: body.nodeId,
orgChild3Id: IsNull(),
};
} else if (body.node === 3) {
typeCondition = {
orgChild3Id: body.nodeId,
orgChild4Id: IsNull(),
};
} else if (body.node === 4) {
typeCondition = {
orgChild4Id: body.nodeId,
};
}
} else {
if (body.node === 0) {
typeCondition = {
orgRootId: body.nodeId,
};
} else if (body.node === 1) {
typeCondition = {
orgChild1Id: body.nodeId,
};
} else if (body.node === 2) {
typeCondition = {
orgChild2Id: body.nodeId,
};
} else if (body.node === 3) {
typeCondition = {
orgChild3Id: body.nodeId,
};
} else if (body.node === 4) {
typeCondition = {
orgChild4Id: body.nodeId,
};
}
}
let profile = await this.profileRepo.find({
where: { isLeave: false, isRetirement: false, current_holders: typeCondition },
relations: [
"posType",
"posLevel",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
});
if (body.isRetirement) {
profile = await this.profileRepo.find({
where: {
isLeave: false,
current_holders: typeCondition,
isRetirement: true,
},
relations: [
"posType",
"posLevel",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
});
}
let findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true },
});
if (body.revisionId) {
findRevision = await this.orgRevisionRepo.findOne({
where: { id: body.revisionId },
});
}
const profile_ = await Promise.all(
profile.map((item: Profile) => {
const shortName =
item.current_holders.length == 0
? null
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4 !=
null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4.orgChild4ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild3 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild3.orgChild3ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild2 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild2.orgChild2ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild1 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild1.orgChild1ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgRoot != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot.orgRootShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: null;
const Oc =
item.current_holders.length == 0
? null
: body.node == 4 && item.current_holders[0].orgChild4 != null
? `${item.current_holders[0].orgChild4.orgChild4Name}/${item.current_holders[0].orgChild3.orgChild3Name}/${item.current_holders[0].orgChild2.orgChild2Name}/${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 3 && item.current_holders[0].orgChild3 != null
? `${item.current_holders[0].orgChild3.orgChild3Name}/${item.current_holders[0].orgChild2.orgChild2Name}/${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 2 && item.current_holders[0].orgChild2 != null
? `${item.current_holders[0].orgChild2.orgChild2Name}/${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 1 && item.current_holders[0].orgChild1 != null
? `${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 0 && item.current_holders[0].orgRoot != null
? `${item.current_holders[0].orgRoot.orgRootName}`
: null;
return {
id: item.id,
prefix: item.prefix,
firstName: item.firstName,
lastName: item.lastName,
citizenId: item.citizenId,
keycloak: item.keycloak,
posNo: shortName,
position: item.position,
positionLevel: item.posLevel?.posLevelName ?? null,
positionType: item.posType?.posTypeName ?? null,
oc: Oc,
};
}),
);
return new HttpSuccess(profile_);
}
/**
* 5. เอารายชื่อคนที่มีการ ,map keycloak id แล้ว
*
* @summary 5. เอารายชื่อคนที่มีการ ,map keycloak id แล้ว
*
*/
@Post("keycloak-all-employee")
async PostProfileWithKeycloakAllEmployee(
@Request() req: RequestWithUser,
@Body()
body: {
node: number;
nodeId: string;
isAll: boolean;
revisionId?: string;
},
) {
let typeCondition: any = {};
if (body.isAll == false) {
if (body.node === 0) {
typeCondition = {
orgRootId: body.nodeId,
orgChild1Id: IsNull(),
};
} else if (body.node === 1) {
typeCondition = {
orgChild1Id: body.nodeId,
orgChild2Id: IsNull(),
};
} else if (body.node === 2) {
typeCondition = {
orgChild2Id: body.nodeId,
orgChild3Id: IsNull(),
};
} else if (body.node === 3) {
typeCondition = {
orgChild3Id: body.nodeId,
orgChild4Id: IsNull(),
};
} else if (body.node === 4) {
typeCondition = {
orgChild4Id: body.nodeId,
};
}
} else {
if (body.node === 0) {
typeCondition = {
orgRootId: body.nodeId,
};
} else if (body.node === 1) {
typeCondition = {
orgChild1Id: body.nodeId,
};
} else if (body.node === 2) {
typeCondition = {
orgChild2Id: body.nodeId,
};
} else if (body.node === 3) {
typeCondition = {
orgChild3Id: body.nodeId,
};
} else if (body.node === 4) {
typeCondition = {
orgChild4Id: body.nodeId,
};
}
}
const profile = await this.profileEmpRepo.find({
where: { keycloak: Not(IsNull()) || Not(""), isLeave: false, current_holders: typeCondition },
relations: [
"posType",
"posLevel",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
});
let findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true },
});
if (body.revisionId) {
findRevision = await this.orgRevisionRepo.findOne({
where: { id: body.revisionId },
});
}
const profile_ = await Promise.all(
profile.map((item: ProfileEmployee) => {
const shortName =
item.current_holders.length == 0
? null
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4 !=
null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4.orgChild4ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild3 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild3.orgChild3ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild2 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild2.orgChild2ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild1 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild1.orgChild1ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgRoot != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot.orgRootShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: null;
const Oc =
item.current_holders.length == 0
? null
: body.node == 4 && item.current_holders[0].orgChild4 != null
? `${item.current_holders[0].orgChild4.orgChild4Name}/${item.current_holders[0].orgChild3.orgChild3Name}/${item.current_holders[0].orgChild2.orgChild2Name}/${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 3 && item.current_holders[0].orgChild3 != null
? `${item.current_holders[0].orgChild3.orgChild3Name}/${item.current_holders[0].orgChild2.orgChild2Name}/${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 2 && item.current_holders[0].orgChild2 != null
? `${item.current_holders[0].orgChild2.orgChild2Name}/${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 1 && item.current_holders[0].orgChild1 != null
? `${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 0 && item.current_holders[0].orgRoot != null
? `${item.current_holders[0].orgRoot.orgRootName}`
: null;
return {
id: item.id,
prefix: item.prefix,
firstName: item.firstName,
lastName: item.lastName,
citizenId: item.citizenId,
keycloak: item.keycloak,
posNo: shortName,
position: item.position,
positionLevel: item.posLevel
? `${item.posType.posTypeShortName} ${item.posLevel.posLevelName}`
: "-",
positionType: item.posType?.posTypeName ?? null,
oc: Oc,
};
}),
);
return new HttpSuccess(profile_);
}
/**
* 5. เอารายชื่อคน ลูกจ้างประจำ (none-validate-keycloak)
*
* @summary 5. เอารายชื่อคน ลูกจ้างประจำ (none-validate-keycloak)
*
*/
@Post("none-validate-keycloak-all-employee")
async PostProfileWithNoneValidateKeycloakAllEmployee(
@Request() req: RequestWithUser,
@Body()
body: {
node: number;
nodeId: string;
isAll: boolean;
revisionId?: string;
},
) {
let typeCondition: any = {};
if (body.isAll == false) {
if (body.node === 0) {
typeCondition = {
orgRootId: body.nodeId,
orgChild1Id: IsNull(),
};
} else if (body.node === 1) {
typeCondition = {
orgChild1Id: body.nodeId,
orgChild2Id: IsNull(),
};
} else if (body.node === 2) {
typeCondition = {
orgChild2Id: body.nodeId,
orgChild3Id: IsNull(),
};
} else if (body.node === 3) {
typeCondition = {
orgChild3Id: body.nodeId,
orgChild4Id: IsNull(),
};
} else if (body.node === 4) {
typeCondition = {
orgChild4Id: body.nodeId,
};
}
} else {
if (body.node === 0) {
typeCondition = {
orgRootId: body.nodeId,
};
} else if (body.node === 1) {
typeCondition = {
orgChild1Id: body.nodeId,
};
} else if (body.node === 2) {
typeCondition = {
orgChild2Id: body.nodeId,
};
} else if (body.node === 3) {
typeCondition = {
orgChild3Id: body.nodeId,
};
} else if (body.node === 4) {
typeCondition = {
orgChild4Id: body.nodeId,
};
}
}
const profile = await this.profileEmpRepo.find({
where: { isLeave: false, isRetirement: false, current_holders: typeCondition },
relations: [
"posType",
"posLevel",
"current_holders",
"current_holders.orgRoot",
"current_holders.orgChild1",
"current_holders.orgChild2",
"current_holders.orgChild3",
"current_holders.orgChild4",
],
});
let findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true },
});
if (body.revisionId) {
findRevision = await this.orgRevisionRepo.findOne({
where: { id: body.revisionId },
});
}
const profile_ = await Promise.all(
profile.map((item: ProfileEmployee) => {
const shortName =
item.current_holders.length == 0
? null
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4 !=
null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild4.orgChild4ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild3 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild3.orgChild3ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild2 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild2.orgChild2ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) != null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgChild1 != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgChild1.orgChild1ShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: item.current_holders.find((x) => x.orgRevisionId == findRevision?.id) !=
null &&
item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)
?.orgRoot != null
? `${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.orgRoot.orgRootShortName}${item.current_holders.find((x) => x.orgRevisionId == findRevision?.id)?.posMasterNo}`
: null;
const Oc =
item.current_holders.length == 0
? null
: body.node == 4 && item.current_holders[0].orgChild4 != null
? `${item.current_holders[0].orgChild4.orgChild4Name}/${item.current_holders[0].orgChild3.orgChild3Name}/${item.current_holders[0].orgChild2.orgChild2Name}/${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 3 && item.current_holders[0].orgChild3 != null
? `${item.current_holders[0].orgChild3.orgChild3Name}/${item.current_holders[0].orgChild2.orgChild2Name}/${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 2 && item.current_holders[0].orgChild2 != null
? `${item.current_holders[0].orgChild2.orgChild2Name}/${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 1 && item.current_holders[0].orgChild1 != null
? `${item.current_holders[0].orgChild1.orgChild1Name}/${item.current_holders[0].orgRoot.orgRootName}`
: body.node == 0 && item.current_holders[0].orgRoot != null
? `${item.current_holders[0].orgRoot.orgRootName}`
: null;
return {
id: item.id,
prefix: item.prefix,
firstName: item.firstName,
lastName: item.lastName,
citizenId: item.citizenId,
keycloak: item.keycloak,
posNo: shortName,
position: item.position,
positionLevel: item.posLevel
? `${item.posType.posTypeShortName} ${item.posLevel.posLevelName}`
: "-",
positionType: item.posType?.posTypeName ?? null,
oc: Oc,
};
}),
);
return new HttpSuccess(profile_);
}
/**
* 4. API Update รอบการลงเวลา ในตาราง profile
*
* @summary 4. API Update รอบการลงเวลา ในตาราง profile
*
*/
@Put("update-dutytime")
async UpdateDutyTimeAsync(
@Request() req: RequestWithUser,
@Body()
body: {
profileId: string;
roundId: string;
effectiveDate: Date;
},
) {
const profile = await this.profileRepo.findOne({
where: { id: body.profileId },
});
if (!profile) {
const profile = await this.profileEmpRepo.findOne({
where: { id: body.profileId },
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
Object.assign(profile, body);
profile.dutyTimeId = body.roundId;
profile.dutyTimeEffectiveDate = body.effectiveDate;
profile.lastUpdateUserId = req.user.sub;
profile.lastUpdateFullName = req.user.name;
profile.lastUpdatedAt = new Date();
await this.profileEmpRepo.save(profile);
return new HttpSuccess();
}
Object.assign(profile, body);
profile.dutyTimeId = body.roundId;
profile.dutyTimeEffectiveDate = body.effectiveDate;
profile.lastUpdateUserId = req.user.sub;
profile.lastUpdateFullName = req.user.name;
profile.lastUpdatedAt = new Date();
await this.profileRepo.save(profile);
return new HttpSuccess();
}
/**
* API เพิ่มข้อมูลเครื่องราชอิสริยาภรณ์
*
* @summary ORG_ - เพิ่มข้อมูลเครื่องราชอิสริยาภรณ์ (ADMIN) #
*
*/
@Post("insignia/Dumb")
public async newInsignia(@Request() req: RequestWithUser, @Body() body: CreateProfileInsignia) {
if (!body.profileId) {
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
}
const profile = await this.profileRepo.findOneBy({ id: body.profileId });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
// const insignia = await this.insigniaMetaRepo.findOne({
// where: { name: body.insigniaId },
// });
// const _null: any = null;
// if (body && body.insigniaId) {
// const findPosLevel = await this.insigniaMetaRepo.findOne({
// where: { name: body.insigniaId },
// select: ["id", "name"],
// });
// if (findPosLevel) {
// body.insigniaId = findPosLevel.id;
// } else {
// body.insigniaId = _null;
// }
// } else {
// body.insigniaId = _null;
// }
const data = new ProfileInsignia();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
createdAt: new Date(),
lastUpdatedAt: new Date(),
};
Object.assign(data, { ...body, ...meta });
await this.insigniaRepo.save(data);
return new HttpSuccess();
}
/**
* 2. API ข้อมูลหน่วยงานตามโครงสร้าง
*
* @summary 2. API ข้อมูลหน่วยงานตามโครงสร้าง
*
* @param {string} id Id หน่วยงาน
*/
@Get("email/{id}")
async GetEmailById(@Path() id: string) {
const profile = await this.profileRepo.findOne({
where: { id: id },
});
if (profile != null) return new HttpSuccess(profile.email);
const profileEmp = await this.profileEmpRepo.findOne({
where: { id: id },
});
if (profileEmp != null) return new HttpSuccess(profileEmp.email);
return new HttpSuccess(null);
}
}