From f4c54936f53c5c996cb7fafe78406d42638bab96 Mon Sep 17 00:00:00 2001 From: Kittapath Date: Fri, 26 Jan 2024 16:41:52 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89child1=203=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/OrgChild1Controller.ts | 36 +++++--- src/controllers/OrgChild3Controller.ts | 29 ++++-- src/controllers/OrgChild4Controller.ts | 106 +++++++++++----------- src/controllers/OrganizationController.ts | 51 ++++++----- 4 files changed, 128 insertions(+), 94 deletions(-) diff --git a/src/controllers/OrgChild1Controller.ts b/src/controllers/OrgChild1Controller.ts index bc47b23b..b9d0281a 100644 --- a/src/controllers/OrgChild1Controller.ts +++ b/src/controllers/OrgChild1Controller.ts @@ -21,7 +21,8 @@ import { import HttpStatusCode from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; -@Route("api/v1/organization/child1") +import { Not } from "typeorm"; +@Route("api/v1/org/child1") @Tags("OrgChild1") @Security("bearerAuth") @Response( @@ -30,7 +31,7 @@ import HttpError from "../interfaces/http-error"; ) @SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class OrgChild1Controller { - private orgRootRepository = AppDataSource.getRepository(OrgRoot) + private orgRootRepository = AppDataSource.getRepository(OrgRoot); private child1Repository = AppDataSource.getRepository(OrgChild1); private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); @@ -101,12 +102,14 @@ export class OrgChild1Controller { @Request() request: { user: Record }, ) { try { - const rootIdExits = await this.orgRootRepository.findOne({ where: {id:requestBody.orgRootId} }); - if(!rootIdExits){ + const rootIdExits = await this.orgRootRepository.findOne({ + where: { id: requestBody.orgRootId }, + }); + if (!rootIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootId"); } - const validOrgChild1Ranks = ["DEPARTMENT", "OFFICE", "DIVISION", "SECTION"]; + const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"]; if (!validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase())) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild1Rank"); } @@ -115,15 +118,19 @@ export class OrgChild1Controller { where: { orgRootId: requestBody.orgRootId, orgChild1Code: requestBody.orgChild1Code }, }); if (chkCode != null) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว", + ); } - + const child1 = Object.assign(new OrgChild1(), requestBody) as OrgChild1; child1.orgChild1Name = requestBody.orgChild1Name; child1.createdUserId = request.user.sub; child1.createdFullName = request.user.name; child1.lastUpdateUserId = request.user.sub; child1.lastUpdateFullName = request.user.name; + child1.orgRootId = String(rootIdExits?.id); await this.child1Repository.save(child1); return new HttpSuccess(); } catch (error) { @@ -145,17 +152,23 @@ export class OrgChild1Controller { @Request() request: { user: Record }, ) { try { - const rootIdExits = await this.orgRootRepository.findOne({ where: {id:requestBody.orgRootId} }); - if(!rootIdExits){ + const rootIdExits = await this.orgRootRepository.findOne({ + where: { id: requestBody.orgRootId }, + }); + if (!rootIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RootId"); } - + const child1 = await this.child1Repository.findOne({ where: { id } }); if (!child1) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } const chkCode = await this.child1Repository.findOne({ - where: { id: id, orgRootId: requestBody.orgRootId, orgChild1Code: requestBody.orgChild1Code }, + where: { + id: Not(id), + orgRootId: requestBody.orgRootId, + orgChild1Code: requestBody.orgChild1Code, + }, }); if (chkCode != null) { throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); @@ -163,6 +176,7 @@ export class OrgChild1Controller { child1.lastUpdateUserId = request.user.sub; child1.lastUpdateFullName = request.user.name; child1.lastUpdatedAt = new Date(); + child1.orgRootId = String(rootIdExits?.id); this.child1Repository.merge(child1, requestBody); await this.child1Repository.save(child1); return new HttpSuccess(); diff --git a/src/controllers/OrgChild3Controller.ts b/src/controllers/OrgChild3Controller.ts index 4d1ac8b4..27715369 100644 --- a/src/controllers/OrgChild3Controller.ts +++ b/src/controllers/OrgChild3Controller.ts @@ -20,7 +20,7 @@ import { import HttpStatusCode from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; -@Route("api/v1/organization/child3") +@Route("api/v1/org/child3") @Tags("OrgChild3") @Security("bearerAuth") @Response( @@ -52,17 +52,22 @@ export class OrgChild3Controller { if (!child2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } + + const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"]; + if (!validOrgChild3Ranks.includes(requestBody.orgChild3Rank.toUpperCase())) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild3Rank"); + } + const child3 = Object.assign(new OrgChild3(), requestBody) as OrgChild3; child3.orgChild3Name = requestBody.orgChild3Name; child3.createdUserId = request.user.sub; child3.createdFullName = request.user.name; - child3.createdAt = new Date(); child3.lastUpdateUserId = request.user.sub; child3.lastUpdateFullName = request.user.name; - child3.lastUpdatedAt = new Date(); - (child3.orgRootId = String(child2?.orgRootId)), - (child3.orgChild1Id = String(child2?.orgChild1Id)), - await this.child3Repository.save(child3); + child3.orgRootId = String(child2?.orgRootId); + child3.orgChild1Id = String(child2?.orgChild1Id); + child3.orgChild2Id = String(child2?.id); + await this.child3Repository.save(child3); return new HttpSuccess(); } catch (error) { return error; @@ -83,6 +88,13 @@ export class OrgChild3Controller { @Request() request: { user: Record }, ) { try { + const child2IdExits = await this.child2Repository.findOne({ + where: { id: requestBody.orgChild2Id }, + }); + if (!child2IdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child2Id"); + } + const child3 = await this.child3Repository.findOne({ where: { id } }); if (!child3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); @@ -91,6 +103,9 @@ export class OrgChild3Controller { child3.lastUpdateUserId = request.user.sub; child3.lastUpdateFullName = request.user.name; child3.lastUpdatedAt = new Date(); + child3.orgRootId = String(child2IdExits?.orgRootId); + child3.orgChild1Id = String(child2IdExits?.orgChild1Id); + child3.orgChild2Id = String(child2IdExits?.id); this.child3Repository.merge(child3, requestBody); await this.child3Repository.save(child3); return new HttpSuccess(); @@ -116,7 +131,7 @@ export class OrgChild3Controller { const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } }); if (exitsChild4) { throw new HttpError( - HttpStatusCode.NOT_FOUND, + HttpStatusCode.INTERNAL_SERVER_ERROR, "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", ); } diff --git a/src/controllers/OrgChild4Controller.ts b/src/controllers/OrgChild4Controller.ts index 43a49ccd..b9a01dbe 100644 --- a/src/controllers/OrgChild4Controller.ts +++ b/src/controllers/OrgChild4Controller.ts @@ -13,20 +13,27 @@ 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 { CreateOrgChild4, OrgChild4 } from "../entities/OrgChild4"; +import { CreateOrgChild4, OrgChild4, UpdateOrgChild4 } from "../entities/OrgChild4"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild3 } from "../entities/OrgChild3"; -@Route("organization") +@Route("api/v1/org/child4") @Tags("OrgChild4") -// @Security("bearerAuth") +@Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class OrgChild4Controller extends Controller { - private orgChild3Repository = AppDataSource.getRepository(OrgChild3); - private orgChild4Repository = AppDataSource.getRepository(OrgChild4); + private child3Repository = AppDataSource.getRepository(OrgChild3); + private child4Repository = AppDataSource.getRepository(OrgChild4); /** * สร้างโครงสร้างระดับ4 Child4 @@ -54,36 +61,31 @@ export class OrgChild4Controller extends Controller { @Request() request: { user: Record }, ) { try { - const orgChild3 = await this.orgChild3Repository.findOne({ + //BE ใช้ orgChild3Id หา orgChild1Id, orgRootId + const child3 = await this.child3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); - const orgChild4 = Object.assign(new OrgChild4(), requestBody); - if (!orgChild4) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!child3) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "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"); + 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.orgChild3Id = String(child3?.id); + await this.child4Repository.save(child4); + return new HttpSuccess(); } catch (error) { return error; @@ -114,33 +116,30 @@ export class OrgChild4Controller extends Controller { async update( @Path() id: string, @Body() - requestBody: CreateOrgChild4, + requestBody: UpdateOrgChild4, @Request() request: { user: Record }, ) { try { - const orgChild3 = await this.orgChild3Repository.findOne({ + const child3IdExits = await this.child3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); - const orgChild4 = await this.orgChild4Repository.findOne({ where: { id } }); - if (!orgChild4) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + if (!child3IdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child3Id"); } - 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"); + 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.orgChild3Id = String(child3IdExits?.id); + this.child4Repository.merge(child4, requestBody); + await this.child4Repository.save(child4); return new HttpSuccess(); } catch (error) { return error; @@ -157,14 +156,11 @@ export class OrgChild4Controller extends Controller { @Delete("child4/{id}") async delete(@Path() id: string) { try { - const orgChild4 = await this.orgChild4Repository.findOne({ where: { id } }); - - if (!orgChild4) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + const child4 = await this.child4Repository.findOne({ where: { id } }); + if (!child4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } - - await this.orgChild4Repository.remove(orgChild4); - + await this.child4Repository.remove(child4); return new HttpSuccess(); } catch (error) { return error; diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index a07aa36c..3ad5edfc 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -12,6 +12,8 @@ import { Path, Request, Example, + SuccessResponse, + Response, } from "tsoa"; import { OrgRevision } from "../entities/OrgRevision"; import { AppDataSource } from "../database/data-source"; @@ -20,11 +22,15 @@ import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; -@Route("organization") +@Route("api/v1/org") @Tags("Organization") @Security("bearerAuth") +@Response( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", +) +@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") export class OrganizationController extends Controller { - private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** @@ -37,10 +43,17 @@ export class OrganizationController extends Controller { async GetHistory() { try { const orgRevision = await this.orgRevisionRepository.find({ - select: ["id", "orgRevisionName", "orgRevisionIsCurrent", "orgRevisionCreatedAt", "orgRevisionIsDraft"], + select: [ + "id", + "orgRevisionName", + "orgRevisionIsCurrent", + "orgRevisionCreatedAt", + "orgRevisionIsDraft", + ], + order: { orgRevisionCreatedAt: "DESC" }, }); if (!orgRevision) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); + return new HttpSuccess([]); } const mapOrgRevisions = orgRevision.map((revision) => ({ orgRevisionId: revision.id, @@ -49,7 +62,7 @@ export class OrganizationController extends Controller { orgRevisionCreatedAt: revision.orgRevisionCreatedAt, orgRevisionIsDraft: revision.orgRevisionIsDraft, })); - + return new HttpSuccess(mapOrgRevisions); } catch (error) { return error; @@ -66,19 +79,16 @@ export class OrganizationController extends Controller { async GetActive() { try { const orgRevisionActive = await this.orgRevisionRepository.findOne({ - where: { orgRevisionIsCurrent : true, orgRevisionIsDraft : false } + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, }); const orgRevisionDraf = await this.orgRevisionRepository.findOne({ - where: { orgRevisionIsCurrent : false, orgRevisionIsDraft : true } + where: { orgRevisionIsCurrent: false, orgRevisionIsDraft: true }, }); - if (!orgRevisionActive || !orgRevisionDraf) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); - } const mapData = { - activeId: orgRevisionActive.id, - activeName: orgRevisionActive.orgRevisionName, - draftId: orgRevisionDraf.id, - draftName: orgRevisionDraf.orgRevisionName + activeId: orgRevisionActive == null ? null : orgRevisionActive.id, + activeName: orgRevisionActive == null ? null : orgRevisionActive.orgRevisionName, + draftId: orgRevisionDraf == null ? null : orgRevisionDraf.id, + draftName: orgRevisionDraf == null ? null : orgRevisionDraf.orgRevisionName, }; return new HttpSuccess(mapData); } catch (error) { @@ -96,12 +106,11 @@ export class OrganizationController extends Controller { async CreateOrgRevision( @Body() requestBody: CreateOrgChild1, @Request() request: { user: Record }, - ){ - try { - return new HttpSuccess(); - } catch (error) { - return error; - } + ) { + try { + return new HttpSuccess(); + } catch (error) { + return error; } - + } }