import { AppDataSource } from "../database/data-source"; import { Controller, Get, Post, Put, Delete, Patch, Route, Security, Tags, Path, Body, Request, Example, SuccessResponse, Response, } from "tsoa"; import HttpStatusCode from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; import { CreateOrgChild4, OrgChild4, UpdateOrgChild4 } from "../entities/OrgChild4"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild3 } from "../entities/OrgChild3"; @Route("api/v1/org/child4") @Tags("OrgChild4") @Security("bearerAuth") @Response( HttpStatusCode.INTERNAL_SERVER_ERROR, "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class OrgChild4Controller extends Controller { private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); /** * สร้างโครงสร้างระดับ4 Child4 * * @summary ORG_013 - สร้างโครงสร้างระดับ4 (ADMIN) #13 * */ @Post() @Example([ { orgChild4Name: "string", //ชื่อหน่วยงาน orgChild4ShortName: "string", //อักษรย่อ orgChild4Code: "string", //รหัสหน่วยงาน // orgChild4Order: "number", //ลำดับที่ของหน่วยงาน orgChild4PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgChild4PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgChild4Fax: "string", //หมายเลขโทรสาร // orgChild4IsNormal: "boolean", //สถานะของหน่วยงาน orgChild3Id: "Guid", //id Child1 }, ]) async create( @Body() requestBody: CreateOrgChild4, @Request() request: { user: Record }, ) { try { //BE ใช้ orgChild3Id หา orgChild1Id, orgRootId const child3 = await this.child3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); if (!child3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } const validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"]; if (!validOrgChild4Ranks.includes(requestBody.orgChild4Rank.toUpperCase())) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild4Rank"); } const child4 = Object.assign(new OrgChild4(), requestBody) as OrgChild4; child4.orgChild4Name = requestBody.orgChild4Name; child4.createdUserId = request.user.sub; child4.createdFullName = request.user.name; child4.lastUpdateUserId = request.user.sub; child4.lastUpdateFullName = request.user.name; child4.orgRootId = String(child3?.orgRootId); child4.orgChild1Id = String(child3?.orgChild1Id); child4.orgChild2Id = String(child3?.orgChild2Id); child4.orgRevisionId = String(child3?.orgRevisionId); child4.orgChild3Id = String(child3?.id); await this.child4Repository.save(child4); return new HttpSuccess(); } catch (error) { return error; } } /** * แก้ไขโครงสร้างระดับ4 Child4 * * @summary ORG_014 - แก้ไขโครงสร้างระดับ4 (ADMIN) #14 * * @param {string} id Guid, *Id Child4 */ @Put("{id}") @Example([ { orgChild4Name: "string", //ชื่อหน่วยงาน orgChild4ShortName: "string", //อักษรย่อ orgChild4Code: "string", //รหัสหน่วยงาน // orgChild4Order: "number", //ลำดับที่ของหน่วยงาน orgChild4PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgChild4PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgChild4Fax: "string", //หมายเลขโทรสาร // orgChild4IsNormal: "boolean", //สถานะของหน่วยงาน orgChild3Id: "Guid", //id Child1 }, ]) async update( @Path() id: string, @Body() requestBody: UpdateOrgChild4, @Request() request: { user: Record }, ) { try { const child3IdExits = await this.child3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); if (!child3IdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child3Id"); } const validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"]; if ( requestBody.orgChild4Rank == null || !validOrgChild4Ranks.includes(requestBody.orgChild4Rank.toUpperCase()) ) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank"); } const child4 = await this.child4Repository.findOne({ where: { id } }); if (!child4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } child4.lastUpdateUserId = request.user.sub; child4.lastUpdateFullName = request.user.name; child4.lastUpdatedAt = new Date(); child4.orgRootId = String(child3IdExits?.orgRootId); child4.orgChild1Id = String(child3IdExits?.orgChild1Id); child4.orgChild2Id = String(child3IdExits?.orgChild2Id); child4.orgRevisionId = String(child3IdExits?.orgRevisionId); child4.orgChild3Id = String(child3IdExits?.id); this.child4Repository.merge(child4, requestBody); await this.child4Repository.save(child4); return new HttpSuccess(); } catch (error) { return error; } } /** * ลบโครงสร้างระดับ4 Child4 * * @summary ORG_015 - ลบโครงสร้างระดับ4 (ADMIN) #15 * * @param {string} id Guid, *Id Child4 */ @Delete("{id}") async delete(@Path() id: string) { try { const child4 = await this.child4Repository.findOne({ where: { id } }); if (!child4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } await this.child4Repository.remove(child4); return new HttpSuccess(); } catch (error) { return error; } } }