From 595ff527a7d10d50973cf3021c115e69c2f4f926 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Tue, 6 Feb 2024 15:18:20 +0700 Subject: [PATCH] fix delete function --- src/controllers/OrgChild1Controller.ts | 36 +++++++++++++++++++------- src/controllers/OrgChild2Controller.ts | 34 ++++++++++++++++++------ src/controllers/OrgChild3Controller.ts | 31 ++++++++++++++++------ src/controllers/OrgChild4Controller.ts | 16 +++++++++++- src/entities/PosMaster.ts | 8 +++--- 5 files changed, 95 insertions(+), 30 deletions(-) diff --git a/src/controllers/OrgChild1Controller.ts b/src/controllers/OrgChild1Controller.ts index c318c635..4ad0c29b 100644 --- a/src/controllers/OrgChild1Controller.ts +++ b/src/controllers/OrgChild1Controller.ts @@ -22,7 +22,10 @@ import { import HttpStatusCode from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; -import { Not } from "typeorm"; +import { In, Not } from "typeorm"; +import { OrgChild4 } from "../entities/OrgChild4"; +import { PosMaster } from "../entities/PosMaster"; +import { Position } from "../entities/Position"; @Route("api/v1/org/child1") @Tags("OrgChild1") @Security("bearerAuth") @@ -36,6 +39,9 @@ export class OrgChild1Controller { private child1Repository = AppDataSource.getRepository(OrgChild1); private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); + private child4Repository = AppDataSource.getRepository(OrgChild4); + private posMasterRepository = AppDataSource.getRepository(PosMaster); + private positionRepository = AppDataSource.getRepository(Position); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** @@ -240,15 +246,27 @@ export class OrgChild1Controller { "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", - ); - } + // const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } }); + // if (exitsChild2) { + // throw new HttpError( + // HttpStatusCode.INTERNAL_SERVER_ERROR, + // "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2", + // ); + // } try { - await this.child1Repository.remove(child1); + const posMasters = await this.posMasterRepository.find({ + where: { orgChild1Id: 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.child4Repository.delete({ orgChild1Id: id }); + await this.child3Repository.delete({ orgChild1Id: id }); + await this.child2Repository.delete({ orgChild1Id: id }); + await this.child1Repository.delete({id}); return new HttpSuccess(); } catch (error) { return error; diff --git a/src/controllers/OrgChild2Controller.ts b/src/controllers/OrgChild2Controller.ts index a2dc7d11..8bc250a6 100644 --- a/src/controllers/OrgChild2Controller.ts +++ b/src/controllers/OrgChild2Controller.ts @@ -19,11 +19,15 @@ import { import HttpStatusCode from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; +import { In } from "typeorm"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; import { CreateOrgChild2, OrgChild2, UpdateOrgChild2 } from "../entities/OrgChild2"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild3 } from "../entities/OrgChild3"; +import { OrgChild4 } from "../entities/OrgChild4"; +import { PosMaster } from "../entities/PosMaster"; +import { Position } from "../entities/Position"; @Route("api/v1/org/child2") @Tags("OrgChild2") @Security("bearerAuth") @@ -37,6 +41,9 @@ export class OrgChild2Controller extends Controller { private child1Repository = AppDataSource.getRepository(OrgChild1); private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); + private child4Repository = AppDataSource.getRepository(OrgChild4); + private posMasterRepository = AppDataSource.getRepository(PosMaster); + private positionRepository = AppDataSource.getRepository(Position); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** @@ -253,15 +260,26 @@ export class OrgChild2Controller extends Controller { ); } - 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); + const posMasters = await this.posMasterRepository.find({ + where: { orgChild2Id: 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.child4Repository.delete({ orgChild2Id: id }); + await this.child3Repository.delete({ orgChild2Id: id }); + await this.child2Repository.delete({ id }); return new HttpSuccess(); } catch (error) { return error; diff --git a/src/controllers/OrgChild3Controller.ts b/src/controllers/OrgChild3Controller.ts index 58dc225f..a5b0bb25 100644 --- a/src/controllers/OrgChild3Controller.ts +++ b/src/controllers/OrgChild3Controller.ts @@ -22,6 +22,9 @@ import { import HttpStatusCode from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; +import { In } from "typeorm"; +import { PosMaster } from "../entities/PosMaster"; +import { Position } from "../entities/Position"; @Route("api/v1/org/child3") @Tags("OrgChild3") @Security("bearerAuth") @@ -35,6 +38,8 @@ export class OrgChild3Controller { private child2Repository = AppDataSource.getRepository(OrgChild2); private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); + private posMasterRepository = AppDataSource.getRepository(PosMaster); + private positionRepository = AppDataSource.getRepository(Position); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** @@ -225,15 +230,25 @@ export class OrgChild3Controller { ); } - 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); + const posMasters = await this.posMasterRepository.find({ + where: { orgChild3Id: 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.child4Repository.delete({ orgChild3Id: id }); + await this.child3Repository.delete( id ); return new HttpSuccess(); } catch (error) { return error; diff --git a/src/controllers/OrgChild4Controller.ts b/src/controllers/OrgChild4Controller.ts index 08d15b0c..f590cec1 100644 --- a/src/controllers/OrgChild4Controller.ts +++ b/src/controllers/OrgChild4Controller.ts @@ -19,11 +19,14 @@ import { import HttpStatusCode from "../interfaces/http-status"; import HttpSuccess from "../interfaces/http-success"; import HttpError from "../interfaces/http-error"; +import { In } from "typeorm"; import { OrgRevision } from "../entities/OrgRevision"; import { OrgRoot } from "../entities/OrgRoot"; import { CreateOrgChild4, OrgChild4, UpdateOrgChild4 } from "../entities/OrgChild4"; import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild3 } from "../entities/OrgChild3"; +import { PosMaster } from "../entities/PosMaster"; +import { Position } from "../entities/Position"; @Route("api/v1/org/child4") @Tags("OrgChild4") @@ -37,6 +40,8 @@ export class OrgChild4Controller extends Controller { private orgRootRepository = AppDataSource.getRepository(OrgRoot); private child3Repository = AppDataSource.getRepository(OrgChild3); private child4Repository = AppDataSource.getRepository(OrgChild4); + private posMasterRepository = AppDataSource.getRepository(PosMaster); + private positionRepository = AppDataSource.getRepository(Position); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** @@ -258,7 +263,16 @@ export class OrgChild4Controller extends Controller { ); } try { - await this.child4Repository.remove(child4); + const posMasters = await this.posMasterRepository.find({ + where: { orgChild4Id: 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.child4Repository.delete( id ); return new HttpSuccess(); } catch (error) { return error; diff --git a/src/entities/PosMaster.ts b/src/entities/PosMaster.ts index 461a97e1..8971835a 100644 --- a/src/entities/PosMaster.ts +++ b/src/entities/PosMaster.ts @@ -127,7 +127,7 @@ export class PosMaster extends EntityBase { "คนครองปัจจุบัน เมื่อทำสำเนาโครงสร้างและตำแหน่งพร้อมกับคนครองมา คนครองจะอยู่ในฟิลด์นี้", default: null, }) - profileIdCurrentHolder: string; + current_holderId: string; @Column({ nullable: true, @@ -136,7 +136,7 @@ export class PosMaster extends EntityBase { "คนที่กำลังจะมาครอง ตอนปรับโครงสร้าง ถ้าเลือกให้ใครมาครอง ProfileId ของคนนั้นจะมาอยู่ในช่องนี้ รวมทั้งตอนเลือกตำแหน่งเพื่อบรรจุ แต่งตั้ง เลื่อน ย้าย ในระบบบรรจุแต่งตั้งด้วย", default: null, }) - profileIdNextHolder: string; + next_holderId: string; @Column({ length: 40, @@ -169,11 +169,11 @@ export class PosMaster extends EntityBase { orgChild4: OrgChild4; @OneToOne(() => Profile) - @JoinColumn() + @JoinColumn({ name: "current_holderId" }) current_holder: Profile; @OneToOne(() => Profile) - @JoinColumn() + @JoinColumn({ name: "next_holderId" }) next_holder: Profile; @OneToMany(() => Position, (position) => position.posMaster)