diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 2254124f..78ffd155 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -61,7 +61,7 @@ import { enableStatus, getUserByUsername, getRoleMappings, - removeUserRoles + removeUserRoles, } from "../keycloak"; import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation"; import { ProfileEducationHistory } from "../entities/ProfileEducationHistory"; @@ -75,6 +75,10 @@ import querystring from "querystring"; import { SubDistrict } from "../entities/SubDistrict"; import { District } from "../entities/District"; import { Province } from "../entities/Province"; +import { ProfileAssistance } from "../entities/ProfileAssistance"; +import { ProfileAssistanceHistory } from "../entities/ProfileAssistanceHistory"; +import { ProfileActposition } from "../entities/ProfileActposition"; +import { ProfileActpositionHistory } from "../entities/ProfileActpositionHistory"; @Route("api/v1/org/command") @Tags("Command") @@ -114,6 +118,10 @@ export class CommandController extends Controller { private provinceRepo = AppDataSource.getRepository(Province); private districtRepo = AppDataSource.getRepository(District); private subDistrictRepo = AppDataSource.getRepository(SubDistrict); + private assistanceRepository = AppDataSource.getRepository(ProfileAssistance); + private assistanceHistoryRepository = AppDataSource.getRepository(ProfileAssistanceHistory); + private actpositionRepository = AppDataSource.getRepository(ProfileActposition); + private actpositionHistoryRepository = AppDataSource.getRepository(ProfileActpositionHistory); /** * API list รายการคำสั่ง @@ -1350,22 +1358,22 @@ export class CommandController extends Controller { const adminToken = response.data.access_token; const today = new Date(); today.setUTCHours(0, 0, 0, 0); - let type: string = "OFFICER" + let type: string = "OFFICER"; try { const response_ = await axios.get( - process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`, + process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`, { headers: { Authorization: `Bearer ${adminToken}`, "Content-Type": "application/json", api_key: process.env.API_KEY, }, - } + }, ); if (response && response_.data.result.length > 0) { let profiles: Profile[] = []; await Promise.all( - response_.data.result.map(async (x:any) => { + response_.data.result.map(async (x: any) => { const _profile = await this.profileRepository.findOneBy({ id: x.profileId }); if (_profile) { _profile.isRetirement = true; @@ -1376,28 +1384,28 @@ export class CommandController extends Controller { _profile.lastUpdatedAt = new Date(); profiles.push(_profile); } - }) + }), ); await this.profileRepository.save(profiles); } } catch {} - type = "EMPLOYEE" + type = "EMPLOYEE"; try { const response_ = await axios.get( - process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`, + process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`, { headers: { Authorization: `Bearer ${adminToken}`, "Content-Type": "application/json", api_key: process.env.API_KEY, }, - } + }, ); if (response && response_.data.result.length > 0) { let profiles: ProfileEmployee[] = []; await Promise.all( - response_.data.result.map(async (x:any) => { + response_.data.result.map(async (x: any) => { const _profileEmp = await this.profileEmployeeRepository.findOneBy({ id: x.profileId }); if (_profileEmp) { _profileEmp.isRetirement = true; @@ -1408,7 +1416,7 @@ export class CommandController extends Controller { _profileEmp.lastUpdatedAt = new Date(); profiles.push(_profileEmp); } - }) + }), ); await this.profileEmployeeRepository.save(profiles); } @@ -3069,6 +3077,9 @@ export class CommandController extends Controller { mpCee?: string | null; refCommandCode?: string | null; refCommandName?: string | null; + officerOrg?: string | null; + dateStart?: Date | null; + dateEnd?: Date | null; }[]; }, ) { @@ -3135,6 +3146,41 @@ export class CommandController extends Controller { setLogDataDiff(req, { before, after: data }); history.profileSalaryId = data.id; await this.salaryHistoryRepo.save(history, { data: req }); + + if (item.commandId) { + const command = await this.commandRepository.findOne({ + where: { id: item.commandId }, + relations: ["commandType"], + }); + if (command != null && command.commandType.code == "C-PM-15") { + // ประวัติคำสั่งให้ช่วยราชการ + const dataAssis = new ProfileAssistance(); + + const metaAssis = { + profileId: item.profileId, + agency: item.officerOrg, + dateStart: item.dateStart, + dateEnd: item.dateEnd, + commandNo: item.refCommandNo, + refCommandDate: new Date(), + commandId: item.commandId, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(dataAssis, metaAssis); + const historyAssis = new ProfileAssistanceHistory(); + Object.assign(historyAssis, { ...dataAssis, id: undefined }); + + await this.assistanceRepository.save(dataAssis); + historyAssis.profileAssistanceId = dataAssis.id; + await this.assistanceHistoryRepository.save(historyAssis); + } + } }), ); @@ -3159,6 +3205,7 @@ export class CommandController extends Controller { positionLevel: string | null; refCommandNo: string | null; templateDoc: string | null; + commandId?: string | null; orgRoot?: string | null; orgChild1?: string | null; orgChild2?: string | null; @@ -3167,6 +3214,9 @@ export class CommandController extends Controller { mpCee?: string | null; refCommandCode?: string | null; refCommandName?: string | null; + officerOrg?: string | null; + dateStart?: Date | null; + dateEnd?: Date | null; }[]; }, ) { @@ -3208,6 +3258,40 @@ export class CommandController extends Controller { setLogDataDiff(req, { before, after: data }); history.profileSalaryId = data.id; await this.salaryHistoryRepo.save(history, { data: req }); + if (item.commandId) { + const command = await this.commandRepository.findOne({ + where: { id: item.commandId }, + relations: ["commandType"], + }); + if (command != null && command.commandType.code == "C-PM-15") { + // ประวัติคำสั่งให้ช่วยราชการ + const dataAssis = new ProfileAssistance(); + + const metaAssis = { + profileId: item.profileId, + agency: item.officerOrg, + dateStart: item.dateStart, + dateEnd: item.dateEnd, + commandNo: item.refCommandNo, + refCommandDate: new Date(), + commandId: item.commandId, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(dataAssis, metaAssis); + const historyAssis = new ProfileAssistanceHistory(); + Object.assign(historyAssis, { ...dataAssis, id: undefined }); + + await this.assistanceRepository.save(dataAssis); + historyAssis.profileAssistanceId = dataAssis.id; + await this.assistanceHistoryRepository.save(historyAssis); + } + } }), ); @@ -4216,8 +4300,7 @@ export class CommandController extends Controller { where: { name: Like("USER") }, }); const list = await getRoles(); - if (!Array.isArray(list)) - throw new Error("Failed. Cannot get role(s) data from the server."); + if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); const _null: any = null; await Promise.all( body.data.map(async (item) => { @@ -4246,7 +4329,7 @@ export class CommandController extends Controller { ) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } - + let registrationProvinceId = await this.provinceRepo.findOneBy({ id: item.bodyProfile.registrationProvinceId ?? "", }); @@ -4265,22 +4348,28 @@ export class CommandController extends Controller { let currentSubDistrictId = await this.subDistrictRepo.findOneBy({ id: item.bodyProfile.currentSubDistrictId ?? "", }); - - let _dateRetire = item.bodyProfile.birthDate == null - ? _null - : calculateRetireDate(item.bodyProfile.birthDate); - let _dateRetireLaw = item.bodyProfile.birthDate == null - ? _null - : calculateRetireLaw(item.bodyProfile.birthDate); - - let userKeycloakId:any - let result:any + + let _dateRetire = + item.bodyProfile.birthDate == null + ? _null + : calculateRetireDate(item.bodyProfile.birthDate); + let _dateRetireLaw = + item.bodyProfile.birthDate == null + ? _null + : calculateRetireLaw(item.bodyProfile.birthDate); + + let userKeycloakId: any; + let result: any; const checkUser = await getUserByUsername(item.bodyProfile.citizenId); if (checkUser.length == 0) { - userKeycloakId = await createUser(item.bodyProfile.citizenId, item.bodyProfile.citizenId, { - firstName: item.bodyProfile.firstName, - lastName: item.bodyProfile.lastName, - }); + userKeycloakId = await createUser( + item.bodyProfile.citizenId, + item.bodyProfile.citizenId, + { + firstName: item.bodyProfile.firstName, + lastName: item.bodyProfile.lastName, + }, + ); result = await addUserRoles( userKeycloakId, list @@ -4294,14 +4383,11 @@ export class CommandController extends Controller { userKeycloakId = checkUser[0].id; const rolesData = await getRoleMappings(userKeycloakId); if (rolesData) { - const _delRole = rolesData.map((x:any) => ({ - id : x.id, - name: x.name + const _delRole = rolesData.map((x: any) => ({ + id: x.id, + name: x.name, })); - await removeUserRoles( - userKeycloakId, - _delRole - ); + await removeUserRoles(userKeycloakId, _delRole); } result = await addUserRoles( userKeycloakId, @@ -4315,19 +4401,26 @@ export class CommandController extends Controller { } let profile: any = await this.profileRepository.findOne({ - where: { citizenId: item.bodyProfile.citizenId/*, isActive: true */}, + where: { citizenId: item.bodyProfile.citizenId /*, isActive: true */ }, relations: ["roleKeycloaks"], }); if (!profile) { profile = Object.assign({ ...item.bodyProfile, ...meta }); profile.dateRetire = _dateRetire; - profile.dateRetireLaw = _dateRetireLaw + profile.dateRetireLaw = _dateRetireLaw; profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : []; - profile.keycloak = userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : "" - profile.registrationProvinceId = registrationProvinceId ? registrationProvinceId.id : _null; - profile.registrationDistrictId = registrationDistrictId ? registrationDistrictId.id : _null; - profile.registrationSubDistrictId = registrationSubDistrictId ? registrationSubDistrictId.id : _null; + profile.keycloak = + userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : ""; + profile.registrationProvinceId = registrationProvinceId + ? registrationProvinceId.id + : _null; + profile.registrationDistrictId = registrationDistrictId + ? registrationDistrictId.id + : _null; + profile.registrationSubDistrictId = registrationSubDistrictId + ? registrationSubDistrictId.id + : _null; profile.currentProvinceId = currentProvinceId ? currentProvinceId.id : _null; profile.currentDistrictId = currentDistrictId ? currentDistrictId.id : _null; profile.currentSubDistrictId = currentSubDistrictId ? currentSubDistrictId.id : _null; @@ -4339,20 +4432,27 @@ export class CommandController extends Controller { await this.profileRepository.save(profile); setLogDataDiff(req, { before, after: profile }); } - //ขรก.ในระบบ หรือ ขรก.ในระบบที่สถานะพ้นจากราชการ + //ขรก.ในระบบ หรือ ขรก.ในระบบที่สถานะพ้นจากราชการ else { profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : []; - profile.keycloak = userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : ""; + profile.keycloak = + userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : ""; profile.isProbation = item.bodyProfile.isProbation; profile.isLeave = item.bodyProfile.isLeave; profile.isRetirement = false; profile.isActive = true; profile.dateLeave = _null; profile.dateRetire = _dateRetire; - profile.dateRetireLaw = _dateRetireLaw - profile.registrationProvinceId = registrationProvinceId ? registrationProvinceId.id : _null; - profile.registrationDistrictId = registrationDistrictId ? registrationDistrictId.id : _null; - profile.registrationSubDistrictId = registrationSubDistrictId ? registrationSubDistrictId.id : _null; + profile.dateRetireLaw = _dateRetireLaw; + profile.registrationProvinceId = registrationProvinceId + ? registrationProvinceId.id + : _null; + profile.registrationDistrictId = registrationDistrictId + ? registrationDistrictId.id + : _null; + profile.registrationSubDistrictId = registrationSubDistrictId + ? registrationSubDistrictId.id + : _null; profile.currentProvinceId = currentProvinceId ? currentProvinceId.id : _null; profile.currentDistrictId = currentDistrictId ? currentDistrictId.id : _null; profile.currentSubDistrictId = currentSubDistrictId ? currentSubDistrictId.id : _null; @@ -4372,7 +4472,7 @@ export class CommandController extends Controller { await this.profileRepository.save(profile); setLogDataDiff(req, { before, after: profile }); } - + if (profile && profile.id) { //Educations if (item.bodyEducations && item.bodyEducations.length > 0) { @@ -4805,12 +4905,79 @@ export class CommandController extends Controller { ) { const posMasters = await this.posMasterActRepository.find({ where: { id: In(body.refIds.map((x) => x.refId)) }, + relations: [ + "posMasterChild", + "posMaster", + "posMaster.current_holder", + "posMaster.orgRoot", + "posMaster.orgChild1", + "posMaster.orgChild2", + "posMaster.orgChild3", + "posMaster.orgChild4", + ], }); const data = posMasters.map((_data) => ({ ..._data, statusReport: "PENDING", })); await this.posMasterActRepository.save(data); + + await Promise.all( + posMasters.map(async (item) => { + if (item.posMasterChild != null && item.posMasterChild.current_holderId != null) { + // ประวัติคำสั่งให้รักษาการแทน + const dataAct = new ProfileActposition(); + const _actposition = await this.actpositionRepository.find({ + where: { profileId: item.posMasterChild.current_holderId }, + }); + const data = _actposition.map((_data) => ({ + ..._data, + status: false, + dateEnd: _data.status == true ? new Date() : _data.dateEnd, + })); + await this.posMasterActRepository.save(data); + item.posMasterChild.current_holderId; + + const shortName = + item.posMaster != null && item.posMaster.orgChild4 != null + ? `${item.posMaster.orgChild4.orgChild4ShortName}${item.posMaster.posMasterNo}` + : item.posMaster != null && item.posMaster?.orgChild3 != null + ? `${item.posMaster.orgChild3.orgChild3ShortName}${item.posMaster.posMasterNo}` + : item.posMaster != null && item.posMaster?.orgChild2 != null + ? `${item.posMaster.orgChild2.orgChild2ShortName}${item.posMaster.posMasterNo}` + : item.posMaster != null && item.posMaster?.orgChild1 != null + ? `${item.posMaster.orgChild1.orgChild1ShortName}${item.posMaster.posMasterNo}` + : item.posMaster != null && item.posMaster?.orgRoot != null + ? `${item.posMaster.orgRoot.orgRootShortName}${item.posMaster.posMasterNo}` + : null; + const metaAct = { + profileId: item.posMasterChild.current_holderId, + dateStart: new Date(), + dateEnd: null, + posNo: shortName, + position: item.posMaster.current_holder.position, + status: true, + commandNo: `${body.refIds[0].commandNo}/${Extension.ToThaiYear(body.refIds[0].commandYear)}`, + refCommandDate: new Date(), + commandId: body.refIds[0].commandId, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(dataAct, metaAct); + const historyAct = new ProfileActpositionHistory(); + Object.assign(historyAct, { ...dataAct, id: undefined }); + + await this.actpositionRepository.save(dataAct); + historyAct.profileActpositionId = dataAct.id; + await this.actpositionHistoryRepository.save(historyAct); + } + }), + ); return new HttpSuccess(); } diff --git a/src/entities/ProfileActposition.ts b/src/entities/ProfileActposition.ts index dd95a458..bdfd2391 100644 --- a/src/entities/ProfileActposition.ts +++ b/src/entities/ProfileActposition.ts @@ -3,6 +3,7 @@ import { EntityBase } from "./base/Base"; import { Profile } from "./Profile"; import { ProfileActpositionHistory } from "./ProfileActpositionHistory"; import { ProfileEmployee } from "./ProfileEmployee"; +import { Command } from "./Command"; @Entity("profileActposition") export class ProfileActposition extends EntityBase { @@ -50,6 +51,33 @@ export class ProfileActposition extends EntityBase { }) status: boolean; + @Column({ + nullable: true, + comment: "เลขที่คำสั่ง", + type: "text", + default: null, + }) + refCommandNo: string; + + @Column({ + comment: "วันที่ออกคำสั่ง", + type: "datetime", + nullable: true, + }) + refCommandDate: Date; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง command", + default: null, + }) + commandId: string; + + @ManyToOne(() => Command, (command) => command.profileSalarys) + @JoinColumn({ name: "commandId" }) + command: Command; + @Column({ nullable: true, length: 40, diff --git a/src/entities/ProfileAssistance.ts b/src/entities/ProfileAssistance.ts index de1f0f3e..c74af049 100644 --- a/src/entities/ProfileAssistance.ts +++ b/src/entities/ProfileAssistance.ts @@ -3,6 +3,7 @@ import { EntityBase } from "./base/Base"; import { Profile } from "./Profile"; import { ProfileAssistanceHistory } from "./ProfileAssistanceHistory"; import { ProfileEmployee } from "./ProfileEmployee"; +import { Command } from "./Command"; @Entity("profileAssistance") export class ProfileAssistance extends EntityBase { @@ -57,6 +58,25 @@ export class ProfileAssistance extends EntityBase { }) isUpload: boolean; + @Column({ + comment: "วันที่ออกคำสั่ง", + type: "datetime", + nullable: true, + }) + refCommandDate: Date; + + @Column({ + nullable: true, + length: 40, + comment: "คีย์นอก(FK)ของตาราง command", + default: null, + }) + commandId: string; + + @ManyToOne(() => Command, (command) => command.profileSalarys) + @JoinColumn({ name: "commandId" }) + command: Command; + @Column({ nullable: true, length: 40, @@ -87,6 +107,8 @@ export class CreateProfileAssistance { dateEnd: Date | null; commandNo: string | null; document: string | null; + refCommandDate?: string | null; + commandId?: string | null; } export class CreateProfileAssistanceEmployee { @@ -96,6 +118,8 @@ export class CreateProfileAssistanceEmployee { dateEnd: Date | null; commandNo: string | null; document: string | null; + refCommandDate?: string | null; + commandId?: string | null; } export type UpdateProfileAssistance = { @@ -105,4 +129,6 @@ export type UpdateProfileAssistance = { commandNo?: string | null; document?: string | null; isUpload?: boolean | null; + refCommandDate?: string | null; + commandId?: string | null; };