From 5b5970cda2361e5d2963f5376dbaf1e78523c6b4 Mon Sep 17 00:00:00 2001 From: Bright Date: Mon, 29 Jan 2024 13:02:32 +0700 Subject: [PATCH] validate orgRevisionId (child2, 3, 4) --- src/controllers/OrgChild2Controller.ts | 24 ++++++++++++++++++++++++ src/controllers/OrgChild3Controller.ts | 26 +++++++++++++++++++++++++- src/controllers/OrgChild4Controller.ts | 24 ++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/src/controllers/OrgChild2Controller.ts b/src/controllers/OrgChild2Controller.ts index 3a1f181b..a9676790 100644 --- a/src/controllers/OrgChild2Controller.ts +++ b/src/controllers/OrgChild2Controller.ts @@ -19,6 +19,7 @@ import { import HttpStatusCode from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; +import { OrgRevision } from "../entities/OrgRevision"; import { CreateOrgChild2, OrgChild2, UpdateOrgChild2 } from "../entities/OrgChild2"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild3 } from "../entities/OrgChild3"; @@ -34,6 +35,7 @@ export class OrgChild2Controller extends Controller { private child1Repository = AppDataSource.getRepository(OrgChild1); private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); + private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** * สร้างโครงสร้างระดับ2 Child2 @@ -69,6 +71,13 @@ export class OrgChild2Controller extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child1.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"]; if (!validOrgChild2Ranks.includes(requestBody.orgChild2Rank.toUpperCase())) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild2Rank"); @@ -125,6 +134,13 @@ export class OrgChild2Controller extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child1Id"); } + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child1IdExits.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + const validOrgChild2Ranks = ["OFFICE", "DIVISION", "SECTION"]; if ( requestBody.orgChild2Rank == null || @@ -166,6 +182,14 @@ export class OrgChild2Controller extends Controller { if (!child2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child2.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + const exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } }); if (exitsChild3) { throw new HttpError( diff --git a/src/controllers/OrgChild3Controller.ts b/src/controllers/OrgChild3Controller.ts index 5b9a5bee..46a168e4 100644 --- a/src/controllers/OrgChild3Controller.ts +++ b/src/controllers/OrgChild3Controller.ts @@ -1,4 +1,5 @@ import { AppDataSource } from "../database/data-source"; +import { OrgRevision } from "../entities/OrgRevision"; import { OrgChild2 } from "../entities/OrgChild2"; import { OrgChild3, CreateOrgChild3, UpdateOrgChild3 } from "../entities/OrgChild3"; import { OrgChild4 } from "../entities/OrgChild4"; @@ -32,7 +33,8 @@ export class OrgChild3Controller { private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); - + private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); + /** * API สร้างโครงสร้างระดับ3 * @@ -53,6 +55,13 @@ export class OrgChild3Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child2.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"]; if (!validOrgChild3Ranks.includes(requestBody.orgChild3Rank.toUpperCase())) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgChild3Rank"); @@ -96,6 +105,13 @@ export class OrgChild3Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child2Id"); } + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child2IdExits.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + const validOrgChild3Ranks = ["OFFICE", "DIVISION", "SECTION"]; if ( requestBody.orgChild3Rank == null || @@ -138,6 +154,14 @@ export class OrgChild3Controller { if (!child3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child3.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } }); if (exitsChild4) { throw new HttpError( diff --git a/src/controllers/OrgChild4Controller.ts b/src/controllers/OrgChild4Controller.ts index d276f7ff..8af06bfe 100644 --- a/src/controllers/OrgChild4Controller.ts +++ b/src/controllers/OrgChild4Controller.ts @@ -19,6 +19,7 @@ import { import HttpStatusCode from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; +import { OrgRevision } from "../entities/OrgRevision"; import { CreateOrgChild4, OrgChild4, UpdateOrgChild4 } from "../entities/OrgChild4"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild3 } from "../entities/OrgChild3"; @@ -34,6 +35,7 @@ import { OrgChild3 } from "../entities/OrgChild3"; export class OrgChild4Controller extends Controller { private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); + private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** * สร้างโครงสร้างระดับ4 Child4 @@ -68,6 +70,13 @@ export class OrgChild4Controller extends Controller { if (!child3) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child3.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } const validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"]; if (!validOrgChild4Ranks.includes(requestBody.orgChild4Rank.toUpperCase())) { @@ -128,6 +137,13 @@ export class OrgChild4Controller extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. Child3Id"); } + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child3IdExits.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + const validOrgChild4Ranks = ["OFFICE", "DIVISION", "SECTION"]; if ( requestBody.orgChild4Rank == null || @@ -171,6 +187,14 @@ export class OrgChild4Controller extends Controller { if (!child4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: child4.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } + await this.child4Repository.remove(child4); return new HttpSuccess(); } catch (error) {