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

2131 lines
85 KiB
TypeScript
Raw Normal View History

2024-02-08 10:56:03 +07:00
import {
Controller,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
Get,
Query,
2024-03-24 19:43:37 +07:00
Example,
2024-02-08 10:56:03 +07:00
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
2024-02-08 10:56:03 +07:00
import HttpError from "../interfaces/http-error";
2024-05-03 17:38:00 +07:00
import {
Profile,
CreateProfile,
UpdateProfile,
ProfileHistory,
CreateProfileAllFields,
} from "../entities/Profile";
import { Brackets, IsNull, Like, Not } from "typeorm";
import { OrgRevision } from "../entities/OrgRevision";
import { PosMaster } from "../entities/PosMaster";
2024-02-08 15:40:47 +07:00
import { PosLevel } from "../entities/PosLevel";
import { PosType } from "../entities/PosType";
2024-05-17 09:32:07 +07:00
import {
calculateAge,
calculateRetireDate,
calculateRetireLaw,
calculateRetireYear,
} from "../interfaces/utils";
import { RequestWithUser } from "../middlewares/user";
import { Position } from "../entities/Position";
2024-05-17 09:32:07 +07:00
import { ProfileEmployee } from "../entities/ProfileEmployee";
2024-02-08 10:56:03 +07:00
@Route("api/v1/org/profile")
@Tags("Profile")
@Security("bearerAuth")
@Response(
HttpStatus.INTERNAL_SERVER_ERROR,
2024-02-08 10:56:03 +07:00
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatus.OK, "สำเร็จ")
2024-02-08 10:56:03 +07:00
export class ProfileController extends Controller {
private orgRevisionRepo = AppDataSource.getRepository(OrgRevision);
private posMasterRepo = AppDataSource.getRepository(PosMaster);
private profileRepo = AppDataSource.getRepository(Profile);
2024-05-17 09:32:07 +07:00
private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee);
private profileHistoryRepo = AppDataSource.getRepository(ProfileHistory);
private posLevelRepo = AppDataSource.getRepository(PosLevel);
private posTypeRepo = AppDataSource.getRepository(PosType);
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
private positionRepository = AppDataSource.getRepository(Position);
2024-02-08 10:56:03 +07:00
/**
*
*
* @param {string} id Id
*/
@Get("placement/{id}")
async getProfilePlacement(@Path() id: string) {
const profile = await this.profileRepo.findOne({
where: { id },
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const _caregiver = await this.profileRepo.find({
relations: { posLevel: true, posType: true },
});
const _commander = await this.profileRepo.find({
relations: { posLevel: true, posType: true },
});
const _chairman = await this.profileRepo.find({
relations: { posLevel: true, posType: true },
});
const caregiver = _caregiver.map((_data) => ({
id: _data.id,
prefix: _data.prefix,
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
position: _data.position,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true,
}));
const commander = _commander.map((_data) => ({
id: _data.id,
prefix: _data.prefix,
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
position: _data.position,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true,
}));
const chairman = _chairman.map((_data) => ({
id: _data.id,
prefix: _data.prefix,
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
position: _data.position,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true,
}));
return new HttpSuccess({ caregiver, commander, chairman });
}
/**
*
*
*/
@Get("commander")
async getProfileCommander(@Request() request: RequestWithUser) {
const profile = await this.profileRepo.findOne({
where: { keycloak: request.user.sub },
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const _caregiver = await this.profileRepo.find({
relations: { posLevel: true, posType: true },
});
const _commander = await this.profileRepo.find({
relations: { posLevel: true, posType: true },
});
const _chairman = await this.profileRepo.find({
relations: { posLevel: true, posType: true },
});
const caregiver = _caregiver.map((_data) => ({
id: _data.id,
prefix: _data.prefix,
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
position: _data.position,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true,
}));
const commander = _commander.map((_data) => ({
id: _data.id,
prefix: _data.prefix,
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
position: _data.position,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true,
}));
const chairman = _chairman.map((_data) => ({
id: _data.id,
prefix: _data.prefix,
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
position: _data.position,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
isDirector: true,
}));
return new HttpSuccess({ caregiver, commander, chairman });
}
2024-02-08 10:56:03 +07:00
/**
* API
*
* @summary ORG_065 - (ADMIN) #70
*
*/
@Post()
async createProfile(@Request() request: RequestWithUser, @Body() body: CreateProfile) {
if (await this.profileRepo.findOneBy({ citizenId: body.citizenId })) {
throw new HttpError(
HttpStatus.INTERNAL_SERVER_ERROR,
2024-05-15 13:38:00 +07:00
"รหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว",
);
2024-02-21 13:53:02 +07:00
}
2024-02-08 15:40:47 +07:00
if (body.posLevelId === "") body.posLevelId = null;
if (body.posTypeId === "") body.posTypeId = null;
2024-02-08 10:56:03 +07:00
if (body.posLevelId && !(await this.posLevelRepo.findOneBy({ id: body.posLevelId }))) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้");
}
if (body.posTypeId && !(await this.posTypeRepo.findOneBy({ id: body.posTypeId }))) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
}
2024-05-15 13:38:00 +07:00
if (body.citizenId.length !== 13) {
2024-05-16 13:39:25 +07:00
throw new HttpError(
HttpStatus.NOT_FOUND,
"กรุณากรอกข้อมูลรหัสบัตรประจำตัวประชาชนให้ครบ 13 หลัก",
);
2024-05-15 13:38:00 +07:00
}
if (body.citizenId) {
const citizenIdDigits = body.citizenId.toString().split("").map(Number);
const cal =
citizenIdDigits[0] * 13 +
citizenIdDigits[1] * 12 +
citizenIdDigits[2] * 11 +
citizenIdDigits[3] * 10 +
citizenIdDigits[4] * 9 +
citizenIdDigits[5] * 8 +
citizenIdDigits[6] * 7 +
citizenIdDigits[7] * 6 +
citizenIdDigits[8] * 5 +
citizenIdDigits[9] * 4 +
citizenIdDigits[10] * 3 +
citizenIdDigits[11] * 2;
const calStp2 = cal % 11;
let chkDigit = 11 - calStp2;
2024-05-16 13:39:25 +07:00
if (chkDigit === 10) {
chkDigit = 1;
2024-05-16 13:39:25 +07:00
} else if (chkDigit === 11) {
2024-05-15 13:38:00 +07:00
chkDigit = chkDigit % 10;
}
2024-05-15 13:38:00 +07:00
// else if(chkDigit === 11){
// chkDigit = cal % 10;
// }
2024-05-16 13:39:25 +07:00
if (citizenIdDigits[12] !== chkDigit) {
2024-05-15 13:38:00 +07:00
throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง");
}
}
2024-05-15 13:38:00 +07:00
// if (body.citizenId && (await this.profileRepo.findOneBy({ citizenId: body.citizenId }))) {
// throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว");
// }
const profile = Object.assign(new Profile(), body);
profile.isProbation = false;
profile.isLeave = false;
2024-02-28 14:47:28 +07:00
profile.createdUserId = request.user.sub;
profile.createdFullName = request.user.name;
profile.lastUpdateUserId = request.user.sub;
profile.lastUpdateFullName = request.user.name;
2024-05-15 18:06:28 +07:00
profile.dateRetire = calculateRetireDate(profile.birthDate);
2024-05-16 15:22:30 +07:00
profile.dateRetireLaw = calculateRetireLaw(profile.birthDate);
await this.profileRepo.save(profile);
2024-02-28 14:47:28 +07:00
return new HttpSuccess();
2024-02-08 10:56:03 +07:00
}
/**
* API
*
* @summary ORG_065 - (ADMIN) #XXX
*
*/
@Post("all")
2024-05-03 17:38:00 +07:00
async createProfileAll(
@Request() request: RequestWithUser,
@Body() body: CreateProfileAllFields,
) {
2024-05-14 17:24:38 +07:00
const profileExist = await this.profileRepo.findOneBy({ citizenId: body.citizenId });
if (profileExist) {
return new HttpSuccess(profileExist.id);
}
if (body.posLevelId === "") body.posLevelId = null;
if (body.posTypeId === "") body.posTypeId = null;
if (body.posLevelId && !(await this.posLevelRepo.findOneBy({ id: body.posLevelId }))) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้");
}
if (body.posTypeId && !(await this.posTypeRepo.findOneBy({ id: body.posTypeId }))) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
}
const profile: Profile = Object.assign(new Profile(), body);
profile.createdUserId = request.user.sub;
profile.createdFullName = request.user.name;
profile.lastUpdateUserId = request.user.sub;
profile.lastUpdateFullName = request.user.name;
await this.profileRepo.save(profile);
return new HttpSuccess(profile.id);
}
/**
* API
*
*
*/
@Post("cal/retire")
async calDateRetire(@Body() birthDate: Date) {
const retireDate = await calculateRetireDate(birthDate);
const age = calculateAge(birthDate);
return new HttpSuccess({ retireDate, age });
}
2024-02-08 10:56:03 +07:00
/**
* API
*
* @summary ORG_065 - (ADMIN) #70
*
* @param {string} id Id
*/
@Put("{id}")
async updateProfile(
@Request() request: RequestWithUser,
2024-02-08 10:56:03 +07:00
@Path() id: string,
@Body() body: UpdateProfile,
2024-02-08 10:56:03 +07:00
) {
const exists =
!!body.citizenId &&
(await this.profileRepo.findOne({
where: { id: Not(id), citizenId: body.citizenId },
}));
2024-02-08 10:56:03 +07:00
if (exists) {
2024-05-15 13:38:00 +07:00
throw new HttpError(HttpStatus.CONFLICT, "รหัสบัตรประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว");
2024-02-21 13:53:02 +07:00
}
if (body.posLevelId === "") body.posLevelId = null;
if (body.posTypeId === "") body.posTypeId = null;
2024-02-08 15:40:47 +07:00
if (body.posLevelId && !(await this.posLevelRepo.findOneBy({ id: body.posLevelId }))) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้");
}
if (body.posTypeId && !(await this.posTypeRepo.findOneBy({ id: body.posTypeId }))) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
2024-02-08 15:40:47 +07:00
}
2024-05-16 13:39:25 +07:00
2024-05-15 13:38:00 +07:00
if (body.citizenId && body.citizenId.length !== 13) {
2024-05-16 13:39:25 +07:00
throw new HttpError(
HttpStatus.NOT_FOUND,
"กรุณากรอกข้อมูลรหัสบัตรประจำตัวประชาชนให้ครบ 13 หลัก",
);
2024-05-15 13:38:00 +07:00
}
if (body.citizenId) {
const citizenIdDigits = body.citizenId.toString().split("").map(Number);
const cal =
citizenIdDigits[0] * 13 +
citizenIdDigits[1] * 12 +
citizenIdDigits[2] * 11 +
citizenIdDigits[3] * 10 +
citizenIdDigits[4] * 9 +
citizenIdDigits[5] * 8 +
citizenIdDigits[6] * 7 +
citizenIdDigits[7] * 6 +
citizenIdDigits[8] * 5 +
citizenIdDigits[9] * 4 +
citizenIdDigits[10] * 3 +
citizenIdDigits[11] * 2;
const calStp2 = cal % 11;
let chkDigit = 11 - calStp2;
2024-05-16 13:39:25 +07:00
if (chkDigit === 10) {
2024-05-15 13:38:00 +07:00
chkDigit = 1;
2024-05-16 13:39:25 +07:00
} else if (chkDigit === 11) {
2024-05-15 13:38:00 +07:00
chkDigit = chkDigit % 10;
}
// else if(chkDigit === 11){
// chkDigit = cal % 10;
// }
2024-05-16 13:39:25 +07:00
if (citizenIdDigits[12] !== chkDigit) {
2024-05-15 13:38:00 +07:00
throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง");
}
}
2024-02-08 15:40:47 +07:00
const record = await this.profileRepo.findOneBy({ id });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้");
await this.profileHistoryRepo.save(
Object.assign(new ProfileHistory(), {
...record,
profileId: id,
id: undefined,
}),
);
Object.assign(record, body);
record.lastUpdateUserId = request.user.sub;
record.lastUpdateFullName = request.user.name;
2024-05-15 18:06:28 +07:00
record.dateRetire = calculateRetireDate(record.birthDate);
2024-05-16 15:22:30 +07:00
record.dateRetireLaw = calculateRetireLaw(record.birthDate);
await this.profileRepo.save(record);
2024-02-28 14:47:28 +07:00
return new HttpSuccess();
2024-02-08 10:56:03 +07:00
}
/**
* API
*
* @summary ORG_065 - (ADMIN) #70
*
* @param {string} id Id
*/
@Delete("{id}")
async deleteProfile(@Path() id: string) {
const result = await this.profileRepo.delete({ id });
if (result.affected && result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
2024-02-08 10:56:03 +07:00
}
2024-02-28 14:47:28 +07:00
return new HttpSuccess();
2024-02-08 10:56:03 +07:00
}
/**
* API
*
* @summary ORG_065 - (ADMIN) #70
*
* @param {string} id Id
*/
@Get("{id}")
async getProfile(@Path() id: string) {
const profile = await this.profileRepo.findOne({
2024-03-20 15:35:18 +07:00
relations: {
posLevel: true,
posType: true,
},
2024-02-08 10:56:03 +07:00
where: { id },
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
2024-02-28 14:47:28 +07:00
return new HttpSuccess(profile);
2024-02-08 10:56:03 +07:00
}
@Get("history/{id}")
async getProfileHistory(@Path() id: string) {
const profile = await this.profileHistoryRepo.find({
relations: {
posLevel: true,
posType: true,
},
where: { profileId: id },
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess(profile);
}
2024-02-08 10:56:03 +07:00
/**
* API
*
* @summary ORG_065 - (ADMIN) #70
*
*/
@Get()
2024-03-24 19:43:37 +07:00
@Example({
status: 200,
message: "สำเร็จ",
result: {
data: [
{
id: "ecb0b34c-037e-41f2-b95e-7e19f88b42ae",
createdAt: "2024-03-24T12:39:12.105Z",
createdUserId: "00000000-0000-0000-0000-000000000000",
lastUpdatedAt: "2024-03-24T12:41:43.164Z",
lastUpdateUserId: "00000000-0000-0000-0000-000000000000",
createdFullName: "string",
lastUpdateFullName: "string",
rank: null,
2024-03-24 19:43:37 +07:00
prefix: null,
firstName: "Methapon",
lastName: "Metanipat",
citizenId: null,
position: null,
posLevelId: null,
posTypeId: null,
email: null,
phone: null,
keycloak: null,
isProbation: false,
dateRetire: null,
birthDate: null,
ethnicity: null,
telephoneNumber: null,
gender: null,
2024-03-24 19:43:37 +07:00
relationship: null,
bloodGroup: null,
posLevel: null,
posType: null,
2024-04-23 13:59:45 +07:00
org: null,
2024-03-24 19:43:37 +07:00
},
],
total: 1,
},
})
2024-02-27 17:30:37 +07:00
async listProfile(
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
2024-03-24 19:43:37 +07:00
@Query() searchField?: "firstName" | "lastName" | "fullName" | "citizenId" | "position",
2024-03-22 16:04:09 +07:00
@Query() searchKeyword: string = "",
2024-03-26 09:47:17 +07:00
@Query() posType?: string,
@Query() posLevel?: string,
@Query() yearLeave?: number,
@Query() isProbation?: boolean,
@Query() isRetire?: boolean,
) {
2024-03-26 09:47:17 +07:00
let queryLike =
"CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword";
if (searchField == "citizenId") {
queryLike = "profile.citizenId LIKE :keyword";
} else if (searchField == "position") {
queryLike = "profile.position LIKE :keyword";
2024-03-24 19:43:37 +07:00
}
2024-03-26 09:47:17 +07:00
const [record, total] = await this.profileRepo
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType")
.leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("current_holders.positions", "positions")
.leftJoinAndSelect("positions.posExecutive", "posExecutive")
2024-03-26 09:47:17 +07:00
.leftJoinAndSelect("current_holders.orgRoot", "orgRoot")
.leftJoinAndSelect("current_holders.orgChild1", "orgChild1")
.leftJoinAndSelect("current_holders.orgChild2", "orgChild2")
.leftJoinAndSelect("current_holders.orgChild3", "orgChild3")
.leftJoinAndSelect("current_holders.orgChild4", "orgChild4")
.andWhere(
searchKeyword != undefined && searchKeyword != null && searchKeyword != ""
? queryLike
: "1=1",
{
keyword: `%${searchKeyword}%`,
},
)
.andWhere(
posType != undefined && posType != null && posType != ""
? "posType.posTypeName LIKE :keyword1"
: "1=1",
{
keyword1: `${posType}`,
},
)
.andWhere(
posLevel != undefined && posLevel != null && posLevel != ""
? "posLevel.posLevelName LIKE :keyword2"
: "1=1",
{
keyword2: `${posLevel}`,
},
)
.andWhere(
isProbation != undefined && isProbation != null
? `profile.isProbation = ${isProbation}`
: "1=1",
)
.andWhere(
isRetire != undefined && isRetire != null
? isRetire == true
? `profile.dateRetire IS null`
: `profile.dateRetire IS NOT NULL`
: "1=1",
)
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
const findRevision = await this.orgRevisionRepository.findOne({
where: { orgRevisionIsCurrent: true },
});
if (!findRevision) {
throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision");
}
const data = await Promise.all(
record.map((_data) => {
const posExecutive =
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.positions.length ==
0 ||
_data.current_holders
.find((x) => x.orgRevisionId == findRevision.id)
?.positions.find((x: any) => x.positionIsSelected == true) == null ||
_data.current_holders
.find((x) => x.orgRevisionId == findRevision.id)
?.positions.find((x: any) => x.positionIsSelected == true)?.posExecutive == null
? null
: _data.current_holders
.find((x) => x.orgRevisionId == findRevision.id)
?.positions.find((x: any) => x.positionIsSelected == true)?.posExecutive
?.posExecutiveName;
const shortName =
_data.current_holders.length == 0
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 !=
null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgChild3 != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgChild2 != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgChild1 != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) !=
null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgRoot != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: null;
const root =
_data.current_holders.length == 0 ||
(_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null)
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot;
2024-04-18 12:49:02 +07:00
const child1 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1;
2024-04-23 13:59:45 +07:00
2024-04-18 12:49:02 +07:00
const child2 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2;
2024-04-23 13:59:45 +07:00
2024-04-18 12:49:02 +07:00
const child3 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3;
2024-04-23 13:59:45 +07:00
2024-04-18 12:49:02 +07:00
const child4 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4;
2024-04-23 13:59:45 +07:00
let _child1 = child1 == null ? "" : `${child1.orgChild1Name}/`;
let _child2 = child2 == null ? "" : `${child2.orgChild2Name}/`;
let _child3 = child3 == null ? "" : `${child3.orgChild3Name}/`;
let _child4 = child4 == null ? "" : `${child4.orgChild4Name}/`;
2024-04-18 12:49:02 +07:00
return {
id: _data.id,
2024-05-16 13:39:25 +07:00
avatar: _data.avatar,
avatarName: _data.avatarName,
prefix: _data.prefix,
rank: _data.rank,
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
posLevelId: _data.posLevel == null ? null : _data.posLevel.id,
posTypeId: _data.posType == null ? null : _data.posType.id,
position: _data.position,
posExecutive: posExecutive,
posNo: shortName,
rootId: root == null ? null : root.id,
root: root == null ? null : root.orgRootName,
orgRootShortName: root == null ? null : root.orgRootShortName,
orgRevisionId: root == null ? null : root.orgRevisionId,
2024-04-18 12:49:02 +07:00
org: `${_child4}${_child3}${_child2}${_child1}${root?.orgRootName}`,
};
}),
);
2024-03-24 19:43:37 +07:00
return new HttpSuccess({ data: data, total });
2024-02-08 10:56:03 +07:00
}
/**
* API
*
* @summary ORG_063 - (ADMIN) #68
*/
@Post("search")
async searchProfileOrg(
@Body()
requestBody: {
2024-05-02 17:39:07 +07:00
position?: string | null;
posLevelId?: string | null;
posTypeId?: string | null;
page: number;
pageSize: number;
keyword?: string;
},
) {
const orgRevision = await this.orgRevisionRepo.findOne({
2024-02-28 14:47:28 +07:00
where: {
orgRevisionIsDraft: true,
orgRevisionIsCurrent: false,
},
relations: ["posMasters"],
});
if (!orgRevision) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง");
2024-02-28 14:47:28 +07:00
}
const [profiles, total] = await this.profileRepo
2024-02-28 14:47:28 +07:00
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.next_holders", "next_holders")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType")
.where(
requestBody.position != null && requestBody.position != ""
? "profile.position LIKE :position"
: "1=1",
{
position: `%${requestBody.position}%`,
},
2024-02-28 14:47:28 +07:00
)
.andWhere(
new Brackets((qb) => {
qb.where(
requestBody.keyword != null && requestBody.keyword != ""
? "profile.prefix LIKE :keyword"
: "1=1",
{
keyword: `%${requestBody.keyword}%`,
},
)
.orWhere(
requestBody.keyword != null && requestBody.keyword != ""
2024-02-28 14:47:28 +07:00
? "profile.firstName LIKE :keyword"
: "1=1",
{
keyword: `%${requestBody.keyword}%`,
},
)
2024-02-28 14:47:28 +07:00
.orWhere(
requestBody.keyword != null && requestBody.keyword != ""
? "profile.lastName LIKE :keyword"
: "1=1",
{
keyword: `%${requestBody.keyword}%`,
},
)
.orWhere(
requestBody.keyword != null && requestBody.keyword != ""
? "profile.citizenId LIKE :keyword"
: "1=1",
{
keyword: `%${requestBody.keyword}%`,
},
);
}),
)
.andWhere(
requestBody.posTypeId != null && requestBody.posTypeId != ""
? "profile.posTypeId LIKE :posTypeId"
: "1=1",
{
posTypeId: `%${requestBody.posTypeId}%`,
},
)
.andWhere(
requestBody.posLevelId != null && requestBody.posLevelId != ""
? "profile.posLevelId LIKE :posLevelId"
: "1=1",
{
posLevelId: `%${requestBody.posLevelId}%`,
},
)
.andWhere(
new Brackets((qb) => {
qb.where("profile.id NOT IN (:...ids)", {
ids:
orgRevision.posMasters
.filter((x) => x.next_holderId != null)
.map((x) => x.next_holderId).length == 0
? ["zxc"]
: orgRevision.posMasters
.filter((x) => x.next_holderId != null)
.map((x) => x.next_holderId),
2024-02-28 14:47:28 +07:00
});
}),
)
.skip((requestBody.page - 1) * requestBody.pageSize)
.take(requestBody.pageSize)
.getManyAndCount();
2024-02-28 14:47:28 +07:00
const data = profiles.map((_data) => ({
id: _data.id,
prefix: _data.prefix,
rank: _data.rank,
2024-02-28 14:47:28 +07:00
firstName: _data.firstName,
lastName: _data.lastName,
citizenId: _data.citizenId,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
posType: _data.posType == null ? null : _data.posType.posTypeName,
position: _data.position,
}));
2024-02-28 14:47:28 +07:00
return new HttpSuccess({ data: data, total });
}
/**
* API keycloak
*
* @summary ORG_065 - keycloak (ADMIN) #70
*
*/
@Get("keycloak/position")
async getProfileByKeycloak(@Request() request: { user: Record<string, any> }) {
const profile = await this.profileRepo.findOne({
where: { keycloak: request.user.sub },
2024-02-27 17:30:37 +07:00
relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"],
});
if (!profile) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
2024-02-27 17:30:37 +07:00
}
const orgRevisionPublish = await this.orgRevisionRepo
2024-02-27 17:30:37 +07:00
.createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true")
.getOne();
if (!orgRevisionPublish) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง");
}
2024-02-27 17:30:37 +07:00
2024-05-01 11:08:44 +07:00
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);
2024-05-14 17:24:38 +07:00
const position = await this.positionRepository.findOne({
relations: ["posExecutive"],
2024-05-14 17:24:38 +07:00
where: {
posMasterId: posMaster?.id,
},
});
2024-04-23 13:59:45 +07:00
const _profile: any = {
profileId: profile.id,
prefix: profile.prefix,
rank: profile.rank,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
position: profile.position,
2024-05-01 11:08:44 +07:00
posMaster: 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,
2024-05-14 17:24:38 +07:00
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,
2024-02-27 17:30:37 +07:00
rootId:
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)
?.orgRootId,
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
.orgRootName,
child1Id:
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)
?.orgChild1Id,
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
.orgChild1Name,
child2Id:
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)
?.orgChild2Id,
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
.orgChild2Name,
child3Id:
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)
?.orgChild3Id,
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
.orgChild3Name,
child4Id:
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)
?.orgChild4Id,
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
.orgChild4Name,
2024-04-23 13:59:45 +07:00
node: null,
nodeId: null,
};
2024-04-23 13:59:45 +07:00
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;
}
2024-02-28 14:47:28 +07:00
return new HttpSuccess(_profile);
}
/**
* API profileid
*
* @summary ORG_065 - profileid (ADMIN) #70
*
*/
@Get("profileid/position/{id}")
async getProfileByProfileid(
@Request() request: { user: Record<string, any> },
@Path() id: string,
) {
const profile = await this.profileRepo.findOne({
where: { id: id },
relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"],
});
if (!profile) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
}
2024-05-02 17:39:07 +07:00
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 _profile: any = {
profileId: profile.id,
prefix: profile.prefix,
rank: profile.rank,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
2024-05-03 17:38:00 +07:00
birthDate: profile.birthDate,
2024-05-02 17:39:07 +07:00
position: profile.position,
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,
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);
}
/**
2024-05-02 17:54:01 +07:00
* API citizenId
2024-05-02 17:39:07 +07:00
*
2024-05-02 17:54:01 +07:00
* @summary ORG_065 - citizenId (ADMIN) #70
*
*/
@Get("citizenid/position/{id}")
async getProfileByCitizenId(
@Request() request: { user: Record<string, any> },
@Path() id: string,
) {
const profile = await this.profileRepo.findOne({
where: { citizenId: id },
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 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 _profile: any = {
profileId: profile.id,
prefix: profile.prefix,
rank: profile.rank,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
position: profile.position,
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,
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;
_profile.nodeShortName = _profile.child4ShortName;
} else if (_profile.child3Id != null) {
_profile.node = 3;
_profile.nodeId = _profile.child3Id;
_profile.nodeShortName = _profile.child3ShortName;
} else if (_profile.child2Id != null) {
_profile.node = 2;
_profile.nodeId = _profile.child2Id;
_profile.nodeShortName = _profile.child2ShortName;
} else if (_profile.child1Id != null) {
_profile.node = 1;
_profile.nodeId = _profile.child1Id;
_profile.nodeShortName = _profile.child1ShortName;
} else if (_profile.rootId != null) {
_profile.node = 0;
_profile.nodeId = _profile.rootId;
_profile.nodeShortName = _profile.rootShortName;
}
return new HttpSuccess(_profile);
}
/**
* API profileempid
*
* @summary ORG_065 - profileempid (ADMIN) #70
2024-05-02 17:39:07 +07:00
*
*/
@Get("profileempid/position/{id}")
2024-05-02 17:54:01 +07:00
async getProfileByProfileempid(
2024-05-02 17:39:07 +07:00
@Request() request: { user: Record<string, any> },
@Path() id: string,
) {
2024-05-17 09:32:07 +07:00
const profile = await this.profileEmpRepo.findOne({
2024-05-02 17:39:07 +07:00
where: { id: id },
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 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 _profile: any = {
profileId: profile.id,
prefix: profile.prefix,
rank: profile.rank,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
position: profile.position,
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,
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);
}
/**
* API
*
* @summary ORG_065 - (ADMIN) #70
*
*/
@Post("search-personal")
async getProfileBySearchKeyword(
@Body()
body: {
fieldName: string;
2024-02-16 12:16:47 +07:00
keyword?: string;
},
) {
2024-02-28 14:47:28 +07:00
let findProfile: any;
switch (body.fieldName) {
case "idcard":
findProfile = await this.profileRepo.find({
2024-02-28 14:47:28 +07:00
where: { citizenId: Like(`%${body.keyword}%`) },
relations: ["posType", "posLevel"],
});
break;
2024-02-28 14:47:28 +07:00
case "firstname":
findProfile = await this.profileRepo.find({
2024-02-28 14:47:28 +07:00
where: { firstName: Like(`%${body.keyword}%`) },
relations: ["posType", "posLevel"],
});
break;
2024-02-28 14:47:28 +07:00
case "lastname":
findProfile = await this.profileRepo.find({
2024-02-28 14:47:28 +07:00
where: { lastName: Like(`%${body.keyword}%`) },
relations: ["posType", "posLevel"],
});
break;
2024-02-28 14:47:28 +07:00
default:
findProfile = await this.profileRepo.find({
2024-02-28 14:47:28 +07:00
relations: ["posType", "posLevel"],
});
break;
}
2024-02-28 14:47:28 +07:00
const mapDataProfile = await Promise.all(
findProfile.map(async (item: Profile) => {
return {
id: item.id,
prefix: item.prefix,
rank: item.rank,
2024-02-28 14:47:28 +07:00
firstName: item.firstName,
lastName: item.lastName,
position: item.position,
idcard: item.citizenId,
email: item.email,
phone: item.phone,
};
}),
);
2024-02-28 14:47:28 +07:00
return new HttpSuccess(mapDataProfile);
}
/**
* API
*
* @summary ORG_069 - (ADMIN) #75
*
*/
@Get("search/commander")
async searchCommander(@Request() request: { user: Record<string, any> }) {
let fullName_: any = {};
let position_: any = {};
let commanderAboveFullname_: any = {};
let commanderAbovePosition_: any = {};
let commanderFullname_: any = {};
let commanderPosition_: any = {};
const findProfile = await this.profileRepo.findOne({
where: { keycloak: request.user.sub },
});
const findRevision = await this.orgRevisionRepo.findOne({
where: {
orgRevisionIsCurrent: true,
},
});
const findPosMaster = await this.posMasterRepo.findOne({
where: {
current_holderId: findProfile?.id,
orgRevisionId: findRevision?.id,
},
});
let node = 4;
let childId = findPosMaster?.orgChild4Id;
let condition: any = { orgChild4Id: childId };
if (findPosMaster?.orgChild4Id == null && findPosMaster?.orgChild3Id != null) {
node = 3;
childId = findPosMaster?.orgChild3Id;
condition = { orgChild3Id: childId, orgChild4Id: IsNull() };
} else if (findPosMaster?.orgChild3Id == null && findPosMaster?.orgChild2Id != null) {
node = 2;
childId = findPosMaster?.orgChild2Id;
condition = { orgChild2Id: childId, orgChild3Id: IsNull() };
} else if (findPosMaster?.orgChild2Id == null && findPosMaster?.orgChild1Id != null) {
node = 1;
childId = findPosMaster?.orgChild1Id;
condition = { orgChild1Id: childId, orgChild2Id: IsNull() };
} else if (findPosMaster?.orgChild1Id == null) {
node = 0;
childId = findPosMaster?.orgRootId;
condition = { orgRootId: childId, orgChild1Id: IsNull() };
}
const findCmd = await this.posMasterRepo.findOne({
where: {
current_holderId: Not(IsNull()) || Not(""),
orgRevisionId: findRevision?.id,
...condition,
},
relations: ["current_holder"],
order: { posMasterOrder: "ASC" },
});
let findOSAB: PosMaster | null = null;
let findTSAB: PosMaster | null = null;
//หาผู้บังคับบัญชาที่เหนือขึ้นไปอีก 1 ขั้น
if (node !== 0) {
findOSAB = await AppDataSource.getRepository(PosMaster)
.createQueryBuilder("posMaster")
.leftJoinAndSelect("posMaster.current_holder", "current_holder")
.where("posMaster.current_holderId IS NOT NULL")
.andWhere("posMaster.orgRevisionId = :revisionId", { revisionId: findRevision?.id })
.andWhere(
new Brackets((qb) => {
if (node === 4) {
qb.andWhere("posMaster.orgChild4Id IS NULL");
qb.andWhere("posMaster.orgChild3Id = :childId", {
childId: findPosMaster?.orgChild3Id,
});
} else if (node === 3) {
qb.andWhere("posMaster.orgChild3Id IS NULL");
qb.andWhere("posMaster.orgChild2Id = :childId", {
childId: findPosMaster?.orgChild2Id,
});
} else if (node === 2) {
qb.andWhere("posMaster.orgChild2Id IS NULL");
qb.andWhere("posMaster.orgChild1Id = :childId", {
childId: findPosMaster?.orgChild1Id,
});
} else if (node === 1) {
qb.andWhere("posMaster.orgChild1Id IS NULL");
qb.andWhere("posMaster.orgRootId = :childId", { childId: findPosMaster?.orgRootId });
}
}),
)
.orderBy("posMaster.posMasterOrder", "ASC")
.getOne();
}
//หาผู้บังคับบัญชาที่เหนือขึ้นไปอีก 2 ขั้น
if (node !== 0 && node !== 1) {
findTSAB = await AppDataSource.getRepository(PosMaster)
.createQueryBuilder("posMaster")
.leftJoinAndSelect("posMaster.current_holder", "current_holder")
.where("posMaster.current_holderId IS NOT NULL")
.andWhere("posMaster.orgRevisionId = :revisionId", { revisionId: findRevision?.id })
.andWhere(
new Brackets((qb) => {
if (node === 4) {
qb.andWhere("posMaster.orgChild3Id IS NULL");
qb.andWhere("posMaster.orgChild2Id = :childId", {
childId: findPosMaster?.orgChild2Id,
});
} else if (node === 3) {
qb.andWhere("posMaster.orgChild2Id IS NULL");
qb.andWhere("posMaster.orgChild1Id = :childId", {
childId: findPosMaster?.orgChild1Id,
});
} else if (node === 2) {
qb.andWhere("posMaster.orgChild1Id IS NULL");
qb.andWhere("posMaster.orgRootId = :childId", {
childId: findPosMaster?.orgRootId,
});
}
}),
)
.orderBy("posMaster.posMasterOrder", "ASC")
.getOne();
}
2024-02-21 13:53:02 +07:00
fullName_ =
(findProfile?.prefix ?? "") +
(findProfile?.firstName ?? "") +
" " +
(findProfile?.lastName ?? "");
position_ = findProfile?.position ?? "";
2024-02-21 13:53:02 +07:00
commanderFullname_ =
(findCmd?.current_holder?.prefix ?? "") +
(findCmd?.current_holder?.firstName ?? "") +
" " +
(findCmd?.current_holder?.lastName ?? "");
commanderPosition_ = findCmd?.current_holder?.position ?? "";
2024-02-21 13:53:02 +07:00
commanderAboveFullname_ =
(findOSAB?.current_holder?.prefix ?? "") +
(findOSAB?.current_holder?.firstName ?? "") +
" " +
(findOSAB?.current_holder?.lastName ?? "");
commanderAbovePosition_ = findOSAB?.current_holder?.position ?? "";
if (findCmd?.current_holderId == findProfile?.id) {
2024-02-21 13:53:02 +07:00
commanderFullname_ =
(findOSAB?.current_holder?.prefix ?? "") +
(findOSAB?.current_holder?.firstName ?? "") +
" " +
(findOSAB?.current_holder?.lastName ?? "");
commanderPosition_ = findOSAB?.current_holder?.position ?? "";
2024-02-21 13:53:02 +07:00
commanderAboveFullname_ =
(findTSAB?.current_holder?.prefix ?? "") +
(findTSAB?.current_holder?.firstName ?? "") +
" " +
(findTSAB?.current_holder?.lastName ?? "");
commanderAbovePosition_ = findTSAB?.current_holder?.position ?? "";
const formattedDataTSAB = {
fullname: fullName_,
position: position_,
commanderAboveFullname: commanderAboveFullname_,
commanderAbovePosition: commanderAbovePosition_,
commanderFullname: commanderFullname_,
commanderPosition: commanderPosition_,
};
return new HttpSuccess(formattedDataTSAB);
}
const formattedData = {
fullname: fullName_,
position: position_,
commanderAboveFullname: commanderAboveFullname_,
commanderAbovePosition: commanderAbovePosition_,
commanderFullname: commanderFullname_,
commanderPosition: commanderPosition_,
};
return new HttpSuccess(formattedData);
}
2024-02-21 13:53:02 +07:00
/**
* API
*
* @summary (ADMIN)
*
* @param {string} id Id
*/
@Put("citizenId/{id}")
async checkCitizenIdProfile(
@Path() id: string,
@Body()
requestBody: { citizenId: string },
) {
const profile = await this.profileRepo.findOne({
2024-02-21 13:53:02 +07:00
where: { id: Not(id), citizenId: requestBody.citizenId },
});
if (profile) {
throw new HttpError(
HttpStatus.INTERNAL_SERVER_ERROR,
"เลขประจำตัวประชาชนนี้มีอยู่ในระบบแล้ว",
);
2024-02-21 13:53:02 +07:00
}
2024-02-28 14:47:28 +07:00
return new HttpSuccess();
2024-02-21 13:53:02 +07:00
}
/**
* API
*
* @summary (ADMIN)
*
*/
@Post("probation")
async getProfileBySearchKeywordProbation(
@Body()
body: {
page: number;
pageSize: number;
keyword?: string;
},
) {
2024-02-28 14:47:28 +07:00
const [findProfile, total] = await AppDataSource.getRepository(Profile)
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType")
2024-02-28 14:47:28 +07:00
.leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("current_holders.orgRevision", "orgRevision")
.leftJoinAndSelect("current_holders.orgRoot", "orgRoot")
.leftJoinAndSelect("current_holders.orgChild1", "orgChild1")
.leftJoinAndSelect("current_holders.orgChild2", "orgChild2")
.leftJoinAndSelect("current_holders.orgChild3", "orgChild3")
.leftJoinAndSelect("current_holders.orgChild4", "orgChild4")
.leftJoinAndSelect("current_holders.positions", "positions")
.leftJoinAndSelect("positions.posExecutive", "posExecutive")
2024-02-28 14:47:28 +07:00
.where("profile.prefix LIKE :keyword", { keyword: `%${body.keyword}%` })
.orWhere("profile.firstName LIKE :keyword", { keyword: `%${body.keyword}%` })
.orWhere("profile.lastName LIKE :keyword", { keyword: `%${body.keyword}%` })
.orWhere("profile.position LIKE :keyword", { keyword: `%${body.keyword}%` })
.orWhere("posLevel.posLevelName LIKE :keyword", { keyword: `%${body.keyword}%` })
.orWhere("posType.posTypeName LIKE :keyword", { keyword: `%${body.keyword}%` })
2024-02-28 14:47:28 +07:00
.orderBy("profile.citizenId", "ASC")
.skip((body.page - 1) * body.pageSize)
.take(body.pageSize)
.getManyAndCount();
const orgRevisionActive = await this.orgRevisionRepo.findOne({
2024-02-28 14:47:28 +07:00
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
const findRevision = await this.orgRevisionRepository.findOne({
where: { orgRevisionIsCurrent: true },
});
if (!findRevision) {
throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision");
}
2024-02-28 14:47:28 +07:00
const mapDataProfile = await Promise.all(
findProfile.map(async (item: Profile) => {
const posMaster =
item.current_holders == null ||
item.current_holders.length == 0 ||
item.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: item.current_holders.find((x) => x.orgRevisionId == findRevision.id);
const position =
posMaster == null ||
item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.positions == null ||
item.current_holders?.find((x) => x.orgRevisionId == findRevision.id)?.positions.length ==
0 ||
item.current_holders
.find((x) => x.orgRevisionId == findRevision.id)
?.positions?.find((position) => position.positionIsSelected == true) == null
? null
: item.current_holders
.find((x) => x.orgRevisionId == findRevision.id)
?.positions?.find((position) => position.positionIsSelected == true);
const posExecutive =
position == null ||
item.current_holders
.find((x) => x.orgRevisionId == findRevision.id)
?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive ==
null ||
item.current_holders
.find((x) => x.orgRevisionId == findRevision.id)
?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive
?.posExecutiveName == null
? null
: item.current_holders
.find((x) => x.orgRevisionId == findRevision.id)
?.positions?.find((position) => position.positionIsSelected == true)?.posExecutive
.posExecutiveName;
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;
2024-02-28 14:47:28 +07:00
return {
id: item.id,
prefix: item.prefix,
rank: item.rank,
2024-02-28 14:47:28 +07:00
firstName: item.firstName,
lastName: item.lastName,
position: item.position,
idcard: item.citizenId,
posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName,
posTypeName: item.posType == null ? null : item.posType.posTypeName,
posNo: `${posMaster == null ? null : posMaster.posMasterNo}${shortName}`,
positionField: position == null ? null : position.positionField,
positionArea: position == null ? null : position.positionArea,
posExecutiveName: posExecutive,
positionExecutiveField: position == null ? null : position.positionExecutiveField,
2024-02-28 14:47:28 +07:00
isProbation: item.isProbation,
orgRootName:
item.current_holders == null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgRoot ==
null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgRoot
?.orgRootName == null
? null
: item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgRoot
?.orgRootName,
orgChild1Name:
item.current_holders == null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild1 ==
null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild1
?.orgChild1Name == null
? null
: item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)
?.orgChild1?.orgChild1Name,
orgChild2Name:
item.current_holders == null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild2 ==
null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild2
?.orgChild2Name == null
? null
: item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)
?.orgChild2?.orgChild2Name,
orgChild3Name:
item.current_holders == null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild3 ==
null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild3
?.orgChild3Name == null
? null
: item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)
?.orgChild3?.orgChild3Name,
orgChild4Name:
item.current_holders == null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) == null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild4 ==
null ||
item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild4
?.orgChild4Name == null
? null
: item.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)
?.orgChild4?.orgChild4Name,
};
}),
);
2024-02-28 14:47:28 +07:00
return new HttpSuccess({ data: mapDataProfile, total });
}
2024-02-27 17:20:48 +07:00
/**
* API
*
* @summary ORG_072 - #76
*
*/
2024-02-28 13:38:08 +07:00
@Post("salary/gen")
async salaryGen(
@Body()
body: {
page: number;
pageSize: number;
keyword?: string;
2024-03-06 18:28:22 +07:00
rootId?: string;
year: number;
period: string;
2024-02-28 13:38:08 +07:00
},
) {
const findRevision = await this.orgRevisionRepo.findOne({
2024-02-27 17:20:48 +07:00
where: { orgRevisionIsCurrent: true },
});
if (!findRevision) {
throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision");
2024-02-27 17:20:48 +07:00
}
2024-02-28 14:47:28 +07:00
2024-02-28 14:12:59 +07:00
const [findPosMaster, total] = await AppDataSource.getRepository(PosMaster)
2024-02-27 17:20:48 +07:00
.createQueryBuilder("posMaster")
.leftJoinAndSelect("posMaster.current_holder", "current_holder")
.leftJoinAndSelect("posMaster.orgRoot", "orgRoot")
.leftJoinAndSelect("posMaster.orgChild1", "orgChild1")
.leftJoinAndSelect("posMaster.orgChild2", "orgChild2")
.leftJoinAndSelect("posMaster.orgChild3", "orgChild3")
.leftJoinAndSelect("posMaster.orgChild4", "orgChild4")
.leftJoinAndSelect("posMaster.positions", "positions")
.leftJoinAndSelect("positions.posExecutive", "posExecutive")
.leftJoinAndSelect("current_holder.profileSalary", "profileSalary")
2024-05-14 17:24:38 +07:00
.leftJoinAndSelect("current_holder.profileDisciplines", "profileDisciplines")
2024-02-28 13:38:08 +07:00
.leftJoinAndSelect("current_holder.posLevel", "posLevel")
.leftJoinAndSelect("current_holder.posType", "posType")
2024-03-07 11:00:22 +07:00
.where((qb) => {
if (body.rootId) {
qb.andWhere("posMaster.orgRootId = :rootId", { rootId: body.rootId });
}
qb.andWhere("posMaster.current_holderId IS NOT NULL");
qb.andWhere("posMaster.orgRevisionId = :orgRevisionId", {
orgRevisionId: findRevision?.id,
});
2024-02-27 17:20:48 +07:00
})
2024-02-28 13:38:08 +07:00
.andWhere(
new Brackets((qb) => {
qb.where(
body.keyword != null && body.keyword != ""
? "current_holder.prefix LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
2024-02-28 14:47:28 +07:00
.orWhere(
body.keyword != null && body.keyword != ""
? "current_holder.firstName LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "current_holder.lastName LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "current_holder.position LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "current_holder.citizenId LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "posType.posTypeName LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
)
.orWhere(
body.keyword != null && body.keyword != ""
? "posLevel.posLevelName LIKE :keyword"
: "1=1",
{
keyword: `%${body.keyword}%`,
},
);
2024-02-28 13:38:08 +07:00
}),
)
.orderBy("current_holder.citizenId", "ASC")
2024-02-28 14:12:59 +07:00
.skip((body.page - 1) * body.pageSize)
2024-02-28 13:38:08 +07:00
.take(body.pageSize)
2024-02-28 14:12:59 +07:00
.getManyAndCount();
2024-02-27 17:20:48 +07:00
if (!findPosMaster) {
throw new HttpError(HttpStatus.NOT_FOUND, "not found. PosMaster");
2024-02-27 17:20:48 +07:00
}
2024-02-28 14:47:28 +07:00
2024-02-27 17:20:48 +07:00
const formattedData = findPosMaster.map((item) => {
let orgShortName = "";
if (item.orgChild1Id === null) {
orgShortName = item.orgRoot?.orgRootShortName;
} else if (item.orgChild2Id === null) {
orgShortName = item.orgChild1?.orgChild1ShortName;
} else if (item.orgChild3Id === null) {
orgShortName = item.orgChild2?.orgChild2ShortName;
} else if (item.orgChild4Id === null) {
orgShortName = item.orgChild3?.orgChild3ShortName;
} else {
orgShortName = item.orgChild4?.orgChild4ShortName;
}
const posExecutive =
item.positions == null ||
item.positions?.find((position) => position.positionIsSelected == true) == null ||
item.positions?.find((position) => position.positionIsSelected == true)?.posExecutive ==
null ||
item.positions?.find((position) => position.positionIsSelected == true)?.posExecutive
?.posExecutiveName == null
2024-02-27 17:20:48 +07:00
? null
: item.positions?.find((position) => position.positionIsSelected == true)?.posExecutive
2024-02-27 17:20:48 +07:00
.posExecutiveName;
const amount =
item.current_holder == null || item.current_holder.profileSalary.length == 0
? null
: item.current_holder.profileSalary.sort((a: any, b: any) => b.date - a.date)[0].amount;
let datePeriodStart = new Date(
`${new Date().getFullYear()}-${String(new Date().getMonth() + 1).padStart(2, "0")}-${String(new Date().getDate() + 1).padStart(2, "0")}T00:00:00.000Z`,
);
let datePeriodEnd = new Date(
`${new Date().getFullYear()}-${String(new Date().getMonth() + 1).padStart(2, "0")}-${String(new Date().getDate() + 1).padStart(2, "0")}T00:00:00.000Z`,
);
if (body.period.toLocaleUpperCase() == "APR") {
datePeriodStart = new Date(`${body.year}-03-31T00:00:00.000Z`);
datePeriodEnd = new Date(`${body.year}-03-31T00:00:00.000Z`);
}
if (body.period.toLocaleUpperCase() == "OCT") {
datePeriodStart = new Date(`${body.year}-09-30T00:00:00.000Z`);
datePeriodEnd = new Date(`${body.year}-09-30T00:00:00.000Z`);
}
datePeriodStart = new Date(
new Date(datePeriodStart.setDate(datePeriodStart.getDate() + 1)).setMonth(
datePeriodStart.getMonth() - 6,
),
);
2024-02-27 17:20:48 +07:00
return {
prefix: item.current_holder.prefix,
rank: item.current_holder.rank,
2024-02-27 17:20:48 +07:00
firstName: item.current_holder.firstName,
lastName: item.current_holder.lastName,
citizenId: item.current_holder.citizenId,
posMasterNoPrefix: item.posMasterNoPrefix,
posMasterNo: item.posMasterNo,
posMasterNoSuffix: item.posMasterNoSuffix,
orgShortName: orgShortName,
position: item.current_holder.position,
2024-03-19 11:30:49 +07:00
posType:
item.current_holder.posType == null ? null : item.current_holder.posType.posTypeName,
posLevel:
item.current_holder.posLevel == null ? null : item.current_holder.posLevel.posLevelName,
2024-02-27 17:20:48 +07:00
posExecutive: posExecutive,
amount: amount ? amount : null,
2024-02-28 13:38:08 +07:00
// revisionId: item.orgRevisionId,
2024-02-27 17:20:48 +07:00
rootId: item.orgRootId,
2024-02-28 14:47:28 +07:00
root: item.orgRoot?.orgRootName ? item.orgRoot.orgRootName : null,
2024-02-27 17:20:48 +07:00
child1Id: item.orgChild1Id,
2024-02-28 14:47:28 +07:00
child1: item.orgChild1?.orgChild1Name ? item.orgChild1.orgChild1Name : null,
2024-02-27 17:20:48 +07:00
child2Id: item.orgChild2Id,
2024-02-28 14:47:28 +07:00
child2: item.orgChild2?.orgChild2Name ? item.orgChild2.orgChild2Name : null,
2024-02-27 17:20:48 +07:00
child3Id: item.orgChild3Id,
2024-02-28 14:47:28 +07:00
child3: item.orgChild3?.orgChild3Name ? item.orgChild3.orgChild3Name : null,
2024-02-27 17:20:48 +07:00
child4Id: item.orgChild4Id,
2024-02-28 14:47:28 +07:00
child4: item.orgChild4?.orgChild4Name ? item.orgChild4.orgChild4Name : null,
2024-02-28 18:11:56 +07:00
result: null,
duration: null,
isPunish:
2024-05-14 17:24:38 +07:00
item.current_holder.profileDisciplines.filter(
(x: any) =>
new Date(
`${new Date(x.date).getFullYear()}-${String(new Date(x.date).getMonth() + 1).padStart(2, "0")}-${String(new Date(x.date).getDate() + 1).padStart(2, "0")}T00:00:00.000Z`,
) >= datePeriodStart &&
new Date(
`${new Date(x.date).getFullYear()}-${String(new Date(x.date).getMonth() + 1).padStart(2, "0")}-${String(new Date(x.date).getDate() + 1).padStart(2, "0")}T00:00:00.000Z`,
) <= datePeriodEnd,
).length > 0
? true
: false,
isSuspension: item.current_holder.dateRetire == null ? false : true,
isAbsent: false,
isLeave: false,
isRetired:
item.current_holder.birthDate == null ||
calculateRetireDate(item.current_holder.birthDate).getFullYear() != body.year
? false
: true,
2024-02-27 17:20:48 +07:00
};
});
2024-02-28 14:47:28 +07:00
return new HttpSuccess({ data: formattedData, total: total });
2024-02-27 17:20:48 +07:00
}
/**
* API keycloak by revisionId
*
* @summary keycloak by revisionId (ADMIN)
*
*/
@Get("keycloak/position/{revisionId}")
async getProfileByKeycloakByRevision(
@Path() revisionId: string,
@Request() request: { user: Record<string, any> },
) {
const profile = await this.profileRepo.findOne({
where: { keycloak: request.user.sub },
relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"],
});
if (!profile) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
}
const posMaster = await this.posMasterRepo.findOne({
where: {
current_holderId: profile.id,
2024-05-14 17:24:38 +07:00
orgRevisionId: revisionId,
},
});
2024-05-14 17:24:38 +07:00
const position = await this.positionRepository.findOne({
relations: ["posExecutive"],
2024-05-14 17:24:38 +07:00
where: {
posMasterId: posMaster?.id,
},
});
const _profile = {
profileId: profile.id,
prefix: profile.prefix,
rank: profile.rank,
firstName: profile.firstName,
lastName: profile.lastName,
citizenId: profile.citizenId,
position: profile.position,
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,
2024-05-14 17:24:38 +07:00
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:
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgRoot == null
? null
: profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgRootId,
root:
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgRoot == null
? null
: profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgRoot.orgRootName,
child1Id:
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild1 == null
? null
: profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild1Id,
child1:
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild1 == null
? null
: profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild1
.orgChild1Name,
child2Id:
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild2 == null
? null
: profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild2Id,
child2:
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild2 == null
? null
: profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild2
.orgChild2Name,
child3Id:
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild3 == null
? null
: profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild3Id,
child3:
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild3 == null
? null
: profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild3
.orgChild3Name,
child4Id:
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild4 == null
? null
: profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild4Id,
child4:
profile.current_holders == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId) == null ||
profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild4 == null
? null
: profile.current_holders.find((x) => x.orgRevisionId == revisionId)?.orgChild4
.orgChild4Name,
};
2024-02-28 14:47:28 +07:00
return new HttpSuccess(_profile);
}
2024-02-08 10:56:03 +07:00
}