diff --git a/src/controllers/OrgRootController.ts b/src/controllers/OrgRootController.ts index 0b2c2f9d..2dda6f63 100644 --- a/src/controllers/OrgRootController.ts +++ b/src/controllers/OrgRootController.ts @@ -17,9 +17,15 @@ import { CreateOrgRoot, OrgRoot } from "../entities/OrgRoot"; import { AppDataSource } from "../database/data-source"; import HttpSuccess from "../interfaces/http-success"; import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1"; +import { In, IsNull, Not } from "typeorm"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { OrgRevision } from "../entities/OrgRevision"; +import { OrgChild2 } from "../entities/OrgChild2"; +import { OrgChild3 } from "../entities/OrgChild3"; +import { OrgChild4 } from "../entities/OrgChild4"; +import { PosMaster } from "../entities/PosMaster"; +import { Position } from "../entities/Position"; @Route("api/v1/org/root") @Tags("OrgRoot") @@ -27,7 +33,12 @@ import { OrgRevision } from "../entities/OrgRevision"; export class OrgRootController extends Controller { private orgRootRepository = AppDataSource.getRepository(OrgRoot); private orgChild1Repository = AppDataSource.getRepository(OrgChild1); + private orgChild2Repository = AppDataSource.getRepository(OrgChild2); + private orgChild3Repository = AppDataSource.getRepository(OrgChild3); + private orgChild4Repository = AppDataSource.getRepository(OrgChild4); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); + private posMasterRepository = AppDataSource.getRepository(PosMaster); + private positionRepository = AppDataSource.getRepository(Position); /** * API รายละเอียดโครงสร้างระดับ Root @@ -220,13 +231,13 @@ export class OrgRootController extends Controller { ); } - const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } }); - if (orgChild1 != null) { - throw new HttpError( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1", - ); - } + // const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } }); + // if (orgChild1 != null) { + // throw new HttpError( + // HttpStatusCode.INTERNAL_SERVER_ERROR, + // "ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1", + // ); + // } const revisionIdExits = await this.orgRevisionRepository.findOne({ where: { id: orgRoot.orgRevisionId }, @@ -244,7 +255,20 @@ export class OrgRootController extends Controller { ); } try { - await this.orgRootRepository.remove(orgRoot); + const posMasters = await this.posMasterRepository.find({ + where: { orgRootId: 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.orgChild4Repository.delete({ orgRootId: id }); + await this.orgChild3Repository.delete({ orgRootId: id }); + await this.orgChild2Repository.delete({ orgRootId: id }); + await this.orgChild1Repository.delete({ orgRootId: id }); + await this.orgRootRepository.delete({id}); return new HttpSuccess(); } catch (error) { diff --git a/src/controllers/ReportController.ts b/src/controllers/ReportController.ts index ee41c1e5..f018ac2d 100644 --- a/src/controllers/ReportController.ts +++ b/src/controllers/ReportController.ts @@ -155,25 +155,15 @@ export class ReportController extends Controller { .orderBy("orgChild4.orgChild4Order", "ASC") .getMany() : []; - const posMasters = await AppDataSource.getRepository(PosMaster) - .createQueryBuilder("posMaster") - .where("posMaster.orgRevisionId IN (:...ids)", { - ids: [orgRevision.id], - }) - .select([ - "posMaster.id", - "posMaster.posMasterNoPrefix", - "posMaster.posMasterNo", - "posMaster.posMasterNoSuffix", - "posMaster.orgRootId", - "posMaster.orgChild1Id", - "posMaster.orgChild2Id", - "posMaster.orgChild3Id", - "posMaster.orgChild4Id", - "posMaster.next_holder", - ]) - .orderBy("posMaster.posMasterOrder", "ASC") - .getMany(); + + const posMasters = await this.posMasterRepository.find({ + where: { + orgRevisionId: orgRevision.id, + }, + relations: ["next_holder", "current_holder"], + order: { posMasterOrder: "ASC" }, + }); + const positions = await this.positionRepository.find({ where: { posMasterId: In(posMasters.map((posMaster: any) => posMaster.id)), @@ -191,6 +181,19 @@ export class ReportController extends Controller { if (_orgRevisionActive.length > 0) orgRevisionActive = _orgRevisionActive[0]; } + const posMasterOlds = await this.posMasterRepository.find({ + where: { + orgRevisionId: orgRevisionActive.id, + }, + relations: ["next_holder", "current_holder"], + }); + const positionOlds = await this.positionRepository.find({ + where: { + posMasterId: In(posMasterOlds.map((posMaster: any) => posMaster.id)), + }, + relations: ["posLevel", "posType", "posExecutive"], + }); + let data = new Array(); await Promise.all( orgRootData.map(async (orgRoot) => { @@ -202,18 +205,39 @@ export class ReportController extends Controller { posMasters .filter((posMaster) => posMaster.orgRootId === orgRoot.id) .map(async (posMaster) => { + let posMasterOld: any = null; + if (posMaster.next_holder != null) { + posMasterOld = posMasterOlds.find((posMasOld) => + posMasOld.current_holder == null + ? false + : posMasOld.current_holder.id === posMaster.next_holder.id, + ); + } + let positionOld: any = null; + if (posMasterOld != null) + positionOld = positionOlds.find( + (positionold) => positionold.posMasterId === posMasterOld.id, + ); return { posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNo: posMaster.posMasterNo, posMasterNoSuffix: posMaster.posMasterNoSuffix, - fullname: `${posMaster.next_holder}`, - // fullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profileFullname: + posMaster.next_holder == null + ? null + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: posMasterOld == null ? null : posMasterOld.posMasterNo, + profilePositionName: positionOld == null ? null : positionOld.positionName, + profilePosType: positionOld == null ? null : positionOld.posType.posTypeName, + profilePosLevel: positionOld == null ? null : positionOld.posLevel.posLevelName, + profilePositionField: positionOld == null ? null : positionOld.positionField, positions: await Promise.all( positions .filter((position) => position.posMasterId === posMaster.id) .map(async (position) => { return { positionName: position.positionName, + positionField: position.positionField, posType: position.posType == null ? null : position.posType.posTypeName, posLevel: position.posLevel == null ? null : position.posLevel.posLevelName, @@ -242,10 +266,37 @@ export class ReportController extends Controller { posMasters .filter((posMaster) => posMaster.orgChild1Id === orgChild1.id) .map(async (posMaster) => { + let posMasterOld: any = null; + if (posMaster.next_holder != null) { + posMasterOld = posMasterOlds.find((posMasOld) => + posMasOld.current_holder == null + ? false + : posMasOld.current_holder.id === posMaster.next_holder.id, + ); + } + let positionOld: any = null; + if (posMasterOld != null) + positionOld = positionOlds.find( + (positionold) => positionold.posMasterId === posMasterOld.id, + ); return { posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNo: posMaster.posMasterNo, posMasterNoSuffix: posMaster.posMasterNoSuffix, + fullname: + posMaster.next_holder == null + ? null + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: + posMasterOld == null ? null : posMasterOld.posMasterNo, + profilePositionName: + positionOld == null ? null : positionOld.positionName, + profilePosType: + positionOld == null ? null : positionOld.posType.posTypeName, + profilePosLevel: + positionOld == null ? null : positionOld.posLevel.posLevelName, + profilePositionField: + positionOld == null ? null : positionOld.positionField, positions: await Promise.all( positions .filter((position) => position.posMasterId === posMaster.id) @@ -282,10 +333,37 @@ export class ReportController extends Controller { posMasters .filter((posMaster) => posMaster.orgChild2Id === orgChild2.id) .map(async (posMaster) => { + let posMasterOld: any = null; + if (posMaster.next_holder != null) { + posMasterOld = posMasterOlds.find((posMasOld) => + posMasOld.current_holder == null + ? false + : posMasOld.current_holder.id === posMaster.next_holder.id, + ); + } + let positionOld: any = null; + if (posMasterOld != null) + positionOld = positionOlds.find( + (positionold) => positionold.posMasterId === posMasterOld.id, + ); return { posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNo: posMaster.posMasterNo, posMasterNoSuffix: posMaster.posMasterNoSuffix, + fullname: + posMaster.next_holder == null + ? null + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: + posMasterOld == null ? null : posMasterOld.posMasterNo, + profilePositionName: + positionOld == null ? null : positionOld.positionName, + profilePosType: + positionOld == null ? null : positionOld.posType.posTypeName, + profilePosLevel: + positionOld == null ? null : positionOld.posLevel.posLevelName, + profilePositionField: + positionOld == null ? null : positionOld.positionField, positions: await Promise.all( positions .filter((position) => position.posMasterId === posMaster.id) @@ -324,10 +402,43 @@ export class ReportController extends Controller { posMasters .filter((posMaster) => posMaster.orgChild3Id === orgChild3.id) .map(async (posMaster) => { + let posMasterOld: any = null; + if (posMaster.next_holder != null) { + posMasterOld = posMasterOlds.find((posMasOld) => + posMasOld.current_holder == null + ? false + : posMasOld.current_holder.id === + posMaster.next_holder.id, + ); + } + let positionOld: any = null; + if (posMasterOld != null) + positionOld = positionOlds.find( + (positionold) => + positionold.posMasterId === posMasterOld.id, + ); return { posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNo: posMaster.posMasterNo, posMasterNoSuffix: posMaster.posMasterNoSuffix, + fullname: + posMaster.next_holder == null + ? null + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: + posMasterOld == null ? null : posMasterOld.posMasterNo, + profilePositionName: + positionOld == null ? null : positionOld.positionName, + profilePosType: + positionOld == null + ? null + : positionOld.posType.posTypeName, + profilePosLevel: + positionOld == null + ? null + : positionOld.posLevel.posLevelName, + profilePositionField: + positionOld == null ? null : positionOld.positionField, positions: await Promise.all( positions .filter( @@ -370,10 +481,47 @@ export class ReportController extends Controller { (posMaster) => posMaster.orgChild4Id === orgChild4.id, ) .map(async (posMaster) => { + let posMasterOld: any = null; + if (posMaster.next_holder != null) { + posMasterOld = posMasterOlds.find((posMasOld) => + posMasOld.current_holder == null + ? false + : posMasOld.current_holder.id === + posMaster.next_holder.id, + ); + } + let positionOld: any = null; + if (posMasterOld != null) + positionOld = positionOlds.find( + (positionold) => + positionold.posMasterId === posMasterOld.id, + ); return { posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNo: posMaster.posMasterNo, posMasterNoSuffix: posMaster.posMasterNoSuffix, + fullname: + posMaster.next_holder == null + ? null + : `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, + profilePosMasterNo: + posMasterOld == null + ? null + : posMasterOld.posMasterNo, + profilePositionName: + positionOld == null ? null : positionOld.positionName, + profilePosType: + positionOld == null + ? null + : positionOld.posType.posTypeName, + profilePosLevel: + positionOld == null + ? null + : positionOld.posLevel.posLevelName, + profilePositionField: + positionOld == null + ? null + : positionOld.positionField, positions: await Promise.all( positions .filter(