From f509e10f8c4558d6e484bbc13273f71c9acacd71 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Fri, 26 Jan 2024 17:09:28 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B8=8A=E0=B8=B7?= =?UTF-8?q?=E0=B9=88=E0=B8=AD=20tag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/OrgChild1Controller.ts | 8 ++ src/controllers/OrgChild2Controller.ts | 104 +++++++++++++++---------- src/controllers/OrgChild3Controller.ts | 8 ++ src/controllers/OrgChild4Controller.ts | 12 ++- src/entities/OrgChild1.ts | 20 ++--- src/entities/OrgChild2.ts | 33 ++++---- src/entities/OrgChild3.ts | 20 ++--- src/entities/OrgChild4.ts | 32 ++++---- tsoa.json | 21 ++++- 9 files changed, 151 insertions(+), 107 deletions(-) diff --git a/src/controllers/OrgChild1Controller.ts b/src/controllers/OrgChild1Controller.ts index b9d0281a..6e31d04f 100644 --- a/src/controllers/OrgChild1Controller.ts +++ b/src/controllers/OrgChild1Controller.ts @@ -159,6 +159,14 @@ export class OrgChild1Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RootId"); } + const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"]; + if ( + requestBody.orgChild1Rank == null || + !validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase()) + ) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank"); + } + const child1 = await this.child1Repository.findOne({ where: { id } }); if (!child1) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); diff --git a/src/controllers/OrgChild2Controller.ts b/src/controllers/OrgChild2Controller.ts index 0c699512..7cac1b45 100644 --- a/src/controllers/OrgChild2Controller.ts +++ b/src/controllers/OrgChild2Controller.ts @@ -13,21 +13,28 @@ import { 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 { CreateOrgChild2, OrgChild2 } from "../entities/OrgChild2"; +import { CreateOrgChild2, OrgChild2, UpdateOrgChild2 } from "../entities/OrgChild2"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild3 } from "../entities/OrgChild3"; @Route("organization") @Tags("OrgChild2") @Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class OrgChild2Controller extends Controller { - private orgChild1Repository = AppDataSource.getRepository(OrgChild1); - private orgChild2Repository = AppDataSource.getRepository(OrgChild2); - private orgChild3Repository = AppDataSource.getRepository(OrgChild3); + private child1Repository = AppDataSource.getRepository(OrgChild1); + private child2Repository = AppDataSource.getRepository(OrgChild2); + private child3Repository = AppDataSource.getRepository(OrgChild3); /** * สร้างโครงสร้างระดับ2 Child2 @@ -35,7 +42,7 @@ export class OrgChild2Controller extends Controller { * @summary ORG_007 - สร้างโครงสร้างระดับ2 (ADMIN) #7 * */ - @Post("child2") + @Post() @Example([ { orgChild2Name: "string", //ชื่อหน่วยงาน @@ -45,7 +52,7 @@ export class OrgChild2Controller extends Controller { orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgChild2Fax: "string", //หมายเลขโทรสาร - orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน + // orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน orgChild1Id: "Guid", //id Child1 }, ]) @@ -55,24 +62,28 @@ export class OrgChild2Controller extends Controller { @Request() request: { user: Record }, ) { try { - const orgChild1 = await this.orgChild1Repository.findOne({ + //BE ใช้ orgChild1Id หา orgChild1Id, orgRootId + const child1 = await this.child1Repository.findOne({ where: { id: requestBody.orgChild1Id }, }); - const orgChild2 = Object.assign(new OrgChild2(), requestBody); - if (!orgChild2) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!child1) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } - if (orgChild1) { - 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"); + const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"]; + if (!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank"); } + const child2 = Object.assign(new OrgChild2(), requestBody) as OrgChild2; + child2.orgChild2Name = requestBody.orgChild2Name; + child2.createdUserId = request.user.sub; + child2.createdFullName = request.user.name; + child2.lastUpdateUserId = request.user.sub; + child2.lastUpdateFullName = request.user.name; + child2.orgRootId = String(child1?.orgRootId); + child2.orgChild1Id = String(child1?.id); + await this.child2Repository.save(child2); return new HttpSuccess(); } catch (error) { return error; @@ -96,33 +107,44 @@ export class OrgChild2Controller extends Controller { orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgChild2Fax: "string", //หมายเลขโทรสาร - orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน + // orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน orgChild1Id: "Guid", //id Child1 }, ]) async update( @Path() id: string, @Body() - requestBody: CreateOrgChild2, + requestBody: UpdateOrgChild2, @Request() request: { user: Record }, ) { try { - const orgChild1 = await this.orgChild1Repository.findOne({ + const child1IdExits = await this.child1Repository.findOne({ where: { id: requestBody.orgChild1Id }, }); - const orgChild2 = await this.orgChild2Repository.findOne({ where: { id } }); - if (!orgChild2) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!child1IdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id"); } - if (orgChild1) { - orgChild2.lastUpdateUserId = request.user.sub; - orgChild2.lastUpdateFullName = request.user.name; - orgChild2.lastUpdatedAt = new Date(); - await this.orgChild2Repository.save(orgChild2); - } else { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีโครงสร้างระดับ1"); + const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"]; + if ( + requestBody.orgChild2Rank == null || + !validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase()) + ) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank"); } + + const child2 = await this.child2Repository.findOne({ where: { id } }); + if (!child2) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); + } + + child2.lastUpdateUserId = request.user.sub; + child2.lastUpdateFullName = request.user.name; + child2.lastUpdatedAt = new Date(); + child2.orgRootId = String(child1IdExits?.orgRootId); + child2.orgChild1Id = String(child1IdExits?.id); + this.child2Repository.merge(child2, requestBody); + await this.child2Repository.save(child2); return new HttpSuccess(); } catch (error) { return error; @@ -139,22 +161,18 @@ export class OrgChild2Controller extends Controller { @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, "ไม่พบข้อมูล"); + const child2 = await this.child2Repository.findOne({ where: { id } }); + if (!child2) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } - - if (!orgChild3) { - await this.orgChild2Repository.remove(orgChild2); - } else { + const exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } }); + if (exitsChild3) { throw new HttpError( - HttpStatusCode.NOT_FOUND, - "ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ3", + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", ); } - + await this.child2Repository.remove(child2); return new HttpSuccess(); } catch (error) { return error; diff --git a/src/controllers/OrgChild3Controller.ts b/src/controllers/OrgChild3Controller.ts index 27715369..97c66655 100644 --- a/src/controllers/OrgChild3Controller.ts +++ b/src/controllers/OrgChild3Controller.ts @@ -95,6 +95,14 @@ export class OrgChild3Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child2Id"); } + const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"]; + if ( + requestBody.orgChild3Rank == null || + !validOrgChild3Ranks.includes(requestBody.orgChild3Rank.toUpperCase()) + ) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank"); + } + const child3 = await this.child3Repository.findOne({ where: { id } }); if (!child3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); diff --git a/src/controllers/OrgChild4Controller.ts b/src/controllers/OrgChild4Controller.ts index b9a01dbe..e3d8848d 100644 --- a/src/controllers/OrgChild4Controller.ts +++ b/src/controllers/OrgChild4Controller.ts @@ -51,7 +51,7 @@ export class OrgChild4Controller extends Controller { orgChild4PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgChild4PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgChild4Fax: "string", //หมายเลขโทรสาร - orgChild4IsNormal: "boolean", //สถานะของหน่วยงาน + // orgChild4IsNormal: "boolean", //สถานะของหน่วยงาน orgChild3Id: "Guid", //id Child1 }, ]) @@ -109,7 +109,7 @@ export class OrgChild4Controller extends Controller { orgChild4PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก orgChild4PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน orgChild4Fax: "string", //หมายเลขโทรสาร - orgChild4IsNormal: "boolean", //สถานะของหน่วยงาน + // orgChild4IsNormal: "boolean", //สถานะของหน่วยงาน orgChild3Id: "Guid", //id Child1 }, ]) @@ -127,6 +127,14 @@ export class OrgChild4Controller extends Controller { 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."); diff --git a/src/entities/OrgChild1.ts b/src/entities/OrgChild1.ts index d5f3c21a..f107d4f8 100644 --- a/src/entities/OrgChild1.ts +++ b/src/entities/OrgChild1.ts @@ -12,7 +12,6 @@ enum OrgChild1Rank { @Entity("orgChild1") export class OrgChild1 extends EntityBase { - @Column({ nullable: true, comment: "ชื่อส่วนราชการ", @@ -75,7 +74,7 @@ export class OrgChild1 extends EntityBase { @Column({ nullable: true, comment: "สถานะของหน่วยงาน", - default: true + default: true, }) orgChild1IsNormal: boolean; @@ -91,17 +90,15 @@ export class OrgChild1 extends EntityBase { }) isAncestorDNA: string; - @ManyToOne(() => OrgRoot, orgRoot => orgRoot.orgChild1s) + @ManyToOne(() => OrgRoot, (orgRoot) => orgRoot.orgChild1s) @JoinColumn({ name: "orgRootId" }) orgRoot: OrgRoot; - @OneToMany(() => OrgChild2, orgChild2 => orgChild2.orgChild1) + @OneToMany(() => OrgChild2, (orgChild2) => orgChild2.orgChild1) orgChild2s: OrgChild2[]; - } export class CreateOrgChild1 { - @Column() orgChild1Name: string; @@ -110,22 +107,21 @@ export class CreateOrgChild1 { @Column() orgChild1Code?: string; - + @Column() orgChild1Rank: string; @Column() - orgChild1PhoneEx: string; + orgChild1PhoneEx?: string; @Column() - orgChild1PhoneIn: string; + orgChild1PhoneIn?: string; @Column() - orgChild1Fax: string; + orgChild1Fax?: string; - @Column('uuid') + @Column("uuid") orgRootId: string; - } export type UpdateOrgChild1 = Partial & { orgChild1Rank?: OrgChild1Rank }; diff --git a/src/entities/OrgChild2.ts b/src/entities/OrgChild2.ts index 77b4ea2c..f2372f7d 100644 --- a/src/entities/OrgChild2.ts +++ b/src/entities/OrgChild2.ts @@ -14,12 +14,12 @@ enum OrgChild2Rank { } @Entity("orgChild2") export class OrgChild2 extends EntityBase { -// @Column({ -// comment: "", -// length: 40, -// default: "00000000-0000-0000-0000-000000000000", -// }) -// orgChild2Id: string; + // @Column({ + // comment: "", + // length: 40, + // default: "00000000-0000-0000-0000-000000000000", + // }) + // orgChild2Id: string; @Column({ nullable: true, @@ -84,7 +84,7 @@ export class OrgChild2 extends EntityBase { @Column({ nullable: true, comment: "สถานะของหน่วยงาน", //ปกติ = 1 , ยุกเลิก = 0 - default: true + default: true, }) orgChild2IsNormal: boolean; @@ -106,17 +106,15 @@ export class OrgChild2 extends EntityBase { }) isAncestorDNA: string; - @ManyToOne(() => OrgChild1, orgChild1 => orgChild1.orgChild2s) + @ManyToOne(() => OrgChild1, (orgChild1) => orgChild1.orgChild2s) @JoinColumn({ name: "orgChild1Id" }) orgChild1: OrgChild1; - @OneToMany(() => OrgChild3, orgChild3 => orgChild3.orgChild2) + @OneToMany(() => OrgChild3, (orgChild3) => orgChild3.orgChild2) orgChild3s: OrgChild3[]; - } export class CreateOrgChild2 { - @Column() orgChild2Name: string; @@ -125,21 +123,20 @@ export class CreateOrgChild2 { @Column() orgChild2Code: string; - + @Column() orgChild2Rank: string; @Column() - orgChild2PhoneEx: string; + orgChild2PhoneEx?: string; @Column() - orgChild2PhoneIn: string; + orgChild2PhoneIn?: string; @Column() - orgChild2Fax: string; + orgChild2Fax?: string; - @Column('uuid') + @Column("uuid") orgChild1Id: string; - } -export type UpdateOrgChild2 = Partial; +export type UpdateOrgChild2 = Partial & { orgChild2Rank?: OrgChild2Rank }; diff --git a/src/entities/OrgChild3.ts b/src/entities/OrgChild3.ts index 54788411..df79f3c3 100644 --- a/src/entities/OrgChild3.ts +++ b/src/entities/OrgChild3.ts @@ -12,7 +12,6 @@ enum OrgChild3Rank { @Entity("orgChild3") export class OrgChild3 extends EntityBase { - @Column({ nullable: true, comment: "ชื่อส่วนราชการ", @@ -75,7 +74,7 @@ export class OrgChild3 extends EntityBase { @Column({ nullable: true, comment: "สถานะของหน่วยงาน", - default: true + default: true, }) orgChild3IsNormal: boolean; @@ -103,17 +102,15 @@ export class OrgChild3 extends EntityBase { }) isAncestorDNA: string; - @ManyToOne(() => OrgChild2, orgChild2 => orgChild2.orgChild3s) + @ManyToOne(() => OrgChild2, (orgChild2) => orgChild2.orgChild3s) @JoinColumn({ name: "orgChild2Id" }) orgChild2: OrgChild2; - @OneToMany(() => OrgChild4, orgChild4 => orgChild4.orgChild3) + @OneToMany(() => OrgChild4, (orgChild4) => orgChild4.orgChild3) orgChild4s: OrgChild4[]; - } export class CreateOrgChild3 { - @Column() orgChild3Name: string; @@ -122,22 +119,21 @@ export class CreateOrgChild3 { @Column() orgChild3Code: string; - + @Column() orgChild3Rank: string; @Column() - orgChild3PhoneEx: string; + orgChild3PhoneEx?: string; @Column() - orgChild3PhoneIn: string; + orgChild3PhoneIn?: string; @Column() - orgChild3Fax: string; + orgChild3Fax?: string; - @Column('uuid') + @Column("uuid") orgChild2Id: string; - } export type UpdateOrgChild3 = Partial & { orgChild3Rank?: OrgChild3Rank }; diff --git a/src/entities/OrgChild4.ts b/src/entities/OrgChild4.ts index da18570a..93630989 100644 --- a/src/entities/OrgChild4.ts +++ b/src/entities/OrgChild4.ts @@ -13,12 +13,12 @@ enum OrgChild4Rank { } @Entity("orgChild4") export class OrgChild4 extends EntityBase { -// @Column({ -// comment: "", -// length: 40, -// default: "00000000-0000-0000-0000-000000000000", -// }) -// orgChild4Id: string; + // @Column({ + // comment: "", + // length: 40, + // default: "00000000-0000-0000-0000-000000000000", + // }) + // orgChild4Id: string; @Column({ nullable: true, @@ -83,7 +83,7 @@ export class OrgChild4 extends EntityBase { @Column({ nullable: true, comment: "สถานะของหน่วยงาน", //ปกติ = 1 , ยุกเลิก = 0 - default: true + default: true, }) orgChild4IsNormal: boolean; @@ -116,14 +116,13 @@ export class OrgChild4 extends EntityBase { default: "00000000-0000-0000-0000-000000000000", }) isAncestorDNA: string; - - @ManyToOne(() => OrgChild3, orgChild3 => orgChild3.orgChild4s) + + @ManyToOne(() => OrgChild3, (orgChild3) => orgChild3.orgChild4s) @JoinColumn({ name: "orgChild3Id" }) orgChild3: OrgChild3; } export class CreateOrgChild4 { - @Column() orgChild4Name: string; @@ -132,21 +131,20 @@ export class CreateOrgChild4 { @Column() orgChild4Code: string; - + @Column() orgChild4Rank: string; @Column() - orgChild4PhoneEx: string; + orgChild4PhoneEx?: string; @Column() - orgChild4PhoneIn: string; + orgChild4PhoneIn?: string; @Column() - orgChild4Fax: string; - - @Column('uuid') + orgChild4Fax?: string; + + @Column("uuid") orgChild3Id: string; - } export type UpdateOrgChild4 = Partial; diff --git a/tsoa.json b/tsoa.json index a0865329..a4629646 100644 --- a/tsoa.json +++ b/tsoa.json @@ -26,9 +26,24 @@ } }, "tags": [ - { - "name": "Test", "description": "ทดสอบ" - } + { + "name": "Organization", "description": "โครงสร้างส่วนอื่น ๆ" + }, + { + "name": "OrgRoot", "description": "โครงสร้างระดับ root" + }, + { + "name": "OrgChild1", "description": "โครงสร้างระดับ 1" + }, + { + "name": "OrgChild2", "description": "โครงสร้างระดับ 2" + }, + { + "name": "OrgChild3", "description": "โครงสร้างระดับ 3" + }, + { + "name": "OrgChild4", "description": "โครงสร้างระดับ 4" + } ] }, "routes": {