Merge branch 'develop' of github.com:Frappet/bma-ehr-organization into develop

This commit is contained in:
Kittapath 2024-02-06 15:00:59 +07:00
commit 344c277d1f
2 changed files with 201 additions and 29 deletions

View file

@ -17,9 +17,15 @@ import { CreateOrgRoot, OrgRoot } from "../entities/OrgRoot";
import { AppDataSource } from "../database/data-source"; import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1"; import { CreateOrgChild1, OrgChild1 } from "../entities/OrgChild1";
import { In, IsNull, Not } from "typeorm";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import { OrgRevision } from "../entities/OrgRevision"; 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") @Route("api/v1/org/root")
@Tags("OrgRoot") @Tags("OrgRoot")
@ -27,7 +33,12 @@ import { OrgRevision } from "../entities/OrgRevision";
export class OrgRootController extends Controller { export class OrgRootController extends Controller {
private orgRootRepository = AppDataSource.getRepository(OrgRoot); private orgRootRepository = AppDataSource.getRepository(OrgRoot);
private orgChild1Repository = AppDataSource.getRepository(OrgChild1); 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 orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private positionRepository = AppDataSource.getRepository(Position);
/** /**
* API Root * API Root
@ -220,13 +231,13 @@ export class OrgRootController extends Controller {
); );
} }
const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } }); // const orgChild1 = await this.orgChild1Repository.findOne({ where: { orgRootId: id } });
if (orgChild1 != null) { // if (orgChild1 != null) {
throw new HttpError( // throw new HttpError(
HttpStatusCode.INTERNAL_SERVER_ERROR, // HttpStatusCode.INTERNAL_SERVER_ERROR,
"ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1", // "ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ1",
); // );
} // }
const revisionIdExits = await this.orgRevisionRepository.findOne({ const revisionIdExits = await this.orgRevisionRepository.findOne({
where: { id: orgRoot.orgRevisionId }, where: { id: orgRoot.orgRevisionId },
@ -244,7 +255,20 @@ export class OrgRootController extends Controller {
); );
} }
try { 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(); return new HttpSuccess();
} catch (error) { } catch (error) {

View file

@ -155,25 +155,15 @@ export class ReportController extends Controller {
.orderBy("orgChild4.orgChild4Order", "ASC") .orderBy("orgChild4.orgChild4Order", "ASC")
.getMany() .getMany()
: []; : [];
const posMasters = await AppDataSource.getRepository(PosMaster)
.createQueryBuilder("posMaster") const posMasters = await this.posMasterRepository.find({
.where("posMaster.orgRevisionId IN (:...ids)", { where: {
ids: [orgRevision.id], orgRevisionId: orgRevision.id,
}) },
.select([ relations: ["next_holder", "current_holder"],
"posMaster.id", order: { posMasterOrder: "ASC" },
"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 positions = await this.positionRepository.find({ const positions = await this.positionRepository.find({
where: { where: {
posMasterId: In(posMasters.map((posMaster: any) => posMaster.id)), posMasterId: In(posMasters.map((posMaster: any) => posMaster.id)),
@ -191,6 +181,19 @@ export class ReportController extends Controller {
if (_orgRevisionActive.length > 0) orgRevisionActive = _orgRevisionActive[0]; 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(); let data = new Array();
await Promise.all( await Promise.all(
orgRootData.map(async (orgRoot) => { orgRootData.map(async (orgRoot) => {
@ -202,18 +205,39 @@ export class ReportController extends Controller {
posMasters posMasters
.filter((posMaster) => posMaster.orgRootId === orgRoot.id) .filter((posMaster) => posMaster.orgRootId === orgRoot.id)
.map(async (posMaster) => { .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 { return {
posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo, posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix, posMasterNoSuffix: posMaster.posMasterNoSuffix,
fullname: `${posMaster.next_holder}`, profileFullname:
// fullname: `${posMaster.next_holder.prefix}${posMaster.next_holder.firstName} ${posMaster.next_holder.lastName}`, 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: await Promise.all(
positions positions
.filter((position) => position.posMasterId === posMaster.id) .filter((position) => position.posMasterId === posMaster.id)
.map(async (position) => { .map(async (position) => {
return { return {
positionName: position.positionName, positionName: position.positionName,
positionField: position.positionField,
posType: position.posType == null ? null : position.posType.posTypeName, posType: position.posType == null ? null : position.posType.posTypeName,
posLevel: posLevel:
position.posLevel == null ? null : position.posLevel.posLevelName, position.posLevel == null ? null : position.posLevel.posLevelName,
@ -242,10 +266,37 @@ export class ReportController extends Controller {
posMasters posMasters
.filter((posMaster) => posMaster.orgChild1Id === orgChild1.id) .filter((posMaster) => posMaster.orgChild1Id === orgChild1.id)
.map(async (posMaster) => { .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 { return {
posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo, posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix, 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: await Promise.all(
positions positions
.filter((position) => position.posMasterId === posMaster.id) .filter((position) => position.posMasterId === posMaster.id)
@ -282,10 +333,37 @@ export class ReportController extends Controller {
posMasters posMasters
.filter((posMaster) => posMaster.orgChild2Id === orgChild2.id) .filter((posMaster) => posMaster.orgChild2Id === orgChild2.id)
.map(async (posMaster) => { .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 { return {
posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo, posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix, 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: await Promise.all(
positions positions
.filter((position) => position.posMasterId === posMaster.id) .filter((position) => position.posMasterId === posMaster.id)
@ -324,10 +402,43 @@ export class ReportController extends Controller {
posMasters posMasters
.filter((posMaster) => posMaster.orgChild3Id === orgChild3.id) .filter((posMaster) => posMaster.orgChild3Id === orgChild3.id)
.map(async (posMaster) => { .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 { return {
posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo, posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix, 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: await Promise.all(
positions positions
.filter( .filter(
@ -370,10 +481,47 @@ export class ReportController extends Controller {
(posMaster) => posMaster.orgChild4Id === orgChild4.id, (posMaster) => posMaster.orgChild4Id === orgChild4.id,
) )
.map(async (posMaster) => { .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 { return {
posMasterNoPrefix: posMaster.posMasterNoPrefix, posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo, posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix, 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: await Promise.all(
positions positions
.filter( .filter(