From c7c744dd21da26105248e5fcf0882079993f0435 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 6 Feb 2024 14:03:45 +0700 Subject: [PATCH] fix delete function Root --- src/controllers/OrgRootController.ts | 40 ++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/controllers/OrgRootController.ts b/src/controllers/OrgRootController.ts index 0b2c2f9d..2dda6f63 100644 --- a/src/controllers/OrgRootController.ts +++ b/src/controllers/OrgRootController.ts @@ -17,9 +17,15 @@ import { CreateOrgRoot, OrgRoot } from "../entities/OrgRoot"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1"; +import { In, IsNull, Not } from "typeorm"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { OrgRevision } from "../entities/OrgRevision"; +import { OrgChild2 } from "../entities/OrgChild2"; +import { OrgChild3 } from "../entities/OrgChild3"; +import { OrgChild4 } from "../entities/OrgChild4"; +import { PosMaster } from "../entities/PosMaster"; +import { Position } from "../entities/Position"; @Route("api/v1/org/root") @Tags("OrgRoot") @@ -27,7 +33,12 @@ import { OrgRevision } from "../entities/OrgRevision"; export class OrgRootController extends Controller { private orgRootRepository = AppDataSource.getRepository(OrgRoot); private orgChild1Repository = AppDataSource.getRepository(OrgChild1); + private orgChild2Repository = AppDataSource.getRepository(OrgChild2); + private orgChild3Repository = AppDataSource.getRepository(OrgChild3); + private orgChild4Repository = AppDataSource.getRepository(OrgChild4); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); + private posMasterRepository = AppDataSource.getRepository(PosMaster); + private positionRepository = AppDataSource.getRepository(Position); /** * API รายละเอียดโครงสร้างระดับ Root @@ -220,13 +231,13 @@ export class OrgRootController extends Controller { ); } - const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } }); - if (orgChild1 != null) { - 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 }, @@ -244,7 +255,20 @@ export class OrgRootController extends Controller { ); } try { - await this.orgRootRepository.remove(orgRoot); + const posMasters = await this.posMasterRepository.find({ + where: { orgRootId: id} + }); + const positions = await this.positionRepository.find({ + where: [{ posMasterId: In(posMasters.map((x) => x.id)) }], + }); + + await this.positionRepository.remove(positions); + await this.posMasterRepository.remove(posMasters); + await this.orgChild4Repository.delete({ orgRootId: id }); + await this.orgChild3Repository.delete({ orgRootId: id }); + await this.orgChild2Repository.delete({ orgRootId: id }); + await this.orgChild1Repository.delete({ orgRootId: id }); + await this.orgRootRepository.delete({id}); return new HttpSuccess(); } catch (error) {