diff --git a/src/controllers/OrgRootController.ts b/src/controllers/OrgRootController.ts index 241b62f0..49c0c596 100644 --- a/src/controllers/OrgRootController.ts +++ b/src/controllers/OrgRootController.ts @@ -15,12 +15,14 @@ import { import { CreateOrgRoot, OrgRoot } from "../entities/OrgRoot"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; +import { OrgChild1 } from "../entities/OrgChild1"; @Route("organization") @Tags("OrgRoot") // @Security("bearerAuth") export class OrgRootController extends Controller { private orgRootRepository = AppDataSource.getRepository(OrgRoot); + private orgChild1Repository = AppDataSource.getRepository(OrgChild1); /** * สร้างโครงสร้างระดับ Root @@ -117,4 +119,38 @@ export class OrgRootController extends Controller { return error; } } + + /** + * ลบโครงสร้างระดับ Root + * + * @summary ORG_003 - ลบโครงสร้างระดับ Root (ADMIN) #3 + * + * @param {string} id Guid, *Id root + */ + @Delete("root/{id}") + async delete( + @Path() id: string, + ) { + try { + const orgRoot = await this.orgRootRepository.findOne({ where: { id } }); + const orgChild1 = await AppDataSource.getRepository(OrgChild1) + .createQueryBuilder("orgChild1") + .where("orgChild1.orgRootId", { id }) + .getOne(); + + if (!orgRoot) { + return `not found data`; + } + + if(!orgChild1){ + await this.orgRootRepository.remove(orgRoot); + }else{ + return 'ไม่สามารถลบข้อมูลได้' + } + + return new HttpSuccess(); + } catch (error) { + return error; + } + } }