fix delete function
This commit is contained in:
parent
f7eab59cef
commit
595ff527a7
5 changed files with 95 additions and 30 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue