From 8a1f2506bbfceb9421db998f10f9da1a24538fc7 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 13 Jun 2024 14:49:50 +0700 Subject: [PATCH] fix and add api(/search-personal-no-keycloak) --- src/controllers/ProfileController.ts | 171 +++++++++++++++++- src/controllers/ProfileEmployeeController.ts | 176 ++++++++++++++++++- src/controllers/UserController.ts | 20 ++- 3 files changed, 360 insertions(+), 7 deletions(-) diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 3ceb2734..f82cf049 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -2127,7 +2127,10 @@ export class ProfileController extends Controller { posTypeName: item.posType?.posTypeName, posLevelId: item.posLevelId, posLevelName: item.posLevel?.posLevelName, - educationDegree: latestProfileEducation != null && latestProfileEducation.educationLevel != null ? latestProfileEducation.educationLevel : null, + educationDegree: + latestProfileEducation != null && latestProfileEducation.educationLevel != null + ? latestProfileEducation.educationLevel + : null, // ? { // id: latestProfileEducation.id, // degree: latestProfileEducation.degree, @@ -3122,4 +3125,170 @@ export class ProfileController extends Controller { await this.profileRepo.save(profile); return new HttpSuccess(); } + + /** + * API ค้นหาข้อมูลทะเบียนประวัติที่ยังไม่เชื่อม keycloak + * + * @summary ค้นหาข้อมูลทะเบียนประวัติที่ยังไม่เชื่อม keycloak + * + */ + @Post("search-personal-no-keycloak") + async getProfileBySearchKeywordNoKeyCloak( + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Body() + body: { + fieldName: string; + keyword?: string; + }, + ) { + let findProfile: any; + let total: any; + const skip = (page - 1) * pageSize; + const take = pageSize; + switch (body.fieldName) { + case "idcard": + [findProfile, total] = await this.profileRepo.findAndCount({ + where: { + citizenId: Like(`%${body.keyword}%`), + }, + relations: ["posType", "posLevel", "current_holders", "profileSalary"], + skip, + take, + }); + break; + + case "firstname": + [findProfile, total] = await this.profileRepo.findAndCount({ + where: { + keycloak: IsNull(), + firstName: Like(`%${body.keyword}%`), + }, + relations: ["posType", "posLevel", "current_holders", "profileSalary"], + skip, + take, + }); + break; + + case "lastname": + [findProfile, total] = await this.profileRepo.findAndCount({ + where: { + keycloak: IsNull(), + lastName: Like(`%${body.keyword}%`), + }, + relations: ["posType", "posLevel", "current_holders", "profileSalary"], + skip, + take, + }); + break; + + default: + [findProfile, total] = await this.profileRepo.findAndCount({ + where: { + keycloak: IsNull(), + }, + relations: ["posType", "posLevel", "current_holders", "profileSalary"], + skip, + take, + }); + break; + } + + const findRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true }, + }); + if (!findRevision) { + throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); + } + + const mapDataProfile = await Promise.all( + findProfile.map(async (item: Profile) => { + const fullName = `${item.prefix} ${item.firstName} ${item.lastName}`; + 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; + + const root = + item.current_holders.length == 0 || + (item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null) + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; + + let salary: any = ""; + if (item != null && item.profileSalary != null && item.profileSalary.length > 0) { + let _salary: any = item.profileSalary.sort( + (a, b) => + (b.date == null ? 0 : b.date.getTime()) - (a.date == null ? 0 : a.date.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + + const posMasterNo = item.current_holders?.find( + (x) => x.orgRevisionId == findRevision.id, + )?.posMasterNo; + + const latestProfileEducation = await this.profileEducationRepository.findOne({ + where: { profileId: item.id }, + order: { endDate: "DESC" }, + }); + + return { + id: item.id, + prefix: item.prefix, + rank: item.rank, + firstName: item.firstName, + lastName: item.lastName, + position: item.position, + idcard: item.citizenId, + email: item.email, + phone: item.phone, + name: fullName, + birthDate: item.birthDate, + positionLevel: item.posLevelId, + positionLevelName: item.posLevel?.posLevelName, + positionType: item.posTypeId, + positionTypeName: item.posType?.posTypeName, + posNo: shortName, + organization: root == null ? null : root.orgRootShortName, + salary: salary == "" ? "" : salary.amount, + posMasterNo: posMasterNo ?? null, + posTypeId: item.posTypeId, + posTypeName: item.posType?.posTypeName, + posLevelId: item.posLevelId, + posLevelName: item.posLevel?.posLevelName, + educationDegree: + latestProfileEducation != null && latestProfileEducation.educationLevel != null + ? latestProfileEducation.educationLevel + : null, + }; + }), + ); + + return new HttpSuccess({data: mapDataProfile , total}); + } } diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index def15132..92f8bb14 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -1505,11 +1505,10 @@ export class ProfileEmployeeController extends Controller { (x) => x.orgRevisionId == findRevision.id, )?.posMasterNo; - const latestProfileEducation = await this.profileEducationRepository.findOne({ - where: { profileEmployeeId: item.id }, - order: { endDate: "DESC" }, - }); - + const latestProfileEducation = await this.profileEducationRepository.findOne({ + where: { profileEmployeeId: item.id }, + order: { endDate: "DESC" }, + }); return { id: item.id, @@ -2910,4 +2909,171 @@ export class ProfileEmployeeController extends Controller { ); return new HttpSuccess(); } + + /** + * API ค้นหาข้อมูลทะเบียนประวัติที่ยังไม่เชื่อม keycloak + * + * @summary ค้นหาข้อมูลทะเบียนประวัติที่ยังไม่เชื่อม keycloak + * + */ + @Post("search-personal-no-keycloak") + async getProfileBySearchKeywordNoKeyCloak( + @Query("page") page: number = 1, + @Query("pageSize") pageSize: number = 10, + @Body() + body: { + fieldName: string; + keyword?: string; + }, + ) { + let findProfile: any; + let total: any; + const skip = (page - 1) * pageSize; + const take = pageSize; + switch (body.fieldName) { + case "idcard": + [findProfile, total] = await this.profileRepo.findAndCount({ + where: { + keycloak: IsNull(), + citizenId: Like(`%${body.keyword}%`), + }, + relations: ["posType", "posLevel", "current_holders", "profileSalarys"], + skip, + take, + }); + break; + + case "firstname": + [findProfile, total] = await this.profileRepo.findAndCount({ + where: { + keycloak: IsNull(), + firstName: Like(`%${body.keyword}%`), + }, + relations: ["posType", "posLevel", "current_holders", "profileSalarys"], + skip, + take, + }); + break; + + case "lastname": + [findProfile, total] = await this.profileRepo.findAndCount({ + where: { + keycloak: IsNull(), + lastName: Like(`%${body.keyword}%`), + }, + relations: ["posType", "posLevel", "current_holders", "profileSalarys"], + skip, + take, + }); + break; + + default: + [findProfile, total] = await this.profileRepo.findAndCount({ + where: { + keycloak: IsNull(), + }, + relations: ["posType", "posLevel", "current_holders", "profileSalarys"], + skip, + take, + }); + break; + } + + const findRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true }, + }); + if (!findRevision) { + throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision"); + } + + const mapDataProfile = await Promise.all( + findProfile.map(async (item: ProfileEmployee) => { + const fullName = `${item.prefix} ${item.firstName} ${item.lastName}`; + 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; + + const root = + item.current_holders.length == 0 || + (item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null) + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; + + let salary: any = ""; + if (item != null && item.profileSalarys != null && item.profileSalarys.length > 0) { + let _salary: any = item.profileSalarys.sort( + (a, b) => + (b.date == null ? 0 : b.date.getTime()) - (a.date == null ? 0 : a.date.getTime()), + ); + if (_salary.length > 0) { + salary = _salary[0]; + } + } + + const posMasterNo = item.current_holders?.find( + (x) => x.orgRevisionId == findRevision.id, + )?.posMasterNo; + + const latestProfileEducation = await this.profileEducationRepository.findOne({ + where: { profileEmployeeId: item.id }, + order: { endDate: "DESC" }, + }); + + return { + id: item.id, + prefix: item.prefix, + rank: item.rank, + firstName: item.firstName, + lastName: item.lastName, + position: item.position, + idcard: item.citizenId, + email: item.email, + phone: item.phone, + name: fullName, + birthDate: item.birthDate, + positionLevel: item.posLevelId, + positionLevelName: item.posLevel?.posLevelName, + positionType: item.posTypeId, + positionTypeName: item.posType?.posTypeName, + posNo: shortName, + organization: root == null ? null : root.orgRootShortName, + salary: salary == "" ? "" : salary.amount, + posMasterNo: posMasterNo ?? null, + posTypeId: item.posTypeId, + posTypeName: item.posType?.posTypeName, + posLevelId: item.posLevelId, + posLevelName: item.posLevel?.posLevelName, + educationDegree: + latestProfileEducation != null && latestProfileEducation.educationLevel != null + ? latestProfileEducation.educationLevel + : null, + }; + }), + ); + + return new HttpSuccess({ data: mapDataProfile, total }); + } } diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index 9e152c41..9c4f3b74 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -30,6 +30,9 @@ import { getRoleMappings, getUserCount, } from "../keycloak"; +import { AppDataSource } from "../database/data-source"; +import { Profile } from "../entities/Profile"; +import { ProfileEmployee } from "../entities/ProfileEmployee"; // import * as io from "../lib/websocket"; // import elasticsearch from "../elasticsearch"; // import { StorageFolder } from "../interfaces/storage-fs"; @@ -38,7 +41,6 @@ import { // if (!process.env.ELASTICSEARCH_INDEX) throw Error("Default ElasticSearch index must be specified."); // const DEFAULT_INDEX = process.env.ELASTICSEARCH_INDEX; - function stripLeadingSlash(str: string) { return str.replace(/^\//, ""); } @@ -47,6 +49,10 @@ function stripLeadingSlash(str: string) { @Tags("Single-Sign On") @Security("bearerAuth") export class KeycloakController extends Controller { + + private profileRepo = AppDataSource.getRepository(Profile); + private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee); + @Get("user/{id}") async getUser(@Path("id") id: string) { const userData = await getUser(id); @@ -80,6 +86,7 @@ export class KeycloakController extends Controller { lastName?: string; email?: string; roles?: string[]; + profileId?: string; }, ) { const userId = await createUser(body.username, body.password, { @@ -133,6 +140,17 @@ export class KeycloakController extends Controller { role: body.roles || [], }; const addRole = await this.addRole(userId, _roles); + + const profile = await this.profileRepo.findOne({ + where: { + id: body.profileId, + }, + }); + + if (profile) { + profile.keycloak = userId; + await this.profileRepo.save(profile); + } return userId; }