2024-03-12 17:53:54 +07:00
import {
Controller ,
Get ,
Post ,
Put ,
Delete ,
Route ,
Security ,
Tags ,
Body ,
Path ,
Request ,
SuccessResponse ,
Response ,
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" ;
2024-03-13 18:59:35 +07:00
import { In , IsNull , Like , Not , Brackets } from "typeorm" ;
import {
EmployeePosDict ,
CreateEmployeePosDict ,
UpdateEmployeePosDict ,
} from "../entities/EmployeePosDict" ;
import { EmployeePosType } from "../entities/EmployeePosType" ;
import { EmployeePosLevel } from "../entities/EmployeePosLevel" ;
import { CreateEmployeePosMaster , EmployeePosMaster } from "../entities/EmployeePosMaster" ;
import { EmployeePosition } from "../entities/EmployeePosition" ;
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" ;
2024-03-15 14:32:08 +07:00
import { ProfileEmployee } from "../entities/ProfileEmployee" ;
2024-03-12 17:53:54 +07:00
@Route ( "api/v1/org/employee/pos" )
@Tags ( "Employee" )
@Security ( "bearerAuth" )
@Response (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง" ,
)
@SuccessResponse ( HttpStatusCode . OK , "สำเร็จ" )
export class EmployeePositionController extends Controller {
private employeePosDictRepository = AppDataSource . getRepository ( EmployeePosDict ) ;
private employeePosTypeRepository = AppDataSource . getRepository ( EmployeePosType ) ;
private employeePosLevelRepository = AppDataSource . getRepository ( EmployeePosLevel ) ;
2024-03-13 18:59:35 +07:00
private employeePosMasterRepository = AppDataSource . getRepository ( EmployeePosMaster ) ;
private employeePositionRepository = AppDataSource . getRepository ( EmployeePosition ) ;
2024-03-15 14:32:08 +07:00
private profileRepository = AppDataSource . getRepository ( ProfileEmployee ) ;
2024-03-13 18:59:35 +07:00
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 ) ;
2024-03-12 17:53:54 +07:00
/ * *
* API เ พ ิ ่ ม ต ำ แ ห น ่ ง ล ู ก จ ้ า ง ป ร ะ จ ำ
*
* @summary ORG_ - เ พ ิ ่ ม ต ำ แ ห น ่ ง ล ู ก จ ้ า ง ป ร ะ จ ำ ( ADMIN ) #
*
* /
@Post ( "position" )
async CreateEmployeePosition (
@Body ( )
requestBody : CreateEmployeePosDict ,
@Request ( ) request : { user : Record < string , any > } ,
) {
const empPosDict = Object . assign ( new EmployeePosDict ( ) , requestBody ) ;
if ( ! empPosDict ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล" ) ;
}
const EmpPosType = await this . employeePosTypeRepository . findOne ( {
2024-03-13 18:59:35 +07:00
where : { id : String ( requestBody . posTypeId ) } ,
2024-03-12 17:53:54 +07:00
} ) ;
if ( ! EmpPosType ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลกลุ่มงานนี้" ) ;
}
const EmpPosLevel = await this . employeePosLevelRepository . findOne ( {
2024-03-13 18:59:35 +07:00
where : { id : String ( requestBody . posLevelId ) } ,
2024-03-12 17:53:54 +07:00
} ) ;
if ( ! EmpPosLevel ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลระดับชั้นงานนี้" ) ;
}
const rowRepeated = await this . employeePosDictRepository . findOne ( {
where : {
posDictName : String ( requestBody . posDictName ) ,
2024-03-13 18:59:35 +07:00
posTypeId : String ( requestBody . posTypeId ) ,
posLevelId : String ( requestBody . posLevelId ) ,
2024-03-12 17:53:54 +07:00
} ,
} ) ;
if ( rowRepeated ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ข้อมูล Row นี้มีอยู่ในระบบแล้ว" ) ;
}
empPosDict . createdUserId = request . user . sub ;
empPosDict . createdFullName = request . user . name ;
empPosDict . lastUpdateUserId = request . user . sub ;
empPosDict . lastUpdateFullName = request . user . name ;
await this . employeePosDictRepository . save ( empPosDict ) ;
return new HttpSuccess ( empPosDict . id ) ;
}
/ * *
* API แ ก ้ ไ ข ต ำ แ ห น ่ ง ล ู ก จ ้ า ง ป ร ะ จ ำ
*
* @summary แ ก ้ ไ ข ต ำ แ ห น ่ ง ล ู ก จ ้ า ง ป ร ะ จ ำ ( ADMIN )
*
* /
@Put ( "position/{id}" )
async updatePosition (
@Path ( ) id : string ,
@Body ( )
requestBody : UpdateEmployeePosDict ,
@Request ( ) request : { user : Record < string , any > } ,
) {
const empPosDict = await this . employeePosDictRepository . findOne ( {
where : { id : id } ,
} ) ;
if ( ! empPosDict ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งลูกจ้างประจำนี้" ) ;
}
const checkEmpPosTypeId = await this . employeePosTypeRepository . findOne ( {
2024-03-13 18:59:35 +07:00
where : { id : requestBody.posTypeId } ,
2024-03-12 17:53:54 +07:00
} ) ;
if ( ! checkEmpPosTypeId ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลกลุ่มงานนี้" ) ;
}
const checkEmpPosLevelId = await this . employeePosLevelRepository . findOne ( {
2024-03-13 18:59:35 +07:00
where : { id : requestBody.posLevelId } ,
2024-03-12 17:53:54 +07:00
} ) ;
if ( ! checkEmpPosLevelId ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลระดับกลุ่มงานนี้" ) ;
}
const rowRepeated = await this . employeePosDictRepository . findOne ( {
where : {
id : Not ( id ) ,
posDictName : requestBody.posDictName ,
2024-03-13 18:59:35 +07:00
posTypeId : requestBody.posTypeId ,
posLevelId : requestBody.posLevelId ,
2024-03-12 17:53:54 +07:00
} ,
} ) ;
if ( rowRepeated ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ข้อมูล Row นี้มีอยู่ในระบบแล้ว" ) ;
}
empPosDict . lastUpdateUserId = request . user . sub ;
empPosDict . lastUpdateFullName = request . user . name ;
this . employeePosDictRepository . merge ( empPosDict , requestBody ) ;
await this . employeePosDictRepository . save ( empPosDict ) ;
return new HttpSuccess ( empPosDict . id ) ;
}
/ * *
* API ล บ ต ำ แ ห น ่ ง ล ู ก จ ้ า ง ป ร ะ จ ำ
*
* @summary ORG_ - ล บ ต ำ แ ห น ่ ง ล ู ก จ ้ า ง ป ร ะ จ ำ ( ADMIN ) #
*
* @param { string } id Id ต ำ แ ห น ่ ง ล ู ก จ ้ า ง ป ร ะ จ ำ
* /
@Delete ( "position/{id}" )
async delete ( @Path ( ) id : string ) {
const delEmpPosDict = await this . employeePosDictRepository . findOne ( { where : { id } } ) ;
if ( ! delEmpPosDict ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งลูกจ้างประจำนี้" ) ;
}
await this . employeePosDictRepository . remove ( delEmpPosDict ) ;
return new HttpSuccess ( ) ;
}
2024-03-15 11:31:23 +07:00
// /**
// * API รายละเอียดข้อมูลตำแหน่งลูกจ้างประจำ
// *
// * @summary ORG_037 - รายละเอียดข้อมูลตำแหน่งลูกจ้างประจำ (ADMIN) #
// *
// */
// @Get("position/{id}")
// async GetEmpPositionById(@Path() id: string) {
// const empPosDict = await this.employeePosDictRepository.findOne({
// select: ["id", "posDictName", "posTypeId", "posLevelId"],
// where: { id: id },
// });
// if (!empPosDict) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลกลุ่มงานนี้");
// }
// const empPosType = await this.employeePosTypeRepository.findOne({
// select: ["id", "posTypeName", "posTypeRank", "posTypeShortName"],
// where: { id: empPosDict.posTypeId },
// });
// if (!empPosType) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลระดับชั้นงานนี้");
// }
// const empPosLevel = await this.employeePosLevelRepository.findOne({
// select: ["id", "posLevelName", "posLevelRank"],
// where: { id: empPosDict.posLevelId },
// });
// if (!empPosLevel) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งลูกจ้างประจำนี้");
// }
// const mapData = {
// id: empPosDict.id,
// posDictName: empPosDict.posDictName, //ชื่อตำแหน่ง
// posTypeId: empPosType.id,
// posTypeName: empPosType.posTypeName, //กลุ่มงาน
// posLeveId: empPosLevel.id,
// posLevelName: empPosLevel.posLevelName, //ระดับขั้นงาน
// };
// return new HttpSuccess(mapData);
// }
2024-03-12 17:53:54 +07:00
/ * *
2024-03-15 11:31:23 +07:00
* API ร า ย ล ะ เ อ ี ย ด อ ั ต ร า ก ำ ล ั ง ล ู ก จ ้ า ง ป ร ะ จ ำ
2024-03-12 17:53:54 +07:00
*
*
* /
@Get ( "position/{id}" )
2024-03-15 11:31:23 +07:00
async detailPosition ( @Path ( ) id : string ) {
const posMaster = await this . employeePosMasterRepository . findOne ( {
2024-03-15 14:32:08 +07:00
where : { id : id } ,
2024-03-12 17:53:54 +07:00
} ) ;
2024-03-15 11:31:23 +07:00
if ( ! posMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล" ) ;
2024-03-12 17:53:54 +07:00
}
2024-03-15 11:31:23 +07:00
const positions = await this . employeePositionRepository . find ( {
where : { posMasterId : posMaster.id } ,
relations : [ "posType" , "posLevel" ] ,
order : { lastUpdatedAt : "ASC" } ,
2024-03-12 17:53:54 +07:00
} ) ;
2024-03-15 11:31:23 +07:00
const formattedData = {
id : posMaster.id ,
posMasterNoPrefix : posMaster.posMasterNoPrefix ,
posMasterNo : posMaster.posMasterNo ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ,
positions : positions.map ( ( position ) = > ( {
id : position.id ,
positionName : position.positionName ,
posTypeId : position.posTypeId ,
posTypeName : position.posType == null ? null : position . posType . posTypeName ,
posLevelId : position.posLevelId ,
posLevelName : position.posLevel == null ? null : position . posLevel . posLevelName ,
positionIsSelected : position.positionIsSelected ,
} ) ) ,
2024-03-13 18:59:35 +07:00
} ;
2024-03-15 11:31:23 +07:00
return new HttpSuccess ( formattedData ) ;
2024-03-12 17:53:54 +07:00
}
/ * *
* API ค ้ น ห า ร า ย ก า ร ต ำ แ ห น ่ ง ล ู ก จ ้ า ง ป ร ะ จ ำ
*
* @summary ORG_ - ค ้ น ห า ร า ย ก า ร ต ำ แ ห น ่ ง ล ู ก จ ้ า ง ป ร ะ จ ำ ( ADMIN ) #
*
* /
@Get ( "position" )
async GetEmpPosition ( @Query ( "keyword" ) keyword? : string , @Query ( "type" ) type ? : string ) {
let findData : any ;
switch ( type ) {
2024-03-15 10:32:22 +07:00
case "positionName" :
2024-03-12 17:53:54 +07:00
findData = await this . employeePosDictRepository . find ( {
where : { posDictName : Like ( ` % ${ keyword } % ` ) } ,
2024-03-13 18:59:35 +07:00
relations : [ "posType" , "posLevel" ] ,
2024-03-18 15:24:28 +07:00
order : { posLevel : { posLevelName : "ASC" } } ,
2024-03-12 17:53:54 +07:00
} ) ;
break ;
2024-03-15 10:32:22 +07:00
case "positionType" :
2024-03-12 17:53:54 +07:00
const findEmpTypes : EmployeePosType [ ] = await this . employeePosTypeRepository . find ( {
where : { posTypeName : Like ( ` % ${ keyword } % ` ) } ,
select : [ "id" ] ,
} ) ;
findData = await this . employeePosDictRepository . find ( {
2024-03-13 18:59:35 +07:00
where : { posTypeId : In ( findEmpTypes . map ( ( x ) = > x . id ) ) } ,
relations : [ "posType" , "posLevel" ] ,
2024-03-18 15:24:28 +07:00
order : { posLevel : { posLevelName : "ASC" } } ,
2024-03-12 17:53:54 +07:00
} ) ;
break ;
2024-03-15 10:32:22 +07:00
case "positionLevel" :
2024-03-13 18:59:35 +07:00
if ( ! isNaN ( Number ( keyword ) ) ) {
2024-03-12 17:53:54 +07:00
const findEmpLevels : EmployeePosLevel [ ] = await this . employeePosLevelRepository . find ( {
where : { posLevelName : Number ( keyword ) } ,
} ) ;
findData = await this . employeePosDictRepository . find ( {
2024-03-13 18:59:35 +07:00
where : { posLevelId : In ( findEmpLevels . map ( ( x ) = > x . id ) ) } ,
relations : [ "posType" , "posLevel" ] ,
2024-03-18 15:24:28 +07:00
order : { posLevel : { posLevelName : "ASC" } } ,
2024-03-12 17:53:54 +07:00
} ) ;
2024-03-13 18:59:35 +07:00
} else {
//กรณีเลือกค้นหาจาก"ระดับชั้นงาน" แต่กรอกไม่ใช่ number ให้ปล่อยมาหมดเลย
2024-03-12 17:53:54 +07:00
findData = await this . employeePosDictRepository . find ( {
2024-03-13 18:59:35 +07:00
relations : [ "posType" , "posLevel" ] ,
2024-03-18 15:24:28 +07:00
order : { posLevel : { posLevelName : "ASC" } } ,
2024-03-12 17:53:54 +07:00
} ) ;
}
break ;
default :
findData = await this . employeePosDictRepository . find ( {
2024-03-13 18:59:35 +07:00
relations : [ "posType" , "posLevel" ] ,
2024-03-18 15:24:28 +07:00
order : { posLevel : { posLevelName : "ASC" } } ,
2024-03-12 17:53:54 +07:00
} ) ;
break ;
}
const mapDataEmpPosDict = await Promise . all (
findData . map ( async ( item : any ) = > {
2024-03-13 11:22:46 +07:00
let posTypeName = null ;
let posLevelName = null ;
2024-03-13 18:59:35 +07:00
if ( item . posType !== null && item . posType !== undefined ) {
posTypeName = item . posType . posTypeName ;
2024-03-13 11:22:46 +07:00
}
2024-03-13 18:59:35 +07:00
if ( item . posLevel !== null && item . posLevel !== undefined ) {
posLevelName = item . posLevel . posLevelName ;
2024-03-13 11:22:46 +07:00
}
2024-03-12 17:53:54 +07:00
return {
id : item.id ,
posDictName : item.posDictName ,
2024-03-14 15:11:58 +07:00
posTypeId : item.posTypeId ,
2024-03-13 11:22:46 +07:00
posTypeName : posTypeName ,
2024-03-14 15:11:58 +07:00
posLevelId : item.posLevelId ,
2024-03-13 11:22:46 +07:00
posLevelName : posLevelName ,
2024-03-12 17:53:54 +07:00
} ;
} ) ,
) ;
return new HttpSuccess ( mapDataEmpPosDict ) ;
}
2024-03-13 18:59:35 +07:00
/ * *
* API เ พ ิ ่ ม อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_ - เ พ ิ ่ ม อ ั ต ร า ก ำ ล ั ง ( ADMIN )
*
* /
@Post ( "master" )
async createEmpMaster (
@Body ( )
requestBody : CreateEmployeePosMaster ,
@Request ( ) request : { user : Record < string , any > } ,
) {
const posMaster = Object . assign ( new EmployeePosMaster ( ) , requestBody ) ;
if ( ! posMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล" ) ;
}
let orgRoot : any = null ;
let SName : any = null ;
if ( requestBody . orgRootId != null )
orgRoot = await this . orgRootRepository . findOne ( {
where : { id : requestBody.orgRootId } ,
} ) ;
if ( ! orgRoot ) {
let orgChild1 : any = null ;
if ( requestBody . orgChild1Id != null )
orgChild1 = await this . child1Repository . findOne ( {
where : { id : requestBody.orgChild1Id } ,
} ) ;
if ( ! orgChild1 ) {
let orgChild2 : any = null ;
if ( requestBody . orgChild2Id != null )
orgChild2 = await this . child2Repository . findOne ( {
where : { id : requestBody.orgChild2Id } ,
} ) ;
if ( ! orgChild2 ) {
let orgChild3 : any = null ;
if ( requestBody . orgChild3Id != null )
orgChild3 = await this . child3Repository . findOne ( {
where : { id : requestBody.orgChild3Id } ,
} ) ;
if ( ! orgChild3 ) {
let orgChild4 : any = null ;
if ( requestBody . orgChild4Id != null )
orgChild4 = await this . child4Repository . findOne ( {
where : { id : requestBody.orgChild4Id } ,
} ) ;
if ( ! orgChild4 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลโครงสร้าง" ) ;
} else {
const order : any = await this . employeePosMasterRepository . findOne ( {
where : {
orgChild4Id : orgChild4.id ,
} ,
order : { posMasterOrder : "DESC" } ,
} ) ;
posMaster . posMasterOrder =
order !== null && order !== undefined && order . posMasterOrder
? order . posMasterOrder + 1
: 1 ;
posMaster . orgRootId = orgChild4 . orgRootId ;
posMaster . orgChild1Id = orgChild4 . orgChild1Id ;
posMaster . orgChild2Id = orgChild4 . orgChild2Id ;
posMaster . orgChild3Id = orgChild4 . orgChild3Id ;
posMaster . orgChild4Id = orgChild4 . id ;
posMaster . orgRevisionId = orgChild4 . orgRevisionId ;
SName = orgChild4 . orgChild4ShortName ;
}
} else {
const order : any = await this . employeePosMasterRepository . findOne ( {
where : {
orgChild3Id : orgChild3.id ,
orgChild4Id : IsNull ( ) || "" ,
} ,
order : { posMasterOrder : "DESC" } ,
} ) ;
posMaster . posMasterOrder =
order !== null && order !== undefined && order . posMasterOrder
? order . posMasterOrder + 1
: 1 ;
posMaster . orgRootId = orgChild3 . orgRootId ;
posMaster . orgChild1Id = orgChild3 . orgChild1Id ;
posMaster . orgChild2Id = orgChild3 . orgChild2Id ;
posMaster . orgChild3Id = orgChild3 . id ;
posMaster . orgRevisionId = orgChild3 . orgRevisionId ;
SName = orgChild3 . orgChild3ShortName ;
}
} else {
const order : any = await this . employeePosMasterRepository . findOne ( {
where : {
orgChild2Id : orgChild2.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
} ,
order : { posMasterOrder : "DESC" } ,
} ) ;
posMaster . posMasterOrder =
order !== null && order !== undefined && order . posMasterOrder
? order . posMasterOrder + 1
: 1 ;
posMaster . orgRootId = orgChild2 . orgRootId ;
posMaster . orgChild1Id = orgChild2 . orgChild1Id ;
posMaster . orgChild2Id = orgChild2 . id ;
posMaster . orgRevisionId = orgChild2 . orgRevisionId ;
SName = orgChild2 . orgChild2ShortName ;
}
} else {
const order : any = await this . employeePosMasterRepository . findOne ( {
where : {
orgChild1Id : orgChild1.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
} ,
order : { posMasterOrder : "DESC" } ,
} ) ;
posMaster . posMasterOrder =
order !== null && order !== undefined && order . posMasterOrder
? order . posMasterOrder + 1
: 1 ;
posMaster . orgRootId = orgChild1 . orgRootId ;
posMaster . orgChild1Id = orgChild1 . id ;
posMaster . orgRevisionId = orgChild1 . orgRevisionId ;
SName = orgChild1 . orgChild1ShortName ;
}
} else {
const order : any = await this . employeePosMasterRepository . findOne ( {
where : {
orgRootId : orgRoot.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
} ,
order : { posMasterOrder : "DESC" } ,
} ) ;
posMaster . posMasterOrder =
order !== null && order !== undefined && order . posMasterOrder
? order . posMasterOrder + 1
: 1 ;
posMaster . orgRootId = orgRoot . id ;
posMaster . orgRevisionId = orgRoot . orgRevisionId ;
SName = orgRoot . orgRootShortName ;
}
const chk_SName0 = await this . employeePosMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgRoot : { orgRootShortName : SName } ,
orgChild1Id : IsNull ( ) ,
posMasterNo : requestBody.posMasterNo ,
} ,
relations : [ "orgRoot" ] ,
} ) ;
if ( chk_SName0 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
const chk_SName1 = await this . employeePosMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgChild1 : { orgChild1ShortName : SName } ,
orgChild2Id : IsNull ( ) ,
posMasterNo : requestBody.posMasterNo ,
} ,
relations : [ "orgChild1" ] ,
} ) ;
if ( chk_SName1 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
const chk_SName2 = await this . employeePosMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgChild2 : { orgChild2ShortName : SName } ,
orgChild3Id : IsNull ( ) ,
posMasterNo : requestBody.posMasterNo ,
} ,
relations : [ "orgChild2" ] ,
} ) ;
if ( chk_SName2 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
const chk_SName3 = await this . employeePosMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgChild3 : { orgChild3ShortName : SName } ,
orgChild4Id : IsNull ( ) ,
posMasterNo : requestBody.posMasterNo ,
} ,
relations : [ "orgChild3" ] ,
} ) ;
if ( chk_SName3 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
const chk_SName4 = await this . employeePosMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgChild4 : { orgChild4ShortName : SName } ,
posMasterNo : requestBody.posMasterNo ,
} ,
relations : [ "orgChild4" ] ,
} ) ;
if ( chk_SName4 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
posMaster . createdUserId = request . user . sub ;
posMaster . createdFullName = request . user . name ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
await this . employeePosMasterRepository . save ( posMaster ) ;
await Promise . all (
requestBody . positions . map ( async ( x : any ) = > {
const position = Object . assign ( new EmployeePosition ( ) ) ;
position . positionName = x . posDictName ;
position . posTypeId = x . posTypeId == "" ? null : x . posTypeId ;
position . posLevelId = x . posLevelId == "" ? null : x . posLevelId ;
position . positionIsSelected = false ;
position . posMasterId = posMaster . id ;
position . createdUserId = request . user . sub ;
position . createdFullName = request . user . name ;
position . lastUpdateUserId = request . user . sub ;
position . lastUpdateFullName = request . user . name ;
await this . employeePositionRepository . save ( position ) ;
} ) ,
) ;
return new HttpSuccess ( posMaster . id ) ;
}
/ * *
* API แ ก ้ ไ ข เ ล ข ท ี ่ ต ำ แ ห น ่ ง
*
* @summary ORG_ - แ ก ้ ไ ข เ ล ข ท ี ่ ต ำ แ ห น ่ ง ( ADMIN )
*
* /
@Put ( "master/{id}" )
async updateEmpMaster (
@Path ( ) id : string ,
@Body ( )
requestBody : CreateEmployeePosMaster ,
@Request ( ) request : { user : Record < string , any > } ,
) {
const posMaster = await this . employeePosMasterRepository . findOne ( { where : { id : id } } ) ;
if ( ! posMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลอัตรากำลัง" ) ;
}
posMaster . posMasterNo = requestBody . posMasterNo ;
posMaster . posMasterNoPrefix = requestBody . posMasterNoPrefix ;
posMaster . posMasterNoSuffix = requestBody . posMasterNoSuffix ;
posMaster . orgRootId = null ;
posMaster . orgChild1Id = null ;
posMaster . orgChild2Id = null ;
posMaster . orgChild3Id = null ;
posMaster . orgChild4Id = null ;
let orgRoot : any = null ;
let SName : any = null ;
if ( requestBody . orgRootId != null )
orgRoot = await this . orgRootRepository . findOne ( {
where : { id : requestBody.orgRootId } ,
} ) ;
if ( ! orgRoot ) {
let orgChild1 : any = null ;
if ( requestBody . orgChild1Id != null )
orgChild1 = await this . child1Repository . findOne ( {
where : { id : requestBody.orgChild1Id } ,
} ) ;
if ( ! orgChild1 ) {
let orgChild2 : any = null ;
if ( requestBody . orgChild2Id != null )
orgChild2 = await this . child2Repository . findOne ( {
where : { id : requestBody.orgChild2Id } ,
} ) ;
if ( ! orgChild2 ) {
let orgChild3 : any = null ;
if ( requestBody . orgChild3Id != null )
orgChild3 = await this . child3Repository . findOne ( {
where : { id : requestBody.orgChild3Id } ,
} ) ;
if ( ! orgChild3 ) {
let orgChild4 : any = null ;
if ( requestBody . orgChild4Id != null )
orgChild4 = await this . child4Repository . findOne ( {
where : { id : requestBody.orgChild4Id } ,
} ) ;
if ( ! orgChild4 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลโครงสร้าง" ) ;
} else {
posMaster . orgRootId = orgChild4 . orgRootId ;
posMaster . orgChild1Id = orgChild4 . orgChild1Id ;
posMaster . orgChild2Id = orgChild4 . orgChild2Id ;
posMaster . orgChild3Id = orgChild4 . orgChild3Id ;
posMaster . orgChild4Id = orgChild4 . id ;
posMaster . orgRevisionId = orgChild4 . orgRevisionId ;
SName = orgChild4 . orgChild4ShortName ;
}
} else {
posMaster . orgRootId = orgChild3 . orgRootId ;
posMaster . orgChild1Id = orgChild3 . orgChild1Id ;
posMaster . orgChild2Id = orgChild3 . orgChild2Id ;
posMaster . orgChild3Id = orgChild3 . id ;
posMaster . orgRevisionId = orgChild3 . orgRevisionId ;
SName = orgChild3 . orgChild3ShortName ;
}
} else {
posMaster . orgRootId = orgChild2 . orgRootId ;
posMaster . orgChild1Id = orgChild2 . orgChild1Id ;
posMaster . orgChild2Id = orgChild2 . id ;
posMaster . orgRevisionId = orgChild2 . orgRevisionId ;
SName = orgChild2 . orgChild2ShortName ;
}
} else {
posMaster . orgRootId = orgChild1 . orgRootId ;
posMaster . orgChild1Id = orgChild1 . id ;
posMaster . orgRevisionId = orgChild1 . orgRevisionId ;
SName = orgChild1 . orgChild1ShortName ;
}
} else {
posMaster . orgRootId = orgRoot . id ;
posMaster . orgRevisionId = orgRoot . orgRevisionId ;
SName = orgRoot . orgRootShortName ;
}
const chk_SName0 = await this . employeePosMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgRoot : { orgRootShortName : SName } ,
orgChild1Id : IsNull ( ) ,
posMasterNo : requestBody.posMasterNo ,
id : Not ( posMaster . id ) ,
} ,
relations : [ "orgRoot" ] ,
} ) ;
if ( chk_SName0 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
const chk_SName1 = await this . employeePosMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgChild1 : { orgChild1ShortName : SName } ,
orgChild2Id : IsNull ( ) ,
posMasterNo : requestBody.posMasterNo ,
id : Not ( posMaster . id ) ,
} ,
relations : [ "orgChild1" ] ,
} ) ;
if ( chk_SName1 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
const chk_SName2 = await this . employeePosMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgChild2 : { orgChild2ShortName : SName } ,
orgChild3Id : IsNull ( ) ,
posMasterNo : requestBody.posMasterNo ,
id : Not ( posMaster . id ) ,
} ,
relations : [ "orgChild2" ] ,
} ) ;
if ( chk_SName2 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
const chk_SName3 = await this . employeePosMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgChild3 : { orgChild3ShortName : SName } ,
orgChild4Id : IsNull ( ) ,
posMasterNo : requestBody.posMasterNo ,
id : Not ( posMaster . id ) ,
} ,
relations : [ "orgChild3" ] ,
} ) ;
if ( chk_SName3 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
const chk_SName4 = await this . employeePosMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgChild4 : { orgChild4ShortName : SName } ,
posMasterNo : requestBody.posMasterNo ,
id : Not ( posMaster . id ) ,
} ,
relations : [ "orgChild4" ] ,
} ) ;
if ( chk_SName4 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
posMaster . createdUserId = request . user . sub ; //สงสัยว่าทำให้ bug แก้ไขไม่ได้
posMaster . createdFullName = request . user . name ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
await this . employeePosMasterRepository . save ( posMaster ) ;
await this . employeePositionRepository . delete ( { posMasterId : posMaster.id } ) ;
await Promise . all (
requestBody . positions . map ( async ( x : any ) = > {
const position = Object . assign ( new EmployeePosition ( ) ) ;
position . positionName = x . posDictName ;
position . posTypeId = x . posTypeId == "" ? null : x . posTypeId ;
position . posLevelId = x . posLevelId == "" ? null : x . posLevelId ;
position . positionIsSelected = false ;
position . posMasterId = posMaster . id ;
position . createdUserId = request . user . sub ;
position . createdFullName = request . user . name ;
position . lastUpdateUserId = request . user . sub ;
position . lastUpdateFullName = request . user . name ;
await this . employeePositionRepository . save ( position ) ;
} ) ,
) ;
return new HttpSuccess ( posMaster . id ) ;
}
/ * *
* API ร า ย ล ะ เ อ ี ย ด อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_ - ร า ย ล ะ เ อ ี ย ด อ ั ต ร า ก ำ ล ั ง ( ADMIN )
*
* /
// @Get("position/{id}")
// async detailEmpPosition(@Path() id: string) {
// const posMaster = await this.employeePosMasterRepository.findOne({
// where: { id },
// });
// if (!posMaster) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
// }
// const positions = await this.employeePositionRepository.find({
// where: { posMasterId: posMaster.id },
// relations: ["posType", "posLevel"],
// order: { lastUpdatedAt: "ASC" },
// });
// const formattedData = {
// id: posMaster.id,
// posMasterNoPrefix: posMaster.posMasterNoPrefix,
// posMasterNo: posMaster.posMasterNo,
// posMasterNoSuffix: posMaster.posMasterNoSuffix,
// positions: positions.map((position) => ({
// id: position.id,
// positionName: position.positionName,
// posTypeId: position.posTypeId,
// posTypeName: position.posType == null ? null : position.posType.posTypeName,
// posLevelId: position.posLevelId,
// posLevelName: position.posLevel == null ? null : position.posLevel.posLevelName,
// positionIsSelected: position.positionIsSelected,
// })),
// };
// return new HttpSuccess(formattedData);
// }
/ * *
* API ล บ อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_ - ล บ อ ั ต ร า ก ำ ล ั ง ( ADMIN )
*
* @param { string } id Id ต ำ แ ห น ่ ง
* /
@Delete ( "master/{id}" )
async deleteEmpPosMaster ( @Path ( ) id : string ) {
const delPosMaster = await this . employeePosMasterRepository . findOne ( {
where : { id } ,
} ) ;
if ( ! delPosMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งในสายงานนี้" ) ;
}
await this . employeePositionRepository . delete ( { posMasterId : id } ) ;
await this . employeePosMasterRepository . delete ( { id } ) ;
return new HttpSuccess ( ) ;
}
/ * *
* API ร า ย ก า ร อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_ - ร า ย ก า ร อ ั ต ร า ก ำ ล ั ง ( ADMIN )
*
* /
@Post ( "master/list" )
async listEmp (
@Body ( )
body : {
id : string ;
revisionId : string ;
type : number ;
isAll : boolean ;
page : number ;
pageSize : number ;
keyword? : string ;
} ,
) {
let typeCondition : any = { } ;
let checkChildConditions : any = { } ;
let keywordAsInt : any ;
let searchShortName = "" ;
let searchShortName0 = ` CONCAT(orgRoot.orgRootShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) ` ;
let searchShortName1 = ` CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) ` ;
let searchShortName2 = ` CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) ` ;
let searchShortName3 = ` CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) ` ;
let searchShortName4 = ` CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) ` ;
if ( body . type === 0 ) {
typeCondition = {
orgRootId : body.id ,
} ;
if ( ! body . isAll ) {
checkChildConditions = {
orgChild1Id : IsNull ( ) ,
} ;
searchShortName = ` CONCAT(orgRoot.orgRootShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
} else {
}
} else if ( body . type === 1 ) {
typeCondition = {
orgChild1Id : body.id ,
} ;
if ( ! body . isAll ) {
checkChildConditions = {
orgChild2Id : IsNull ( ) ,
} ;
searchShortName = ` CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
} else {
}
} else if ( body . type === 2 ) {
typeCondition = {
orgChild2Id : body.id ,
} ;
if ( ! body . isAll ) {
checkChildConditions = {
orgChild3Id : IsNull ( ) ,
} ;
searchShortName = ` CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
} else {
}
} else if ( body . type === 3 ) {
typeCondition = {
orgChild3Id : body.id ,
} ;
if ( ! body . isAll ) {
checkChildConditions = {
orgChild4Id : IsNull ( ) ,
} ;
searchShortName = ` CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
} else {
}
} else if ( body . type === 4 ) {
typeCondition = {
orgChild4Id : body.id ,
} ;
searchShortName = ` CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
}
let findPosition : any ;
let masterId = new Array ( ) ;
if ( body . keyword != null && body . keyword != "" ) {
const findTypes : EmployeePosType [ ] = await this . employeePosTypeRepository . find ( {
where : { posTypeName : Like ( ` % ${ body . keyword } % ` ) } ,
select : [ "id" ] ,
} ) ;
findPosition = await this . employeePositionRepository . find ( {
where : { posTypeId : In ( findTypes . map ( ( x ) = > x . id ) ) } ,
select : [ "posMasterId" ] ,
} ) ;
masterId = masterId . concat ( findPosition . map ( ( position : any ) = > position . posMasterId ) ) ;
// const findLevel: EmployeePosLevel[] = await this.employeePosLevelRepository.find({
// where: { posLevelName: Like(`%${body.keyword}%`) },
// select: ["id"],
// });
// findPosition = await this.employeePositionRepository.find({
// where: { posLevelId: In(findLevel.map((x) => x.id)) },
// select: ["posMasterId"],
// });
// masterId = masterId.concat(findPosition.map((position: any) => position.posMasterId));
findPosition = await this . employeePositionRepository . find ( {
where : { positionName : Like ( ` % ${ body . keyword } % ` ) } ,
select : [ "posMasterId" ] ,
} ) ;
masterId = masterId . concat ( findPosition . map ( ( position : any ) = > position . posMasterId ) ) ;
keywordAsInt = body . keyword == null ? null : parseInt ( body . keyword , 10 ) ;
if ( isNaN ( keywordAsInt ) ) {
keywordAsInt = "P@ssw0rd!z" ;
}
masterId = [ . . . new Set ( masterId ) ] ;
}
const revisionCondition = {
orgRevisionId : body.revisionId ,
} ;
const conditions = [
{
. . . checkChildConditions ,
. . . typeCondition ,
. . . revisionCondition ,
. . . ( body . keyword &&
( masterId . length > 0
? { id : In ( masterId ) }
: { posMasterNo : Like ( ` % ${ body . keyword } % ` ) } ) ) ,
} ,
] ;
const [ posMaster , total ] = await AppDataSource . getRepository ( EmployeePosMaster )
. createQueryBuilder ( "posMaster" )
. leftJoinAndSelect ( "posMaster.orgRoot" , "orgRoot" )
. leftJoinAndSelect ( "posMaster.orgChild1" , "orgChild1" )
. leftJoinAndSelect ( "posMaster.orgChild2" , "orgChild2" )
. leftJoinAndSelect ( "posMaster.orgChild3" , "orgChild3" )
. leftJoinAndSelect ( "posMaster.orgChild4" , "orgChild4" )
. leftJoinAndSelect ( "posMaster.current_holder" , "current_holder" )
. leftJoinAndSelect ( "posMaster.next_holder" , "next_holder" )
. leftJoinAndSelect ( "posMaster.orgRevision" , "orgRevision" )
. where ( conditions )
. orWhere (
new Brackets ( ( qb ) = > {
qb . andWhere (
body . keyword != null && body . keyword != ""
? body . isAll == false
? searchShortName
: ` CASE WHEN posMaster.orgChild1 is null THEN ${ searchShortName0 } WHEN posMaster.orgChild2 is null THEN ${ searchShortName1 } WHEN posMaster.orgChild3 is null THEN ${ searchShortName2 } WHEN posMaster.orgChild4 is null THEN ${ searchShortName3 } ELSE ${ searchShortName4 } END LIKE '% ${ body . keyword } %' `
: "1=1" ,
)
. andWhere ( checkChildConditions )
. andWhere ( typeCondition )
. andWhere ( revisionCondition ) ;
} ) ,
)
. orWhere (
new Brackets ( ( qb ) = > {
qb . andWhere (
body . keyword != null && body . keyword != ""
? ` CONCAT(current_holder.prefix, current_holder.firstName," ",current_holder.lastName) like '% ${ body . keyword } %' `
: "1=1" ,
{
keyword : ` % ${ body . keyword } % ` ,
} ,
)
. andWhere ( checkChildConditions )
. andWhere ( typeCondition )
. andWhere ( revisionCondition ) ;
} ) ,
)
. orWhere (
new Brackets ( ( qb ) = > {
qb . andWhere (
body . keyword != null && body . keyword != ""
? ` CASE WHEN orgRevision.orgRevisionIsDraft = true THEN CONCAT(next_holder.prefix, next_holder.firstName,' ', next_holder.lastName) ELSE CONCAT(current_holder.prefix, current_holder.firstName,' ' , current_holder.lastName) END LIKE '% ${ body . keyword } %' `
: "1=1" ,
{
keyword : ` % ${ body . keyword } % ` ,
} ,
)
. andWhere ( checkChildConditions )
. andWhere ( typeCondition )
. andWhere ( revisionCondition ) ;
} ) ,
)
. orderBy ( "posMaster.posMasterOrder" , "ASC" )
. skip ( ( body . page - 1 ) * body . pageSize )
. take ( body . pageSize )
. getManyAndCount ( ) ;
const formattedData = await Promise . all (
posMaster . map ( async ( posMaster ) = > {
const positions = await this . employeePositionRepository . find ( {
where : {
posMasterId : posMaster.id ,
} ,
2024-03-14 21:53:19 +07:00
relations : [ "posLevel" , "posType" ] ,
2024-03-13 18:59:35 +07:00
} ) ;
let profile : any ;
const chkRevision = await this . orgRevisionRepository . findOne ( {
where : { id : posMaster.orgRevisionId } ,
} ) ;
if ( chkRevision ? . orgRevisionIsCurrent && ! chkRevision ? . orgRevisionIsDraft ) {
profile = await this . profileRepository . findOne ( {
where : { id : String ( posMaster . current_holderId ) } ,
} ) ;
} else if ( ! chkRevision ? . orgRevisionIsCurrent && chkRevision ? . orgRevisionIsDraft ) {
profile = await this . profileRepository . findOne ( {
where : { id : String ( posMaster . next_holderId ) } ,
} ) ;
}
const type = await this . employeePosTypeRepository . findOne ( {
where : { id : String ( profile ? . posTypeId ) } ,
} ) ;
const level = await this . employeePosLevelRepository . findOne ( {
where : { id : String ( profile ? . posLevelId ) } ,
} ) ;
let shortName = "" ;
if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id == null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null
) {
body . type = 0 ;
shortName = posMaster . orgRoot . orgRootShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null
) {
body . type = 1 ;
shortName = posMaster . orgChild1 . orgChild1ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id == null
) {
body . type = 2 ;
shortName = posMaster . orgChild2 . orgChild2ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id !== null
) {
body . type = 3 ;
shortName = posMaster . orgChild3 . orgChild3ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id !== null
) {
body . type = 4 ;
shortName = posMaster . orgChild4 . orgChild4ShortName ;
}
return {
id : posMaster.id ,
orgRootId : posMaster.orgRootId ,
orgChild1Id : posMaster.orgChild1Id ,
orgChild2Id : posMaster.orgChild2Id ,
orgChild3Id : posMaster.orgChild3Id ,
orgChild4Id : posMaster.orgChild4Id ,
posMasterNoPrefix : posMaster.posMasterNoPrefix ,
posMasterNo : posMaster.posMasterNo ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ,
fullNameCurrentHolder :
posMaster . current_holder == null
? null
: ` ${ posMaster . current_holder . prefix } ${ posMaster . current_holder . firstName } ${ posMaster . current_holder . lastName } ` ,
fullNameNextHolder :
posMaster . next_holder == null
? null
: ` ${ posMaster . next_holder . prefix } ${ posMaster . next_holder . firstName } ${ posMaster . next_holder . lastName } ` ,
orgShortname : shortName ,
isSit : posMaster.isSit ,
profilePosition : profile == null || profile . position == null ? null : profile . position ,
profilePostype : type == null || type . posTypeName == null ? null : type . posTypeName ,
profilePoslevel : level == null || level . posLevelName == null ? null : level . posLevelName ,
positions : positions.map ( ( position ) = > ( {
id : position.id ,
positionName : position.positionName ,
posTypeId : position.posTypeId ,
posTypeName : position.posType == null ? null : position . posType . posTypeName ,
posLevelId : position.posLevelId ,
posLevelName : position.posLevel == null ? null : position . posLevel . posLevelName ,
positionIsSelected : position.positionIsSelected ,
} ) ) ,
} ;
} ) ,
) ;
return new HttpSuccess ( { data : formattedData , total } ) ;
}
/ * *
* API จ ั ด ล ำ ด ั บ ต ำ แ ห น ่ ง
*
* @summary ORG_ - จ ั ด ล ำ ด ั บ ต ำ แ ห น ่ ง ( ADMIN )
*
* /
@Post ( "sort" )
async SortEmp ( @Body ( ) requestBody : { id : string ; type : number ; sortId : string [ ] } ) {
switch ( requestBody . type ) {
case 0 : {
const rootId = await this . employeePosMasterRepository . findOne ( {
where : { orgRootId : requestBody.id } ,
} ) ;
if ( ! rootId ? . id ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found rootId: " + requestBody . id ) ;
}
const listPosMasterId_0 = await this . employeePosMasterRepository . find ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) ,
orgChild2Id : IsNull ( ) ,
orgChild3Id : IsNull ( ) ,
orgChild4Id : IsNull ( ) ,
} ,
select : [ "id" , "posMasterOrder" ] ,
} ) ;
if ( ! listPosMasterId_0 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found masterId type 0." ) ;
}
const sortData_0 = listPosMasterId_0 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
await this . employeePosMasterRepository . save ( sortData_0 ) ;
break ;
}
case 1 : {
const child1Id = await this . employeePosMasterRepository . findOne ( {
where : { orgChild1Id : requestBody.id } ,
} ) ;
if ( ! child1Id ? . id ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found child1Id: " + requestBody . id ) ;
}
const listPosMasterId_1 = await this . employeePosMasterRepository . find ( {
where : {
orgRootId : Not ( IsNull ( ) ) ,
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) ,
orgChild3Id : IsNull ( ) ,
orgChild4Id : IsNull ( ) ,
} ,
select : [ "id" , "posMasterOrder" ] ,
} ) ;
if ( ! listPosMasterId_1 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found masterId type 1." ) ;
}
const sortData_1 = listPosMasterId_1 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
await this . employeePosMasterRepository . save ( sortData_1 ) ;
break ;
}
case 2 : {
const child2Id = await this . employeePosMasterRepository . findOne ( {
where : { orgChild2Id : requestBody.id } ,
} ) ;
if ( ! child2Id ? . id ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found child2Id: " + requestBody . id ) ;
}
const listPosMasterId_2 = await this . employeePosMasterRepository . find ( {
where : {
orgRootId : Not ( IsNull ( ) ) ,
orgChild1Id : Not ( IsNull ( ) ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) ,
orgChild4Id : IsNull ( ) ,
} ,
select : [ "id" , "posMasterOrder" ] ,
} ) ;
if ( ! listPosMasterId_2 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found masterId type 2." ) ;
}
const sortData_2 = listPosMasterId_2 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
await this . employeePosMasterRepository . save ( sortData_2 ) ;
break ;
}
case 3 : {
const child3Id = await this . employeePosMasterRepository . findOne ( {
where : { orgChild3Id : requestBody.id } ,
} ) ;
if ( ! child3Id ? . id ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found chil3Id: " + requestBody . id ) ;
}
const listPosMasterId_3 = await this . employeePosMasterRepository . find ( {
where : {
orgRootId : Not ( IsNull ( ) ) ,
orgChild1Id : Not ( IsNull ( ) ) ,
orgChild2Id : Not ( IsNull ( ) ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) ,
} ,
select : [ "id" , "posMasterOrder" ] ,
} ) ;
if ( ! listPosMasterId_3 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found masterId type 3." ) ;
}
const sortData_3 = listPosMasterId_3 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
await this . employeePosMasterRepository . save ( sortData_3 ) ;
break ;
}
case 4 : {
const child4Id = await this . employeePosMasterRepository . findOne ( {
where : { orgChild4Id : requestBody.id } ,
} ) ;
if ( ! child4Id ? . id ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found child4Id: " + requestBody . id ) ;
}
const listPosMasterId_4 = await this . employeePosMasterRepository . find ( {
where : {
orgRootId : Not ( IsNull ( ) ) ,
orgChild1Id : Not ( IsNull ( ) ) ,
orgChild2Id : Not ( IsNull ( ) ) ,
orgChild3Id : Not ( IsNull ( ) ) ,
orgChild4Id : requestBody.id ,
} ,
select : [ "id" , "posMasterOrder" ] ,
} ) ;
if ( ! listPosMasterId_4 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found masterId type 4." ) ;
}
const sortData_4 = listPosMasterId_4 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
await this . employeePosMasterRepository . save ( sortData_4 ) ;
break ;
}
default :
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found type: " + requestBody . type ) ;
}
return new HttpSuccess ( ) ;
}
/ * *
* API ด ู ป ร ะ ว ั ต ิ อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_ - ด ู ป ร ะ ว ั ต ิ อ ั ต ร า ก ำ ล ั ง ( ADMIN )
*
* @param { string } id Id อ ั ต ร า ก ำ ล ั ง
* /
@Get ( "history/{id}" )
async getEmpHistoryPosMater ( @Path ( ) id : string ) {
const posMaster = await this . employeePosMasterRepository . findOne ( {
where : { id } ,
} ) ;
if ( ! posMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
}
const posMasters = await this . employeePosMasterRepository . find ( {
where : {
ancestorDNA :
posMaster . ancestorDNA == null || posMaster . ancestorDNA == ""
? "123"
: posMaster . ancestorDNA ,
} ,
order : { lastUpdatedAt : "DESC" } ,
relations : [ "orgRoot" , "orgChild1" , "orgChild2" , "orgChild3" , "orgChild4" ] ,
} ) ;
const _data = posMasters . map ( ( item ) = > ( {
id : item.id ,
orgShortName :
item . orgRoot == null
? null
: item . orgChild1 == null
? item . orgRoot . orgRootShortName
: item . orgChild2 == null
? item . orgChild1 . orgChild1ShortName
: item . orgChild3 == null
? item . orgChild2 . orgChild2ShortName
: item . orgChild4 == null
? item . orgChild3 . orgChild3ShortName
: item . orgChild4 . orgChild4ShortName ,
lastUpdatedAt : item.lastUpdatedAt ? item.lastUpdatedAt : null ,
posMasterNoPrefix : item.posMasterNoPrefix ? item.posMasterNoPrefix : null ,
posMasterNo : item.posMasterNo ? item.posMasterNo : null ,
posMasterNoSuffix : item.posMasterNoSuffix ? item.posMasterNoSuffix : null ,
} ) ) ;
return new HttpSuccess ( _data ) ;
}
/ * *
* API ย ้ า ย อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_ - ย ้ า ย อ ั ต ร า ก ำ ล ั ง ( ADMIN )
*
* /
@Post ( "move" )
async moveEmpPosMaster (
@Body ( ) requestBody : { id : string ; type : number ; positionMaster : string [ ] } ,
@Request ( ) request : { user : Record < string , any > } ,
) {
const posMasters = await this . employeePosMasterRepository . find ( {
where : { id : In ( requestBody . positionMaster ) } ,
} ) ;
const type0LastPosMasterNo =
requestBody . type == 0
? await this . employeePosMasterRepository . find ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) ,
} ,
} )
: [ ] ;
const type1LastPosMasterNo =
requestBody . type == 1
? await this . employeePosMasterRepository . find ( {
where : {
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) ,
} ,
} )
: [ ] ;
const type2LastPosMasterNo =
requestBody . type == 2
? await this . employeePosMasterRepository . find ( {
where : {
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) ,
} ,
} )
: [ ] ;
const type3LastPosMasterNo =
requestBody . type == 3
? await this . employeePosMasterRepository . find ( {
where : {
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) ,
} ,
} )
: [ ] ;
const type4LastPosMasterNo =
requestBody . type == 4
? await this . employeePosMasterRepository . find ( {
where : {
orgChild4Id : requestBody.id ,
} ,
} )
: [ ] ;
const allLastPosMasterNo = [
. . . type0LastPosMasterNo ,
. . . type1LastPosMasterNo ,
. . . type2LastPosMasterNo ,
. . . type3LastPosMasterNo ,
. . . type4LastPosMasterNo ,
] ;
let maxPosMasterNo = Math . max ( . . . allLastPosMasterNo . map ( ( pos ) = > pos . posMasterNo ) , 0 ) ;
let maxPosMasterOrder = Math . max ( . . . allLastPosMasterNo . map ( ( pos ) = > pos . posMasterOrder ) , 0 ) ;
posMasters . forEach ( async ( posMaster : any ) = > {
let change = true ;
if ( requestBody . type == 0 ) {
const org = await this . orgRootRepository . findOne ( {
where : { id : requestBody.id } ,
} ) ;
if ( org != null ) {
if (
posMaster . orgRootId == org . id &&
posMaster . orgChild1Id == null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null &&
posMaster . orgChild4Id == null
)
change = false ;
posMaster . orgRootId = org . id ;
posMaster . orgRevisionId = org . orgRevisionId ;
posMaster . orgChild1Id = null ;
posMaster . orgChild2Id = null ;
posMaster . orgChild3Id = null ;
posMaster . orgChild4Id = null ;
}
}
if ( requestBody . type == 1 ) {
const org = await this . child1Repository . findOne ( {
where : { id : requestBody.id } ,
} ) ;
if ( org != null ) {
if (
posMaster . orgChild1Id == org . id &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null &&
posMaster . orgChild4Id == null
)
change = false ;
posMaster . orgRootId = org . orgRootId ;
posMaster . orgChild1Id = org . id ;
posMaster . orgRevisionId = org . orgRevisionId ;
posMaster . orgChild2Id = null ;
posMaster . orgChild3Id = null ;
posMaster . orgChild4Id = null ;
}
}
if ( requestBody . type == 2 ) {
const org = await this . child2Repository . findOne ( {
where : { id : requestBody.id } ,
} ) ;
if ( org != null ) {
if (
posMaster . orgChild2Id == org . id &&
posMaster . orgChild3Id == null &&
posMaster . orgChild4Id == null
)
change = false ;
posMaster . orgRootId = org . orgRootId ;
posMaster . orgChild1Id = org . orgChild1Id ;
posMaster . orgChild2Id = org . id ;
posMaster . orgRevisionId = org . orgRevisionId ;
posMaster . orgChild3Id = null ;
posMaster . orgChild4Id = null ;
}
}
if ( requestBody . type == 3 ) {
const org = await this . child3Repository . findOne ( {
where : { id : requestBody.id } ,
} ) ;
if ( org != null ) {
if ( posMaster . orgChild3Id == org . id && posMaster . orgChild4Id == null ) change = false ;
posMaster . orgRootId = org . orgRootId ;
posMaster . orgChild1Id = org . orgChild1Id ;
posMaster . orgChild2Id = org . orgChild2Id ;
posMaster . orgChild3Id = org . id ;
posMaster . orgRevisionId = org . orgRevisionId ;
posMaster . orgChild4Id = null ;
}
}
if ( requestBody . type == 4 ) {
const org = await this . child4Repository . findOne ( {
where : { id : requestBody.id } ,
} ) ;
if ( org != null ) {
if ( posMaster . orgChild4Id == org . id ) change = false ;
posMaster . orgRootId = org . orgRootId ;
posMaster . orgChild1Id = org . orgChild1Id ;
posMaster . orgChild2Id = org . orgChild2Id ;
posMaster . orgChild3Id = org . orgChild3Id ;
posMaster . orgChild4Id = org . id ;
posMaster . orgRevisionId = org . orgRevisionId ;
}
}
if ( change == true ) {
posMaster . posMasterNo = maxPosMasterNo += 1 ;
posMaster . posMasterOrder = maxPosMasterOrder += 1 ;
posMaster . createdUserId = request . user . sub ;
posMaster . createdFullName = request . user . name ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
await this . employeePosMasterRepository . save ( posMaster ) ;
}
} ) ;
return new HttpSuccess ( ) ;
}
/ * *
* API ต ำ แ ห น ่ ง ท ั ้ ง ห ม ด
*
* @summary ORG_ - ต ำ แ ห น ่ ง ท ั ้ ง ห ม ด ( ADMIN )
*
* /
@Post ( "summary" )
async PositionEmpSummary ( @Body ( ) requestBody : { id : string ; type : number ; isNode : boolean } ) {
let summary : any ;
let totalPosition : any ;
let totalPositionCurrentUse : any ;
let totalPositionCurrentVacant : any ;
let totalPositionNextUse : any ;
let totalPositionNextVacant : any ;
if ( requestBody . isNode === true ) {
switch ( requestBody . type ) {
case 0 : {
totalPosition = await this . employeePosMasterRepository . count ( {
where : { orgRootId : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
case 1 : {
totalPosition = await this . employeePosMasterRepository . count ( {
where : { orgChild1Id : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . employeePosMasterRepository . count ( {
where : {
orgChild1Id : requestBody.id ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . employeePosMasterRepository . count ( {
where : {
orgChild1Id : requestBody.id ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . employeePosMasterRepository . count ( {
where : {
orgChild1Id : requestBody.id ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . employeePosMasterRepository . count ( {
where : {
orgChild1Id : requestBody.id ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
case 2 : {
totalPosition = await this . employeePosMasterRepository . count ( {
where : { orgChild2Id : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . employeePosMasterRepository . count ( {
where : {
orgChild2Id : requestBody.id ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . employeePosMasterRepository . count ( {
where : {
orgChild2Id : requestBody.id ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . employeePosMasterRepository . count ( {
where : {
orgChild2Id : requestBody.id ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . employeePosMasterRepository . count ( {
where : {
orgChild2Id : requestBody.id ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
case 3 : {
totalPosition = await this . employeePosMasterRepository . count ( {
where : { orgChild3Id : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . employeePosMasterRepository . count ( {
where : {
orgChild3Id : requestBody.id ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . employeePosMasterRepository . count ( {
where : {
orgChild3Id : requestBody.id ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . employeePosMasterRepository . count ( {
where : {
orgChild3Id : requestBody.id ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . employeePosMasterRepository . count ( {
where : {
orgChild3Id : requestBody.id ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
case 4 : {
totalPosition = await this . employeePosMasterRepository . count ( {
where : { orgChild4Id : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . employeePosMasterRepository . count ( {
where : {
orgChild4Id : requestBody.id ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . employeePosMasterRepository . count ( {
where : {
orgChild4Id : requestBody.id ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . employeePosMasterRepository . count ( {
where : {
orgChild4Id : requestBody.id ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . employeePosMasterRepository . count ( {
where : {
orgChild4Id : requestBody.id ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
default :
break ;
}
} else {
switch ( requestBody . type ) {
case 0 : {
totalPosition = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionCurrentUse = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
case 1 : {
totalPosition = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionCurrentUse = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
case 2 : {
totalPosition = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionCurrentUse = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
case 3 : {
totalPosition = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionCurrentUse = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) || "" ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) || "" ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
case 4 : {
totalPosition = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild4Id : requestBody.id ,
} ,
} ) ;
totalPositionCurrentUse = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild4Id : requestBody.id ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild4Id : requestBody.id ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild4Id : requestBody.id ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . employeePosMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild4Id : requestBody.id ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
default :
break ;
}
}
summary = {
totalPosition : totalPosition ,
totalPositionCurrentUse : totalPositionCurrentUse ,
totalPositionCurrentVacant : totalPositionCurrentVacant ,
totalPositionNextUse : totalPositionNextUse ,
totalPositionNextVacant : totalPositionNextVacant ,
} ;
return new HttpSuccess ( summary ) ;
}
/ * *
* API ส ร ้ า ง ค น ค ร อ ง ต ำ แ ห น ่ ง
*
* @summary ORG_ - ส ร ้ า ง ค น ค ร อ ง ต ำ แ ห น ่ ง ( ADMIN )
*
* /
@Post ( "profile" )
async createEmpHolder (
@Body ( ) requestBody : { posMaster : string ; position : string ; profileId : string ; isSit : boolean } ,
) {
const dataMaster = await this . employeePosMasterRepository . findOne ( {
where : { id : requestBody.posMaster } ,
relations : [ "positions" ] ,
} ) ;
if ( ! dataMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
}
dataMaster . positions . forEach ( async ( position ) = > {
if ( position . id === requestBody . position ) {
position . positionIsSelected = true ;
} else {
position . positionIsSelected = false ;
}
await this . employeePositionRepository . save ( position ) ;
} ) ;
dataMaster . isSit = requestBody . isSit ;
2024-03-15 11:45:42 +07:00
dataMaster . current_holderId = requestBody . profileId ;
2024-03-19 12:24:29 +07:00
dataMaster . next_holderId = requestBody . profileId ;
2024-03-13 18:59:35 +07:00
await this . employeePosMasterRepository . save ( dataMaster ) ;
return new HttpSuccess ( ) ;
}
/ * *
* API ล บ ค น ค ร อ ง ต ำ แ ห น ่ ง
*
* @summary ORG_ - ล บ ค น ค ร อ ง ต ำ แ ห น ่ ง ( ADMIN )
*
* @param { string } id * Id posMaster
* /
@Post ( "profile/delete/{id}" )
async deleteEmpHolder ( @Path ( ) id : string ) {
const dataMaster = await this . employeePosMasterRepository . findOne ( {
where : { id : id } ,
relations : [ "positions" ] ,
} ) ;
if ( ! dataMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
}
await this . employeePosMasterRepository . update ( id , {
isSit : false ,
next_holderId : null ,
2024-03-15 14:32:08 +07:00
current_holderId : null ,
2024-03-13 18:59:35 +07:00
} ) ;
dataMaster . positions . forEach ( async ( position ) = > {
await this . employeePositionRepository . update ( position . id , {
positionIsSelected : false ,
} ) ;
} ) ;
return new HttpSuccess ( ) ;
}
/ * *
* API ส ื บ ท อ ด ต ำ แ ห น ่ ง
*
* @summary ORG_ - ส ื บ ท อ ด ต ำ แ ห น ่ ง ( ADMIN )
*
* /
@Post ( "dna" )
async dnaEmp ( @Body ( ) requestBody : { draftPositionId : string ; publishPositionId : string } ) {
const findDraft = await this . orgRevisionRepository . findOne ( {
where : {
orgRevisionIsDraft : true ,
} ,
} ) ;
if ( ! findDraft ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลโครงสร้างที่เผยแพร่" ) ;
}
const dataPublish = await this . employeePosMasterRepository . findOne ( {
where : {
id : requestBody.publishPositionId ,
} ,
} ) ;
if ( ! dataPublish ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
}
const dataDraft = await this . employeePosMasterRepository . findOne ( {
where : {
id : requestBody.draftPositionId ,
} ,
} ) ;
if ( ! dataDraft ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
}
await this . employeePosMasterRepository . update (
{ orgRevisionId : findDraft.id , ancestorDNA : dataPublish.ancestorDNA } ,
{ ancestorDNA : "" } ,
) ;
if ( dataPublish . ancestorDNA == null || dataPublish . ancestorDNA == "" )
dataPublish . ancestorDNA = dataPublish . id ;
dataDraft . ancestorDNA = dataPublish . ancestorDNA ;
await this . employeePosMasterRepository . save ( dataDraft ) ;
await this . employeePosMasterRepository . save ( dataPublish ) ;
return new HttpSuccess ( ) ;
}
2024-03-12 17:53:54 +07:00
}