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

374 lines
14 KiB
TypeScript
Raw Normal View History

2024-03-29 09:12:23 +07:00
import { Body, Controller, Example, Get, Patch, Path, Request, Route, Security, Tags } 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";
2024-03-29 09:12:23 +07:00
import { ProfileGovernment, UpdateProfileGovernment } from "../entities/ProfileGovernment";
2025-01-21 10:45:14 +07:00
import {
calculateAge,
calculateGovAge,
calculateRetireDate,
setLogDataDiff,
} from "../interfaces/utils";
import permission from "../interfaces/permission";
import { In } from "typeorm";
2024-03-20 16:29:40 +07:00
@Route("api/v1/org/profile/government")
@Tags("ProfileGovernment")
@Security("bearerAuth")
2024-03-20 16:29:40 +07:00
export class ProfileGovernmentHistoryController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
private govRepo = AppDataSource.getRepository(ProfileGovernment);
2024-05-23 16:44:37 +07:00
/**
*
* @summary
*
*/
@Get("user")
public async getGovHistoryUser(@Request() request: { user: Record<string, any> }) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.profileRepo.findOne({
where: { id: profile.id },
relations: {
posType: true,
posLevel: true,
},
});
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
2026-04-21 17:37:17 +07:00
// ดึงข้อมูลจาก profile ที่เก็บไว้แล้ว
2024-05-23 16:44:37 +07:00
const data = {
2026-04-21 17:37:17 +07:00
org: record.org ?? null, //สังกัด
positionField: record.positionField ?? null, //สายงาน
2024-05-23 16:44:37 +07:00
position: record.position, //ตำแหน่ง
posLevel: record.posLevel == null ? null : record.posLevel.posLevelName, //ระดับ
2026-04-21 17:37:17 +07:00
posMasterNo: record.posMasterNo ?? null, //เลขที่ตำแหน่ง
2024-05-23 16:44:37 +07:00
posType: record.posType == null ? null : record.posType.posTypeName, //ประเภท
2026-04-21 17:37:17 +07:00
posExecutive: record.posExecutive ?? null, //ตำแหน่งทางการบริหาร
positionArea: record.positionArea ?? null, //ด้าน/สาขา
positionExecutiveField: record.positionExecutiveField ?? null, //ด้านทางการบริหาร
2024-05-23 16:44:37 +07:00
dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate),
dateRetireLaw: record.dateRetireLaw ?? null,
// govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
2025-01-21 10:45:14 +07:00
govAge: await calculateGovAge(profile.id, "OFFICER"),
govAgeBkk: await calculateGovAge(profile.id, "OFFICER", true),
2024-05-23 16:44:37 +07:00
dateAppoint: record.dateAppoint,
dateStart: record.dateStart,
govAgeAbsent: record.govAgeAbsent,
govAgePlus: record.govAgePlus,
reasonSameDate: record.reasonSameDate,
};
2026-04-21 17:37:17 +07:00
2024-05-23 16:44:37 +07:00
return new HttpSuccess(data);
}
2026-04-21 17:37:17 +07:00
/**
*
* @summary
*
*/
@Get("{profileId}")
@Example({})
public async getGovHistory(@Path() profileId: string, @Request() req: RequestWithUser) {
2024-10-22 08:20:45 +07:00
let _workflow = await new permission().Workflow(req, profileId, "SYS_REGISTRY_OFFICER");
if (_workflow == false)
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
2026-04-21 17:37:17 +07:00
2025-10-25 01:55:54 +07:00
// ค้นหา profile ก่อน
const record = await this.profileRepo.findOne({
2025-10-25 01:55:54 +07:00
where: { id: profileId },
relations: ["posType", "posLevel"],
});
2026-04-21 17:37:17 +07:00
2025-10-25 01:55:54 +07:00
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล profile");
}
2026-04-21 17:37:17 +07:00
2025-10-25 01:55:54 +07:00
// ค้นหา profileSalary แยกต่างหาก
const profileWithSalary = await this.profileRepo.findOne({
where: {
id: profileId,
2025-10-25 01:55:54 +07:00
profileSalary: {
commandCode: In([
"0",
"9",
"1",
"2",
"3",
"4",
"8",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
2026-01-21 08:00:21 +07:00
"20",
]),
2025-10-25 01:55:54 +07:00
},
},
2025-10-25 01:55:54 +07:00
relations: ["profileSalary"],
2025-05-22 15:05:54 +07:00
order: {
profileSalary: {
order: "DESC",
2025-10-25 01:55:54 +07:00
createdAt: "DESC",
},
},
});
2026-04-21 17:37:17 +07:00
2025-10-25 01:55:54 +07:00
// ใช้ profileSalary จาก query ที่สอง หรือ [] ถ้าไม่เจอ
record.profileSalary = profileWithSalary?.profileSalary || [];
2026-04-21 17:37:17 +07:00
2025-10-25 01:55:54 +07:00
let _OrgLeave: any = [];
let _profileSalary: any = null;
if (record?.isLeave && record?.profileSalary.length > 0) {
2025-10-01 16:31:40 +07:00
if (record.leaveType == "RETIRE") {
2025-10-25 01:55:54 +07:00
_profileSalary =
record?.profileSalary.length > 1
? record?.profileSalary[1]
: record?.profileSalary.length > 0
? record?.profileSalary[0]
: null;
2025-10-01 16:31:40 +07:00
} else {
2025-10-25 01:55:54 +07:00
_profileSalary = record?.profileSalary.length > 0 ? record?.profileSalary[0] : null;
2025-10-01 16:31:40 +07:00
}
if (_profileSalary) {
_OrgLeave = [
_profileSalary.orgChild4 ?? null,
_profileSalary.orgChild3 ?? null,
_profileSalary.orgChild2 ?? null,
_profileSalary.orgChild1 ?? null,
_profileSalary.orgRoot ?? null,
];
} else {
_OrgLeave = [];
}
}
2025-10-25 01:55:54 +07:00
const orgLeave = _OrgLeave.filter((x: any) => x !== undefined && x !== null).join("\n");
2026-04-21 17:37:17 +07:00
// ดึงข้อมูลจาก profile ที่เก็บไว้แล้ว
const data = {
2026-04-21 17:37:17 +07:00
org: record?.isLeave == false ? (record.org ?? null) : orgLeave, //สังกัด
positionField: record.positionField ?? null, //สายงาน
2025-07-30 10:46:01 +07:00
position: record?.position, //ตำแหน่ง
posLevel: record?.posLevel == null ? null : record?.posLevel.posLevelName, //ระดับ
2025-10-25 01:55:54 +07:00
posMasterNo:
record?.isLeave == false
2026-04-21 17:37:17 +07:00
? record.posMasterNo ?? null
2025-10-25 01:55:54 +07:00
: _profileSalary != null
? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}`
: null, //เลขที่ตำแหน่ง
2025-07-30 10:46:01 +07:00
posType: record?.posType == null ? null : record?.posType.posTypeName, //ประเภท
2026-04-21 17:37:17 +07:00
posExecutive: record.posExecutive ?? null, //ตำแหน่งทางการบริหาร
positionArea: record.positionArea ?? null, //ด้าน/สาขา
positionExecutiveField: record.positionExecutiveField ?? null, //ด้านทางการบริหาร
2025-07-30 10:46:01 +07:00
dateLeave: record?.birthDate == null ? null : calculateRetireDate(record?.birthDate),
dateRetireLaw: record?.dateRetireLaw ?? null,
// govAge: record?.dateStart == null ? null : calculateAge(record?.dateStart),
2025-01-21 10:45:14 +07:00
govAge: await calculateGovAge(profileId, "OFFICER"),
govAgeBkk: await calculateGovAge(profileId, "OFFICER", true),
2025-07-30 10:46:01 +07:00
dateAppoint: record?.dateAppoint,
dateStart: record?.dateStart,
govAgeAbsent: record?.govAgeAbsent,
govAgePlus: record?.govAgePlus,
reasonSameDate: record?.reasonSameDate,
};
2026-04-21 17:37:17 +07:00
return new HttpSuccess(data);
}
2026-04-21 17:37:17 +07:00
@Get("admin/{profileId}")
public async getGovHistoryAdmin(@Path() profileId: string) {
2025-10-25 01:55:54 +07:00
// ค้นหา profile ก่อน
const record = await this.profileRepo.findOne({
2025-10-25 01:55:54 +07:00
where: { id: profileId },
relations: ["posType", "posLevel"],
});
2026-04-21 17:37:17 +07:00
2025-10-25 01:55:54 +07:00
if (!record) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล profile");
}
2026-04-21 17:37:17 +07:00
2025-10-25 01:55:54 +07:00
// ค้นหา profileSalary แยกต่างหาก
const profileWithSalary = await this.profileRepo.findOne({
where: {
id: profileId,
profileSalary: {
commandCode: In([
"0",
"9",
"1",
"2",
"3",
"4",
"8",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
2026-01-21 08:00:21 +07:00
"20",
]),
2025-10-25 01:55:54 +07:00
},
},
2025-10-25 01:55:54 +07:00
relations: ["profileSalary"],
order: {
profileSalary: {
order: "DESC",
2025-10-25 01:55:54 +07:00
createdAt: "DESC",
},
},
});
2026-04-21 17:37:17 +07:00
2025-10-25 01:55:54 +07:00
// ใช้ profileSalary จาก query ที่สอง หรือ [] ถ้าไม่เจอ
record.profileSalary = profileWithSalary?.profileSalary || [];
2026-04-21 17:37:17 +07:00
2025-10-25 01:55:54 +07:00
let _OrgLeave: any = [];
let _profileSalary: any = null;
if (record?.isLeave && record?.profileSalary.length > 0) {
2025-10-01 16:31:40 +07:00
if (record.leaveType == "RETIRE") {
2025-10-25 01:55:54 +07:00
_profileSalary =
record?.profileSalary.length > 1
? record?.profileSalary[1]
: record?.profileSalary.length > 0
? record?.profileSalary[0]
: null;
2025-10-01 16:31:40 +07:00
} else {
2025-10-25 01:55:54 +07:00
_profileSalary = record?.profileSalary.length > 0 ? record?.profileSalary[0] : null;
2025-10-01 16:31:40 +07:00
}
if (_profileSalary) {
_OrgLeave = [
_profileSalary.orgChild4 ?? null,
_profileSalary.orgChild3 ?? null,
_profileSalary.orgChild2 ?? null,
_profileSalary.orgChild1 ?? null,
_profileSalary.orgRoot ?? null,
];
} else {
_OrgLeave = [];
}
}
2025-10-25 01:55:54 +07:00
const orgLeave = _OrgLeave.filter((x: any) => x !== undefined && x !== null).join("\n");
2026-04-21 17:37:17 +07:00
// ดึงข้อมูลจาก profile ที่เก็บไว้แล้ว
const data = {
2026-04-21 17:37:17 +07:00
org: record?.isLeave == false ? (record.org ?? null) : orgLeave, //สังกัด
positionField: record.positionField ?? null, //สายงาน
2025-07-30 10:46:01 +07:00
position: record?.position, //ตำแหน่ง
posLevel: record?.posLevel == null ? null : record?.posLevel.posLevelName, //ระดับ
2025-10-25 01:55:54 +07:00
posMasterNo:
record?.isLeave == false
2026-04-21 17:37:17 +07:00
? record.posMasterNo ?? null
2025-10-25 01:55:54 +07:00
: _profileSalary != null
? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}`
: null, //เลขที่ตำแหน่ง
2025-07-30 10:46:01 +07:00
posType: record?.posType == null ? null : record?.posType.posTypeName, //ประเภท
2026-04-21 17:37:17 +07:00
posExecutive: record.posExecutive ?? null, //ตำแหน่งทางการบริหาร
positionArea: record.positionArea ?? null, //ด้าน/สาขา
positionExecutiveField: record.positionExecutiveField ?? null, //ด้านทางการบริหาร
2025-07-30 10:46:01 +07:00
dateLeave: record?.birthDate == null ? null : calculateRetireDate(record?.birthDate),
dateRetireLaw: record?.dateRetireLaw ?? null,
// govAge: record?.dateStart == null ? null : calculateAge(record?.dateStart),
2025-01-21 10:45:14 +07:00
govAge: await calculateGovAge(profileId, "OFFICER"),
govAgeBkk: await calculateGovAge(profileId, "OFFICER", true),
2025-07-30 10:46:01 +07:00
dateAppoint: record?.dateAppoint,
dateStart: record?.dateStart,
govAgeAbsent: record?.govAgeAbsent,
govAgePlus: record?.govAgePlus,
reasonSameDate: record?.reasonSameDate,
2025-10-25 01:55:54 +07:00
isLeave: record?.isLeave,
};
2026-04-21 17:37:17 +07:00
return new HttpSuccess(data);
}
2026-04-21 17:37:17 +07:00
/**
*
* @summary by keycloak
*
*/
@Get("history/user")
public async govHistoryUser(@Request() request: RequestWithUser) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const record = await this.govRepo.find({
order: { lastUpdatedAt: "DESC" },
where: { profileId: profile.id },
});
return new HttpSuccess(record);
}
2026-04-21 17:37:17 +07:00
/**
*
* @summary
*
*/
@Get("history/{profileId}")
@Example({})
public async govHistory(@Path() profileId: string, @Request() req: RequestWithUser) {
2024-10-22 08:20:45 +07:00
let _workflow = await new permission().Workflow(req, profileId, "SYS_REGISTRY_OFFICER");
if (_workflow == false)
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
const record = await this.govRepo.find({
order: { lastUpdatedAt: "DESC" },
2024-03-29 14:01:44 +07:00
where: { profileId: profileId },
});
2026-04-21 17:37:17 +07:00
2024-03-29 14:01:44 +07:00
// record.pop();
2026-04-21 17:37:17 +07:00
return new HttpSuccess(record);
}
2026-04-21 17:37:17 +07:00
/**
*
* @summary
*
*/
@Patch("{profileId}")
2024-03-22 16:32:15 +07:00
public async editGov(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileGovernment,
@Path() profileId: string,
) {
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profileId);
const record = await this.profileRepo.findOne({
where: { id: profileId },
});
2026-04-21 17:37:17 +07:00
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const before = structuredClone(record);
const history = new ProfileGovernment();
2026-04-21 17:37:17 +07:00
Object.assign(record, body);
2024-09-01 22:44:23 +07:00
Object.assign(history, { ...record, id: undefined });
2026-04-21 17:37:17 +07:00
history.profileId = profileId;
record.lastUpdateUserId = req.user.sub;
record.lastUpdateFullName = req.user.name;
2024-08-30 18:02:34 +07:00
record.lastUpdatedAt = new Date();
history.lastUpdateUserId = req.user.sub;
history.lastUpdateFullName = req.user.name;
history.createdUserId = req.user.sub;
history.createdFullName = req.user.name;
2024-08-30 18:02:34 +07:00
history.createdAt = new Date();
history.lastUpdatedAt = new Date();
2026-04-21 17:37:17 +07:00
await Promise.all([
this.profileRepo.save(record, { data: req }),
setLogDataDiff(req, { before, after: record }),
2024-10-07 14:53:27 +07:00
this.govRepo.save(history, { data: req }),
]);
2026-04-21 17:37:17 +07:00
return new HttpSuccess();
}
}
2026-04-21 17:37:17 +07:00