diff --git a/src/controllers/OrgChild1Controller.ts b/src/controllers/OrgChild1Controller.ts index df5d62e3..384fef28 100644 --- a/src/controllers/OrgChild1Controller.ts +++ b/src/controllers/OrgChild1Controller.ts @@ -82,7 +82,7 @@ export class OrgChild1Controller { const chkCode = await this.child1Repository.findOne({ where: { id: id, orgChild1Code: requestBody.orgChild1Code }, }); - if (chkCode != null) { + if (chkCode?.id != id && chkCode != null) { throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); } const child1 = await this.child1Repository.findOne({ where: { id } }); diff --git a/src/controllers/OrgChild2Controller.ts b/src/controllers/OrgChild2Controller.ts index 549b691b..1c0b3622 100644 --- a/src/controllers/OrgChild2Controller.ts +++ b/src/controllers/OrgChild2Controller.ts @@ -157,7 +157,7 @@ export class OrgChild2Controller extends Controller { async delete(@Path() id: string) { try { const orgChild2 = await this.orgChild2Repository.findOne({ where: { id } }); - const orgChild3 = await this.orgChild3Repository.findOne({ where: { id: id } }); + const orgChild3 = await this.orgChild3Repository.findOne({ where: { orgChild2Id: id } }); if (!orgChild2) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); diff --git a/src/controllers/OrgChild4Controller.ts b/src/controllers/OrgChild4Controller.ts index df97b3a5..ae173f6f 100644 --- a/src/controllers/OrgChild4Controller.ts +++ b/src/controllers/OrgChild4Controller.ts @@ -54,7 +54,6 @@ export class OrgChild4Controller extends Controller { @Request() request: { user: Record }, ) { try { - const orgChild3 = await this.orgChild3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); @@ -159,17 +158,12 @@ export class OrgChild4Controller extends Controller { async delete(@Path() id: string) { try { const orgChild4 = await this.orgChild4Repository.findOne({ where: { id } }); - const orgChild3 = await this.orgChild3Repository.findOne({ where: { id: id } }); if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - - if (!orgChild3) { - await this.orgChild4Repository.remove(orgChild4); - } else { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่สามารถลบข้อมูลได้"); - } + + await this.orgChild4Repository.remove(orgChild4); return new HttpSuccess(); } catch (error) { diff --git a/src/controllers/OrgRootController.ts b/src/controllers/OrgRootController.ts index 23ae19d8..decdec47 100644 --- a/src/controllers/OrgRootController.ts +++ b/src/controllers/OrgRootController.ts @@ -25,6 +25,7 @@ import HttpStatusCode from "../interfaces/http-status"; @Security("bearerAuth") export class OrgRootController extends Controller { private orgRootRepository = AppDataSource.getRepository(OrgRoot); + private orgChild1Repository = AppDataSource.getRepository(OrgChild1); /** * สร้างโครงสร้างระดับ Root @@ -118,7 +119,7 @@ export class OrgRootController extends Controller { const chkCode = await this.orgRootRepository.findOne({ where: { orgRootCode: requestBody.orgRootCode }, }); - if (chkCode != null) { + if (chkCode?.id != id && chkCode != null ) { throw new HttpError(HttpStatusCode.NOT_FOUND, "รหัสหน่วยงานนี้มีอยู่ในระบบแล้ว"); } @@ -151,7 +152,7 @@ export class OrgRootController extends Controller { async delete(@Path() id: string) { try { const orgRoot = await this.orgRootRepository.findOne({ where: { id } }); - const orgChild1 = await this.orgRootRepository.findOne({ where: { id: id } }); + const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } }); if (!orgRoot) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");