From 9eac546f55cd007430a39527f52e951d510b7c24 Mon Sep 17 00:00:00 2001 From: kittapath Date: Fri, 20 Dec 2024 12:00:55 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=20=E0=B8=AD?= =?UTF-8?q?=E0=B8=AD=E0=B8=81=E0=B8=84=E0=B8=B3=E0=B8=AA=E0=B8=B1=E0=B9=88?= =?UTF-8?q?=E0=B8=87=E0=B8=A7=E0=B8=B4=E0=B8=99=E0=B8=B1=E0=B8=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 423 +++++++++++++++++++-------- 1 file changed, 295 insertions(+), 128 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index ae303916..c65c39f8 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -2568,6 +2568,7 @@ export class CommandController extends Controller { body: { data: { profileId: string; + profileType?: string | null; date?: Date | null; refCommandNo?: string | null; salaryRef?: string | null; @@ -2588,149 +2589,141 @@ export class CommandController extends Controller { ) { await Promise.all( body.data.map(async (item) => { - const profile = await this.profileRepository.findOne({ - relations: ["profileSalary", "posLevel", "posType", "current_holders", "roleKeycloaks"], - where: { id: item.profileId }, - order: { - profileSalary: { - order: "DESC", + if (item.profileType && item.profileType.trim().toUpperCase() == "OFFICER") { + const profile = await this.profileRepository.findOne({ + relations: ["profileSalary", "posLevel", "posType", "current_holders", "roleKeycloaks"], + where: { id: item.profileId }, + order: { + profileSalary: { + order: "DESC", + }, }, - }, - }); - if (!profile) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); - } - const orgRevision = await this.orgRevisionRepo.findOne({ - where: { - orgRevisionIsCurrent: true, - orgRevisionIsDraft: false, - }, - }); - const shortName = - !profile.current_holders || profile.current_holders.length == 0 - ? null - : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != null && - profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) - ?.orgChild4 != null - ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild4.orgChild4ShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` + }); + if (!profile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); + } + const orgRevision = await this.orgRevisionRepo.findOne({ + where: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + }, + }); + const shortName = + !profile.current_holders || profile.current_holders.length == 0 + ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) - ?.orgChild3 != null - ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild3.orgChild3ShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` + ?.orgChild4 != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild4.orgChild4ShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) - ?.orgChild2 != null - ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild2.orgChild2ShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` + ?.orgChild3 != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild3.orgChild3ShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) - ?.orgChild1 != null - ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild1.orgChild1ShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` + ?.orgChild2 != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild2.orgChild2ShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) - ?.orgRoot != null - ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgRoot.orgRootShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` - : null; - let position = - profile.current_holders - .filter((x) => x.orgRevisionId == orgRevision?.id)[0] - ?.positions?.filter((pos) => pos.positionIsSelected === true)[0] ?? null; - // ประวัติตำแหน่ง - const data = new ProfileSalary(); - const meta = { - profileId: profile.id, - commandId: item.commandId, - date: item.date, - refCommandNo: item.refCommandNo, - templateDoc: item.salaryRef, - position: profile.position, - positionType: profile.posType.posTypeName, - positionLevel: profile.posLevel.posLevelName, - posNo: shortName ? shortName : "-", - positionLine: position?.positionField ?? "-", - positionPathSide: position?.positionArea ?? "-", - positionExecutive: position?.posExecutive?.posExecutiveName ?? "-", - amount: item.amount ? item.amount : null, - positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null, - mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null, - order: - profile.profileSalary.length >= 0 - ? profile.profileSalary.length > 0 - ? profile.profileSalary[0].order + 1 - : 1 - : null, - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - createdAt: new Date(), - lastUpdatedAt: new Date(), - dateGovernment: new Date(), - isGovernment: item.isGovernment, - }; + ?.orgChild1 != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild1.orgChild1ShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` + : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != + null && + profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) + ?.orgRoot != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgRoot.orgRootShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` + : null; + let position = + profile.current_holders + .filter((x) => x.orgRevisionId == orgRevision?.id)[0] + ?.positions?.filter((pos) => pos.positionIsSelected === true)[0] ?? null; + // ประวัติตำแหน่ง + const data = new ProfileSalary(); + const meta = { + profileId: profile.id, + commandId: item.commandId, + date: item.date, + refCommandNo: item.refCommandNo, + templateDoc: item.salaryRef, + position: profile.position, + positionType: profile.posType.posTypeName, + positionLevel: profile.posLevel.posLevelName, + posNo: shortName ? shortName : "-", + positionLine: position?.positionField ?? "-", + positionPathSide: position?.positionArea ?? "-", + positionExecutive: position?.posExecutive?.posExecutiveName ?? "-", + amount: item.amount ? item.amount : null, + positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null, + mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null, + order: + profile.profileSalary.length >= 0 + ? profile.profileSalary.length > 0 + ? profile.profileSalary[0].order + 1 + : 1 + : null, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + dateGovernment: new Date(), + isGovernment: item.isGovernment, + }; - Object.assign(data, meta); - const history = new ProfileSalaryHistory(); - Object.assign(history, { ...data, id: undefined }); + Object.assign(data, meta); + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); - await this.salaryRepo.save(data); - history.profileSalaryId = data.id; - await this.salaryHistoryRepo.save(history); + await this.salaryRepo.save(data); + history.profileSalaryId = data.id; + await this.salaryHistoryRepo.save(history); - // ประวัติวินัย - const dataDis = new ProfileDiscipline(); + // ประวัติวินัย + const dataDis = new ProfileDiscipline(); - const metaDis = { - createdUserId: req.user.sub, - createdFullName: req.user.name, - lastUpdateUserId: req.user.sub, - lastUpdateFullName: req.user.name, - createdAt: new Date(), - lastUpdatedAt: new Date(), - }; + const metaDis = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; - Object.assign(dataDis, { ...body, ...metaDis }); - const historyDis = new ProfileDisciplineHistory(); - Object.assign(historyDis, { ...dataDis, id: undefined }); + Object.assign(dataDis, { ...body, ...metaDis }); + const historyDis = new ProfileDisciplineHistory(); + Object.assign(historyDis, { ...dataDis, id: undefined }); - await this.disciplineRepository.save(dataDis); - historyDis.profileDisciplineId = dataDis.id; - await this.disciplineHistoryRepository.save(historyDis); + await this.disciplineRepository.save(dataDis); + historyDis.profileDisciplineId = dataDis.id; + await this.disciplineHistoryRepository.save(historyDis); - // ทะเบียนประวัติ - if (item.isLeave != null) { - const _profile = await this.profileRepository.findOne({ - where: { id: item.profileId }, - }); - if (!_profile) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); - } - const _null: any = null; - _profile.isLeave = item.isLeave; - _profile.leaveReason = item.leaveReason ?? _null; - _profile.dateLeave = item.dateLeave ?? _null; - _profile.lastUpdateUserId = req.user.sub; - _profile.lastUpdateFullName = req.user.name; - _profile.lastUpdatedAt = new Date(); - const exceptClear = await checkExceptCommandType(String(item.commandId)); - if (item.isLeave == true && !exceptClear) { - await removeProfileInOrganize(_profile.id, "OFFICER"); - } - //คำสั่งพักราชการ หรือ ให้ออกจากราชการไว้ก่อน solutionเดิม ให้ disable user ไว้แต่ยังไม่ลบ เปลี่ยนเป็นลบ user ออกเลย - else if (item.isLeave == true && exceptClear && _profile.keycloak != null) { - // const enableActive = await enableStatus(_profile.keycloak, false); - // if (!enableActive) throw new Error("Failed. Cannot change enable status."); - const delUserKeycloak = await deleteUser(_profile.keycloak); - if (delUserKeycloak) { - _profile.keycloak = _null; - _profile.roleKeycloaks = []; - _profile.isActive = false; + // ทะเบียนประวัติ + if (item.isLeave != null) { + const _profile = await this.profileRepository.findOne({ + where: { id: item.profileId }, + }); + if (!_profile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); } - } - const clearProfile = await checkCommandType(String(item.commandId)); - if (clearProfile) { - if (_profile.keycloak != null) { + const _null: any = null; + _profile.isLeave = item.isLeave; + _profile.leaveReason = item.leaveReason ?? _null; + _profile.dateLeave = item.dateLeave ?? _null; + _profile.lastUpdateUserId = req.user.sub; + _profile.lastUpdateFullName = req.user.name; + _profile.lastUpdatedAt = new Date(); + const exceptClear = await checkExceptCommandType(String(item.commandId)); + if (item.isLeave == true && !exceptClear) { + await removeProfileInOrganize(_profile.id, "OFFICER"); + } + //คำสั่งพักราชการ หรือ ให้ออกจากราชการไว้ก่อน solutionเดิม ให้ disable user ไว้แต่ยังไม่ลบ เปลี่ยนเป็นลบ user ออกเลย + else if (item.isLeave == true && exceptClear && _profile.keycloak != null) { + // const enableActive = await enableStatus(_profile.keycloak, false); + // if (!enableActive) throw new Error("Failed. Cannot change enable status."); const delUserKeycloak = await deleteUser(_profile.keycloak); if (delUserKeycloak) { _profile.keycloak = _null; @@ -2738,11 +2731,185 @@ export class CommandController extends Controller { _profile.isActive = false; } } - _profile.position = _null; - _profile.posTypeId = _null; - _profile.posLevelId = _null; + const clearProfile = await checkCommandType(String(item.commandId)); + if (clearProfile) { + if (_profile.keycloak != null) { + const delUserKeycloak = await deleteUser(_profile.keycloak); + if (delUserKeycloak) { + _profile.keycloak = _null; + _profile.roleKeycloaks = []; + _profile.isActive = false; + } + } + _profile.position = _null; + _profile.posTypeId = _null; + _profile.posLevelId = _null; + } + await this.profileRepository.save(_profile); + } + } else { + const profile = await this.profileEmployeeRepository.findOne({ + relations: ["profileSalary", "posLevel", "posType", "current_holders", "roleKeycloaks"], + where: { id: item.profileId }, + order: { + profileSalary: { + order: "DESC", + }, + }, + }); + if (!profile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); + } + const orgRevision = await this.orgRevisionRepo.findOne({ + where: { + orgRevisionIsCurrent: true, + orgRevisionIsDraft: false, + }, + }); + const shortName = + !profile.current_holders || profile.current_holders.length == 0 + ? null + : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != null && + profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) + ?.orgChild4 != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild4.orgChild4ShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` + : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != null && + profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) + ?.orgChild3 != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild3.orgChild3ShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` + : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != + null && + profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) + ?.orgChild2 != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild2.orgChild2ShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` + : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != + null && + profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) + ?.orgChild1 != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild1.orgChild1ShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` + : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != + null && + profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) + ?.orgRoot != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgRoot.orgRootShortName}${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}` + : null; + // let position = + // profile.current_holders + // .filter((x) => x.orgRevisionId == orgRevision?.id)[0] + // ?.positions?.filter((pos) => pos.positionIsSelected === true)[0] ?? null; + // ประวัติตำแหน่ง + const data = new ProfileSalary(); + const meta = { + profileEmployeeId: profile.id, + commandId: item.commandId, + date: item.date, + refCommandNo: item.refCommandNo, + templateDoc: item.salaryRef, + position: profile.position, + positionType: profile.posType.posTypeName, + positionLevel: profile.posLevel.posLevelName, + posNo: shortName ? shortName : "-", + // positionLine: position?.positionField ?? "-", + // positionPathSide: position?.positionArea ?? "-", + // positionExecutive: position?.posExecutive?.posExecutiveName ?? "-", + amount: item.amount ? item.amount : null, + positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null, + mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null, + order: + profile.profileSalary.length >= 0 + ? profile.profileSalary.length > 0 + ? profile.profileSalary[0].order + 1 + : 1 + : null, + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + dateGovernment: new Date(), + isGovernment: item.isGovernment, + }; + + Object.assign(data, meta); + const history = new ProfileSalaryHistory(); + Object.assign(history, { ...data, id: undefined }); + + await this.salaryRepo.save(data); + history.profileSalaryId = data.id; + await this.salaryHistoryRepo.save(history); + + // ประวัติวินัย + const dataDis = new ProfileDiscipline(); + + const metaDis = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + + Object.assign(dataDis, { + ...body, + ...metaDis, + profileEmployeeId: item.profileId, + profileId: undefined, + }); + const historyDis = new ProfileDisciplineHistory(); + Object.assign(historyDis, { ...dataDis, id: undefined }); + + await this.disciplineRepository.save(dataDis); + historyDis.profileDisciplineId = dataDis.id; + await this.disciplineHistoryRepository.save(historyDis); + + // ทะเบียนประวัติ + if (item.isLeave != null) { + const _profile = await this.profileEmployeeRepository.findOne({ + where: { id: item.profileId }, + }); + if (!_profile) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); + } + const _null: any = null; + _profile.isLeave = item.isLeave; + _profile.leaveReason = item.leaveReason ?? _null; + _profile.dateLeave = item.dateLeave ?? _null; + _profile.lastUpdateUserId = req.user.sub; + _profile.lastUpdateFullName = req.user.name; + _profile.lastUpdatedAt = new Date(); + const exceptClear = await checkExceptCommandType(String(item.commandId)); + if (item.isLeave == true && !exceptClear) { + await removeProfileInOrganize(_profile.id, "EMPLOYEE"); + } + //คำสั่งพักราชการ หรือ ให้ออกจากราชการไว้ก่อน solutionเดิม ให้ disable user ไว้แต่ยังไม่ลบ เปลี่ยนเป็นลบ user ออกเลย + else if (item.isLeave == true && exceptClear && _profile.keycloak != null) { + // const enableActive = await enableStatus(_profile.keycloak, false); + // if (!enableActive) throw new Error("Failed. Cannot change enable status."); + const delUserKeycloak = await deleteUser(_profile.keycloak); + if (delUserKeycloak) { + _profile.keycloak = _null; + _profile.roleKeycloaks = []; + _profile.isActive = false; + } + } + const clearProfile = await checkCommandType(String(item.commandId)); + if (clearProfile) { + if (_profile.keycloak != null) { + const delUserKeycloak = await deleteUser(_profile.keycloak); + if (delUserKeycloak) { + _profile.keycloak = _null; + _profile.roleKeycloaks = []; + _profile.isActive = false; + } + } + _profile.position = _null; + _profile.posTypeId = _null; + _profile.posLevelId = _null; + } + await this.profileEmployeeRepository.save(_profile); } - await this.profileRepository.save(_profile); } }), );