diff --git a/src/controllers/OrgChild1Controller.ts b/src/controllers/OrgChild1Controller.ts index 2a694012..1496f3a9 100644 --- a/src/controllers/OrgChild1Controller.ts +++ b/src/controllers/OrgChild1Controller.ts @@ -47,15 +47,15 @@ export class OrgChild1Controller { */ @Get("{id}") async GetChild1(@Path() id: string) { + const orgChild1 = await this.child1Repository.findOne({ where: { id } }); + if (!orgChild1) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 1"); + } + const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild1.orgRootId } }); + if (!orgRoot) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); + } try { - const orgChild1 = await this.child1Repository.findOne({ where: { id } }); - if (!orgChild1) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 1"); - } - const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild1.orgRootId } }); - if (!orgRoot) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); - } const getOrgChild1 = { "orgChild1Id" : orgChild1.id, "orgChild1Name" : orgChild1.orgChild1Name, @@ -85,39 +85,39 @@ export class OrgChild1Controller { async save( @Body() requestBody: CreateOrgChild1, @Request() request: { user: Record }, - ) { + ){ + + const rootIdExits = await this.orgRootRepository.findOne({ + where: { id: requestBody.orgRootId }, + }); + if (!rootIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootId"); + } + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: rootIdExits.orgRevisionId } + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } + + const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"]; + if (!validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase())) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild1Rank"); + } + + const chkCode = await this.child1Repository.findOne({ + where: { orgRootId: requestBody.orgRootId, orgChild1Code: requestBody.orgChild1Code }, + }); + if (chkCode != null) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว", + ); + } try { - const rootIdExits = await this.orgRootRepository.findOne({ - where: { id: requestBody.orgRootId }, - }); - if (!rootIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootId"); - } - const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: rootIdExits.orgRevisionId } - }); - if (!revisionIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); - } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); - } - - const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"]; - if (!validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase())) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild1Rank"); - } - - const chkCode = await this.child1Repository.findOne({ - where: { orgRootId: requestBody.orgRootId, orgChild1Code: requestBody.orgChild1Code }, - }); - if (chkCode != null) { - 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; @@ -146,45 +146,45 @@ export class OrgChild1Controller { @Body() requestBody: UpdateOrgChild1, @Request() request: { user: Record }, ) { + const rootIdExits = await this.orgRootRepository.findOne({ + where: { id: requestBody.orgRootId }, + }); + if (!rootIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RootId"); + } + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: rootIdExits.orgRevisionId }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } + + 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."); + } + const chkCode = await this.child1Repository.findOne({ + where: { + id: Not(id), + orgRootId: requestBody.orgRootId, + orgChild1Code: requestBody.orgChild1Code, + }, + }); + if (chkCode != null) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); + } try { - const rootIdExits = await this.orgRootRepository.findOne({ - where: { id: requestBody.orgRootId }, - }); - if (!rootIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RootId"); - } - const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: rootIdExits.orgRevisionId }, - }); - if (!revisionIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); - } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); - } - - 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."); - } - const chkCode = await this.child1Repository.findOne({ - where: { - id: Not(id), - orgRootId: requestBody.orgRootId, - orgChild1Code: requestBody.orgChild1Code, - }, - }); - if (chkCode != null) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); - } child1.lastUpdateUserId = request.user.sub; child1.lastUpdateFullName = request.user.name; child1.lastUpdatedAt = new Date(); @@ -207,29 +207,27 @@ export class OrgChild1Controller { */ @Delete("{id}") async delete(@Path() id: string) { + const child1 = await this.child1Repository.findOne({ where: { id } }); + if (!child1) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); + } + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child1.orgRevisionId }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } + const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } }); + if (exitsChild2) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2", + ); + } try { - const child1 = await this.child1Repository.findOne({ where: { id } }); - if (!child1) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); - } - - const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: child1.orgRevisionId }, - }); - if (!revisionIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); - } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); - } - - const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } }); - if (exitsChild2) { - throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2", - ); - } await this.child1Repository.remove(child1); return new HttpSuccess(); } catch (error) { diff --git a/src/controllers/OrgChild2Controller.ts b/src/controllers/OrgChild2Controller.ts index 37e04c04..8b374c85 100644 --- a/src/controllers/OrgChild2Controller.ts +++ b/src/controllers/OrgChild2Controller.ts @@ -48,15 +48,15 @@ export class OrgChild2Controller extends Controller { */ @Get("{id}") async GetChild2(@Path() id: string) { + const orgChild2 = await this.child2Repository.findOne({ where: { id } }); + if (!orgChild2) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 2"); + } + const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild2.orgRootId } }); + if (!orgRoot) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); + } try { - const orgChild2 = await this.child2Repository.findOne({ where: { id } }); - if (!orgChild2) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 2"); - } - const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild2.orgRootId } }); - if (!orgRoot) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); - } const getOrgChild2 = { "orgChild2Id" : orgChild2.id, "orgChild2Name" : orgChild2.orgChild2Name, @@ -101,30 +101,28 @@ export class OrgChild2Controller extends Controller { requestBody: CreateOrgChild2, @Request() request: { user: Record }, ) { + const child1 = await this.child1Repository.findOne({ + where: { id: requestBody.orgChild1Id }, + }); + if (!child1) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); + } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child1.orgRevisionId }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } + + const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"]; + if (!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank"); + } try { - //BE ใช้ orgChild1Id หา orgChild1Id, orgRootId - const child1 = await this.child1Repository.findOne({ - where: { id: requestBody.orgChild1Id }, - }); - if (!child1) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); - } - - const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: child1.orgRevisionId }, - }); - if (!revisionIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); - } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); - } - - 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; @@ -168,37 +166,36 @@ export class OrgChild2Controller extends Controller { requestBody: UpdateOrgChild2, @Request() request: { user: Record }, ) { + const child1IdExits = await this.child1Repository.findOne({ + where: { id: requestBody.orgChild1Id }, + }); + if (!child1IdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id"); + } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child1IdExits.orgRevisionId }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } + + 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."); + } try { - const child1IdExits = await this.child1Repository.findOne({ - where: { id: requestBody.orgChild1Id }, - }); - if (!child1IdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id"); - } - - const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: child1IdExits.orgRevisionId }, - }); - if (!revisionIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); - } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); - } - - 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(); @@ -222,29 +219,29 @@ export class OrgChild2Controller extends Controller { */ @Delete("{id}") async delete(@Path() id: string) { - try { - const child2 = await this.child2Repository.findOne({ where: { id } }); - if (!child2) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); - } - - const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: child2.orgRevisionId }, - }); - if (!revisionIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); - } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); - } + const child2 = await this.child2Repository.findOne({ where: { id } }); + if (!child2) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); + } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child2.orgRevisionId }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } - const exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } }); - if (exitsChild3) { - throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", - ); - } + const exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } }); + if (exitsChild3) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", + ); + } + try { await this.child2Repository.remove(child2); return new HttpSuccess(); } catch (error) { diff --git a/src/controllers/OrgChild3Controller.ts b/src/controllers/OrgChild3Controller.ts index 4ed237c1..b239a6f6 100644 --- a/src/controllers/OrgChild3Controller.ts +++ b/src/controllers/OrgChild3Controller.ts @@ -46,15 +46,15 @@ export class OrgChild3Controller { */ @Get("{id}") async GetChild3(@Path() id: string) { + const orgChild3 = await this.child3Repository.findOne({ where: { id } }); + if (!orgChild3) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 3"); + } + const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild3.orgRootId } }); + if (!orgRoot) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); + } try { - const orgChild3 = await this.child3Repository.findOne({ where: { id } }); - if (!orgChild3) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 3"); - } - const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild3.orgRootId } }); - if (!orgRoot) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); - } const getOrgChild3 = { "orgChild3Id" : orgChild3.id, "orgChild3Name" : orgChild3.orgChild3Name, @@ -85,30 +85,28 @@ export class OrgChild3Controller { @Body() requestBody: CreateOrgChild3, @Request() request: { user: Record }, ) { + const child2 = await this.child2Repository.findOne({ + where: { id: requestBody.orgChild2Id }, + }); + if (!child2) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); + } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child2.orgRevisionId }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } + + const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"]; + if (!validOrgChild3Ranks.includes(requestBody.orgChild3Rank.toUpperCase())) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild3Rank"); + } try { - //BE ใช้ orgChild2Id หา orgChild1Id, orgRootId - const child2 = await this.child2Repository.findOne({ - where: { id: requestBody.orgChild2Id }, - }); - if (!child2) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); - } - - const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: child2.orgRevisionId }, - }); - if (!revisionIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); - } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); - } - - 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; @@ -139,37 +137,36 @@ export class OrgChild3Controller { @Body() requestBody: UpdateOrgChild3, @Request() request: { user: Record }, ) { + const child2IdExits = await this.child2Repository.findOne({ + where: { id: requestBody.orgChild2Id }, + }); + if (!child2IdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child2Id"); + } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child2IdExits.orgRevisionId }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } + + 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."); + } try { - const child2IdExits = await this.child2Repository.findOne({ - where: { id: requestBody.orgChild2Id }, - }); - if (!child2IdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child2Id"); - } - - const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: child2IdExits.orgRevisionId }, - }); - if (!revisionIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); - } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); - } - - 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."); - } - child3.lastUpdateUserId = request.user.sub; child3.lastUpdateFullName = request.user.name; child3.lastUpdatedAt = new Date(); @@ -194,29 +191,29 @@ export class OrgChild3Controller { */ @Delete("{id}") async delete(@Path() id: string) { - try { - const child3 = await this.child3Repository.findOne({ where: { id } }); - if (!child3) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); - } - - const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: child3.orgRevisionId }, - }); - if (!revisionIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); - } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); - } + const child3 = await this.child3Repository.findOne({ where: { id } }); + if (!child3) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); + } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child3.orgRevisionId }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } - const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } }); - if (exitsChild4) { - throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", - ); - } + const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } }); + if (exitsChild4) { + throw new HttpError( + HttpStatusCode.INTERNAL_SERVER_ERROR, + "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", + ); + } + try { await this.child3Repository.remove(child3); return new HttpSuccess(); } catch (error) { diff --git a/src/controllers/OrgChild4Controller.ts b/src/controllers/OrgChild4Controller.ts index 54910bac..4dba61a5 100644 --- a/src/controllers/OrgChild4Controller.ts +++ b/src/controllers/OrgChild4Controller.ts @@ -47,16 +47,16 @@ export class OrgChild4Controller extends Controller { * @param {string} id Id Child4 */ @Get("{id}") - async GetChild4(@Path() id: string) { - try { - const orgChild4 = await this.child4Repository.findOne({ where: { id } }); - if (!orgChild4) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 4"); - } - const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild4.orgRootId } }); + async GetChild4(@Path() id: string) { + const orgChild4 = await this.child4Repository.findOne({ where: { id } }); + if (!orgChild4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ 4"); + } + const orgRoot = await this.orgRootRepository.findOne({ where: { id: orgChild4.orgRootId } }); if (!orgRoot) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล โครงสร้างระดับ Root"); } + try { const getOrgChild4 = { "orgChild4Id" : orgChild4.id, "orgChild4Name" : orgChild4.orgChild4Name, @@ -101,8 +101,6 @@ export class OrgChild4Controller extends Controller { requestBody: CreateOrgChild4, @Request() request: { user: Record }, ) { - try { - //BE ใช้ orgChild3Id หา orgChild1Id, orgRootId const child3 = await this.child3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); @@ -124,7 +122,7 @@ export class OrgChild4Controller extends Controller { if (!validOrgChild4Ranks.includes(requestBody.orgChild4Rank.toUpperCase())) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild4Rank"); } - + try { const child4 = Object.assign(new OrgChild4(), requestBody) as OrgChild4; child4.orgChild4Name = requestBody.orgChild4Name; child4.createdUserId = request.user.sub; @@ -171,37 +169,36 @@ export class OrgChild4Controller extends Controller { requestBody: UpdateOrgChild4, @Request() request: { user: Record }, ) { + const child3IdExits = await this.child3Repository.findOne({ + where: { id: requestBody.orgChild3Id }, + }); + if (!child3IdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child3Id"); + } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child3IdExits.orgRevisionId }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } + + 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."); + } try { - const child3IdExits = await this.child3Repository.findOne({ - where: { id: requestBody.orgChild3Id }, - }); - if (!child3IdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child3Id"); - } - - const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: child3IdExits.orgRevisionId }, - }); - if (!revisionIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); - } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); - } - - 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(); @@ -227,22 +224,21 @@ export class OrgChild4Controller extends Controller { */ @Delete("{id}") async delete(@Path() id: string) { + const child4 = await this.child4Repository.findOne({ where: { id } }); + if (!child4) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); + } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child4.orgRevisionId }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); + } try { - const child4 = await this.child4Repository.findOne({ where: { id } }); - if (!child4) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); - } - - const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: child4.orgRevisionId }, - }); - if (!revisionIdExits) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); - } - if(revisionIdExits.orgRevisionIsDraft != true && revisionIdExits.orgRevisionIsCurrent != false){ - throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false"); - } - await this.child4Repository.remove(child4); return new HttpSuccess(); } catch (error) {