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

482 lines
22 KiB
TypeScript
Raw Normal View History

import {
Controller,
Post,
Put,
Delete,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
Get,
Query,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { Not } from "typeorm";
import { OrgRoot } from "../entities/OrgRoot";
import { OrgRevision } from "../entities/OrgRevision";
import { RequestWithUser } from "../middlewares/user";
import { PermissionOrg } from "../entities/PermissionOrg";
import { Profile } from "../entities/Profile";
import HttpStatus from "../interfaces/http-status";
@Route("api/v1/org/permission-org")
@Tags("PermissionOrg")
@Security("bearerAuth")
@Response(
HttpStatusCode.INTERNAL_SERVER_ERROR,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class PermissionOrgController extends Controller {
private orgRootRepository = AppDataSource.getRepository(OrgRoot);
private profileRepository = AppDataSource.getRepository(Profile);
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);
private permissionOrgRepository = AppDataSource.getRepository(PermissionOrg);
/**
* API
*
* @summary
*
*/
@Get()
async GetActiveRootIdAdmin(@Request() request: RequestWithUser) {
if (!request.user.role.includes("SUPER_ADMIN")) {
throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์ใช้งานระบบนี้");
}
const orgRevisionActive = await this.orgRevisionRepository.findOne({
where: { orgRevisionIsCurrent: false, orgRevisionIsDraft: true },
});
if (!orgRevisionActive) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบโครงสร้างที่แบบร่างอยู่ตอนนี้");
}
const data = await this.orgRootRepository.find({
where: { orgRevisionId: orgRevisionActive.id },
});
return new HttpSuccess(data);
}
@Get("profile")
async listProfile(
@Request() request: RequestWithUser,
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query()
searchField?: "fullName" | "position" | "posNo" | "postype" | "poslevel",
@Query() searchKeyword: string = "",
) {
if (!request.user.role.includes("SUPER_ADMIN")) {
throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์ใช้งานระบบนี้");
}
let queryLike =
"CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword";
if (searchField == "postype") {
queryLike = "posLevel.name LIKE :keyword";
} else if (searchField == "poslevel") {
queryLike = "posType.name LIKE :keyword";
} else if (searchField == "position") {
queryLike = "profile.position LIKE :keyword";
} else if (searchField == "posNo") {
queryLike = `CONCAT(
IFNULL(orgChild4.orgChild4ShortName, ''),
IFNULL(orgChild3.orgChild3ShortName, ''),
IFNULL(orgChild2.orgChild2ShortName, ''),
IFNULL(orgChild1.orgChild1ShortName, ''),
IFNULL(orgRoot.orgRootShortName, ''),
IFNULL(current_holders.posMasterNo , '')
) LIKE :keyword`;
}
const [record, total] = await this.profileRepository
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType")
.leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("current_holders.positions", "positions")
.leftJoinAndSelect("positions.posExecutive", "posExecutive")
.leftJoinAndSelect("current_holders.orgRoot", "orgRoot")
.leftJoinAndSelect("current_holders.orgChild1", "orgChild1")
.leftJoinAndSelect("current_holders.orgChild2", "orgChild2")
.leftJoinAndSelect("current_holders.orgChild3", "orgChild3")
.leftJoinAndSelect("current_holders.orgChild4", "orgChild4")
.andWhere(
searchKeyword != undefined && searchKeyword != null && searchKeyword != ""
? queryLike
: "1=1",
{
keyword: `%${searchKeyword}%`,
},
)
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
const findRevision = await this.orgRevisionRepository.findOne({
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
if (!findRevision) {
throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision");
}
const data = await Promise.all(
record.map((_data) => {
const shortName =
_data.current_holders.length == 0
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 !=
null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgChild3 != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgChild2 != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgChild1 != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) !=
null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgRoot != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: null;
const root =
_data.current_holders.length == 0 ||
(_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null)
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot;
const child1 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1;
const child2 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2;
const child3 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3;
const child4 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4;
let _child1 = child1 == null ? "" : `${child1.orgChild1Name}/`;
let _child2 = child2 == null ? "" : `${child2.orgChild2Name}/`;
let _child3 = child3 == null ? "" : `${child3.orgChild3Name}/`;
let _child4 = child4 == null ? "" : `${child4.orgChild4Name}/`;
return {
id: _data.id,
avatar: _data.avatar,
avatarName: _data.avatarName,
prefix: _data.prefix,
rank: _data.rank,
firstName: _data.firstName,
lastName: _data.lastName,
org: `${_child4}${_child3}${_child2}${_child1}${root?.orgRootName ?? ""}`,
posNo: shortName,
position: _data.position,
posType: _data.posType == null ? null : _data.posType.posTypeName,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
};
}),
);
return new HttpSuccess({ data: data, total });
}
/**
* API
*
* @summary - CRUD (ADMIN)
*
* @param {string} id Id
*/
2024-09-06 10:33:13 +07:00
@Post("profile")
async GetById(
@Request() request: RequestWithUser,
2024-09-06 10:33:13 +07:00
@Body()
requestBody: {
id: string | null;
page: number;
pageSize: number;
searchField?: "fullName" | "position" | "posNo" | "postype" | "poslevel";
searchKeyword: string;
},
) {
if (!request.user.role.includes("SUPER_ADMIN")) {
throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์ใช้งานระบบนี้");
}
2024-09-06 10:33:13 +07:00
let profiles: any = [];
if (requestBody.id != null) {
const _permissionOrg = await this.orgRootRepository.findOne({
where: { id: requestBody.id },
relations: ["permissionOrgRoots", "permissionOrgRoots.profileTree"],
});
if (!_permissionOrg) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์โครงสร้างนี้");
}
profiles = await _permissionOrg.permissionOrgRoots.map((x) => x.profileTree.id);
}
let queryLike =
"CONCAT(profile.prefix, profile.firstName, ' ', profile.lastName) LIKE :keyword";
2024-09-06 10:33:13 +07:00
if (requestBody.searchField == "postype") {
queryLike = "posLevel.name LIKE :keyword";
2024-09-06 10:33:13 +07:00
} else if (requestBody.searchField == "poslevel") {
queryLike = "posType.name LIKE :keyword";
2024-09-06 10:33:13 +07:00
} else if (requestBody.searchField == "position") {
queryLike = "profile.position LIKE :keyword";
2024-09-06 10:33:13 +07:00
} else if (requestBody.searchField == "posNo") {
queryLike = `CONCAT(
IFNULL(orgChild4.orgChild4ShortName, ''),
IFNULL(orgChild3.orgChild3ShortName, ''),
IFNULL(orgChild2.orgChild2ShortName, ''),
IFNULL(orgChild1.orgChild1ShortName, ''),
IFNULL(orgRoot.orgRootShortName, ''),
IFNULL(current_holders.posMasterNo , '')
) LIKE :keyword`;
}
const [record, total] = await this.profileRepository
.createQueryBuilder("profile")
.leftJoinAndSelect("profile.posLevel", "posLevel")
.leftJoinAndSelect("profile.posType", "posType")
.leftJoinAndSelect("profile.current_holders", "current_holders")
.leftJoinAndSelect("current_holders.positions", "positions")
.leftJoinAndSelect("current_holders.orgRoot", "orgRoot")
.leftJoinAndSelect("current_holders.orgChild1", "orgChild1")
.leftJoinAndSelect("current_holders.orgChild2", "orgChild2")
.leftJoinAndSelect("current_holders.orgChild3", "orgChild3")
.leftJoinAndSelect("current_holders.orgChild4", "orgChild4")
2024-09-06 10:33:13 +07:00
.andWhere(`profile.id IN (:...profiles)`, {
profiles: profiles == null ? [] : profiles,
})
.andWhere(
2024-09-06 10:33:13 +07:00
requestBody.searchKeyword != undefined &&
requestBody.searchKeyword != null &&
requestBody.searchKeyword != ""
? queryLike
: "1=1",
{
2024-09-06 10:33:13 +07:00
keyword: `%${requestBody.searchKeyword}%`,
},
)
2024-09-06 10:33:13 +07:00
.skip((requestBody.page - 1) * requestBody.pageSize)
.take(requestBody.pageSize)
.getManyAndCount();
const findRevision = await this.orgRevisionRepository.findOne({
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
});
if (!findRevision) {
throw new HttpError(HttpStatus.NOT_FOUND, "not found. OrgRevision");
}
const data = await Promise.all(
record.map((_data) => {
const shortName =
_data.current_holders.length == 0
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 !=
null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgChild3 != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgChild2 != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgChild1 != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id) !=
null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)
?.orgRoot != null
? `${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName}${_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}`
: null;
const root =
_data.current_holders.length == 0 ||
(_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null &&
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null)
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot;
const child1 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1;
const child2 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2;
const child3 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3;
const child4 =
_data.current_holders == null ||
_data.current_holders.length == 0 ||
_data.current_holders.find((x) => x.orgRevisionId == findRevision.id) == null
? null
: _data.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4;
let _child1 = child1 == null ? "" : `${child1.orgChild1Name}/`;
let _child2 = child2 == null ? "" : `${child2.orgChild2Name}/`;
let _child3 = child3 == null ? "" : `${child3.orgChild3Name}/`;
let _child4 = child4 == null ? "" : `${child4.orgChild4Name}/`;
return {
id: _data.id,
avatar: _data.avatar,
avatarName: _data.avatarName,
prefix: _data.prefix,
rank: _data.rank,
firstName: _data.firstName,
lastName: _data.lastName,
org: `${_child4}${_child3}${_child2}${_child1}${root?.orgRootName ?? ""}`,
posNo: shortName,
position: _data.position,
posType: _data.posType == null ? null : _data.posType.posTypeName,
posLevel: _data.posLevel == null ? null : _data.posLevel.posLevelName,
};
}),
);
return new HttpSuccess({ data, total });
}
/**
* API body
*
* @summary - CRUD (ADMIN)
*
*/
@Post()
async Post(
@Request() request: RequestWithUser,
@Body() requestBody: { nodeId: string; personId: string },
) {
if (!request.user.role.includes("SUPER_ADMIN")) {
throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์ใช้งานระบบนี้");
}
const orgRoot = await this.orgRootRepository.findOne({
where: { id: requestBody.nodeId },
});
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสำนัก");
}
const profile = await this.profileRepository.findOne({
where: { id: requestBody.personId },
});
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัติ");
}
const checkDup = await this.permissionOrgRepository.findOne({
where: {
orgRootTree: { id: requestBody.nodeId },
profileTree: { id: requestBody.personId },
},
});
if (checkDup) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "มีสิทธิ์นี้อยู่ในระบบแล้ว");
}
const _permissionOrg = new PermissionOrg();
_permissionOrg.orgRootTree = orgRoot;
_permissionOrg.profileTree = profile;
_permissionOrg.createdUserId = request.user.sub;
_permissionOrg.createdFullName = request.user.name;
_permissionOrg.lastUpdateUserId = request.user.sub;
_permissionOrg.lastUpdateFullName = request.user.name;
_permissionOrg.createdAt = new Date();
_permissionOrg.lastUpdatedAt = new Date();
await this.permissionOrgRepository.save(_permissionOrg);
return new HttpSuccess();
}
/**
* API
*
* @summary - CRUD (ADMIN)
*
* @param {string} id Id
*/
@Delete("{nodeId}/{personId}")
async Delete(
@Request() request: RequestWithUser,
@Path() nodeId: string,
@Path() personId: string,
) {
if (!request.user.role.includes("SUPER_ADMIN")) {
throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์ใช้งานระบบนี้");
}
const orgRoot = await this.orgRootRepository.findOne({
where: { id: nodeId },
relations: ["permissionOrgRoots"],
});
if (!orgRoot) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสำนัก");
}
const profile = await this.profileRepository.findOne({
where: { id: personId },
relations: ["permissionOrgRoots"],
});
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัติ");
}
const _delPermissionOrg = await this.permissionOrgRepository.findOne({
where: {
orgRootTree: { id: nodeId },
profileTree: { id: personId },
},
});
if (!_delPermissionOrg) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบสิทธิ์นี้อยู่ในระบบแล้ว");
}
await this.permissionOrgRepository.delete(_delPermissionOrg.id);
return new HttpSuccess();
}
}