From 9ee8240b1c618f12c1c0ca8ed4a76572af95dcc7 Mon Sep 17 00:00:00 2001 From: Bright Date: Tue, 28 May 2024 16:23:09 +0700 Subject: [PATCH] OrgDotnetController (update) --- .../OrganizationDotnetController.ts | 309 ++++++++++++------ 1 file changed, 212 insertions(+), 97 deletions(-) diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index 58fcc341..04feff71 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -19,39 +19,12 @@ 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 { - Profile, - CreateProfile, - UpdateProfile, - ProfileHistory, - CreateProfileAllFields, -} from "../entities/Profile"; +import { RequestWithUser } from "../middlewares/user"; +import { Profile } from "../entities/Profile"; import { Brackets, IsNull, Like, Not } from "typeorm"; import { OrgRevision } from "../entities/OrgRevision"; -import { PosMaster } from "../entities/PosMaster"; -import { PosLevel } from "../entities/PosLevel"; -import { PosType } from "../entities/PosType"; -import { - calculateAge, - calculateRetireDate, - calculateRetireLaw, - calculateRetireYear, -} from "../interfaces/utils"; -import { RequestWithUser } from "../middlewares/user"; -import { Position } from "../entities/Position"; +import { OrgRoot } from "../entities/OrgRoot"; import { ProfileEmployee } from "../entities/ProfileEmployee"; -import { Province } from "../entities/Province"; -import { District } from "../entities/District"; -import { SubDistrict } from "../entities/SubDistrict"; -import { ProfileCertificate } from "../entities/ProfileCertificate"; -import { ProfileTraining } from "../entities/ProfileTraining"; -import { ProfileDiscipline } from "../entities/ProfileDiscipline"; -import { ProfileEducation } from "../entities/ProfileEducation"; -import { ProfileSalary } from "../entities/ProfileSalary"; -import { ProfileFamilyCouple } from "../entities/ProfileFamilyCouple"; -import { ProfileFamilyMother } from "../entities/ProfileFamilyMother"; -import { ProfileFamilyFather } from "../entities/ProfileFamilyFather"; -import Extension from "../interfaces/extension"; @Route("api/v1/org/dotnet") @Tags("Dotnet") @@ -62,25 +35,9 @@ import Extension from "../interfaces/extension"; ) @SuccessResponse(HttpStatus.OK, "สำเร็จ") export class OrganizationDotnetController extends Controller { + private orgRootRepo = AppDataSource.getRepository(OrgRoot); private orgRevisionRepo = AppDataSource.getRepository(OrgRevision); - private posMasterRepo = AppDataSource.getRepository(PosMaster); private profileRepo = AppDataSource.getRepository(Profile); - private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee); - private profileHistoryRepo = AppDataSource.getRepository(ProfileHistory); - private posLevelRepo = AppDataSource.getRepository(PosLevel); - private posTypeRepo = AppDataSource.getRepository(PosType); - private positionRepository = AppDataSource.getRepository(Position); - private provinceRepository = AppDataSource.getRepository(Province); - private districtRepository = AppDataSource.getRepository(District); - private subDistrict = AppDataSource.getRepository(SubDistrict); - private certificateRepository = AppDataSource.getRepository(ProfileCertificate); - private profileFamilyCoupleRepository = AppDataSource.getRepository(ProfileFamilyCouple); - private profileFamilyMotherRepository = AppDataSource.getRepository(ProfileFamilyMother); - private profileFamilyFatherRepository = AppDataSource.getRepository(ProfileFamilyFather); - private trainingRepository = AppDataSource.getRepository(ProfileTraining); - private disciplineRepository = AppDataSource.getRepository(ProfileDiscipline); - private educationRepository = AppDataSource.getRepository(ProfileEducation); - private salaryRepository = AppDataSource.getRepository(ProfileSalary); /** * 1. API Search Profile @@ -89,7 +46,7 @@ export class OrganizationDotnetController extends Controller { * */ @Post("search") - public async searchProfile( + public async SearchProfile( @Body() body: { citizenId?: string | null; @@ -118,71 +75,229 @@ export class OrganizationDotnetController extends Controller { }), ); } - - const profiles = await queryBuilder.getMany(); - - if (!profiles.length) { - throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบข้อมูลโปรไฟล์"); - } - - const formattedProfiles = profiles.map((profile) => ({ - avatar: profile.avatar, - avatarName: profile.avatarName, - rank: profile.rank, - prefix: profile.prefix, - firstName: profile.firstName, - lastName: profile.lastName, - citizenId: profile.citizenId, - position: profile.position, - posLevelId: profile.posLevelId, - posLevelName: profile.posLevel.posLevelName, - posTypeId: profile.posTypeId, - posTypeName: profile.posType.posTypeName, - email: profile.email, - phone: profile.phone, - keycloak: profile.keycloak, - isProbation: profile.isProbation, - isLeave: profile.isLeave, - leaveReason: profile.leaveReason, - dateRetire: profile.dateRetire, - dateAppoint: profile.dateAppoint, - dateRetireLaw: profile.dateRetireLaw, - dateStart: profile.dateStart, - govAgeAbsent: profile.govAgeAbsent, - govAgePlus: profile.govAgePlus, - birthDate: profile.birthDate, - reasonSameDate: profile.reasonSameDate, - ethnicity: profile.ethnicity, - telephoneNumber: profile.telephoneNumber, - nationality: profile.nationality, - gender: profile.gender, - relationship: profile.relationship, - religion: profile.religion, - bloodGroup: profile.bloodGroup, - })); - - return new HttpSuccess(formattedProfiles); + const profiles = await queryBuilder.getMany(); + return new HttpSuccess(profiles); } + /** + * 6. Search Employee + * + * @summary 6. Search Employee + * + */ + @Post("search-employee") + public async SearchProfileEmployee( + @Body() + body: { + citizenId?: string | null; + firstName?: string | null; + lastName?: string | null; + }, + ) { + const profileRepository = AppDataSource.getRepository(ProfileEmployee); + const queryBuilder = profileRepository + .createQueryBuilder("profile") + .leftJoinAndSelect("profile.posLevel", "posLevel") + .leftJoinAndSelect("profile.posType", "posType"); + + if (body.citizenId || body.firstName || body.lastName) { + queryBuilder.where( + new Brackets((qb) => { + if (body.citizenId) { + qb.orWhere("profile.citizenId LIKE :citizenId", { citizenId: `%${body.citizenId}%` }); + } + if (body.firstName) { + qb.orWhere("profile.firstName LIKE :firstName", { firstName: `%${body.firstName}%` }); + } + if (body.lastName) { + qb.orWhere("profile.lastName LIKE :lastName", { lastName: `%${body.lastName}%` }); + } + }), + ); + } + const profileEmp = await queryBuilder.getMany(); + return new HttpSuccess(profileEmp); + } + + /** + * 2. API ข้อมูลหน่วยงานตามโครงสร้าง + * + * @summary 2. API ข้อมูลหน่วยงานตามโครงสร้าง + * + * @param {string} id Id หน่วยงาน + */ + @Get("org/{id}") + async GetOrganizationById(@Path() id: string) { + const orgRoot = await this.orgRootRepo.findOne({ + where: { id: id }, + }); + if (!orgRoot) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + return new HttpSuccess(orgRoot); + } + + @Get("agency/{id}") + async GetOrgAgencyById(@Path() id: string) { + const orgRoot = await this.orgRootRepo.findOne({ + where: { id: id }, + }); + if (!orgRoot) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + return new HttpSuccess(orgRoot); + } + + @Get("go-agency/{id}") + async GetOrgGoAgencyById(@Path() id: string) { + const orgRoot = await this.orgRootRepo.findOne({ + where: { id: id }, + }); + if (!orgRoot) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + return new HttpSuccess(orgRoot); + } + /** * 3. API Get Profile จาก keycloak id * * @summary 3. API Get Profile จาก keycloak id * + * @param {string} keycloakId Id keycloak */ - @Get("keycloak/{id}") - async getProfileByKeycloakId(@Path() id: string) { + @Get("keycloak/{keycloakId}") + async GetProfileByKeycloakIdAsync(@Path() keycloakId: string) { const profile = await this.profileRepo.findOne({ relations: { posLevel: true, posType: true, }, - where: { keycloak: id }, + where: { keycloak: keycloakId }, }); - if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - return new HttpSuccess(profile); } + /** + * 7.Get ข้อมูล GetUserFullName + * + * @summary 7.1 Get ข้อมูล GetUserFullName + * + * @param {string} keycloakId Id keycloak + */ + @Get("user-fullname/{keycloakId}") + async GetUserFullName(@Path() keycloakId: string) { + const profile = await this.profileRepo.findOne({ + where: { keycloak: keycloakId }, + select: ["prefix", "firstName", "lastName"] + }); + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const fullName = profile? `${profile.prefix}${profile.firstName} ${profile.lastName}` : "-"; + return new HttpSuccess(fullName); + } + + /** + * 7.Get ข้อมูล GetUserOCId + * + * @summary 7.2 Get ข้อมูล GetUserOCId + * + * @param {string} keycloakId Id keycloak + */ + @Get("user-oc/{keycloakId}") + async getProfileByKeycloak(@Path() keycloakId: string) { + const profile = await this.profileRepo.findOne({ + where: { keycloak: keycloakId }, + relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"], + }); + if (!profile) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); + } + + const orgRevisionPublish = await this.orgRevisionRepo + .createQueryBuilder("orgRevision") + .where("orgRevision.orgRevisionIsDraft = false") + .andWhere("orgRevision.orgRevisionIsCurrent = true") + .getOne(); + if (!orgRevisionPublish) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); + } + + const root = + profile.current_holders == null || + profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot == null + ? null + : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot; + + const _profile: any = { + profileId: profile.id, + prefix: profile.prefix, + rank: profile.rank, + avatar: profile.avatar, + avatarName: profile.avatarName, + firstName: profile.firstName, + lastName: profile.lastName, + citizenId: profile.citizenId, + birthDate: profile.birthDate, + position: profile.position, + rootId: root == null ? null : root.id, + root: root == null ? null : root.orgRootName, + rootShortName: root == null ? null : root.orgRootShortName, + }; + return new HttpSuccess(_profile); + } + + /** + * 8. หา root OC Id + * + * @summary 8. หา root OC Id + * + * @param {string} ocId Id หน่วยงาน + */ + @Get("root-oc/{ocId}") + async GetRootOcId(@Path() ocId: string) { + const orgRoot = await this.orgRootRepo.findOne({ + where: { id: ocId }, + }); + if (!orgRoot) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + const root = orgRoot? orgRoot.id : ""; + return new HttpSuccess(root); + } + + /** + * 5. เอารายชื่อคนที่มีการ ,map keycloak id แล้ว + * + * @summary 5. เอารายชื่อคนที่มีการ ,map keycloak id แล้ว + * + */ + @Get("keycloak") + async GetProfileWithKeycloak() { + const profile = await this.profileRepo.find({ + where: { keycloak: Not(IsNull()) || Not(""), }, + }); + return new HttpSuccess(profile); + } + + /** + * 4. API Update รอบการลงเวลา ในตาราง profile + * + * @summary 4. API Update รอบการลงเวลา ในตาราง profile + * + */ + @Put("update-dutytime") + async UpdateDutyTimeAsync( + @Request() req: RequestWithUser, + @Body() body: { + profileId: string; + roundId: string; + effectiveDate: Date; + } + ) { + const profile = await this.profileRepo.findOne({ + where: { id: body.profileId }, + }); + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + Object.assign(profile, body); + profile.dutyTimeId = body.roundId; + profile.dutyTimeEffectiveDate = body.effectiveDate; + profile.lastUpdateUserId = req.user.sub; + profile.lastUpdateFullName = req.user.name; + await this.profileRepo.save(profile); + return new HttpSuccess(); + } + }