From 4efc3f381b6a2681cea48c9762e2a9cd9d34aa2e Mon Sep 17 00:00:00 2001 From: Bright Date: Tue, 8 Oct 2024 16:35:08 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=AD=E0=B8=AD=E0=B8=81=E0=B8=84=E0=B8=B3?= =?UTF-8?q?=E0=B8=AA=E0=B8=B1=E0=B9=88=E0=B8=87=2001=2002?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/CommandController.ts | 231 ++++++++++++++++++++++++++- 1 file changed, 228 insertions(+), 3 deletions(-) diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 6ddc0829..0c969531 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -22,7 +22,7 @@ import { Command } from "../entities/Command"; import { Brackets, LessThan, MoreThan, Double, In } from "typeorm"; import { CommandType } from "../entities/CommandType"; import { CommandSend } from "../entities/CommandSend"; -import { Profile } from "../entities/Profile"; +import { Profile, CreateProfileAllFields } from "../entities/Profile"; import { RequestWithUser } from "../middlewares/user"; import { OrgRevision } from "../entities/OrgRevision"; import { CommandSendCC } from "../entities/CommandSendCC"; @@ -32,9 +32,16 @@ import HttpStatus from "../interfaces/http-status"; import Extension from "../interfaces/extension"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import CallAPI from "../interfaces/call-api"; -import { ProfileSalary } from "../entities/ProfileSalary"; +import { ProfileSalary, CreateProfileSalary } from "../entities/ProfileSalary"; import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory"; -import { removeProfileInOrganize, setLogDataDiff } from "../interfaces/utils"; +import { + calculateAge, + calculateRetireDate, + calculateRetireLaw, + calculateRetireYear, + removeProfileInOrganize, + setLogDataDiff, +} from "../interfaces/utils"; import { Position } from "../entities/Position"; import { PosMaster } from "../entities/PosMaster"; import { EmployeePosition } from "../entities/EmployeePosition"; @@ -42,6 +49,35 @@ import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { ProfileDiscipline } from "../entities/ProfileDiscipline"; import { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory"; import { PosMasterAct } from "../entities/PosMasterAct"; +import { PosLevel } from "../entities/PosLevel"; +import { PosType } from "../entities/PosType"; +import { + addUserGroup, + addUserRoles, + createGroup, + createUser, + deleteGroup, + deleteUser, + editUser, + getGroups, + getRoles, + getUser, + getUserGroups, + getUserList, + removeUserGroup, + removeUserRoles, + getRoleMappings, + getUserCount, + enableStatus, +} from "../keycloak"; +import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation"; +import { ProfileEducationHistory } from "../entities/ProfileEducationHistory"; +import { + CreateProfileCertificate, + ProfileCertificate, + UpdateProfileCertificate, +} from "../entities/ProfileCertificate"; +import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory"; @Route("api/v1/org/command") @Tags("Command") @@ -70,6 +106,12 @@ export class CommandController extends Controller { private disciplineRepository = AppDataSource.getRepository(ProfileDiscipline); private disciplineHistoryRepository = AppDataSource.getRepository(ProfileDisciplineHistory); private posMasterActRepository = AppDataSource.getRepository(PosMasterAct); + private posLevelRepo = AppDataSource.getRepository(PosLevel); + private posTypeRepo = AppDataSource.getRepository(PosType); + private profileEducationRepo = AppDataSource.getRepository(ProfileEducation); + private profileEducationHistoryRepo = AppDataSource.getRepository(ProfileEducationHistory); + private certificateRepo = AppDataSource.getRepository(ProfileCertificate); + private certificateHistoryRepo = AppDataSource.getRepository(ProfileCertificateHistory); /** * API list รายการคำสั่ง @@ -1990,6 +2032,189 @@ export class CommandController extends Controller { return new HttpSuccess(); } + @Post("excexute/create-officer-profile") + public async CreateOfficeProfileExcecute( + @Request() req: RequestWithUser, + @Body() + body: { + data: { + bodyProfile: CreateProfileAllFields + bodyEducations: CreateProfileEducation[] + bodyCertificates: CreateProfileCertificate[] + bodySalarys: CreateProfileSalary + bodyPosition: { + posmasterId: string; + positionId: string; + } + }[]; + }, + ) { + await Promise.all( + body.data.map(async (item) => { + const profile = await this.profileRepository.findOneBy({ citizenId: item.bodyProfile.citizenId }); + if (profile) { + throw new HttpError(HttpStatus.BAD_REQUEST, "พบ profile ซ้ำ"); + } + const before = null; + const meta = { + createdUserId: req.user.sub, + createdFullName: req.user.name, + lastUpdateUserId: req.user.sub, + lastUpdateFullName: req.user.name, + createdAt: new Date(), + lastUpdatedAt: new Date(), + }; + if (item.bodyProfile.posLevelId === "") item.bodyProfile.posLevelId = null; + if (item.bodyProfile.posTypeId === "") item.bodyProfile.posTypeId = null; + + if (item.bodyProfile.posLevelId && !(await this.posLevelRepo.findOneBy({ id: item.bodyProfile.posLevelId }))) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้"); + } + + if (item.bodyProfile.posTypeId && !(await this.posTypeRepo.findOneBy({ id: item.bodyProfile.posTypeId }))) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); + } + + const profile_: Profile = Object.assign({ ...item.bodyProfile, ...meta }); + const _null: any = null; + profile_.dateRetire = item.bodyProfile.birthDate == null ? _null : calculateRetireDate(item.bodyProfile.birthDate); + profile_.dateRetireLaw = item.bodyProfile.birthDate == null ? _null : calculateRetireLaw(item.bodyProfile.birthDate); + await this.profileRepository.save(profile_); + // setLogDataDiff(req, { before, after: profile_ }); + if(profile_ && profile_.id) { + const userKeycloakId = await createUser(profile_.citizenId, profile_.citizenId, { //User, Password + firstName: profile_.firstName, + lastName: profile_.lastName, + email: profile_.email, + requiredActions: ["UPDATE_PASSWORD"], + }); + if (typeof userKeycloakId !== "string") { + throw new Error(userKeycloakId.errorMessage); + } + const list = await getRoles(); + if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); + const result = await addUserRoles( + userKeycloakId, + list + .filter((v) => v.name === "USER") + .map((x) => ({ + id: x.id, + name: x.name, + })) + ); + if (!result) throw new Error("Failed. Cannot set user's role."); + + //Educations + await Promise.all( + item.bodyEducations.map(async (education) => { + const profileEdu = new ProfileEducation(); + Object.assign(profileEdu, { ...education, ...meta }); + const eduHistory = new ProfileEducationHistory(); + Object.assign(eduHistory, { ...profileEdu, id: undefined }); + profileEdu.profileId = profile_.id; + await this.profileEducationRepo.save(profileEdu, { data: req }); + setLogDataDiff(req, { before, after: profileEdu }); + eduHistory.profileEducationId = profileEdu.id; + await this.profileEducationHistoryRepo.save(eduHistory, { data: req }); + }) + ); + + //Certificates + await Promise.all( + item.bodyCertificates.map(async (cer) => { + const profileCer = new ProfileCertificate(); + Object.assign(profileCer, { ...cer, ...meta }); + const cerHistory = new ProfileCertificateHistory(); + Object.assign(cerHistory, { ...profileCer, id: undefined }); + profileCer.profileId = profile_.id + await this.certificateRepo.save(profileCer, { data: req }); + setLogDataDiff(req, { before, after: profileCer }); + cerHistory.profileCertificateId = profileCer.id; + await this.certificateHistoryRepo.save(cerHistory, { data: req }); + }) + ); + + //Salary + const dest_item = await this.salaryRepo.findOne({ + where: { profileId: profile_.id }, + order: { order: "DESC" }, + }); + const profileSal = new ProfileSalary(); + Object.assign(profileSal, { ...item.bodySalarys, ...meta }); + const salaryHistory = new ProfileSalaryHistory(); + Object.assign(history, { ...profileSal, id: undefined }); + profileSal.order = dest_item == null ? 1 : dest_item.order + 1, + profileSal.profileId = profile_.id + await this.salaryRepo.save(profileSal, { data: req }); + setLogDataDiff(req, { before, after: profileSal }); + salaryHistory.profileSalaryId = profileSal.id; + await this.salaryHistoryRepo.save(salaryHistory, { data: req }); + + //Position + const posMaster = await this.posMasterRepository.findOne({ + where: { id: item.bodyPosition.posmasterId }, + }); + if (posMaster == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); + + const posMasterOld = await this.posMasterRepository.findOne({ + where: { + current_holderId: profile_.id, + orgRevisionId: posMaster.orgRevisionId, + }, + }); + if (posMasterOld != null) posMasterOld.current_holderId = null; + + const positionOld = await this.positionRepository.findOne({ + where: { + posMasterId: posMasterOld?.id, + positionIsSelected: true, + }, + }); + if (positionOld != null) { + positionOld.positionIsSelected = false; + await this.positionRepository.save(positionOld); + } + + const checkPosition = await this.positionRepository.find({ + where: { + posMasterId: item.bodyPosition.posmasterId, + positionIsSelected: true, + }, + }); + if (checkPosition.length > 0) { + const clearPosition = checkPosition.map((positions) => ({ + ...positions, + positionIsSelected: false, + })); + await this.positionRepository.save(clearPosition); + } + + posMaster.current_holderId = profile_.id; + if (posMasterOld != null) await this.posMasterRepository.save(posMasterOld); + await this.posMasterRepository.save(posMaster); + + const positionNew = await this.positionRepository.findOne({ + where: { + id: item.bodyPosition.positionId, + posMasterId: item.bodyPosition.posmasterId, + }, + }); + if (positionNew != null) { + positionNew.positionIsSelected = true; + profile_.posLevelId = positionNew.posLevelId; + profile_.posTypeId = positionNew.posTypeId; + profile_.position = positionNew.positionName; + profile_.keycloak = userKeycloakId; // Update KeyCloak + await this.profileRepository.save(profile_, { data: req }); + setLogDataDiff(req, { before, after: profile_ }); + await this.positionRepository.save(positionNew, { data: req }); + } + } + }) + ); + return new HttpSuccess(); + } + @Post("command21/employee/report/excecute") public async command21SalaryEmployeeExcecute( @Request() req: RequestWithUser,