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 { Like } from "typeorm"; @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); /** * API รายการตำแหน่งทางการบริหาร * * @summary ORG_026 - รายการตำแหน่งทางการบริหาร (ADMIN) #28 * */ @Get("executive") @Example([ { id: "00000000-0000-0000-0000-000000000000", posExecutiveName: "นักบริหาร", posExecutivePriority: 1 }, ]) async GetPosExecutive() { try { const posExecutive = await this.posExecutiveRepository.find({ select: [ "id", "posExecutiveName", "posExecutivePriority" ] }); if (!posExecutive) { return new HttpSuccess([]); } return new HttpSuccess(posExecutive); } catch (error) { return error; } } /** * API รายการประเภทตำแหน่ง * * @summary ORG_027 - รายการประเภทตำแหน่ง (ADMIN) #29 * */ @Get("type") @Example([ { id: "00000000-0000-0000-0000-000000000000", posTypeName: "นักบริหาร", posTypeRank: 1, posLevels: [ { id : "00000000-0000-0000-0000-000000000000", posLevelName : "นักบริหาร", posLevelRank : 1, posLevelAuthority : "HEAD", } ] }, ]) async GetPosType() { try { const posType = await this.posTypeRepository.find({ select: [ "id", "posTypeName", "posTypeRank" ], relations: ["posLevels"], }); if (!posType) { return new HttpSuccess([]); } const mapPosType = posType.map(item => ({ id: item.id, posTypeName: item.posTypeName, posTypeRank: item.posTypeRank, posLevels: item.posLevels.map((posLevel) => ({ id: posLevel.id, posLevelName: posLevel.posLevelName, posLevelRank: posLevel.posLevelRank, posLevelAuthority: posLevel.posLevelAuthority })) })); return new HttpSuccess(mapPosType); } catch (error) { return error; } } /** * API รายการระดับตำแหน่ง * * @summary ORG_028 - รายการระดับตำแหน่ง (ADMIN) #30 * */ @Get("level") @Example([ { id: "00000000-0000-0000-0000-000000000000", posLevelName: "นักบริหาร", posLevelRank: 1, posLevelAuthority: "HEAD", posTypes: { id: "00000000-0000-0000-0000-000000000000", posTypeName: "นักบริหาร", posTypeRank: 1 } }, ]) async GetPosLevel() { try { const posLevel = await this.posLevelRepository.find({ select: [ "id", "posLevelName", "posLevelRank", "posLevelAuthority", "posTypeId" ], relations: ["posType"], }); if (!posLevel) { return new HttpSuccess([]); } const mapPosLevel = posLevel.map(item => ({ id: item.id, posLevelName: item.posLevelName, posLevelRank: item.posLevelRank, posLevelAuthority: item.posLevelAuthority, posTypes: { id: item.posType.id, posTypeName: item.posType.posTypeName, posTypeRank: item.posType.posTypeRank, } })); return new HttpSuccess(mapPosLevel); } catch (error) { return error; } } /** * 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: requestBody.posTypeId }}); if (!checkPosTypeId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosTypeId"); } const checkPosLevelId = await this.posLevelRepository.findOne({where: { id: requestBody.posLevelId }}); if (!checkPosLevelId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล PosLevelId"); } const checkPosExecutiveId = await this.posExecutiveRepository.findOne({where: { id: requestBody.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("{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){ // { id: "positionName", name: "ตำแหน่งในสายงาน" } // { id: "positionField", name: "สายงาน" } // { id: "positionType", name: "ประเภทตำแหน่ง" } // { id: "positionLevel", name: "ระดับตำแหน่ง" } // { id: "positionExecutive", name: "ตำแหน่งทางการบริหาร" } // { id: "positionExecutiveField", name: "ด้านทางการบริหาร" } // { id: "positionArea", name: "ด้าน/สาขา" } try{ let findPosDict: any; console.log("type: ", type) console.log("keyword: ", keyword) switch(type){ case "positionName": findPosDict = await this.posDictRepository.find({ where: { posDictName: Like(`%${keyword}%`) } }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); } case "positionField": findPosDict = await this.posDictRepository.find({ where: { posDictField: Like(`%${keyword}%`) } }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); } case "positionType": findPosDict = await this.posDictRepository.find({ where: { posTypeId: Like(`%${keyword}%`) } }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); } case "positionLevel": findPosDict = await this.posDictRepository.find({ where: { posLevelId: Like(`%${keyword}%`) } }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); } case "positionExecutive": findPosDict = await this.posDictRepository.find({ where: { posExecutiveId: Like(`%${keyword}%`) } }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); } case "positionExecutiveField": findPosDict = await this.posDictRepository.find({ where: { posDictExecutiveField: Like(`%${keyword}%`) } }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); } case "positionArea": findPosDict = await this.posDictRepository.find({ where: { posDictArea: Like(`%${keyword}%`) } }); if (!findPosDict) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); } } return new HttpSuccess(findPosDict); } catch (error) { return error; } } }