import { Controller, Get, Post, Put, Delete, Patch, Route, Security, Tags, Body, Path, Request, Example, SuccessResponse, Response, Query, } from "tsoa"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import HttpStatusCode from "../interfaces/http-status"; import { PosExecutive } from "../entities/PosExecutive"; import { PosType } from "../entities/PosType"; import { PosLevel } from "../entities/PosLevel"; import { CreatePosDict, PosDict } from "../entities/PosDict"; import HttpError from "../interfaces/http-error"; import { Equal, ILike, In, IsNull, Like, Not } from "typeorm"; import { CreatePosMaster, PosMaster } from "../entities/PosMaster"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; import { Position } from "../entities/Position"; import { Brackets } from "typeorm/browser"; @Route("api/v1/org/pos") @Tags("Position") // @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class PositionController extends Controller { private posExecutiveRepository = AppDataSource.getRepository(PosExecutive); private posTypeRepository = AppDataSource.getRepository(PosType); private posLevelRepository = AppDataSource.getRepository(PosLevel); private posDictRepository = AppDataSource.getRepository(PosDict); private posMasterRepository = AppDataSource.getRepository(PosMaster); private positionRepository = AppDataSource.getRepository(Position); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private orgRootRepository = AppDataSource.getRepository(OrgRoot); private child1Repository = AppDataSource.getRepository(OrgChild1); private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); /** * API เพิ่มตำแหน่ง * * @summary ORG_030 - เพิ่มตำแหน่ง (ADMIN) #33 * */ @Post("position") @Example([ { positionName: "นักบริหาร", positionField: "บริหาร", posTypeId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", posLevelId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", posExecutiveId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", positionExecutiveField: "นักบริหาร", positionArea: "บริหาร", }, ]) async createPosition( @Body() requestBody: CreatePosDict, @Request() request: { user: Record }, ) { const posDict = Object.assign(new PosDict(), requestBody); if (!posDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const checkPosTypeId = await this.posTypeRepository.findOne({ where: { id: posDict.posTypeId }, }); if (!checkPosTypeId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosTypeId"); } const checkPosLevelId = await this.posLevelRepository.findOne({ where: { id: posDict.posLevelId }, }); if (!checkPosLevelId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosLevelId"); } if (posDict.posExecutiveId == "") { posDict.posExecutiveId = null; } if (posDict.posExecutiveId != null) { const checkPosExecutiveId = await this.posExecutiveRepository.findOne({ where: { id: posDict.posExecutiveId }, }); if (!checkPosExecutiveId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosExecutiveId"); } } try { posDict.createdUserId = request.user.sub; posDict.createdFullName = request.user.name; posDict.lastUpdateUserId = request.user.sub; posDict.lastUpdateFullName = request.user.name; await this.posDictRepository.save(posDict); return new HttpSuccess(posDict.id); } catch (error) { return error; } } /** * API ลบตำแหน่ง * * @summary ORG_032 - ลบตำแหน่ง (ADMIN) #40 * * @param {string} id Id ตำแหน่ง */ @Delete("position/{id}") async delete(@Path() id: string) { const delPosDict = await this.posDictRepository.findOne({ where: { id } }); if (!delPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); } try { await this.posDictRepository.remove(delPosDict); return new HttpSuccess(); } catch (error) { return error; } } /** * API ค้นหารายการตำแหน่ง * * @summary ORG_029 - ค้นหารายการตำแหน่ง (ADMIN) #32 * */ @Get("position") async findPosition(@Query("keyword") keyword?: string, @Query("type") type?: string) { try { let findPosDict: any; switch (type) { case "positionName": findPosDict = await this.posDictRepository.find({ where: { posDictName: Like(`%${keyword}%`) }, relations: ["posType", "posLevel", "posExecutive"], }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); } break; case "positionField": findPosDict = await this.posDictRepository.find({ where: { posDictField: Like(`%${keyword}%`) }, relations: ["posType", "posLevel", "posExecutive"], }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); } break; case "positionType": const findTypes: PosType[] = await this.posTypeRepository.find({ where: { posTypeName: Like(`%${keyword}%`) }, select: ["id"], }); findPosDict = await this.posDictRepository.find({ where: { posTypeId: In(findTypes.map((x) => x.id)) }, relations: ["posType", "posLevel", "posExecutive"], }); break; case "positionLevel": const findLevel: PosLevel[] = await this.posLevelRepository.find({ where: { posLevelName: Like(`%${keyword}%`) }, select: ["id"], }); findPosDict = await this.posDictRepository.find({ where: { posLevelId: In(findLevel.map((x) => x.id)) }, relations: ["posType", "posLevel", "posExecutive"], }); break; case "positionExecutive": const findExecutive: PosExecutive[] = await this.posExecutiveRepository.find({ where: { posExecutiveName: Like(`%${keyword}%`) }, select: ["id"], }); findPosDict = await this.posDictRepository.find({ where: { posExecutiveId: In(findExecutive.map((x) => x.id)) }, relations: ["posType", "posLevel", "posExecutive"], }); break; case "positionExecutiveField": findPosDict = await this.posDictRepository.find({ where: { posDictExecutiveField: Like(`%${keyword}%`) }, relations: ["posType", "posLevel", "posExecutive"], }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); } break; case "positionArea": findPosDict = await this.posDictRepository.find({ where: { posDictArea: Like(`%${keyword}%`) }, relations: ["posType", "posLevel", "posExecutive"], }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); } break; default: findPosDict = await this.posDictRepository.find({ relations: ["posType", "posLevel", "posExecutive"], }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } break; } const mapDataPosDict = await Promise.all( findPosDict.map(async (item: any) => { return { id: item.id, positionName: item.posDictName, positionField: item.posDictField, posTypeId: item.posTypeId, posTypeName: item.posType == null ? null : item.posType.posTypeName, posLevelId: item.posLevelId, posLevelName: item.posLevel == null ? null : item.posLevel.posLevelName, posExecutiveId: item.posExecutiveId, posExecutiveName: item.posExecutive == null ? null : item.posExecutive.posExecutiveName, positionExecutiveField: item.posDictExecutiveField, positionArea: item.posDictArea, positionIsSelected: false, }; }), ); return new HttpSuccess(mapDataPosDict); } catch (error) { return error; } } /** * API เพิ่มอัตรากำลัง * * @summary ORG_033 - เพิ่มอัตรากำลัง (ADMIN) #35 * */ @Post("master") @Example({ posMasterNoPrefix: "กบ.", posMasterNo: 1, posMasterNoSuffix: "ช", posId: ["08db9e81-fc46-4e95-8b33-be4ca0016abf", "08db9e81-fc46-4e95-8b33-be4ca0016abf"], orgRootId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", orgChild1Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf", orgChild2Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf", orgChild3Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf", orgChild4Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf", positions: [ { posDictName: "นักบริหาร", posDictField: "บริหาร", posTypeId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", posLevelId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", posExecutiveId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", posDictExecutiveField: "นักบริหาร", posDictArea: "บริหาร", }, ], }) async createMaster( @Body() requestBody: CreatePosMaster, @Request() request: { user: Record }, ) { const posMaster = Object.assign(new PosMaster(), requestBody); if (!posMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } let orgRoot: any = null; if (requestBody.orgRootId != null) orgRoot = await this.orgRootRepository.findOne({ where: { id: requestBody.orgRootId }, }); if (!orgRoot) { let orgChild1: any = null; if (requestBody.orgChild1Id != null) orgChild1 = await this.child1Repository.findOne({ where: { id: requestBody.orgChild1Id }, }); if (!orgChild1) { let orgChild2: any = null; if (requestBody.orgChild2Id != null) orgChild2 = await this.child2Repository.findOne({ where: { id: requestBody.orgChild2Id }, }); if (!orgChild2) { let orgChild3: any = null; if (requestBody.orgChild3Id != null) orgChild3 = await this.child3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); if (!orgChild3) { let orgChild4: any = null; if (requestBody.orgChild4Id != null) orgChild4 = await this.child4Repository.findOne({ where: { id: requestBody.orgChild4Id }, }); if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้าง"); } else { posMaster.orgRootId = orgChild4.orgRootId; posMaster.orgChild1Id = orgChild4.orgChild1Id; posMaster.orgChild2Id = orgChild4.orgChild2Id; posMaster.orgChild3Id = orgChild4.orgChild3Id; posMaster.orgChild4Id = orgChild4.id; posMaster.orgRevisionId = orgChild4.orgRevisionId; } } else { posMaster.orgRootId = orgChild3.orgRootId; posMaster.orgChild1Id = orgChild3.orgChild1Id; posMaster.orgChild2Id = orgChild3.orgChild2Id; posMaster.orgChild3Id = orgChild3.id; posMaster.orgRevisionId = orgChild3.orgRevisionId; } } else { posMaster.orgRootId = orgChild2.orgRootId; posMaster.orgChild1Id = orgChild2.orgChild1Id; posMaster.orgChild2Id = orgChild2.id; posMaster.orgRevisionId = orgChild2.orgRevisionId; } } else { posMaster.orgRootId = orgChild1.orgRootId; posMaster.orgChild1Id = orgChild1.id; posMaster.orgRevisionId = orgChild1.orgRevisionId; } } else { posMaster.orgRootId = orgRoot.id; posMaster.orgRevisionId = orgRoot.orgRevisionId; } posMaster.createdUserId = request.user.sub; posMaster.createdFullName = request.user.name; posMaster.lastUpdateUserId = request.user.sub; posMaster.lastUpdateFullName = request.user.name; await this.posMasterRepository.save(posMaster); requestBody.positions.forEach(async (x: any) => { const position = Object.assign(new Position()); position.positionName = x.posDictName; position.positionField = x.posDictField; position.posTypeId = x.posTypeId; position.posLevelId = x.posLevelId; position.posExecutiveId = x.posExecutiveId; position.positionExecutiveField = x.posDictExecutiveField; position.positionArea = x.posDictArea; position.positionIsSelected = false; position.posMasterId = posMaster.id; position.createdUserId = request.user.sub; position.createdFullName = request.user.name; position.lastUpdateUserId = request.user.sub; position.lastUpdateFullName = request.user.name; await this.positionRepository.save(position); }); return new HttpSuccess(posMaster.id); } /** * API แก้ไขเลขที่ตำแหน่ง * * @summary ORG_034 - แก้ไขเลขที่ตำแหน่ง (ADMIN) #37 * */ @Put("master/{id}") @Example({ posMasterNoPrefix: "กบ.", posMasterNo: "1", posMasterNoSuffix: "ช", posId: ["08db9e81-fc46-4e95-8b33-be4ca0016abf", "08db9e81-fc46-4e95-8b33-be4ca0016abf"], orgRootId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", orgChild1Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf", orgChild2Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf", orgChild3Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf", orgChild4Id: "08db9e81-fc46-4e95-8b33-be4ca0016abf", positions: [ { posDictName: "นักบริหาร", posDictField: "บริหาร", posTypeId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", posLevelId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", posExecutiveId: "08db9e81-fc46-4e95-8b33-be4ca0016abf", posDictExecutiveField: "นักบริหาร", posDictArea: "บริหาร", }, ], }) async updateMaster( @Path() id: string, @Body() requestBody: CreatePosMaster, @Request() request: { user: Record }, ) { const posMaster = await this.posMasterRepository.findOne({ where: { id: id } }); if (!posMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลอัตรากำลัง"); } posMaster.orgRootId = null; posMaster.orgChild1Id = null; posMaster.orgChild2Id = null; posMaster.orgChild3Id = null; posMaster.orgChild4Id = null; let orgRoot: any = null; if (requestBody.orgRootId != null) orgRoot = await this.orgRootRepository.findOne({ where: { id: requestBody.orgRootId }, }); if (!orgRoot) { let orgChild1: any = null; if (requestBody.orgChild1Id != null) orgChild1 = await this.child1Repository.findOne({ where: { id: requestBody.orgChild1Id }, }); if (!orgChild1) { let orgChild2: any = null; if (requestBody.orgChild2Id != null) orgChild2 = await this.child2Repository.findOne({ where: { id: requestBody.orgChild2Id }, }); if (!orgChild2) { let orgChild3: any = null; if (requestBody.orgChild3Id != null) orgChild3 = await this.child3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); if (!orgChild3) { let orgChild4: any = null; if (requestBody.orgChild4Id != null) orgChild4 = await this.child4Repository.findOne({ where: { id: requestBody.orgChild4Id }, }); if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้าง"); } else { posMaster.orgRootId = orgChild4.orgRootId; posMaster.orgChild1Id = orgChild4.orgChild1Id; posMaster.orgChild2Id = orgChild4.orgChild2Id; posMaster.orgChild3Id = orgChild4.orgChild3Id; posMaster.orgChild4Id = orgChild4.id; posMaster.orgRevisionId = orgChild4.orgRevisionId; } } else { posMaster.orgRootId = orgChild3.orgRootId; posMaster.orgChild1Id = orgChild3.orgChild1Id; posMaster.orgChild2Id = orgChild3.orgChild2Id; posMaster.orgChild3Id = orgChild3.id; posMaster.orgRevisionId = orgChild3.orgRevisionId; } } else { posMaster.orgRootId = orgChild2.orgRootId; posMaster.orgChild1Id = orgChild2.orgChild1Id; posMaster.orgChild2Id = orgChild2.id; posMaster.orgRevisionId = orgChild2.orgRevisionId; } } else { posMaster.orgRootId = orgChild1.orgRootId; posMaster.orgChild1Id = orgChild1.id; posMaster.orgRevisionId = orgChild1.orgRevisionId; } } else { posMaster.orgRootId = orgRoot.id; posMaster.orgRevisionId = orgRoot.orgRevisionId; } posMaster.createdUserId = request.user.sub; //สงสัยว่าทำให้ bug แก้ไขไม่ได้ posMaster.createdFullName = request.user.name; posMaster.lastUpdateUserId = request.user.sub; posMaster.lastUpdateFullName = request.user.name; await this.posMasterRepository.save(posMaster); await this.positionRepository.delete({ posMasterId: posMaster.id }); requestBody.positions.forEach(async (x: any) => { const position = Object.assign(new Position()); position.positionName = x.posDictName; position.positionField = x.posDictField; position.posTypeId = x.posTypeId; position.posLevelId = x.posLevelId; position.posExecutiveId = x.posExecutiveId; position.positionExecutiveField = x.posDictExecutiveField; position.positionArea = x.posDictArea; position.positionIsSelected = false; position.posMasterId = posMaster.id; position.createdUserId = request.user.sub; position.createdFullName = request.user.name; position.lastUpdateUserId = request.user.sub; position.lastUpdateFullName = request.user.name; await this.positionRepository.save(position); }); return new HttpSuccess(posMaster.id); } /** * API รายละเอียดอัตรากำลัง * * @summary ORG_037 - รายละเอียดอัตรากำลัง (ADMIN) #36 * */ @Get("position/{id}") async detailPosition(@Path() id: string) { try { const posMaster = await this.posMasterRepository.findOne({ where: { id }, }); if (!posMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const positions = await this.positionRepository.find({ where: { posMasterId: posMaster.id }, relations: ["posType", "posLevel", "posExecutive"], }); const formattedData = { id: posMaster.id, posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNo: posMaster.posMasterNo, posMasterNoSuffix: posMaster.posMasterNoSuffix, positions: positions.map((position) => ({ id: position.id, positionName: position.positionName, positionField: position.positionField, posTypeId: position.posTypeId, posTypeName: position.posType == null ? null : position.posType.posTypeName, posLevelId: position.posLevelId, posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, posExecutiveId: position.posExecutiveId, posExecutiveName: position.posExecutive == null ? null : position.posExecutive.posExecutiveName, positionExecutiveField: position.positionExecutiveField, positionArea: position.positionArea, positionIsSelected: position.positionIsSelected, })), }; return new HttpSuccess(formattedData); } catch (error) { return error; } } /** * API ลบอัตรากำลัง * * @summary ORG_035 - ลบอัตรากำลัง (ADMIN) #38 * * @param {string} id Id ตำแหน่ง */ @Delete("master/{id}") async deletePosMaster(@Path() id: string) { const delPosMaster = await this.posMasterRepository.findOne({ where: { id }, // relations: ["position"], }); if (!delPosMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); } try { await this.positionRepository.delete({ posMasterId: id }); await this.posMasterRepository.delete({ id }); return new HttpSuccess(); } catch (error) { return error; } } /** * API รายการอัตรากำลัง * * @summary ORG_053 - รายการอัตรากำลัง (ADMIN) #56 * */ @Post("master/list") async list( @Body() body: { id: string; type: number; isAll: boolean; page: number; pageSize: number; keyword?: string; }, ) { try { let typeCondition: any = {}; let checkChildConditions: any = {}; if (body.type === 0) { typeCondition = { orgRootId: body.id, }; if (!body.isAll) { checkChildConditions = { orgChild1Id: IsNull(), }; } } else if (body.type === 1) { typeCondition = { orgChild1Id: body.id, }; if (!body.isAll) { checkChildConditions = { orgChild2Id: IsNull(), }; } } else if (body.type === 2) { typeCondition = { orgChild2Id: body.id, }; if (!body.isAll) { checkChildConditions = { orgChild3Id: IsNull(), }; } } else if (body.type === 3) { typeCondition = { orgChild3Id: body.id, }; if (!body.isAll) { checkChildConditions = { orgChild4Id: IsNull(), }; } } else if (body.type === 4) { typeCondition = { orgChild4Id: body.id, }; } let findPosition: any; let masterId = new Array(); if (body.keyword != null && body.keyword != "") { const findTypes: PosType[] = await this.posTypeRepository.find({ where: { posTypeName: Like(`%${body.keyword}%`) }, select: ["id"], }); findPosition = await this.positionRepository.find({ where: { posTypeId: In(findTypes.map((x) => x.id)) }, select: ["posMasterId"], }); masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); const findLevel: PosLevel[] = await this.posLevelRepository.find({ where: { posLevelName: Like(`%${body.keyword}%`) }, select: ["id"], }); findPosition = await this.positionRepository.find({ where: { posLevelId: In(findLevel.map((x) => x.id)) }, select: ["posMasterId"], }); masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); const findExecutive: PosExecutive[] = await this.posExecutiveRepository.find({ where: { posExecutiveName: Like(`%${body.keyword}%`) }, select: ["id"], }); findPosition = await this.positionRepository.find({ where: { posExecutiveId: In(findExecutive.map((x) => x.id)) }, select: ["posMasterId"], }); masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); findPosition = await this.positionRepository.find({ where: { positionName: Like(`%${body.keyword}%`) }, select: ["posMasterId"], }); masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId)); let keywordAsInt: any; keywordAsInt = body.keyword == null ? null : parseInt(body.keyword, 10); if (isNaN(keywordAsInt)) { keywordAsInt = "P@ssw0rd!z"; } masterId = [...new Set(masterId)]; } const keywordConditions = [ { ...checkChildConditions, ...typeCondition, }, // { // ...checkChildConditions, // ...typeCondition, // id: masterId.length > 0 ? In(masterId) : false, // }, // { // ...checkChildConditions, // ...typeCondition, // posMasterNo: Like(`%${keywordAsInt}%`), // }, ]; const [posMaster, total] = await this.posMasterRepository.findAndCount({ where: keywordConditions, order: { posMasterOrder: "ASC" }, relations: [ "orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4", "current_holder", "next_holder", ], skip: (body.page - 1) * body.pageSize, take: body.pageSize, }); const formattedData = await Promise.all( posMaster.map(async (posMaster) => { const positions = await this.positionRepository.find({ where: { posMasterId: posMaster.id, }, relations: ["posLevel", "posType", "posExecutive"], }); let shortName = "-"; if (body.isAll === true) { if ( posMaster.orgRootId !== null && posMaster.orgChild1Id == null && posMaster.orgChild2Id == null && posMaster.orgChild3Id == null ) { body.type = 0; shortName = posMaster.orgRoot.orgRootShortName; } else if ( posMaster.orgRootId !== null && posMaster.orgChild1Id !== null && posMaster.orgChild2Id == null && posMaster.orgChild3Id == null ) { body.type = 1; shortName = posMaster.orgChild1.orgChild1ShortName; } else if ( posMaster.orgRootId !== null && posMaster.orgChild1Id !== null && posMaster.orgChild2Id !== null && posMaster.orgChild3Id == null ) { body.type = 2; shortName = posMaster.orgChild2.orgChild2ShortName; } else if ( posMaster.orgRootId !== null && posMaster.orgChild1Id !== null && posMaster.orgChild2Id !== null && posMaster.orgChild3Id !== null ) { body.type = 3; shortName = posMaster.orgChild3.orgChild3ShortName; } else if ( posMaster.orgRootId !== null && posMaster.orgChild1Id !== null && posMaster.orgChild2Id !== null && posMaster.orgChild3Id !== null ) { body.type = 4; shortName = posMaster.orgChild4.orgChild4ShortName; } } return { id: posMaster.id, orgRootId: posMaster.orgRootId, orgChild1Id: posMaster.orgChild1Id, orgChild2Id: posMaster.orgChild2Id, orgChild3Id: posMaster.orgChild3Id, orgChild4Id: posMaster.orgChild4Id, posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNo: posMaster.posMasterNo, posMasterNoSuffix: posMaster.posMasterNoSuffix, fullNameCurrentHolder: posMaster.current_holder == null ? null : `${posMaster.current_holder.prefix}${posMaster.current_holder.firstName} ${posMaster.current_holder.lastName}`, fullNameNextHolder: posMaster.next_holder == null ? null : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, orgShortname: shortName, positions: positions.map((position) => ({ id: position.id, positionName: position.positionName, positionField: position.positionField, posTypeId: position.posTypeId, posTypeName: position.posType == null ? null : position.posType.posTypeName, posLevelId: position.posLevelId, posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName, posExecutiveId: position.posExecutiveId, posExecutiveName: position.posExecutive == null ? null : position.posExecutive.posExecutiveName, positionExecutiveField: position.positionExecutiveField, positionArea: position.positionArea, positionIsSelected: position.positionIsSelected, })), }; }), ); return new HttpSuccess({ data: formattedData, total }); } catch (error) { return error; } } /** * API จัดลำดับตำแหน่ง * * @summary ORG_040 - จัดลำดับตำแหน่ง (ADMIN) #43 * */ @Post("sort") async Sort(@Body() requestBody: { id: string; type: number; sortId: string[] }) { try { switch (requestBody.type) { case 0: { const rootId = await this.posMasterRepository.findOne({ where: { orgRootId: requestBody.id }, }); if (!rootId?.id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found rootId: " + requestBody.id); } const listPosMasterId_0 = await this.posMasterRepository.find({ where: { orgRootId: requestBody.id, orgChild1Id: IsNull(), orgChild2Id: IsNull(), orgChild3Id: IsNull(), orgChild4Id: IsNull(), }, select: ["id", "posMasterOrder"], }); if (!listPosMasterId_0) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 0."); } const sortData_0 = listPosMasterId_0.map((data) => ({ id: data.id, posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, })); await this.posMasterRepository.save(sortData_0); break; } case 1: { const child1Id = await this.posMasterRepository.findOne({ where: { orgChild1Id: requestBody.id }, }); if (!child1Id?.id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id: " + requestBody.id); } const listPosMasterId_1 = await this.posMasterRepository.find({ where: { orgRootId: Not(IsNull()), orgChild1Id: requestBody.id, orgChild2Id: IsNull(), orgChild3Id: IsNull(), orgChild4Id: IsNull(), }, select: ["id", "posMasterOrder"], }); if (!listPosMasterId_1) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 1."); } const sortData_1 = listPosMasterId_1.map((data) => ({ id: data.id, posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, })); await this.posMasterRepository.save(sortData_1); break; } case 2: { const child2Id = await this.posMasterRepository.findOne({ where: { orgChild2Id: requestBody.id }, }); if (!child2Id?.id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child2Id: " + requestBody.id); } const listPosMasterId_2 = await this.posMasterRepository.find({ where: { orgRootId: Not(IsNull()), orgChild1Id: Not(IsNull()), orgChild2Id: requestBody.id, orgChild3Id: IsNull(), orgChild4Id: IsNull(), }, select: ["id", "posMasterOrder"], }); if (!listPosMasterId_2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 2."); } const sortData_2 = listPosMasterId_2.map((data) => ({ id: data.id, posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, })); await this.posMasterRepository.save(sortData_2); break; } case 3: { const child3Id = await this.posMasterRepository.findOne({ where: { orgChild3Id: requestBody.id }, }); if (!child3Id?.id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found chil3Id: " + requestBody.id); } const listPosMasterId_3 = await this.posMasterRepository.find({ where: { orgRootId: Not(IsNull()), orgChild1Id: Not(IsNull()), orgChild2Id: Not(IsNull()), orgChild3Id: requestBody.id, orgChild4Id: IsNull(), }, select: ["id", "posMasterOrder"], }); if (!listPosMasterId_3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 3."); } const sortData_3 = listPosMasterId_3.map((data) => ({ id: data.id, posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, })); await this.posMasterRepository.save(sortData_3); break; } case 4: { const child4Id = await this.posMasterRepository.findOne({ where: { orgChild4Id: requestBody.id }, }); if (!child4Id?.id) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child4Id: " + requestBody.id); } const listPosMasterId_4 = await this.posMasterRepository.find({ where: { orgRootId: Not(IsNull()), orgChild1Id: Not(IsNull()), orgChild2Id: Not(IsNull()), orgChild3Id: Not(IsNull()), orgChild4Id: requestBody.id, }, select: ["id", "posMasterOrder"], }); if (!listPosMasterId_4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found masterId type 4."); } const sortData_4 = listPosMasterId_4.map((data) => ({ id: data.id, posMasterOrder: requestBody.sortId.indexOf(data.id) + 1, })); await this.posMasterRepository.save(sortData_4); break; } default: throw new HttpError(HttpStatusCode.NOT_FOUND, "not found type: " + requestBody.type); } return new HttpSuccess(); } catch (error) { return error; } } /** * API ดูประวัติอัตรากำลัง * * @summary ORG_054 - ดูประวัติอัตรากำลัง (ADMIN) #58 * * @param {string} id Id อัตรากำลัง */ @Get("history/{id}") async getHistoryPosMater(@Path() id: string) { const posMaster = await this.posMasterRepository.findOne({ where: { id }, }); if (!posMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); } const posMasters = await this.posMasterRepository.find({ where: { ancestorDNA: posMaster.ancestorDNA }, order: { lastUpdatedAt: "DESC" }, relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], }); const _data = posMasters.map((item) => ({ id: item.id, orgShortName: item.orgRoot == null ? "-" : item.orgChild1 == null ? item.orgRoot.orgRootShortName : item.orgChild2 == null ? item.orgChild1.orgChild1ShortName : item.orgChild3 == null ? item.orgChild2.orgChild2ShortName : item.orgChild4 == null ? item.orgChild3.orgChild3ShortName : item.orgChild4.orgChild4ShortName, lastUpdatedAt: item.lastUpdatedAt, posMasterNoPrefix: item.posMasterNoPrefix, posMasterNo: item.posMasterNo, posMasterNoSuffix: item.posMasterNoSuffix, })); return new HttpSuccess(_data); } /** * API ย้ายอัตรากำลัง * * @summary ORG_054 - ย้ายอัตรากำลัง (ADMIN) #59 * */ @Post("move") async movePosMaster( @Body() requestBody: { id: string; type: number; positionMaster: string[] }, @Request() request: { user: Record }, ) { try { const posMasters = await this.posMasterRepository.find({ where: { id: In(requestBody.positionMaster) }, }); posMasters.forEach(async (posMaster: any) => { posMaster.orgRootId = null; posMaster.orgChild1Id = null; posMaster.orgChild2Id = null; posMaster.orgChild3Id = null; posMaster.orgChild4Id = null; if (requestBody.type == 0) { const org = await this.orgRootRepository.findOne({ where: { id: requestBody.id }, }); if (org != null) { posMaster.orgRootId = org.id; posMaster.orgRevisionId = org.orgRevisionId; } } if (requestBody.type == 1) { const org = await this.child1Repository.findOne({ where: { id: requestBody.id }, }); if (org != null) { posMaster.orgRootId = org.orgRootId; posMaster.orgChild1Id = org.id; posMaster.orgRevisionId = org.orgRevisionId; } } if (requestBody.type == 2) { const org = await this.child2Repository.findOne({ where: { id: requestBody.id }, }); if (org != null) { posMaster.orgRootId = org.orgRootId; posMaster.orgChild1Id = org.orgChild1Id; posMaster.orgChild2Id = org.id; posMaster.orgRevisionId = org.orgRevisionId; } } if (requestBody.type == 3) { const org = await this.child3Repository.findOne({ where: { id: requestBody.id }, }); if (org != null) { posMaster.orgRootId = org.orgRootId; posMaster.orgChild1Id = org.orgChild1Id; posMaster.orgChild2Id = org.orgChild2Id; posMaster.orgChild3Id = org.id; posMaster.orgRevisionId = org.orgRevisionId; } } if (requestBody.type == 4) { const org = await this.child4Repository.findOne({ where: { id: requestBody.id }, }); if (org != null) { posMaster.orgRootId = org.orgRootId; posMaster.orgChild1Id = org.orgChild1Id; posMaster.orgChild2Id = org.orgChild2Id; posMaster.orgChild3Id = org.orgChild3Id; posMaster.orgChild4Id = org.id; posMaster.orgRevisionId = org.orgRevisionId; } } posMaster.createdUserId = request.user.sub; posMaster.createdFullName = request.user.name; posMaster.lastUpdateUserId = request.user.sub; posMaster.lastUpdateFullName = request.user.name; await this.posMasterRepository.save(posMaster); }); return new HttpSuccess(); } catch (error) { return error; } } /** * API ตำแหน่งทั้งหมด * * @summary ORG_055 - ตำแหน่งทั้งหมด (ADMIN) #60 * */ @Post("summary") async PositionSummary(@Body() requestBody: { id: string; type: number }) { try { let summary: any; let totalPosition: any; let totalPositionCurrentUse: any; let totalPositionCurrentVacant: any; let totalPositionNextUse: any; let totalPositionNextVacant: any; switch (requestBody.type) { case 0: { const NodeId = await this.posMasterRepository.findOne({ where: { orgRootId: requestBody.id }, }); if (!NodeId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, ); } totalPosition = await this.posMasterRepository.count({ where: { orgRootId: requestBody.id }, }); totalPositionCurrentUse = await this.posMasterRepository.count({ where: { orgRootId: requestBody.id, current_holderId: Not(IsNull()) && Not(""), }, }); totalPositionCurrentVacant = await this.posMasterRepository.count({ where: { orgRootId: requestBody.id, current_holderId: IsNull() && "", }, }); totalPositionNextUse = await this.posMasterRepository.count({ where: { orgRootId: requestBody.id, next_holderId: Not(IsNull()) && Not(""), }, }); totalPositionNextVacant = await this.posMasterRepository.count({ where: { orgRootId: requestBody.id, next_holderId: IsNull() && "", }, }); break; } case 1: { const NodeId = await this.posMasterRepository.findOne({ where: { orgChild1Id: requestBody.id }, }); if (!NodeId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, ); } totalPosition = await this.posMasterRepository.count({ where: { orgChild1Id: requestBody.id }, }); totalPositionCurrentUse = await this.posMasterRepository.count({ where: { orgChild1Id: requestBody.id, current_holderId: Not(IsNull()) && Not(""), }, }); totalPositionCurrentVacant = await this.posMasterRepository.count({ where: { orgChild1Id: requestBody.id, current_holderId: IsNull() && "", }, }); totalPositionNextUse = await this.posMasterRepository.count({ where: { orgChild1Id: requestBody.id, next_holderId: Not(IsNull()) && Not(""), }, }); totalPositionNextVacant = await this.posMasterRepository.count({ where: { orgChild1Id: requestBody.id, next_holderId: IsNull() && "", }, }); break; } case 2: { const NodeId = await this.posMasterRepository.findOne({ where: { orgChild2Id: requestBody.id }, }); if (!NodeId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, ); } totalPosition = await this.posMasterRepository.count({ where: { orgChild2Id: requestBody.id }, }); totalPositionCurrentUse = await this.posMasterRepository.count({ where: { orgChild2Id: requestBody.id, current_holderId: Not(IsNull()) && Not(""), }, }); totalPositionCurrentVacant = await this.posMasterRepository.count({ where: { orgChild2Id: requestBody.id, current_holderId: IsNull() && "", }, }); totalPositionNextUse = await this.posMasterRepository.count({ where: { orgChild2Id: requestBody.id, next_holderId: Not(IsNull()) && Not(""), }, }); totalPositionNextVacant = await this.posMasterRepository.count({ where: { orgChild2Id: requestBody.id, next_holderId: IsNull() && "", }, }); break; } case 3: { const NodeId = await this.posMasterRepository.findOne({ where: { orgChild3Id: requestBody.id }, }); if (!NodeId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, ); } totalPosition = await this.posMasterRepository.count({ where: { orgChild3Id: requestBody.id }, }); totalPositionCurrentUse = await this.posMasterRepository.count({ where: { orgChild3Id: requestBody.id, current_holderId: Not(IsNull()) && Not(""), }, }); totalPositionCurrentVacant = await this.posMasterRepository.count({ where: { orgChild3Id: requestBody.id, current_holderId: IsNull() && "", }, }); totalPositionNextUse = await this.posMasterRepository.count({ where: { orgChild3Id: requestBody.id, next_holderId: Not(IsNull()) && Not(""), }, }); totalPositionNextVacant = await this.posMasterRepository.count({ where: { orgChild3Id: requestBody.id, next_holderId: IsNull() && "", }, }); break; } case 4: { const NodeId = await this.posMasterRepository.findOne({ where: { orgChild4Id: requestBody.id }, }); if (!NodeId) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id, ); } totalPosition = await this.posMasterRepository.count({ where: { orgChild4Id: requestBody.id }, }); totalPositionCurrentUse = await this.posMasterRepository.count({ where: { orgChild4Id: requestBody.id, current_holderId: Not(IsNull()) && Not(""), }, }); totalPositionCurrentVacant = await this.posMasterRepository.count({ where: { orgChild4Id: requestBody.id, current_holderId: IsNull() && "", }, }); totalPositionNextUse = await this.posMasterRepository.count({ where: { orgChild4Id: requestBody.id, next_holderId: Not(IsNull()) && Not(""), }, }); totalPositionNextVacant = await this.posMasterRepository.count({ where: { orgChild4Id: requestBody.id, next_holderId: IsNull() && "", }, }); break; } default: break; } summary = { totalPosition: totalPosition, totalPositionCurrentUse: totalPositionCurrentUse, totalPositionCurrentVacant: totalPositionCurrentVacant, totalPositionNextUse: totalPositionNextUse, totalPositionNextVacant: totalPositionNextVacant, }; return new HttpSuccess(summary); } catch (error) { return error; } } /** * API สร้างคนครองตำแหน่ง * * @summary ORG_064 - สร้างคนครองตำแหน่ง (ADMIN) #70 * */ @Post("profile") async createHolder( @Body() requestBody: { posMaster: string; position: string; profileId: string; isSit: boolean }, ) { const dataMaster = await this.posMasterRepository.findOne({ where: { id: requestBody.posMaster }, relations: ["positions"], }); if (!dataMaster) { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + requestBody.posMaster, ); } try { dataMaster.positions.forEach(async (position) => { if (position.id === requestBody.position) { position.positionIsSelected = true; } else { position.positionIsSelected = false; } await this.positionRepository.save(position); }); dataMaster.isSit = requestBody.isSit; dataMaster.next_holderId = requestBody.profileId; await this.posMasterRepository.save(dataMaster); return new HttpSuccess(); } catch (error) { return error; } } /** * API สร้างทะเบียนประวัติ * * @summary ORG_066 - ลบคนครองตำแหน่ง (ADMIN) #71 * * @param {string} id *Id posMaster */ @Post("profile/delete/{id}") async deleteHolder(@Path() id: string) { const dataMaster = await this.posMasterRepository.findOne({ where: { id: id }, relations: ["positions"], }); if (!dataMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); } try { await this.posMasterRepository.update(id, { isSit: false, next_holderId: null, }); dataMaster.positions.forEach(async (position) => { await this.positionRepository.update(position.id, { positionIsSelected: false, }); }); return new HttpSuccess(); } catch (error) { return error; } } }