From bd6ac8ae3c2152f3b344717b903f761eb7ca4feb Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Mon, 5 Feb 2024 11:35:35 +0700 Subject: [PATCH] checkpoint --- src/controllers/BloodGroupController.ts | 172 ++++++++++++++++++++++++ src/controllers/PositionController.ts | 38 +++--- 2 files changed, 192 insertions(+), 18 deletions(-) create mode 100644 src/controllers/BloodGroupController.ts diff --git a/src/controllers/BloodGroupController.ts b/src/controllers/BloodGroupController.ts new file mode 100644 index 00000000..b2b768b7 --- /dev/null +++ b/src/controllers/BloodGroupController.ts @@ -0,0 +1,172 @@ +import { + Controller, + Post, + Put, + Delete, + Route, + Security, + Tags, + Body, + Path, + Request, + SuccessResponse, + Response, + Get, + } from "tsoa"; + import { AppDataSource } from "../database/data-source"; + import HttpSuccess from "../interfaces/http-success"; + import HttpStatusCode from "../interfaces/http-status"; + import HttpError from "../interfaces/http-error"; + import { CreateBloodGroup, BloodGroup } from "../entities/BloodGroup"; + import { Not } from "typeorm"; + @Route("api/v1/org/bloodGroup") + @Tags("BloodGroup") + @Security("bearerAuth") + @Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", + ) + @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") + export class BloodGroupController extends Controller { + private bloodGroupRepository = AppDataSource.getRepository(BloodGroup); + + /** + * API สร้างกลุ่มเลือด + * + * @summary ORG_062 - สร้างกลุ่มเลือด (ADMIN) #66 + * + */ + @Post() + async createBloodGroup( + @Body() + requestBody: CreateBloodGroup, + @Request() request: { user: Record }, + ) { + const checkName = await this.bloodGroupRepository.findOne({ + where: { name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + const bloodGroup = Object.assign(new BloodGroup(), requestBody); + bloodGroup.createdUserId = request.user.sub; + bloodGroup.createdFullName = request.user.name; + bloodGroup.lastUpdateUserId = request.user.sub; + bloodGroup.lastUpdateFullName = request.user.name; + await this.bloodGroupRepository.save(bloodGroup); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API แก้ไขกลุ่มเลือด + * + * @summary ORG_062 - แก้ไขกลุ่มเลือด (ADMIN) #66 + * + * @param {string} id Id กลุ่มเลือด + */ + @Put("{id}") + async updateBloodGroup( + @Path() id: string, + @Body() + requestBody: CreateBloodGroup, + @Request() request: { user: Record }, + ) { + const bloodGroup = await this.bloodGroupRepository.findOne({ where: { id: id } }); + if (!bloodGroup) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีนี้ : " + id); + } + + const checkName = await this.bloodGroupRepository.findOne({ + where: { id:Not(id),name: requestBody.name }, + }); + + if (checkName) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); + } + + try { + bloodGroup.lastUpdateUserId = request.user.sub; + bloodGroup.lastUpdateFullName = request.user.name; + this.bloodGroupRepository.merge(bloodGroup, requestBody); + await this.bloodGroupRepository.save(bloodGroup); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API ลบกลุ่มเลือด + * + * @summary ORG_062 - ลบกลุ่มเลือด (ADMIN) #66 + * + * @param {string} id Id กลุ่มเลือด + */ + @Delete("{id}") + async deleteBloodGroup(@Path() id: string) { + const delBloodGroup = await this.bloodGroupRepository.findOne({ + where: { id }, + }); + if (!delBloodGroup) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); + } + try { + await this.bloodGroupRepository.delete({ id: id }); + return new HttpSuccess(); + } catch (error) { + return error; + } + } + + /** + * API รายละเอียดรายการกลุ่มเลือด + * + * @summary ORG_062 - รายละเอียดรายการกลุ่มเลือด (ADMIN) #66 + * + * @param {string} id Id กลุ่มเลือด + */ + @Get("{id}") + async detailBloodGroup(@Path() id: string) { + const bloodGroup = await this.bloodGroupRepository.findOne({ + where: { id }, + select: ["id", "name"], + }); + if (!bloodGroup) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + try { + return new HttpSuccess(bloodGroup); + } catch (error) { + return error; + } + } + + /** + * API รายการกลุ่มเลือด + * + * @summary ORG_062 - รายการกลุ่มเลือด (ADMIN) #66 + * + */ + @Get() + async listBloodGroup() { + const bloodGroup = await this.bloodGroupRepository.find({ + select: ["id", "name" ], + }); + + if (!bloodGroup) { + return new HttpSuccess([]); + } + try { + return new HttpSuccess(bloodGroup); + } catch (error) { + return error; + } + } + } + \ No newline at end of file diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index a1e30a1f..7fadb6d6 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -691,29 +691,31 @@ export class PositionController extends Controller { keywordAsInt = ""; } masterId = [...new Set(masterId)]; - const posMaster = await this.posMasterRepository.find({ - where: [ - { - ...checkChildConditions, - ...typeCondition, - }, - { - ...checkChildConditions, - ...typeCondition, - id: In(masterId), - }, - { - ...checkChildConditions, - ...typeCondition, - posMasterNo: Like(`%${keywordAsInt}%`), - }, - ], + + const keywordConditions = [ + { + ...checkChildConditions, + ...typeCondition, + }, + { + ...checkChildConditions, + ...typeCondition, + id: In(masterId), + }, + { + ...checkChildConditions, + ...typeCondition, + posMasterNo: Like(`%${keywordAsInt}%`), + }, + ]; + + const [posMaster, total] = await this.posMasterRepository.findAndCount({ + where: keywordConditions, order: { posMasterOrder: "ASC" }, relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], skip: (body.page - 1) * body.pageSize, take: body.pageSize, }); - const total = posMaster.length; const formattedData = await Promise.all( posMaster.map(async (posMaster) => {