From 55695783a94809d147d29399dc091baa8ee001bd Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 25 Jan 2024 13:26:39 +0700 Subject: [PATCH] org_001 - 003 --- src/controllers/OrgRootController.ts | 89 ++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 24 deletions(-) diff --git a/src/controllers/OrgRootController.ts b/src/controllers/OrgRootController.ts index 49c0c596..f61b0ae8 100644 --- a/src/controllers/OrgRootController.ts +++ b/src/controllers/OrgRootController.ts @@ -10,22 +10,24 @@ import { Tags, Body, Path, + Request, Example, } from "tsoa"; import { CreateOrgRoot, OrgRoot } from "../entities/OrgRoot"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; -import { OrgChild1 } from "../entities/OrgChild1"; +import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1"; +import HttpError from "../interfaces/http-error"; +import HttpStatusCode from "../interfaces/http-status"; @Route("organization") @Tags("OrgRoot") // @Security("bearerAuth") export class OrgRootController extends Controller { private orgRootRepository = AppDataSource.getRepository(OrgRoot); - private orgChild1Repository = AppDataSource.getRepository(OrgChild1); /** - * สร้างโครงสร้างระดับ Root + * สร้างโครงสร้างระดับ Root * * @summary ORG_001 - สร้างโครงสร้างระดับ Root (ADMIN) #1 * @@ -48,13 +50,25 @@ export class OrgRootController extends Controller { // @Path() id: string, @Body() requestBody: CreateOrgRoot, + @Request() request: { user: Record }, ) { try { const orgRoot = Object.assign(new OrgRoot(), requestBody); if (!orgRoot) { - return `not found data`; + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + const chkOrder = await this.orgRootRepository.findOne({ + where: { orgRootOrder: requestBody.orgRootOrder }, + }); + if (chkOrder != null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว"); + } + const chkCode = await this.orgRootRepository.findOne({ + where: { orgRootCode: requestBody.orgRootCode }, + }); + if (chkCode != null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); } - orgRoot.orgRootName = requestBody.orgRootName; orgRoot.orgRootShortName = requestBody.orgRootShortName; orgRoot.orgRootCode = requestBody.orgRootCode; @@ -71,10 +85,8 @@ export class OrgRootController extends Controller { } } - - /** - * แก้ไขโครงสร้างระดับ Root + * แก้ไขโครงสร้างระดับ Root * * @summary ORG_002 - แก้ไขโครงสร้างระดับ Root (ADMIN) #2 * @@ -101,7 +113,19 @@ export class OrgRootController extends Controller { try { const orgRoot = await this.orgRootRepository.findOne({ where: { id } }); if (!orgRoot) { - return `not found data`; + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + const chkOrder = await this.orgRootRepository.findOne({ + where: { orgRootOrder: requestBody.orgRootOrder }, + }); + if (chkOrder != null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว"); + } + const chkCode = await this.orgRootRepository.findOne({ + where: { orgRootCode: requestBody.orgRootCode }, + }); + if (chkCode != null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); } orgRoot.orgRootName = requestBody.orgRootName; @@ -121,36 +145,53 @@ export class OrgRootController extends Controller { } /** - * ลบโครงสร้างระดับ Root + * ลบโครงสร้างระดับ Root * * @summary ORG_003 - ลบโครงสร้างระดับ Root (ADMIN) #3 * * @param {string} id Guid, *Id root */ @Delete("root/{id}") - async delete( - @Path() id: string, - ) { + async delete(@Path() id: string) { try { const orgRoot = await this.orgRootRepository.findOne({ where: { id } }); - const orgChild1 = await AppDataSource.getRepository(OrgChild1) - .createQueryBuilder("orgChild1") - .where("orgChild1.orgRootId", { id }) - .getOne(); - + const orgChild1 = await await this.orgRootRepository.findOne({ where: { id: id } }); + if (!orgRoot) { - return `not found data`; + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - if(!orgChild1){ - await this.orgRootRepository.remove(orgRoot); - }else{ - return 'ไม่สามารถลบข้อมูลได้' + if (!orgChild1) { + await this.orgRootRepository.remove(orgRoot); + } else { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลได้"); } - + return new HttpSuccess(); } catch (error) { return error; } } + + /** + * รายละเอียดโครงสร้างระดับ Root + * + * @summary ORG_016 - รายละเอียดโครงสร้างระดับ Root (ADMIN) #16 + * + * @param {string} id Guid, *Id root + */ + @Get("root/{id}") + async detail(@Path() id: string) { + try { + const orgRoot = await this.orgRootRepository.findOne({ where: { id } }); + + if (!orgRoot) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + } + + return new HttpSuccess(orgRoot); + } catch (error) { + return error; + } + } }