diff --git a/src/controllers/OrgChild1Controller.ts b/src/controllers/OrgChild1Controller.ts index bd451d61..6a97aeb6 100644 --- a/src/controllers/OrgChild1Controller.ts +++ b/src/controllers/OrgChild1Controller.ts @@ -1,4 +1,5 @@ import { AppDataSource } from "../database/data-source"; +import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; import { OrgChild1, CreateOrgChild1, UpdateOrgChild1 } from "../entities/OrgChild1"; import { OrgChild2 } from "../entities/OrgChild2"; @@ -35,6 +36,8 @@ export class OrgChild1Controller { private child1Repository = AppDataSource.getRepository(OrgChild1); private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); + private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); + /** * API รายละเอียดโครงสร้างระดับ1 @@ -108,6 +111,12 @@ export class OrgChild1Controller { if (!rootIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. orgRootId"); } + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: rootIdExits.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"]; if (!validOrgChild1Ranks.includes(requestBody.orgChild1Rank.toUpperCase())) { @@ -159,6 +168,12 @@ export class OrgChild1Controller { if (!rootIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RootId"); } + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: rootIdExits.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); + } const validOrgChild1Ranks = ["OFFICE", "DIVISION", "SECTION"]; if ( @@ -209,6 +224,14 @@ export class OrgChild1Controller { if (!child1) { 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 exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } }); if (exitsChild2) { throw new HttpError( diff --git a/src/controllers/OrgRootController.ts b/src/controllers/OrgRootController.ts index 59f86834..19687488 100644 --- a/src/controllers/OrgRootController.ts +++ b/src/controllers/OrgRootController.ts @@ -72,7 +72,7 @@ export class OrgRootController extends Controller { } const orgRevision = await this.orgRevisionRepository.findOne({ - where: { id: requestBody.orgRevisionId }, + where: { id: requestBody.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, }); if (orgRevision) { @@ -82,7 +82,8 @@ export class OrgRootController extends Controller { orgRoot.lastUpdateFullName = request.user.name; await this.orgRootRepository.save(orgRoot); } else { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี Revision"); + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี Revision"); + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } return new HttpSuccess(); @@ -123,7 +124,7 @@ export class OrgRootController extends Controller { } const revisionIdExits = await this.orgRevisionRepository.findOne({ - where: { id: requestBody.orgRevisionId }, + where: { id: requestBody.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, }); if (!revisionIdExits) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); @@ -164,21 +165,23 @@ export class OrgRootController extends Controller { async delete(@Path() id: string) { try { const orgRoot = await this.orgRootRepository.findOne({ where: { id } }); - const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } }); - if (!orgRoot) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - if (!orgChild1) { - await this.orgRootRepository.remove(orgRoot); - } else { - throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1", - ); + const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } }); + if(orgChild1 != null){ + throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR,"ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1",); + } + + const revisionIdExits = await this.orgRevisionRepository.findOne({ + where: { id: orgRoot.orgRevisionId, orgRevisionIsDraft: true, orgRevisionIsCurrent: false }, + }); + if (!revisionIdExits) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. RevisionId"); } + await this.orgRootRepository.remove(orgRoot); return new HttpSuccess(); } catch (error) { return error;