import { Controller, Get, Post, Put, Delete, Patch, Route, Security, Tags, Body, Path, Request, Example, SuccessResponse, Response, Query, } from "tsoa"; import { OrgRevision } from "../entities/OrgRevision"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { Brackets, IsNull, Not } from "typeorm"; import { OrgRoot } from "../entities/OrgRoot"; import { PosMaster } from "../entities/PosMaster"; @Route("api/v1/org/unauthorize") @Tags("OrganizationUnauthorize") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class OrganizationUnauthorizeController extends Controller { private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private orgRootRepository = AppDataSource.getRepository(OrgRoot); /** * API รายชื่อราชการที่เลื่อนเงินเดือน (unauthorize) * * @summary ORG_072 - รายชื่อราชการที่เลื่อนเงินเดือน #76 (unauthorize) * */ @Post("salary/gen") async salaryGen( @Body() body: { page: number; pageSize: number; keyword?: string; }, ) { const findRevision = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true }, }); if (!findRevision) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. OrgRevision"); } const [findPosMaster, total] = await AppDataSource.getRepository(PosMaster) .createQueryBuilder("posMaster") .leftJoinAndSelect("posMaster.current_holder", "current_holder") .leftJoinAndSelect("posMaster.orgRoot", "orgRoot") .leftJoinAndSelect("posMaster.orgChild1", "orgChild1") .leftJoinAndSelect("posMaster.orgChild2", "orgChild2") .leftJoinAndSelect("posMaster.orgChild3", "orgChild3") .leftJoinAndSelect("posMaster.orgChild4", "orgChild4") .leftJoinAndSelect("posMaster.positions", "positions") .leftJoinAndSelect("positions.posExecutive", "posExecutive") .leftJoinAndSelect("current_holder.profileSalary", "profileSalary") .leftJoinAndSelect("current_holder.posLevel", "posLevel") .leftJoinAndSelect("current_holder.posType", "posType") .where({ orgRevisionId: findRevision?.id, current_holderId: Not(IsNull()), }) .andWhere( new Brackets((qb) => { qb.where( body.keyword != null && body.keyword != "" ? "current_holder.prefix LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "current_holder.firstName LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "current_holder.lastName LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "current_holder.position LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "current_holder.citizenId LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "posType.posTypeName LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) .orWhere( body.keyword != null && body.keyword != "" ? "posLevel.posLevelName LIKE :keyword" : "1=1", { keyword: `%${body.keyword}%`, }, ) }), ) .skip((body.page - 1) * body.pageSize) .take(body.pageSize) .getManyAndCount(); if (!findPosMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. PosMaster"); } const formattedData = findPosMaster.map((item) => { let orgShortName = ""; if (item.orgChild1Id === null) { orgShortName = item.orgRoot?.orgRootShortName; } else if (item.orgChild2Id === null) { orgShortName = item.orgChild1?.orgChild1ShortName; } else if (item.orgChild3Id === null) { orgShortName = item.orgChild2?.orgChild2ShortName; } else if (item.orgChild4Id === null) { orgShortName = item.orgChild3?.orgChild3ShortName; } else { orgShortName = item.orgChild4?.orgChild4ShortName; } const posExecutive = item.positions == null || item.positions?.find((position) => position.positionIsSelected == true) == null || item.positions?.find((position) => position.positionIsSelected == true)?.posExecutive == null || item.positions?.find((position) => position.positionIsSelected == true)?.posExecutive ?.posExecutiveName == null ? null : item.positions?.find((position) => position.positionIsSelected == true)?.posExecutive .posExecutiveName; const amount = item.current_holder == null || item.current_holder.profileSalary.length == 0 ? null : item.current_holder.profileSalary.sort((a: any, b: any) => b.date - a.date)[0].amount; return { prefix: item.current_holder.prefix, firstName: item.current_holder.firstName, lastName: item.current_holder.lastName, citizenId: item.current_holder.citizenId, posMasterNoPrefix: item.posMasterNoPrefix, posMasterNo: item.posMasterNo, posMasterNoSuffix: item.posMasterNoSuffix, orgShortName: orgShortName, position: item.current_holder.position, posType: item.current_holder.posType.posTypeName, posLevel: item.current_holder.posLevel.posLevelName, posExecutive: posExecutive, amount: amount?amount:0, rootId: item.orgRootId, root: item.orgRoot?.orgRootName?item.orgRoot.orgRootName:null, child1Id: item.orgChild1Id, child1: item.orgChild1?.orgChild1Name?item.orgChild1.orgChild1Name:null, child2Id: item.orgChild2Id, child2: item.orgChild2?.orgChild2Name?item.orgChild2.orgChild2Name:null, child3Id: item.orgChild3Id, child3: item.orgChild3?.orgChild3Name?item.orgChild3.orgChild3Name:null, child4Id: item.orgChild4Id, child4: item.orgChild4?.orgChild4Name?item.orgChild4.orgChild4Name:null, isResult: true, isDuration: false, isPunish: true, isRetired: false, isRetired2: true, }; }); return new HttpSuccess({data:formattedData,total:total}); } /** * API หาสำนักทั้งหมด (unauthorize) * * @summary หาสำนักทั้งหมด (unauthorize) * */ @Get("active/root/id") async _GetActiveRootId() { try { const orgRevisionActive = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, }); if (!orgRevisionActive) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างที่เผยแพร๋อยู่ตอนนี้"); } const data = await this.orgRootRepository.find({ where: { orgRevisionId: orgRevisionActive.id }, }); return new HttpSuccess(data.map((x) => x.id)); } catch (error) { return error; } } /** * API หา revision ล่าสุด (unauthorize) * * @summary หา revision ล่าสุด (unauthorize) * */ @Get("revision/latest") async _salaryGen() { const findRevision = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true }, }); if (!findRevision) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างล่าสุด"); } return new HttpSuccess(findRevision.id); } }