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

@ -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;