fix delete function

This commit is contained in:
AdisakKanthawilang 2024-02-06 15:18:20 +07:00
parent f7eab59cef
commit 595ff527a7
5 changed files with 95 additions and 30 deletions

View file

@ -22,7 +22,10 @@ import {
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error"; 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") @Route("api/v1/org/child1")
@Tags("OrgChild1") @Tags("OrgChild1")
@Security("bearerAuth") @Security("bearerAuth")
@ -36,6 +39,9 @@ export class OrgChild1Controller {
private child1Repository = AppDataSource.getRepository(OrgChild1); private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2); private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3); 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); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
/** /**
@ -240,15 +246,27 @@ export class OrgChild1Controller {
"not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false", "not found. orgRevisionIsDraft:true, orgRevisionIsCurrent:false",
); );
} }
const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } }); // const exitsChild2 = await this.child2Repository.findOne({ where: { orgChild1Id: id } });
if (exitsChild2) { // if (exitsChild2) {
throw new HttpError( // throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR, // HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2", // "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ2",
); // );
} // }
try { 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(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;

View file

@ -19,11 +19,15 @@ import {
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { In } from "typeorm";
import { OrgRevision } from "../entities/OrgRevision"; import { OrgRevision } from "../entities/OrgRevision";
import { OrgRoot } from "../entities/OrgRoot"; import { OrgRoot } from "../entities/OrgRoot";
import { CreateOrgChild2, OrgChild2, UpdateOrgChild2 } from "../entities/OrgChild2"; import { CreateOrgChild2, OrgChild2, UpdateOrgChild2 } from "../entities/OrgChild2";
import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild1 } from "../entities/OrgChild1";
import { OrgChild3 } from "../entities/OrgChild3"; 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") @Route("api/v1/org/child2")
@Tags("OrgChild2") @Tags("OrgChild2")
@Security("bearerAuth") @Security("bearerAuth")
@ -37,6 +41,9 @@ export class OrgChild2Controller extends Controller {
private child1Repository = AppDataSource.getRepository(OrgChild1); private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2); private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3); 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); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
/** /**
@ -253,15 +260,26 @@ export class OrgChild2Controller extends Controller {
); );
} }
const exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } }); // const exitsChild3 = await this.child3Repository.findOne({ where: { orgChild2Id: id } });
if (exitsChild3) { // if (exitsChild3) {
throw new HttpError( // throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR, // HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", // "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4",
); // );
} // }
try { 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(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;

View file

@ -22,6 +22,9 @@ import {
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error"; 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") @Route("api/v1/org/child3")
@Tags("OrgChild3") @Tags("OrgChild3")
@Security("bearerAuth") @Security("bearerAuth")
@ -35,6 +38,8 @@ export class OrgChild3Controller {
private child2Repository = AppDataSource.getRepository(OrgChild2); private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3); private child3Repository = AppDataSource.getRepository(OrgChild3);
private child4Repository = AppDataSource.getRepository(OrgChild4); private child4Repository = AppDataSource.getRepository(OrgChild4);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private positionRepository = AppDataSource.getRepository(Position);
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
/** /**
@ -225,15 +230,25 @@ export class OrgChild3Controller {
); );
} }
const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } }); // const exitsChild4 = await this.child4Repository.findOne({ where: { orgChild3Id: id } });
if (exitsChild4) { // if (exitsChild4) {
throw new HttpError( // throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR, // HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4", // "ไม่สามารถลบได้ เนื่องจาก id ผูกกับโครงสร้างระดับ4",
); // );
} // }
try { 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(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;

View file

@ -19,11 +19,14 @@ import {
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { In } from "typeorm";
import { OrgRevision } from "../entities/OrgRevision"; import { OrgRevision } from "../entities/OrgRevision";
import { OrgRoot } from "../entities/OrgRoot"; import { OrgRoot } from "../entities/OrgRoot";
import { CreateOrgChild4, OrgChild4, UpdateOrgChild4 } from "../entities/OrgChild4"; import { CreateOrgChild4, OrgChild4, UpdateOrgChild4 } from "../entities/OrgChild4";
import { OrgChild1 } from "../entities/OrgChild1"; import { OrgChild1 } from "../entities/OrgChild1";
import { OrgChild3 } from "../entities/OrgChild3"; import { OrgChild3 } from "../entities/OrgChild3";
import { PosMaster } from "../entities/PosMaster";
import { Position } from "../entities/Position";
@Route("api/v1/org/child4") @Route("api/v1/org/child4")
@Tags("OrgChild4") @Tags("OrgChild4")
@ -37,6 +40,8 @@ export class OrgChild4Controller extends Controller {
private orgRootRepository = AppDataSource.getRepository(OrgRoot); private orgRootRepository = AppDataSource.getRepository(OrgRoot);
private child3Repository = AppDataSource.getRepository(OrgChild3); private child3Repository = AppDataSource.getRepository(OrgChild3);
private child4Repository = AppDataSource.getRepository(OrgChild4); private child4Repository = AppDataSource.getRepository(OrgChild4);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private positionRepository = AppDataSource.getRepository(Position);
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
/** /**
@ -258,7 +263,16 @@ export class OrgChild4Controller extends Controller {
); );
} }
try { 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(); return new HttpSuccess();
} catch (error) { } catch (error) {
return error; return error;

View file

@ -127,7 +127,7 @@ export class PosMaster extends EntityBase {
"คนครองปัจจุบัน เมื่อทำสำเนาโครงสร้างและตำแหน่งพร้อมกับคนครองมา คนครองจะอยู่ในฟิลด์นี้", "คนครองปัจจุบัน เมื่อทำสำเนาโครงสร้างและตำแหน่งพร้อมกับคนครองมา คนครองจะอยู่ในฟิลด์นี้",
default: null, default: null,
}) })
profileIdCurrentHolder: string; current_holderId: string;
@Column({ @Column({
nullable: true, nullable: true,
@ -136,7 +136,7 @@ export class PosMaster extends EntityBase {
"คนที่กำลังจะมาครอง ตอนปรับโครงสร้าง ถ้าเลือกให้ใครมาครอง ProfileId ของคนนั้นจะมาอยู่ในช่องนี้ รวมทั้งตอนเลือกตำแหน่งเพื่อบรรจุ แต่งตั้ง เลื่อน ย้าย ในระบบบรรจุแต่งตั้งด้วย", "คนที่กำลังจะมาครอง ตอนปรับโครงสร้าง ถ้าเลือกให้ใครมาครอง ProfileId ของคนนั้นจะมาอยู่ในช่องนี้ รวมทั้งตอนเลือกตำแหน่งเพื่อบรรจุ แต่งตั้ง เลื่อน ย้าย ในระบบบรรจุแต่งตั้งด้วย",
default: null, default: null,
}) })
profileIdNextHolder: string; next_holderId: string;
@Column({ @Column({
length: 40, length: 40,
@ -169,11 +169,11 @@ export class PosMaster extends EntityBase {
orgChild4: OrgChild4; orgChild4: OrgChild4;
@OneToOne(() => Profile) @OneToOne(() => Profile)
@JoinColumn() @JoinColumn({ name: "current_holderId" })
current_holder: Profile; current_holder: Profile;
@OneToOne(() => Profile) @OneToOne(() => Profile)
@JoinColumn() @JoinColumn({ name: "next_holderId" })
next_holder: Profile; next_holder: Profile;
@OneToMany(() => Position, (position) => position.posMaster) @OneToMany(() => Position, (position) => position.posMaster)