hrms-api-org/src/controllers/OrganizationDotnetController.ts
2024-10-30 16:35:12 +07:00

1169 lines
44 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, 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";
@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 insigniaRepo = AppDataSource.getRepository(ProfileInsignia);
/**
* 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.profileSalarys", "profileSalarys")
.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("profileSalarys.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,
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: {
posLevel: true,
posType: true,
profileSalary: true,
profileInsignias: true,
},
where: { keycloak: keycloakId },
order: {
profileSalary: {
date: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
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,
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 ? profile.posLevel : null,
posType: profile.posType ? profile.posType : null,
profileSalary: profile.profileSalary.length > 0 ? profile.profileSalary[0] : null,
profileInsignia: profile.profileInsignias.length > 0 ? profile.profileInsignias[0] : null,
};
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: true,
posType: true,
profileSalary: true,
profileInsignias: true,
},
where: { id: profileId },
order: {
profileSalary: {
date: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
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,
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 ? profile.posLevel : null,
posType: profile.posType ? profile.posType : null,
profileSalary: profile.profileSalary,
profileInsignia: profile.profileInsignias,
};
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: true,
posType: true,
profileSalary: true,
profileInsignias: true,
},
where: { citizenId: citizenId },
order: {
profileSalary: {
date: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
if (!profile) {
const _profile = await this.profileEmpRepo.findOne({
relations: {
posLevel: true,
posType: true,
profileSalarys: true,
profileInsignias: true,
},
where: { citizenId: citizenId },
order: {
profileSalarys: {
date: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
if (!_profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
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,
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: null,
dutyTimeEffectiveDate: null,
posLevel: _profile.posLevel ? _profile.posLevel : null,
posType: _profile.posType ? _profile.posType : null,
profileSalary: _profile.profileSalarys,
profileInsignia: _profile.profileInsignias,
profileType: "EMPLOYEE",
};
return new HttpSuccess(_mapProfile);
}
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,
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 ? profile.posLevel : null,
posType: profile.posType ? profile.posType : null,
profileSalary: profile.profileSalary,
profileInsignia: profile.profileInsignias,
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: true,
posType: true,
profileSalary: true,
profileInsignias: true,
},
where: { current_holders: { orgRootId: rootId } },
order: {
profileSalary: {
date: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
// if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const mapProfile = profiles.map((profile) => ({
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,
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 ? profile.posLevel : null,
posType: profile.posType ? profile.posType : null,
profileSalary: profile.profileSalary,
profileInsignia: profile.profileInsignias,
}));
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: true,
posType: true,
profileSalarys: true,
profileInsignias: true,
},
where: { current_holders: { orgRootId: rootId } },
order: {
profileSalarys: {
date: "DESC",
},
profileInsignias: {
receiveDate: "DESC",
},
},
});
// if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const mapProfile = profiles.map((profile) => ({
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,
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 ? profile.posLevel : null,
posType: profile.posType ? profile.posType : null,
profileSalary: profile.profileSalarys,
profileInsignia: profile.profileInsignias,
}));
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,
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,
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,
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.profileSalary.length > 0 ? item.profileSalary[0].positionLevel : null,
posNo: shortName,
};
}),
);
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) 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.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();
}
}