hrms-api-org/src/controllers/ReportController.ts

571 lines
29 KiB
TypeScript
Raw Normal View History

import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
SuccessResponse,
Response,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { PosExecutive } from "../entities/PosExecutive";
import { PosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
import { CreatePosDict, PosDict } from "../entities/PosDict";
import HttpError from "../interfaces/http-error";
import { Equal, ILike, In, IsNull, Like, Not } from "typeorm";
import { CreatePosMaster, PosMaster } from "../entities/PosMaster";
import { OrgRevision } from "../entities/OrgRevision";
import { OrgRoot } from "../entities/OrgRoot";
import { OrgChild1 } from "../entities/OrgChild1";
import { OrgChild2 } from "../entities/OrgChild2";
import { OrgChild3 } from "../entities/OrgChild3";
import { OrgChild4 } from "../entities/OrgChild4";
import { Position } from "../entities/Position";
import { Brackets } from "typeorm/browser";
@Route("api/v1/org/report")
2024-02-03 13:09:44 +07:00
// @Tags("Position")
// @Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class ReportController extends Controller {
private posExecutiveRepository = AppDataSource.getRepository(PosExecutive);
private posTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
private posDictRepository = AppDataSource.getRepository(PosDict);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private positionRepository = AppDataSource.getRepository(Position);
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
private child1Repository = AppDataSource.getRepository(OrgChild1);
private child2Repository = AppDataSource.getRepository(OrgChild2);
private child3Repository = AppDataSource.getRepository(OrgChild3);
private child4Repository = AppDataSource.getRepository(OrgChild4);
/**
* API
*
* @summary ORG_029 - (ADMIN) #32
*
*/
@Get("position")
async findPosition() {
try {
2024-02-03 13:09:44 +07:00
const orgRevision = await this.orgRevisionRepository.findOne({
where: { orgRevisionIsDraft: true, orgRevisionIsCurrent: false },
relations: ["orgRoots"],
});
if (!orgRevision) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-02-03 13:09:44 +07:00
const orgRootData = await AppDataSource.getRepository(OrgRoot)
.createQueryBuilder("orgRoot")
.where("orgRoot.orgRevisionId IN (:...ids)", {
ids: [orgRevision.id],
})
.select([
"orgRoot.id",
"orgRoot.orgRootName",
"orgRoot.orgRootShortName",
"orgRoot.orgRootOrder",
])
.orderBy("orgRoot.orgRootOrder", "ASC")
.getMany();
2024-02-03 13:09:44 +07:00
const orgRootIds = orgRootData.map((orgRoot) => orgRoot.id) || null;
const orgChild1Data =
orgRootIds && orgRootIds.length > 0
? await AppDataSource.getRepository(OrgChild1)
.createQueryBuilder("orgChild1")
.where("orgChild1.orgRootId IN (:...ids)", { ids: orgRootIds })
.select([
"orgChild1.id",
"orgChild1.orgChild1Name",
"orgChild1.orgChild1ShortName",
"orgChild1.orgRootId",
"orgChild1.orgChild1Order",
])
.orderBy("orgChild1.orgChild1Order", "ASC")
.getMany()
: [];
const orgChild1Ids = orgChild1Data.map((orgChild1) => orgChild1.id) || null;
const orgChild2Data =
orgChild1Ids && orgChild1Ids.length > 0
? await AppDataSource.getRepository(OrgChild2)
.createQueryBuilder("orgChild2")
.where("orgChild2.orgChild1Id IN (:...ids)", { ids: orgChild1Ids })
.select([
"orgChild2.id",
"orgChild2.orgChild2Name",
"orgChild2.orgChild2ShortName",
"orgChild2.orgChild1Id",
"orgChild2.orgChild2Order",
])
.orderBy("orgChild2.orgChild2Order", "ASC")
.getMany()
: [];
const orgChild2Ids = orgChild2Data.map((orgChild2) => orgChild2.id) || null;
const orgChild3Data =
orgChild2Ids && orgChild2Ids.length > 0
? await AppDataSource.getRepository(OrgChild3)
.createQueryBuilder("orgChild3")
.where("orgChild3.orgChild2Id IN (:...ids)", { ids: orgChild2Ids })
.select([
"orgChild3.id",
"orgChild3.orgChild3Name",
"orgChild3.orgChild3ShortName",
"orgChild3.orgChild2Id",
"orgChild3.orgChild3Order",
])
.orderBy("orgChild3.orgChild3Order", "ASC")
.getMany()
: [];
const orgChild3Ids = orgChild3Data.map((orgChild3) => orgChild3.id) || null;
const orgChild4Data =
orgChild3Ids && orgChild3Ids.length > 0
? await AppDataSource.getRepository(OrgChild4)
.createQueryBuilder("orgChild4")
.where("orgChild4.orgChild3Id IN (:...ids)", { ids: orgChild3Ids })
.select([
"orgChild4.id",
"orgChild4.orgChild4Name",
"orgChild4.orgChild4ShortName",
"orgChild4.orgChild3Id",
"orgChild4.orgChild4Order",
])
.orderBy("orgChild4.orgChild4Order", "ASC")
.getMany()
: [];
const posMasters = await this.posMasterRepository.find({
where: {
orgRevisionId: orgRevision.id,
},
relations: ["next_holder", "current_holder"],
order: { posMasterOrder: "ASC" },
});
2024-02-03 13:09:44 +07:00
const positions = await this.positionRepository.find({
where: {
posMasterId: In(posMasters.map((posMaster: any) => posMaster.id)),
},
relations: ["posLevel", "posType", "posExecutive"],
});
2024-02-06 11:30:21 +07:00
let orgRevisionActive: any = await this.orgRevisionRepository.findOne({
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
if (orgRevisionActive == null) {
const _orgRevisionActive = await this.orgRevisionRepository.find({
order: { orgRevisionCreatedAt: "DESC" },
skip: 1,
});
if (_orgRevisionActive.length > 0) orgRevisionActive = _orgRevisionActive[0];
}
2024-02-03 13:09:44 +07:00
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"],
});
2024-02-03 13:09:44 +07:00
let data = new Array();
await Promise.all(
orgRootData.map(async (orgRoot) => {
2024-02-05 12:49:22 +07:00
const node = {
orgRootName: orgRoot.orgRootName,
orgTreeName: orgRoot.orgRootName,
orgTreeShortName: orgRoot.orgRootShortName,
posMasters: await Promise.all(
posMasters
.filter((posMaster) => posMaster.orgRootId === orgRoot.id)
.map(async (posMaster) => {
let posMasterOld: any = null;
if (posMaster.next_holder != null) {
2024-02-06 15:44:21 +07:00
// 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,
);
2024-02-05 12:49:22 +07:00
return {
posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix,
2024-02-06 15:44:21 +07:00
// 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,
2024-02-03 13:09:44 +07:00
positions: await Promise.all(
2024-02-05 12:49:22 +07:00
positions
.filter((position) => position.posMasterId === posMaster.id)
.map(async (position) => {
return {
positionName: position.positionName,
positionField: position.positionField,
2024-02-05 12:49:22 +07:00
posType: position.posType == null ? null : position.posType.posTypeName,
posLevel:
position.posLevel == null ? null : position.posLevel.posLevelName,
posExecutive:
position.posExecutive == null
? null
: position.posExecutive.posExecutiveName,
};
}),
),
};
}),
),
};
data.push(node);
await Promise.all(
orgChild1Data
.filter((orgChild1) => orgChild1.orgRootId === orgRoot.id)
.map(async (orgChild1) => {
const node = {
orgRootName: orgChild1.orgChild1Name,
orgTreeName: orgChild1.orgChild1Name,
orgTreeShortName: orgChild1.orgChild1ShortName,
posMasters: await Promise.all(
posMasters
.filter((posMaster) => posMaster.orgChild1Id === orgChild1.id)
.map(async (posMaster) => {
let posMasterOld: any = null;
if (posMaster.next_holder != null) {
2024-02-06 15:44:21 +07:00
// 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,
);
2024-02-05 12:49:22 +07:00
return {
posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix,
2024-02-06 15:44:21 +07:00
// 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,
2024-02-05 12:49:22 +07:00
positions: await Promise.all(
2024-02-03 13:09:44 +07:00
positions
.filter((position) => position.posMasterId === posMaster.id)
.map(async (position) => {
2024-02-05 12:49:22 +07:00
return {
2024-02-03 13:09:44 +07:00
positionName: position.positionName,
posType:
position.posType == null ? null : position.posType.posTypeName,
posLevel:
position.posLevel == null
? null
: position.posLevel.posLevelName,
posExecutive:
position.posExecutive == null
? null
: position.posExecutive.posExecutiveName,
};
}),
2024-02-05 12:49:22 +07:00
),
};
}),
),
};
data.push(node);
await Promise.all(
orgChild2Data
.filter((orgChild2) => orgChild2.orgChild1Id === orgChild1.id)
.map(async (orgChild2) => {
const node = {
orgRootName: orgChild2.orgChild2Name,
orgTreeName: orgChild2.orgChild2Name,
orgTreeShortName: orgChild2.orgChild2ShortName,
posMasters: await Promise.all(
posMasters
.filter((posMaster) => posMaster.orgChild2Id === orgChild2.id)
.map(async (posMaster) => {
let posMasterOld: any = null;
if (posMaster.next_holder != null) {
2024-02-06 15:44:21 +07:00
// 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,
);
2024-02-05 12:49:22 +07:00
return {
posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix,
2024-02-06 15:44:21 +07:00
// 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,
2024-02-05 12:49:22 +07:00
positions: await Promise.all(
2024-02-03 13:09:44 +07:00
positions
.filter((position) => position.posMasterId === posMaster.id)
.map(async (position) => {
2024-02-05 12:49:22 +07:00
return {
2024-02-03 13:09:44 +07:00
positionName: position.positionName,
posType:
position.posType == null
? null
: position.posType.posTypeName,
posLevel:
position.posLevel == null
? null
: position.posLevel.posLevelName,
posExecutive:
position.posExecutive == null
? null
: position.posExecutive.posExecutiveName,
};
}),
2024-02-05 12:49:22 +07:00
),
};
}),
),
};
data.push(node);
await Promise.all(
orgChild3Data
.filter((orgChild3) => orgChild3.orgChild2Id === orgChild2.id)
.map(async (orgChild3) => {
const node = {
orgRootName: orgChild3.orgChild3Name,
orgTreeName: orgChild3.orgChild3Name,
orgTreeShortName: orgChild3.orgChild3ShortName,
posMasters: await Promise.all(
posMasters
.filter((posMaster) => posMaster.orgChild3Id === orgChild3.id)
.map(async (posMaster) => {
let posMasterOld: any = null;
if (posMaster.next_holder != null) {
2024-02-06 15:44:21 +07:00
// 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,
);
2024-02-05 12:49:22 +07:00
return {
posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix,
2024-02-06 15:44:21 +07:00
// 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,
2024-02-05 12:49:22 +07:00
positions: await Promise.all(
2024-02-03 13:09:44 +07:00
positions
.filter(
(position) => position.posMasterId === posMaster.id,
)
.map(async (position) => {
2024-02-05 12:49:22 +07:00
return {
2024-02-03 13:09:44 +07:00
positionName: position.positionName,
posType:
position.posType == null
? null
: position.posType.posTypeName,
posLevel:
position.posLevel == null
? null
: position.posLevel.posLevelName,
posExecutive:
position.posExecutive == null
? null
: position.posExecutive.posExecutiveName,
};
}),
2024-02-05 12:49:22 +07:00
),
};
}),
),
};
data.push(node);
await Promise.all(
orgChild4Data
.filter((orgChild4) => orgChild4.orgChild3Id === orgChild3.id)
.map(async (orgChild4) => {
const node = {
orgRootName: orgChild4.orgChild4Name,
orgTreeName: orgChild4.orgChild4Name,
orgTreeShortName: orgChild4.orgChild4ShortName,
posMasters: await Promise.all(
posMasters
.filter(
(posMaster) => posMaster.orgChild4Id === orgChild4.id,
)
.map(async (posMaster) => {
let posMasterOld: any = null;
if (posMaster.next_holder != null) {
2024-02-06 15:44:21 +07:00
// 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,
);
2024-02-05 12:49:22 +07:00
return {
posMasterNoPrefix: posMaster.posMasterNoPrefix,
posMasterNo: posMaster.posMasterNo,
posMasterNoSuffix: posMaster.posMasterNoSuffix,
2024-02-06 15:44:21 +07:00
// 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,
2024-02-05 12:49:22 +07:00
positions: await Promise.all(
2024-02-03 13:09:44 +07:00
positions
.filter(
(position) =>
position.posMasterId === posMaster.id,
)
.map(async (position) => {
2024-02-05 12:49:22 +07:00
return {
2024-02-03 13:09:44 +07:00
positionName: position.positionName,
posType:
position.posType == null
? null
: position.posType.posTypeName,
posLevel:
position.posLevel == null
? null
: position.posLevel.posLevelName,
posExecutive:
position.posExecutive == null
? null
: position.posExecutive.posExecutiveName,
};
}),
2024-02-05 12:49:22 +07:00
),
};
}),
),
};
data.push(node);
}),
);
}),
);
}),
);
}),
);
2024-02-03 13:09:44 +07:00
}),
);
return new HttpSuccess(data);
} catch (error) {
return error;
}
}
}