import { AppDataSource } from "../database/data-source"; import { Controller, Get, Post, Put, Delete, Patch, Route, Security, Tags, Path, Body, Request, Example, } from "tsoa"; import HttpStatusCode from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import { CreateOrgChild2, OrgChild2 } from "../entities/OrgChild2"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild3 } from "../entities/OrgChild3"; @Route("organization") @Tags("OrgChild2") @Security("bearerAuth") export class OrgChild2Controller extends Controller { private orgChild1Repository = AppDataSource.getRepository(OrgChild1); private orgChild2Repository = AppDataSource.getRepository(OrgChild2); private orgChild3Repository = AppDataSource.getRepository(OrgChild3); /** * สร้างโครงสร้างระดับ2 Child2 * * @summary ORG_007 - สร้างโครงสร้างระดับ2 (ADMIN) #7 * */ @Post("child2") @Example([ { orgChild2Name: "string", //ชื่อหน่วยงาน orgChild2ShortName: "string", //อักษรย่อ orgChild2Code: "string", //รหัสหน่วยงาน // orgChild2Order: "number", //ลำดับที่ของหน่วยงาน orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgChild2Fax: "string", //หมายเลขโทรสาร orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน orgChild1Id: "Guid", //id Child1 }, ]) async create( @Body() requestBody: CreateOrgChild2, @Request() request: { user: Record }, ) { try { const orgChild1 = await this.orgChild1Repository.findOne({ where: { id: requestBody.orgChild1Id }, }); const orgChild2 = Object.assign(new OrgChild2(), requestBody); if (!orgChild2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } if (orgChild1) { orgChild2.orgChild2Name = requestBody.orgChild2Name; orgChild2.orgChild2ShortName = requestBody.orgChild2ShortName; orgChild2.orgChild2Code = requestBody.orgChild2Code; // orgChild2.orgChild2Order = requestBody.orgChild2Order; orgChild2.orgChild2PhoneEx = requestBody.orgChild2PhoneEx; orgChild2.orgChild2PhoneIn = requestBody.orgChild2PhoneIn; orgChild2.orgChild2Fax = requestBody.orgChild2Fax; orgChild2.orgChild2IsNormal = requestBody.orgChild2IsNormal; orgChild2.orgRootId = orgChild1.orgRootId; orgChild2.orgChild1Id = orgChild1.id; orgChild2.createdUserId = request.user.sub; orgChild2.createdFullName = request.user.name; orgChild2.lastUpdateUserId = request.user.sub; orgChild2.lastUpdateFullName = request.user.name; await this.orgChild2Repository.save(orgChild2); } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีโครงสร้างระดับ1"); } return new HttpSuccess(); } catch (error) { return error; } } /** * แก้ไขโครงสร้างระดับ2 Child2 * * @summary ORG_008 - แก้ไขโครงสร้างระดับ2 (ADMIN) #8 * * @param {string} id Guid, *Id Child2 */ @Put("child2/{id}") @Example([ { orgChild2Name: "string", //ชื่อหน่วยงาน orgChild2ShortName: "string", //อักษรย่อ orgChild2Code: "string", //รหัสหน่วยงาน // orgChild2Order: "number", //ลำดับที่ของหน่วยงาน orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgChild2Fax: "string", //หมายเลขโทรสาร orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน orgChild1Id: "Guid", //id Child1 }, ]) async update( @Path() id: string, @Body() requestBody: CreateOrgChild2, @Request() request: { user: Record }, ) { try { const orgChild1 = await this.orgChild1Repository.findOne({ where: { id: requestBody.orgChild1Id }, }); const orgChild2 = await this.orgChild2Repository.findOne({ where: { id } }); if (!orgChild2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } if (orgChild1) { orgChild2.orgChild2Name = requestBody.orgChild2Name; orgChild2.orgChild2ShortName = requestBody.orgChild2ShortName; orgChild2.orgChild2Code = requestBody.orgChild2Code; // orgChild2.orgChild2Order = requestBody.orgChild2Order; orgChild2.orgChild2PhoneEx = requestBody.orgChild2PhoneEx; orgChild2.orgChild2PhoneIn = requestBody.orgChild2PhoneIn; orgChild2.orgChild2Fax = requestBody.orgChild2Fax; orgChild2.orgChild2IsNormal = requestBody.orgChild2IsNormal; orgChild2.lastUpdateUserId = request.user.sub; orgChild2.lastUpdateFullName = request.user.name; await this.orgChild2Repository.save(orgChild2); } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีโครงสร้างระดับ1"); } return new HttpSuccess(); } catch (error) { return error; } } /** * ลบโครงสร้างระดับ Child2 * * @summary ORG_009 - ลบโครงสร้างระดับ2 (ADMIN) #9 * * @param {string} id Guid, *Id Child2 */ @Delete("Child2/{id}") async delete(@Path() id: string) { try { const orgChild2 = await this.orgChild2Repository.findOne({ where: { id } }); const orgChild3 = await this.orgChild3Repository.findOne({ where: { orgChild2Id: id } }); if (!orgChild2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } if (!orgChild3) { await this.orgChild2Repository.remove(orgChild2); } else { throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ3", ); } return new HttpSuccess(); } catch (error) { return error; } } }