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 { CreateOrgChild4, OrgChild4 } from "../entities/OrgChild4"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild3 } from "../entities/OrgChild3"; @Route("organization") @Tags("OrgChild4") // @Security("bearerAuth") export class OrgChild4Controller extends Controller { private orgChild3Repository = AppDataSource.getRepository(OrgChild3); private orgChild4Repository = AppDataSource.getRepository(OrgChild4); /** * สร้างโครงสร้างระดับ4 Child4 * * @summary ORG_013 - สร้างโครงสร้างระดับ4 (ADMIN) #13 * */ @Post("child4") @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 { console.log("child3Id:"+requestBody.orgChild3Id); const orgChild3 = await this.orgChild3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); const orgChild4 = Object.assign(new OrgChild4(), requestBody); if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const chkOrder = await this.orgChild4Repository.findOne({ where: { orgChild3Id: requestBody.orgChild3Id, orgChild4Order: requestBody.orgChild4Order }, }); if (chkOrder != null) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว"); } const chkCode = await this.orgChild4Repository.findOne({ where: { orgChild3Id: requestBody.orgChild3Id, orgChild4Code: requestBody.orgChild4Code }, }); if (chkCode != null) { throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); } if (orgChild3) { orgChild4.orgChild4Name = requestBody.orgChild4Name; orgChild4.orgChild4ShortName = requestBody.orgChild4ShortName; orgChild4.orgChild4Code = requestBody.orgChild4Code; orgChild4.orgChild4Order = requestBody.orgChild4Order; orgChild4.orgChild4PhoneEx = requestBody.orgChild4PhoneEx; orgChild4.orgChild4PhoneIn = requestBody.orgChild4PhoneIn; orgChild4.orgChild4Fax = requestBody.orgChild4Fax; orgChild4.orgChild4IsNormal = requestBody.orgChild4IsNormal; orgChild4.orgRootId = orgChild3.orgRootId; orgChild4.orgChild1Id = orgChild3.orgChild1Id; orgChild4.orgChild2Id = orgChild3.orgChild2Id; orgChild4.orgChild3Id = orgChild3.id; // orgChild4.createdUserId = request.user.sub; // orgChild4.createdFullName = request.user.name; // orgChild4.lastUpdateUserId = request.user.sub; // orgChild4.lastUpdateFullName = request.user.name; await this.orgChild4Repository.save(orgChild4); } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีโครงสร้างระดับ3"); } return new HttpSuccess(); } catch (error) { return error; } } /** * แก้ไขโครงสร้างระดับ4 Child4 * * @summary ORG_014 - แก้ไขโครงสร้างระดับ4 (ADMIN) #14 * * @param {string} id Guid, *Id Child4 */ @Put("child4/{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: CreateOrgChild4, @Request() request: { user: Record }, ) { try { const orgChild3 = await this.orgChild3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); const orgChild4 = await this.orgChild4Repository.findOne({ where: { id } }); if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } const chkOrder = await this.orgChild4Repository.findOne({ where: { orgChild4Order: requestBody.orgChild4Order }, }); if (chkOrder != null) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ลำดับที่ของหน่วยงานนี้มีอยู่ในระบบแล้ว"); } const chkCode = await this.orgChild4Repository.findOne({ where: { orgChild4Code: requestBody.orgChild4Code }, }); if (chkCode != null) { throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); } if (orgChild3) { orgChild4.orgChild4Name = requestBody.orgChild4Name; orgChild4.orgChild4ShortName = requestBody.orgChild4ShortName; orgChild4.orgChild4Code = requestBody.orgChild4Code; orgChild4.orgChild4Order = requestBody.orgChild4Order; orgChild4.orgChild4PhoneEx = requestBody.orgChild4PhoneEx; orgChild4.orgChild4PhoneIn = requestBody.orgChild4PhoneIn; orgChild4.orgChild4Fax = requestBody.orgChild4Fax; orgChild4.orgChild4IsNormal = requestBody.orgChild4IsNormal; orgChild4.lastUpdateUserId = request.user.sub; orgChild4.lastUpdateFullName = request.user.name; await this.orgChild4Repository.save(orgChild4); } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีโครงสร้างระดับ3"); } return new HttpSuccess(); } catch (error) { return error; } } /** * ลบโครงสร้างระดับ4 Child4 * * @summary ORG_015 - ลบโครงสร้างระดับ4 (ADMIN) #15 * * @param {string} id Guid, *Id Child4 */ @Delete("child4/{id}") async delete(@Path() id: string) { try { const orgChild4 = await this.orgChild4Repository.findOne({ where: { id } }); const orgChild3 = await this.orgChild3Repository.findOne({ where: { id: id } }); if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } if (!orgChild3) { await this.orgChild4Repository.remove(orgChild4); } else { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลได้"); } return new HttpSuccess(); } catch (error) { return error; } } }