diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index d0d4ed55..935ff263 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -267,73 +267,115 @@ export class PositionController extends Controller { * */ @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: "ด้าน/สาขา" } - + async findPosition(@Query("keyword") keyword: string, @Query("type") type: string){ try { let findPosDict: any; - console.log("type: ", type); - console.log("keyword: ", keyword); - - switch (type) { + switch(type){ case "positionName": - findPosDict = await this.posDictRepository.find({ - where: { posDictName: Like(`%${keyword}%`) }, - }); + findPosDict = await this.posDictRepository.find({ where: { posDictName: Like(`%${keyword}%`) } }); if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); } + break; + case "positionField": - findPosDict = await this.posDictRepository.find({ - where: { posDictField: Like(`%${keyword}%`) }, - }); + findPosDict = await this.posDictRepository.find({ where: { posDictField: Like(`%${keyword}%`) } }); if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); } + break; + case "positionType": - findPosDict = await this.posDictRepository.find({ - where: { posTypeId: Like(`%${keyword}%`) }, - }); - if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + const findTypes: PosType[] = await this.posTypeRepository.find({ where: { posTypeName: Like(`%${keyword}%`) } }); + if(!findTypes){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword + " ใน posType"); } + for (const types of findTypes) { + findPosDict = await this.posDictRepository.find({ where: { posTypeId: types.id } }); + if (!findPosDict) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); + } + } + break; + case "positionLevel": - findPosDict = await this.posDictRepository.find({ - where: { posLevelId: Like(`%${keyword}%`) }, - }); - if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + const findLevel: PosLevel[] = await this.posLevelRepository.find({ where: { posLevelName: Like(`%${keyword}%`) } }) + if(!findLevel){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword + " ใน posLevel"); } + + for (const types of findLevel) { + findPosDict = await this.posDictRepository.find({ where: { posLevelId: types.id } }); + if (!findPosDict) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); + } + } + break; + case "positionExecutive": - findPosDict = await this.posDictRepository.find({ - where: { posExecutiveId: Like(`%${keyword}%`) }, - }); - if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + const findExecutive: PosExecutive[] = await this.posExecutiveRepository.find({ where: { posExecutiveName: Like(`%${keyword}%`) } }) + if(!findExecutive){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword + " ใน posExecutive"); } + + for (const types of findExecutive) { + findPosDict = await this.posDictRepository.find({ where: { posExecutiveId: types.id } }); + if (!findPosDict) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); + } + } + break; + case "positionExecutiveField": - findPosDict = await this.posDictRepository.find({ - where: { posDictExecutiveField: Like(`%${keyword}%`) }, - }); + findPosDict = await this.posDictRepository.find({ where: { posDictExecutiveField: Like(`%${keyword}%`) } }); if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); } + break; + case "positionArea": - findPosDict = await this.posDictRepository.find({ - where: { posDictArea: Like(`%${keyword}%`) }, - }); + findPosDict = await this.posDictRepository.find({ where: { posDictArea: Like(`%${keyword}%`) } }); if (!findPosDict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword); + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); } + break; + + default: + findPosDict = await this.posDictRepository.find(); + if (!findPosDict) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + break; } - return new HttpSuccess(findPosDict); - } catch (error) { + + if(!findPosDict){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล "+ keyword); + } + + const mapDataPosDict = await Promise.all(findPosDict.map(async (item: any) => { + const posTypeName = await this.posTypeRepository.findOne({ where: { id: item.posTypeId }, select: ["posTypeName"] }); + const posLevelName = await this.posLevelRepository.findOne({ where: { id: item.posLevelId }, select: ["posLevelName"] }); + const posExecutiveName = await this.posExecutiveRepository.findOne({ where: { id: item.posExecutiveId }, select: ["posExecutiveName"] }); + + return { + id: item.id, + positionName: item.posDictName, + positionField: item.posDictField, + posTypeId: item.posTypeId, + posTypeName: posTypeName ? posTypeName.posTypeName : null, + posLevelId: item.posLevelId, + posLevelName: posLevelName ? posLevelName.posLevelName : null, + posExecutiveId: item.posExecutiveId, + posExecutiveName: posExecutiveName ? posExecutiveName.posExecutiveName : null, + positionExecutiveField: item.posDictExecutiveField, + positionArea: item.posDictArea, + positionIsSelected: false + }; + })); + + return new HttpSuccess(mapDataPosDict); + } + catch (error) { return error; } }