2024-01-30 15:10:11 +07:00
import {
Controller ,
Get ,
Post ,
Put ,
Delete ,
Route ,
Security ,
Tags ,
Body ,
Path ,
Request ,
Example ,
Response ,
2024-01-30 18:01:58 +07:00
Query ,
2024-01-30 15:10:11 +07:00
} 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" ;
2024-03-18 11:11:14 +07:00
import { CreatePosDict , CreatePosDictExe , PosDict , UpdatePosDict } from "../entities/PosDict" ;
2024-01-30 16:02:34 +07:00
import HttpError from "../interfaces/http-error" ;
2024-10-18 11:33:04 +07:00
import { In , IsNull , Like , Not , Brackets } from "typeorm" ;
2024-01-31 11:00:51 +07:00
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" ;
2024-02-15 09:39:13 +07:00
import { Profile } from "../entities/Profile" ;
2024-05-17 11:16:39 +07:00
import { EmployeePosMaster } from "../entities/EmployeePosMaster" ;
2024-05-23 15:37:13 +07:00
import { EmployeePosType } from "../entities/EmployeePosType" ;
import { EmployeePosLevel } from "../entities/EmployeePosLevel" ;
2024-06-14 12:28:09 +07:00
import { AuthRole } from "../entities/AuthRole" ;
2024-08-16 17:36:08 +07:00
import { RequestWithUser } from "../middlewares/user" ;
import permission from "../interfaces/permission" ;
2024-10-04 18:12:23 +07:00
import { setLogDataDiff } from "../interfaces/utils" ;
2024-10-07 17:00:54 +07:00
import { PosMasterAssign } from "../entities/PosMasterAssign" ;
import { Assign } from "../entities/Assign" ;
2024-11-14 18:03:56 +07:00
import { ProfileEmployee } from "../entities/ProfileEmployee" ;
2024-01-30 15:10:11 +07:00
@Route ( "api/v1/org/pos" )
@Tags ( "Position" )
2024-02-20 15:21:26 +07:00
@Security ( "bearerAuth" )
2024-01-30 15:10:11 +07:00
@Response (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง" ,
)
export class PositionController extends Controller {
private posExecutiveRepository = AppDataSource . getRepository ( PosExecutive ) ;
2024-01-30 16:02:34 +07:00
private posTypeRepository = AppDataSource . getRepository ( PosType ) ;
2024-01-30 15:10:11 +07:00
private posLevelRepository = AppDataSource . getRepository ( PosLevel ) ;
2024-05-23 15:37:13 +07:00
private posTypeEmployeeRepository = AppDataSource . getRepository ( EmployeePosType ) ;
private posLevelEmployeeRepository = AppDataSource . getRepository ( EmployeePosLevel ) ;
2024-01-30 16:47:39 +07:00
private posDictRepository = AppDataSource . getRepository ( PosDict ) ;
2024-01-31 11:00:51 +07:00
private posMasterRepository = AppDataSource . getRepository ( PosMaster ) ;
2024-11-14 18:03:56 +07:00
private employeePosMasterRepository = AppDataSource . getRepository ( EmployeePosMaster ) ;
2024-01-31 15:09:22 +07:00
private positionRepository = AppDataSource . getRepository ( Position ) ;
2024-02-15 09:39:13 +07:00
private profileRepository = AppDataSource . getRepository ( Profile ) ;
2024-11-14 18:03:56 +07:00
private profileEmployeeRepository = AppDataSource . getRepository ( ProfileEmployee ) ;
2024-01-31 11:00:51 +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-06-14 12:28:09 +07:00
private authRoleRepo = AppDataSource . getRepository ( AuthRole ) ;
2024-10-07 17:00:54 +07:00
private posMasterAssignRepo = AppDataSource . getRepository ( PosMasterAssign ) ;
private assignRepo = AppDataSource . getRepository ( Assign ) ;
2024-01-30 15:10:11 +07:00
2024-01-30 16:02:34 +07:00
/ * *
* API เ พ ิ ่ ม ต ำ แ ห น ่ ง
*
* @summary ORG_030 - เ พ ิ ่ ม ต ำ แ ห น ่ ง ( ADMIN ) # 33
*
* /
@Post ( "position" )
@Example ( [
{
positionName : "นักบริหาร" ,
positionField : "บริหาร" ,
posTypeId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
posLevelId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
posExecutiveId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
positionExecutiveField : "นักบริหาร" ,
2024-01-31 11:00:51 +07:00
positionArea : "บริหาร" ,
2024-01-30 16:02:34 +07:00
} ,
] )
2024-01-31 11:00:51 +07:00
async createPosition (
@Body ( )
requestBody : CreatePosDict ,
2024-08-16 17:36:08 +07:00
@Request ( ) request : RequestWithUser ,
2024-01-31 11:00:51 +07:00
) {
2024-08-23 17:23:27 +07:00
// await new permission().PermissionCreate(request, "SYS_ORG");
2024-01-30 16:47:39 +07:00
const posDict = Object . assign ( new PosDict ( ) , requestBody ) ;
if ( ! posDict ) {
2024-01-30 16:02:34 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล" ) ;
}
2024-01-31 11:00:51 +07:00
const checkPosTypeId = await this . posTypeRepository . findOne ( {
2024-02-07 14:01:39 +07:00
where : { id : posDict.posTypeId } ,
2024-01-31 11:00:51 +07:00
} ) ;
2024-01-30 16:02:34 +07:00
if ( ! checkPosTypeId ) {
2024-03-04 14:54:15 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลประเภทตำแหน่ง" ) ;
2024-01-30 16:02:34 +07:00
}
2024-01-31 11:00:51 +07:00
const checkPosLevelId = await this . posLevelRepository . findOne ( {
2024-02-07 14:01:39 +07:00
where : { id : posDict.posLevelId } ,
2024-01-31 11:00:51 +07:00
} ) ;
2024-01-30 16:02:34 +07:00
if ( ! checkPosLevelId ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล PosLevelId" ) ;
}
2024-02-21 16:18:32 +07:00
const _null : any = null ;
2024-02-07 14:01:39 +07:00
if ( posDict . posExecutiveId == "" ) {
2024-02-20 18:04:13 +07:00
posDict . posExecutiveId = _null ;
2024-02-07 12:43:49 +07:00
}
2024-02-07 12:01:12 +07:00
2024-02-07 14:01:39 +07:00
if ( posDict . posExecutiveId != null ) {
2024-02-07 12:01:12 +07:00
const checkPosExecutiveId = await this . posExecutiveRepository . findOne ( {
2024-02-07 14:01:39 +07:00
where : { id : posDict.posExecutiveId } ,
2024-02-07 12:01:12 +07:00
} ) ;
if ( ! checkPosExecutiveId ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล PosExecutiveId" ) ;
}
}
2024-01-30 16:02:34 +07:00
2024-02-15 10:25:10 +07:00
const rowRepeated = await this . posDictRepository . findOne ( {
2024-02-15 10:31:47 +07:00
where : {
posDictName : posDict.posDictName ,
posDictField : posDict.posDictField ,
2024-02-15 10:25:10 +07:00
posTypeId : posDict.posTypeId ,
posLevelId : posDict.posLevelId ,
posExecutiveId : String ( posDict . posExecutiveId ) ,
2024-02-15 10:31:47 +07:00
posDictExecutiveField : posDict.posDictExecutiveField ,
posDictArea : posDict.posDictArea ,
2024-02-20 17:12:28 +07:00
isSpecial : posDict.isSpecial ,
2024-02-15 10:25:10 +07:00
} ,
2024-02-15 09:41:27 +07:00
} ) ;
2024-02-15 10:25:10 +07:00
if ( rowRepeated ) {
2024-02-15 10:31:47 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ข้อมูล Row นี้มีอยู่ในระบบแล้ว" ) ;
2024-02-14 17:09:05 +07:00
}
2024-10-17 16:39:42 +07:00
const before = null ;
2024-02-28 11:31:01 +07:00
posDict . createdUserId = request . user . sub ;
posDict . createdFullName = request . user . name ;
posDict . lastUpdateUserId = request . user . sub ;
posDict . lastUpdateFullName = request . user . name ;
2024-08-30 21:02:14 +07:00
posDict . createdAt = new Date ( ) ;
posDict . lastUpdatedAt = new Date ( ) ;
2024-10-17 22:11:54 +07:00
await this . posDictRepository . save ( posDict , { data : request } ) ;
2024-10-17 16:39:42 +07:00
setLogDataDiff ( request , { before , after : posDict } ) ;
2024-02-28 11:31:01 +07:00
return new HttpSuccess ( posDict . id ) ;
2024-01-30 16:02:34 +07:00
}
2024-02-21 16:18:32 +07:00
2024-10-07 17:00:54 +07:00
/ * *
* API ม อ บ ห ม า ย
*
* @summary ม อ บ ห ม า ย
*
* /
@Post ( "assign" )
async createPositionMasterAssgin (
@Body ( )
requestBody : {
assignIds : string [ ] ;
posMasterId : string ;
} ,
@Request ( ) request : RequestWithUser ,
) {
const posMaster = await this . posMasterRepository . findOne ( {
where : { id : requestBody.posMasterId } ,
} ) ;
if ( ! posMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่ง" ) ;
}
await this . posMasterAssignRepo . delete ( { posMasterId : posMaster.id } ) ;
const assigns = await this . assignRepo . find ( {
where : { id : In ( requestBody . assignIds ) } ,
} ) ;
await Promise . all (
await assigns . map ( async ( x ) = > {
let _posMasterAssign = await this . posMasterAssignRepo . findOne ( {
where : { posMasterId : requestBody.posMasterId , assignId : x.id } ,
} ) ;
if ( _posMasterAssign == null ) {
const posMasterAssign = new PosMasterAssign ( ) ;
posMasterAssign . posMasterId = requestBody . posMasterId ;
posMasterAssign . assignId = x . id ;
posMasterAssign . createdUserId = request . user . sub ;
posMasterAssign . createdFullName = request . user . name ;
posMasterAssign . lastUpdateUserId = request . user . sub ;
posMasterAssign . lastUpdateFullName = request . user . name ;
posMasterAssign . createdAt = new Date ( ) ;
posMasterAssign . lastUpdatedAt = new Date ( ) ;
await this . posMasterAssignRepo . save ( posMasterAssign ) ;
}
} ) ,
) ;
return new HttpSuccess ( ) ;
}
/ * *
* API ล บ ม อ บ ห ม า ย
*
* @summary ล บ ม อ บ ห ม า ย
*
* @param { string } id Id posMaster
* /
@Delete ( "assign/{id}" )
async deletePositionMasterAssgin ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
const posMaster = await this . posMasterRepository . findOne ( {
where : { id : id } ,
} ) ;
if ( ! posMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่ง" ) ;
}
2024-10-17 16:39:42 +07:00
const posMasterAssigns = await this . posMasterAssignRepo . find ( {
where : { posMasterId : posMaster.id } ,
} ) ;
if ( posMasterAssigns . length > 0 ) {
2024-10-17 22:11:54 +07:00
await this . posMasterAssignRepo . remove ( posMasterAssigns , { data : request } ) ;
2024-10-17 16:39:42 +07:00
}
// await this.posMasterAssignRepo.delete({ posMasterId: posMaster.id });
2024-10-07 17:00:54 +07:00
return new HttpSuccess ( ) ;
}
2024-03-18 11:11:14 +07:00
/ * *
* API เ พ ิ ่ ม ต ำ แ ห น ่ ง
*
* @summary ORG_030 - เ พ ิ ่ ม ต ำ แ ห น ่ ง ( ADMIN ) # 33
*
* /
@Post ( "position/executive" )
@Example ( [
{
positionName : "นักบริหาร" ,
positionField : "บริหาร" ,
posTypeId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
posLevelId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
posExecutiveId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
positionExecutiveField : "นักบริหาร" ,
positionArea : "บริหาร" ,
} ,
] )
async createPositionNameExe (
@Body ( )
requestBody : CreatePosDictExe ,
2024-08-16 17:36:08 +07:00
@Request ( ) request : RequestWithUser ,
2024-03-18 11:11:14 +07:00
) {
2024-08-16 17:36:08 +07:00
await new permission ( ) . PermissionCreate ( request , "SYS_ORG" ) ;
2024-03-18 17:29:51 +07:00
// let posDict: PosDict;
let posDict : any = new PosDict ( ) ;
2024-03-18 14:53:24 +07:00
posDict . posDictName = requestBody . posDictName ;
posDict . posDictField = requestBody . posDictField ;
posDict . posTypeId = requestBody . posTypeId ;
posDict . posLevelId = requestBody . posLevelId ;
posDict . posDictExecutiveField = requestBody . posDictExecutiveField ;
posDict . posDictArea = requestBody . posDictArea ;
posDict . isSpecial = requestBody . isSpecial ;
2024-03-18 11:11:14 +07:00
const checkPosTypeId = await this . posTypeRepository . findOne ( {
where : { id : posDict.posTypeId } ,
} ) ;
if ( ! checkPosTypeId ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลประเภทตำแหน่ง" ) ;
}
const checkPosLevelId = await this . posLevelRepository . findOne ( {
where : { id : posDict.posLevelId } ,
} ) ;
if ( ! checkPosLevelId ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล PosLevelId" ) ;
}
2024-10-17 16:39:42 +07:00
const before = null ;
2024-03-18 17:29:51 +07:00
let posExecutive : any = new PosExecutive ( ) ;
2024-03-18 11:11:14 +07:00
if ( requestBody . posExecutive != null && requestBody . posExecutive != "" ) {
const checkName = await this . posExecutiveRepository . findOne ( {
where : { posExecutiveName : requestBody.posExecutive } ,
} ) ;
if ( checkName ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ชื่อนี้มีอยู่ในระบบแล้ว" ) ;
}
posExecutive . posExecutiveName = requestBody . posExecutive ;
const checkPriority = await this . posExecutiveRepository . findOne ( {
2024-03-18 17:29:51 +07:00
select : [ "posExecutivePriority" ] ,
where : {
posExecutivePriority : Not ( IsNull ( ) ) ,
} ,
2024-03-18 11:11:14 +07:00
order : { posExecutivePriority : "DESC" } ,
} ) ;
if ( checkPriority == null ) {
posExecutive . posExecutivePriority = 1 ;
} else {
posExecutive . posExecutivePriority = checkPriority . posExecutivePriority + 1 ;
}
posExecutive . createdUserId = request . user . sub ;
posExecutive . createdFullName = request . user . name ;
posExecutive . lastUpdateUserId = request . user . sub ;
posExecutive . lastUpdateFullName = request . user . name ;
2024-08-30 21:02:14 +07:00
posExecutive . createdAt = new Date ( ) ;
posExecutive . lastUpdatedAt = new Date ( ) ;
2024-10-17 22:11:54 +07:00
await this . posExecutiveRepository . save ( posExecutive , { data : request } ) ;
setLogDataDiff ( request , { before , after : posExecutive } ) ;
2024-03-18 11:11:14 +07:00
}
const rowRepeated = await this . posDictRepository . findOne ( {
where : {
posDictName : posDict.posDictName ,
posDictField : posDict.posDictField ,
posTypeId : posDict.posTypeId ,
posLevelId : posDict.posLevelId ,
posExecutiveId : posExecutive == null ? null : posExecutive . id ,
posDictExecutiveField : posDict.posDictExecutiveField ,
posDictArea : posDict.posDictArea ,
isSpecial : posDict.isSpecial ,
} ,
} ) ;
if ( rowRepeated ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ข้อมูล Row นี้มีอยู่ในระบบแล้ว" ) ;
}
2024-03-18 20:05:39 +07:00
posDict . posExecutiveId = posExecutive == null ? null : posExecutive . id ;
2024-03-18 11:11:14 +07:00
posDict . createdUserId = request . user . sub ;
posDict . createdFullName = request . user . name ;
posDict . lastUpdateUserId = request . user . sub ;
posDict . lastUpdateFullName = request . user . name ;
2024-08-30 21:02:14 +07:00
posDict . createdAt = new Date ( ) ;
posDict . lastUpdatedAt = new Date ( ) ;
2024-10-17 22:11:54 +07:00
await this . posDictRepository . save ( posDict , { data : request } ) ;
setLogDataDiff ( request , { before , after : posDict } ) ;
2024-03-18 11:11:14 +07:00
return new HttpSuccess ( posDict . id ) ;
}
2024-02-21 16:18:32 +07:00
/ * *
2024-02-20 18:04:13 +07:00
* API แ ก ้ ไ ข ต ำ แ ห น ่ ง
*
2024-02-21 16:18:32 +07:00
* @summary แ ก ้ ไ ข ต ำ แ ห น ่ ง ( ADMIN )
2024-02-20 18:04:13 +07:00
*
* /
2024-02-21 16:18:32 +07:00
@Put ( "position/{id}" )
@Example ( [
{
positionName : "นักบริหาร" ,
positionField : "บริหาร" ,
posTypeId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
posLevelId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
posExecutiveId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
positionExecutiveField : "นักบริหาร" ,
positionArea : "บริหาร" ,
} ,
] )
async updatePosition (
@Path ( ) id : string ,
@Body ( )
requestBody : UpdatePosDict ,
2024-08-16 17:36:08 +07:00
@Request ( ) request : RequestWithUser ,
2024-02-21 16:18:32 +07:00
) {
2024-08-23 17:23:27 +07:00
// await new permission().PermissionUpdate(request, "SYS_ORG");
2024-02-21 16:18:32 +07:00
const posDict = await this . posDictRepository . findOne ( {
where : { id : id } ,
} ) ;
if ( ! posDict ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล" ) ;
}
const checkPosTypeId = await this . posTypeRepository . findOne ( {
where : { id : requestBody.posTypeId } ,
} ) ;
if ( ! checkPosTypeId ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล PosTypeId" ) ;
}
const checkPosLevelId = await this . posLevelRepository . findOne ( {
where : { id : requestBody.posLevelId } ,
} ) ;
if ( ! checkPosLevelId ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล PosLevelId" ) ;
}
const _null : any = null ;
if ( requestBody . posExecutiveId == "" ) {
requestBody . posExecutiveId = _null ;
}
2024-05-02 17:39:07 +07:00
if ( requestBody . posExecutiveId != null && requestBody . posExecutiveId != "" ) {
2024-02-21 16:18:32 +07:00
const checkPosExecutiveId = await this . posExecutiveRepository . findOne ( {
where : { id : requestBody.posExecutiveId } ,
2024-02-20 18:04:13 +07:00
} ) ;
2024-02-21 16:18:32 +07:00
if ( ! checkPosExecutiveId ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล PosExecutiveId" ) ;
2024-02-20 18:04:13 +07:00
}
}
2024-02-21 16:18:32 +07:00
const rowRepeated = await this . posDictRepository . findOne ( {
where : {
id : Not ( id ) ,
posDictName : requestBody.posDictName ,
posDictField : requestBody.posDictField ,
posTypeId : requestBody.posTypeId ,
posLevelId : requestBody.posLevelId ,
2024-06-09 22:17:14 +07:00
posExecutiveId : requestBody.posExecutiveId ? requestBody . posExecutiveId : "" ,
posDictExecutiveField : requestBody.posDictExecutiveField
? requestBody . posDictExecutiveField
: "" ,
posDictArea : requestBody.posDictArea ? requestBody . posDictArea : "" ,
2024-02-21 16:18:32 +07:00
isSpecial : requestBody.isSpecial ,
} ,
} ) ;
if ( rowRepeated ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ข้อมูล Row นี้มีอยู่ในระบบแล้ว" ) ;
}
2024-10-17 16:39:42 +07:00
const before = structuredClone ( posDict ) ;
2024-06-04 16:49:52 +07:00
Object . assign ( posDict , requestBody ) ;
2024-02-28 11:31:01 +07:00
posDict . lastUpdateUserId = request . user . sub ;
posDict . lastUpdateFullName = request . user . name ;
2024-08-30 21:02:14 +07:00
posDict . lastUpdatedAt = new Date ( ) ;
posDict . posDictName = requestBody . posDictName ;
posDict . posDictField = requestBody . posDictField ;
posDict . posTypeId = requestBody . posTypeId ;
posDict . posLevelId = requestBody . posLevelId ;
posDict . posExecutiveId = requestBody . posExecutiveId ? requestBody.posExecutiveId : null ;
posDict . posDictExecutiveField = requestBody . posDictExecutiveField
? requestBody . posDictExecutiveField
: "" ;
posDict . posDictArea = requestBody . posDictArea ? requestBody . posDictArea : "" ;
posDict . isSpecial = requestBody . isSpecial ;
// this.posDictRepository.merge(posDict, requestBody);
2024-10-17 22:11:54 +07:00
await this . posDictRepository . save ( posDict , { data : request } ) ;
setLogDataDiff ( request , { before , after : posDict } ) ;
2024-02-28 11:31:01 +07:00
return new HttpSuccess ( ) ;
2024-02-21 16:18:32 +07:00
}
2024-01-30 16:47:39 +07:00
/ * *
* API ล บ ต ำ แ ห น ่ ง
*
* @summary ORG_032 - ล บ ต ำ แ ห น ่ ง ( ADMIN ) # 40
*
* @param { string } id Id ต ำ แ ห น ่ ง
* /
2024-01-31 15:24:28 +07:00
@Delete ( "position/{id}" )
2024-08-16 17:36:08 +07:00
async delete ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
2024-08-23 17:23:27 +07:00
// await new permission().PermissionDelete(request, "SYS_ORG");
2024-01-30 16:47:39 +07:00
const delPosDict = await this . posDictRepository . findOne ( { where : { id } } ) ;
if ( ! delPosDict ) {
2024-02-28 14:00:38 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งในสายงานนี้" ) ;
2024-01-30 16:47:39 +07:00
}
2024-10-17 22:11:54 +07:00
await this . posDictRepository . remove ( delPosDict , { data : request } ) ;
2024-02-28 11:31:01 +07:00
return new HttpSuccess ( ) ;
2024-01-30 18:01:58 +07:00
}
2024-01-31 11:00:51 +07:00
2024-01-30 18:01:58 +07:00
/ * *
* API ค ้ น ห า ร า ย ก า ร ต ำ แ ห น ่ ง
*
* @summary ORG_029 - ค ้ น ห า ร า ย ก า ร ต ำ แ ห น ่ ง ( ADMIN ) # 32
*
* /
@Get ( "position" )
2024-01-31 15:02:54 +07:00
async findPosition ( @Query ( "keyword" ) keyword? : string , @Query ( "type" ) type ? : string ) {
2024-02-28 11:31:01 +07:00
let findPosDict : any ;
switch ( type ) {
case "positionName" :
findPosDict = await this . posDictRepository . find ( {
where : { posDictName : Like ( ` % ${ keyword } % ` ) } ,
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-05-15 15:29:12 +07:00
order : { posDictName : "ASC" } ,
2024-02-28 11:31:01 +07:00
} ) ;
break ;
case "positionField" :
findPosDict = await this . posDictRepository . find ( {
where : { posDictField : Like ( ` % ${ keyword } % ` ) } ,
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-05-15 15:29:12 +07:00
order : { posDictField : "ASC" } ,
2024-02-28 11:31:01 +07:00
} ) ;
break ;
2024-01-31 13:16:22 +07:00
2024-02-28 11:31:01 +07:00
case "positionType" :
const findTypes : PosType [ ] = await this . posTypeRepository . find ( {
where : { posTypeName : Like ( ` % ${ keyword } % ` ) } ,
2024-05-15 15:29:12 +07:00
order : { posTypeName : "ASC" } ,
2024-02-28 11:31:01 +07:00
select : [ "id" ] ,
} ) ;
findPosDict = await this . posDictRepository . find ( {
where : { posTypeId : In ( findTypes . map ( ( x ) = > x . id ) ) } ,
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-05-15 15:29:12 +07:00
order : { posDictName : "ASC" } ,
2024-02-28 11:31:01 +07:00
} ) ;
break ;
2024-01-31 13:16:22 +07:00
2024-02-28 11:31:01 +07:00
case "positionLevel" :
const findLevel : PosLevel [ ] = await this . posLevelRepository . find ( {
where : { posLevelName : Like ( ` % ${ keyword } % ` ) } ,
2024-05-15 15:29:12 +07:00
order : { posLevelName : "ASC" } ,
2024-02-28 11:31:01 +07:00
select : [ "id" ] ,
} ) ;
findPosDict = await this . posDictRepository . find ( {
where : { posLevelId : In ( findLevel . map ( ( x ) = > x . id ) ) } ,
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-05-15 15:29:12 +07:00
order : { posDictName : "ASC" } ,
2024-02-28 11:31:01 +07:00
} ) ;
break ;
2024-01-31 13:16:22 +07:00
2024-02-28 11:31:01 +07:00
case "positionExecutive" :
const findExecutive : PosExecutive [ ] = await this . posExecutiveRepository . find ( {
where : { posExecutiveName : Like ( ` % ${ keyword } % ` ) } ,
2024-05-15 15:29:12 +07:00
order : { posExecutiveName : "ASC" } ,
2024-02-28 11:31:01 +07:00
select : [ "id" ] ,
} ) ;
findPosDict = await this . posDictRepository . find ( {
where : { posExecutiveId : In ( findExecutive . map ( ( x ) = > x . id ) ) } ,
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-05-15 15:29:12 +07:00
order : { posDictName : "ASC" } ,
2024-02-28 11:31:01 +07:00
} ) ;
break ;
2024-01-31 13:16:22 +07:00
2024-02-28 11:31:01 +07:00
case "positionExecutiveField" :
findPosDict = await this . posDictRepository . find ( {
where : { posDictExecutiveField : Like ( ` % ${ keyword } % ` ) } ,
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-05-15 15:29:12 +07:00
order : { posDictName : "ASC" } ,
2024-02-28 11:31:01 +07:00
} ) ;
break ;
case "positionArea" :
findPosDict = await this . posDictRepository . find ( {
where : { posDictArea : Like ( ` % ${ keyword } % ` ) } ,
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-05-15 15:29:12 +07:00
order : { posDictName : "ASC" } ,
2024-02-28 11:31:01 +07:00
} ) ;
break ;
2024-09-03 17:44:47 +07:00
2024-09-13 10:29:08 +07:00
case "ALL" :
findPosDict = await this . posDictRepository . find ( {
where : [
{ posDictName : Like ( ` % ${ keyword } % ` ) } ,
{ posDictField : Like ( ` % ${ keyword } % ` ) } ,
{
posTypeId : In (
await this . posTypeRepository
. find ( { where : { posTypeName : Like ( ` % ${ keyword } % ` ) } , select : [ "id" ] } )
. then ( ( types ) = > types . map ( ( type ) = > type . id ) ) ,
) ,
} ,
{
posLevelId : In (
await this . posLevelRepository
. find ( { where : { posLevelName : Like ( ` % ${ keyword } % ` ) } , select : [ "id" ] } )
. then ( ( levels ) = > levels . map ( ( level ) = > level . id ) ) ,
) ,
} ,
{
posExecutiveId : In (
await this . posExecutiveRepository
. find ( { where : { posExecutiveName : Like ( ` % ${ keyword } % ` ) } , select : [ "id" ] } )
. then ( ( executives ) = > executives . map ( ( exec ) = > exec . id ) ) ,
) ,
} ,
{ posDictExecutiveField : Like ( ` % ${ keyword } % ` ) } ,
{ posDictArea : Like ( ` % ${ keyword } % ` ) } ,
] ,
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
order : { posDictName : "ASC" } ,
} ) ;
break ;
2024-02-28 11:31:01 +07:00
default :
findPosDict = await this . posDictRepository . find ( {
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-05-15 15:29:12 +07:00
order : { posDictName : "ASC" } ,
2024-02-28 11:31:01 +07:00
} ) ;
break ;
}
2024-01-31 13:16:22 +07:00
2024-02-28 11:31:01 +07:00
const mapDataPosDict = await Promise . all (
findPosDict . map ( async ( item : any ) = > {
return {
id : item.id ,
positionName : item.posDictName ,
positionField : item.posDictField ,
posTypeId : item.posTypeId ,
posTypeName : item.posType == null ? null : item . posType . posTypeName ,
posLevelId : item.posLevelId ,
posLevelName : item.posLevel == null ? null : item . posLevel . posLevelName ,
posExecutiveId : item.posExecutiveId ,
posExecutiveName : item.posExecutive == null ? null : item . posExecutive . posExecutiveName ,
positionExecutiveField : item.posDictExecutiveField ,
positionArea : item.posDictArea ,
isSpecial : item.isSpecial ,
positionIsSelected : false ,
} ;
} ) ,
) ;
2024-01-31 13:16:22 +07:00
2024-02-28 11:31:01 +07:00
return new HttpSuccess ( mapDataPosDict ) ;
2024-01-30 18:01:58 +07:00
}
2024-01-31 11:00:51 +07:00
/ * *
* API เ พ ิ ่ ม อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_033 - เ พ ิ ่ ม อ ั ต ร า ก ำ ล ั ง ( ADMIN ) # 35
*
* /
@Post ( "master" )
@Example ( {
posMasterNoPrefix : "กบ." ,
posMasterNo : 1 ,
posMasterNoSuffix : "ช" ,
posId : [ "08db9e81-fc46-4e95-8b33-be4ca0016abf" , "08db9e81-fc46-4e95-8b33-be4ca0016abf" ] ,
orgRootId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
orgChild1Id : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
orgChild2Id : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
orgChild3Id : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
orgChild4Id : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
2024-04-09 12:18:52 +07:00
reason : "บริหาร" ,
2024-01-31 11:00:51 +07:00
positions : [
{
posDictName : "นักบริหาร" ,
posDictField : "บริหาร" ,
posTypeId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
posLevelId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
posExecutiveId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
posDictExecutiveField : "นักบริหาร" ,
posDictArea : "บริหาร" ,
} ,
] ,
} )
async createMaster (
@Body ( )
requestBody : CreatePosMaster ,
2024-08-16 17:36:08 +07:00
@Request ( ) request : RequestWithUser ,
2024-01-31 11:00:51 +07:00
) {
2024-08-16 17:36:08 +07:00
await new permission ( ) . PermissionCreate ( request , "SYS_ORG" ) ;
2024-01-31 11:00:51 +07:00
const posMaster = Object . assign ( new PosMaster ( ) , requestBody ) ;
if ( ! posMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล" ) ;
}
2024-01-31 18:24:38 +07:00
let orgRoot : any = null ;
2024-02-21 16:13:23 +07:00
let SName : any = null ;
2024-01-31 18:24:38 +07:00
if ( requestBody . orgRootId != null )
orgRoot = await this . orgRootRepository . findOne ( {
where : { id : requestBody.orgRootId } ,
2024-01-31 11:00:51 +07:00
} ) ;
2024-01-31 18:24:38 +07:00
if ( ! orgRoot ) {
let orgChild1 : any = null ;
if ( requestBody . orgChild1Id != null )
orgChild1 = await this . child1Repository . findOne ( {
where : { id : requestBody.orgChild1Id } ,
2024-01-31 11:00:51 +07:00
} ) ;
2024-01-31 18:24:38 +07:00
if ( ! orgChild1 ) {
let orgChild2 : any = null ;
if ( requestBody . orgChild2Id != null )
orgChild2 = await this . child2Repository . findOne ( {
where : { id : requestBody.orgChild2Id } ,
2024-01-31 11:00:51 +07:00
} ) ;
2024-01-31 18:24:38 +07:00
if ( ! orgChild2 ) {
let orgChild3 : any = null ;
if ( requestBody . orgChild3Id != null )
orgChild3 = await this . child3Repository . findOne ( {
where : { id : requestBody.orgChild3Id } ,
2024-01-31 11:00:51 +07:00
} ) ;
2024-01-31 18:24:38 +07:00
if ( ! orgChild3 ) {
let orgChild4 : any = null ;
if ( requestBody . orgChild4Id != null )
orgChild4 = await this . child4Repository . findOne ( {
where : { id : requestBody.orgChild4Id } ,
} ) ;
2024-01-31 11:00:51 +07:00
if ( ! orgChild4 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลโครงสร้าง" ) ;
} else {
2024-02-13 15:36:23 +07:00
const order : any = await this . posMasterRepository . findOne ( {
2024-02-12 15:00:30 +07:00
where : {
2024-02-13 15:36:23 +07:00
orgChild4Id : orgChild4.id ,
2024-02-12 15:00:30 +07:00
} ,
2024-02-13 18:27:03 +07:00
order : { posMasterOrder : "DESC" } ,
2024-02-13 15:36:23 +07:00
} ) ;
posMaster . posMasterOrder =
2024-02-13 19:03:13 +07:00
order !== null && order !== undefined && order . posMasterOrder
? order . posMasterOrder + 1
: 1 ;
2024-01-31 11:00:51 +07:00
posMaster . orgRootId = orgChild4 . orgRootId ;
posMaster . orgChild1Id = orgChild4 . orgChild1Id ;
posMaster . orgChild2Id = orgChild4 . orgChild2Id ;
posMaster . orgChild3Id = orgChild4 . orgChild3Id ;
posMaster . orgChild4Id = orgChild4 . id ;
posMaster . orgRevisionId = orgChild4 . orgRevisionId ;
2024-02-29 11:29:16 +07:00
SName = orgChild4 . orgChild4ShortName ;
2024-01-31 11:00:51 +07:00
}
} else {
2024-02-13 15:36:23 +07:00
const order : any = await this . posMasterRepository . findOne ( {
where : {
2024-02-12 15:00:30 +07:00
orgChild3Id : orgChild3.id ,
2024-02-13 15:36:23 +07:00
orgChild4Id : IsNull ( ) || "" ,
2024-02-12 15:00:30 +07:00
} ,
2024-02-13 18:27:03 +07:00
order : { posMasterOrder : "DESC" } ,
2024-02-13 15:36:23 +07:00
} ) ;
2024-02-13 19:03:13 +07:00
posMaster . posMasterOrder =
order !== null && order !== undefined && order . posMasterOrder
? order . posMasterOrder + 1
: 1 ;
2024-01-31 11:00:51 +07:00
posMaster . orgRootId = orgChild3 . orgRootId ;
posMaster . orgChild1Id = orgChild3 . orgChild1Id ;
posMaster . orgChild2Id = orgChild3 . orgChild2Id ;
posMaster . orgChild3Id = orgChild3 . id ;
posMaster . orgRevisionId = orgChild3 . orgRevisionId ;
2024-02-29 11:29:16 +07:00
SName = orgChild3 . orgChild3ShortName ;
2024-01-31 11:00:51 +07:00
}
} else {
2024-02-13 15:36:23 +07:00
const order : any = await this . posMasterRepository . findOne ( {
where : {
2024-02-12 15:00:30 +07:00
orgChild2Id : orgChild2.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
} ,
2024-02-13 18:27:03 +07:00
order : { posMasterOrder : "DESC" } ,
2024-02-13 15:36:23 +07:00
} ) ;
2024-02-13 19:03:13 +07:00
posMaster . posMasterOrder =
order !== null && order !== undefined && order . posMasterOrder
? order . posMasterOrder + 1
: 1 ;
2024-01-31 11:00:51 +07:00
posMaster . orgRootId = orgChild2 . orgRootId ;
posMaster . orgChild1Id = orgChild2 . orgChild1Id ;
posMaster . orgChild2Id = orgChild2 . id ;
posMaster . orgRevisionId = orgChild2 . orgRevisionId ;
2024-02-29 11:29:16 +07:00
SName = orgChild2 . orgChild2ShortName ;
2024-01-31 11:00:51 +07:00
}
} else {
2024-02-13 15:36:23 +07:00
const order : any = await this . posMasterRepository . findOne ( {
where : {
2024-02-12 15:00:30 +07:00
orgChild1Id : orgChild1.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
} ,
2024-02-13 18:27:03 +07:00
order : { posMasterOrder : "DESC" } ,
2024-02-13 15:36:23 +07:00
} ) ;
2024-02-13 19:03:13 +07:00
posMaster . posMasterOrder =
order !== null && order !== undefined && order . posMasterOrder
? order . posMasterOrder + 1
: 1 ;
2024-01-31 11:00:51 +07:00
posMaster . orgRootId = orgChild1 . orgRootId ;
posMaster . orgChild1Id = orgChild1 . id ;
posMaster . orgRevisionId = orgChild1 . orgRevisionId ;
2024-02-29 11:29:16 +07:00
SName = orgChild1 . orgChild1ShortName ;
2024-01-31 11:00:51 +07:00
}
} else {
2024-02-13 15:36:23 +07:00
const order : any = await this . posMasterRepository . findOne ( {
where : {
2024-02-12 15:00:30 +07:00
orgRootId : orgRoot.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
} ,
2024-02-13 18:27:03 +07:00
order : { posMasterOrder : "DESC" } ,
2024-02-13 15:36:23 +07:00
} ) ;
2024-02-13 19:03:13 +07:00
posMaster . posMasterOrder =
order !== null && order !== undefined && order . posMasterOrder
? order . posMasterOrder + 1
: 1 ;
2024-01-31 11:00:51 +07:00
posMaster . orgRootId = orgRoot . id ;
posMaster . orgRevisionId = orgRoot . orgRevisionId ;
2024-02-29 11:29:16 +07:00
SName = orgRoot . orgRootShortName ;
2024-01-31 11:00:51 +07:00
}
2024-04-25 18:45:22 +07:00
2024-02-21 16:13:23 +07:00
const chk_SName0 = await this . posMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgRoot : { orgRootShortName : SName } ,
orgChild1Id : IsNull ( ) ,
2024-04-25 15:44:56 +07:00
posMasterNo : requestBody.posMasterNo ,
2024-02-21 16:13:23 +07:00
} ,
2024-02-21 17:10:30 +07:00
relations : [ "orgRoot" ] ,
2024-02-21 16:13:23 +07:00
} ) ;
2024-02-21 17:10:30 +07:00
if ( chk_SName0 != null ) {
2024-02-21 16:13:23 +07:00
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
const chk_SName1 = await this . posMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgChild1 : { orgChild1ShortName : SName } ,
orgChild2Id : IsNull ( ) ,
posMasterNo : requestBody.posMasterNo ,
} ,
2024-02-21 17:10:30 +07:00
relations : [ "orgChild1" ] ,
2024-02-21 16:13:23 +07:00
} ) ;
2024-02-21 17:10:30 +07:00
if ( chk_SName1 != null ) {
2024-02-21 16:13:23 +07:00
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
const chk_SName2 = await this . posMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgChild2 : { orgChild2ShortName : SName } ,
orgChild3Id : IsNull ( ) ,
posMasterNo : requestBody.posMasterNo ,
} ,
2024-02-21 17:10:30 +07:00
relations : [ "orgChild2" ] ,
2024-02-21 16:13:23 +07:00
} ) ;
2024-02-21 17:10:30 +07:00
if ( chk_SName2 != null ) {
2024-02-21 16:13:23 +07:00
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
const chk_SName3 = await this . posMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgChild3 : { orgChild3ShortName : SName } ,
orgChild4Id : IsNull ( ) ,
posMasterNo : requestBody.posMasterNo ,
} ,
2024-02-21 17:10:30 +07:00
relations : [ "orgChild3" ] ,
2024-02-21 16:13:23 +07:00
} ) ;
2024-02-21 17:10:30 +07:00
if ( chk_SName3 != null ) {
2024-02-21 16:13:23 +07:00
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
const chk_SName4 = await this . posMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgChild4 : { orgChild4ShortName : SName } ,
posMasterNo : requestBody.posMasterNo ,
} ,
2024-02-21 17:10:30 +07:00
relations : [ "orgChild4" ] ,
2024-02-21 16:13:23 +07:00
} ) ;
2024-02-21 17:10:30 +07:00
if ( chk_SName4 != null ) {
2024-02-21 16:13:23 +07:00
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
}
2024-10-04 18:12:23 +07:00
const before = null ;
2024-01-31 11:00:51 +07:00
posMaster . createdUserId = request . user . sub ;
posMaster . createdFullName = request . user . name ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
2024-08-30 21:02:14 +07:00
posMaster . createdAt = new Date ( ) ;
posMaster . lastUpdatedAt = new Date ( ) ;
2024-10-04 18:12:23 +07:00
await this . posMasterRepository . save ( posMaster , { data : request } ) ;
setLogDataDiff ( request , { before , after : posMaster } ) ;
2024-02-21 16:18:32 +07:00
await Promise . all (
requestBody . positions . map ( async ( x : any ) = > {
const position = Object . assign ( new Position ( ) ) ;
position . positionName = x . posDictName ;
position . positionField = x . posDictField ;
2024-02-21 17:10:30 +07:00
position . posTypeId = x . posTypeId == "" ? null : x . posTypeId ;
position . posLevelId = x . posLevelId == "" ? null : x . posLevelId ;
position . posExecutiveId = x . posExecutiveId == "" ? null : x . posExecutiveId ;
2024-02-21 16:18:32 +07:00
position . positionExecutiveField = x . posDictExecutiveField ;
position . positionArea = x . posDictArea ;
position . isSpecial = x . isSpecial ;
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 ;
2024-08-30 21:02:14 +07:00
position . createdAt = new Date ( ) ;
position . lastUpdatedAt = new Date ( ) ;
2024-10-04 18:12:23 +07:00
await this . positionRepository . save ( position , { data : request } ) ;
2024-02-21 16:18:32 +07:00
} ) ,
) ;
2024-01-31 11:00:51 +07:00
return new HttpSuccess ( posMaster . id ) ;
}
/ * *
2024-02-01 13:29:15 +07:00
* API แ ก ้ ไ ข เ ล ข ท ี ่ ต ำ แ ห น ่ ง
2024-01-31 11:00:51 +07:00
*
2024-02-01 13:29:15 +07:00
* @summary ORG_034 - แ ก ้ ไ ข เ ล ข ท ี ่ ต ำ แ ห น ่ ง ( ADMIN ) # 37
2024-01-31 11:00:51 +07:00
*
* /
@Put ( "master/{id}" )
@Example ( {
posMasterNoPrefix : "กบ." ,
2024-01-31 17:20:08 +07:00
posMasterNo : "1" ,
2024-01-31 11:00:51 +07:00
posMasterNoSuffix : "ช" ,
posId : [ "08db9e81-fc46-4e95-8b33-be4ca0016abf" , "08db9e81-fc46-4e95-8b33-be4ca0016abf" ] ,
orgRootId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
orgChild1Id : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
orgChild2Id : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
orgChild3Id : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
orgChild4Id : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
2024-04-09 12:18:52 +07:00
reason : "บริหาร" ,
2024-01-31 11:00:51 +07:00
positions : [
{
posDictName : "นักบริหาร" ,
posDictField : "บริหาร" ,
posTypeId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
posLevelId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
posExecutiveId : "08db9e81-fc46-4e95-8b33-be4ca0016abf" ,
posDictExecutiveField : "นักบริหาร" ,
posDictArea : "บริหาร" ,
} ,
] ,
} )
async updateMaster (
@Path ( ) id : string ,
@Body ( )
requestBody : CreatePosMaster ,
2024-08-16 17:36:08 +07:00
@Request ( ) request : RequestWithUser ,
2024-01-31 11:00:51 +07:00
) {
2024-08-16 17:36:08 +07:00
await new permission ( ) . PermissionUpdate ( request , "SYS_ORG" ) ;
2024-02-12 10:57:35 +07:00
const posMaster = await this . posMasterRepository . findOne ( { where : { id : id } } ) ;
2024-01-31 11:00:51 +07:00
if ( ! posMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลอัตรากำลัง" ) ;
}
2024-09-13 10:29:08 +07:00
let _null : any = null ;
2024-06-12 14:41:16 +07:00
posMaster . isDirector = requestBody . isDirector ;
2024-10-03 15:37:28 +07:00
posMaster . isStaff =
requestBody . isStaff == null || requestBody . isStaff == undefined ? _null : requestBody.isStaff ;
2024-09-13 10:29:08 +07:00
// posMaster.isOfficer = requestBody.isOfficer;
2024-10-03 15:37:28 +07:00
posMaster . positionSign =
requestBody . positionSign == null || requestBody . positionSign == undefined
? _null
: requestBody . positionSign ;
2024-04-25 18:09:58 +07:00
posMaster . posMasterNo = requestBody . posMasterNo ;
posMaster . posMasterNoPrefix = requestBody . posMasterNoPrefix ;
posMaster . posMasterNoSuffix = requestBody . posMasterNoSuffix ;
posMaster . reason = requestBody . reason == null ? "" : requestBody . reason ;
2024-04-25 18:45:22 +07:00
2024-04-26 11:04:15 +07:00
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 {
SName = orgChild4 . orgChild4ShortName ;
}
} else {
SName = orgChild3 . orgChild3ShortName ;
}
} else {
SName = orgChild2 . orgChild2ShortName ;
}
} else {
SName = orgChild1 . orgChild1ShortName ;
}
} else {
SName = orgRoot . orgRootShortName ;
}
// if (posMaster.orgChild4Id != null) {
2024-04-29 16:01:47 +07:00
const chk_SName4 = await this . posMasterRepository . findOne ( {
where : {
orgChild4 : { orgChild4ShortName : SName } ,
posMasterNo : requestBody.posMasterNo ,
id : Not ( posMaster . id ) ,
2024-04-30 00:09:32 +07:00
orgRevisionId : posMaster.orgRevisionId ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( chk_SName4 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถแก้ไขชื่อตำแหน่งซ้ำกับข้อมูลที่มีอยู่แล้วได้" ,
) ;
}
2024-04-26 11:04:15 +07:00
// } else if (posMaster.orgChild3Id != null) {
2024-04-29 16:01:47 +07:00
const chk_SName3 = await this . posMasterRepository . findOne ( {
where : {
orgChild3 : { orgChild3ShortName : SName } ,
posMasterNo : requestBody.posMasterNo ,
orgChild4Id : IsNull ( ) ,
id : Not ( posMaster . id ) ,
2024-04-30 00:09:32 +07:00
orgRevisionId : posMaster.orgRevisionId ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( chk_SName3 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถแก้ไขชื่อตำแหน่งซ้ำกับข้อมูลที่มีอยู่แล้วได้" ,
) ;
}
2024-04-26 11:04:15 +07:00
// } else if (posMaster.orgChild2Id != null) {
2024-04-29 16:01:47 +07:00
const chk_SName2 = await this . posMasterRepository . findOne ( {
where : {
orgChild2 : { orgChild2ShortName : SName } ,
posMasterNo : requestBody.posMasterNo ,
orgChild3Id : IsNull ( ) ,
id : Not ( posMaster . id ) ,
2024-04-30 00:09:32 +07:00
orgRevisionId : posMaster.orgRevisionId ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( chk_SName2 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถแก้ไขชื่อตำแหน่งซ้ำกับข้อมูลที่มีอยู่แล้วได้" ,
) ;
}
2024-04-26 11:04:15 +07:00
// } else if (posMaster.orgChild1Id != null) {
2024-04-29 16:01:47 +07:00
const chk_SName1 = await this . posMasterRepository . findOne ( {
where : {
orgChild1 : { orgChild1ShortName : SName } ,
posMasterNo : requestBody.posMasterNo ,
orgChild2Id : IsNull ( ) ,
id : Not ( posMaster . id ) ,
2024-04-30 00:09:32 +07:00
orgRevisionId : posMaster.orgRevisionId ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( chk_SName1 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถแก้ไขชื่อตำแหน่งซ้ำกับข้อมูลที่มีอยู่แล้วได้" ,
) ;
}
2024-04-26 11:04:15 +07:00
// } else if (posMaster.orgRootId != null) {
2024-04-29 16:01:47 +07:00
const chk_SName0 = await this . posMasterRepository . findOne ( {
where : {
orgRoot : { orgRootShortName : SName } ,
posMasterNo : requestBody.posMasterNo ,
orgChild1Id : IsNull ( ) ,
id : Not ( posMaster . id ) ,
2024-04-30 00:09:32 +07:00
orgRevisionId : posMaster.orgRevisionId ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( chk_SName0 != null ) {
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถแก้ไขชื่อตำแหน่งซ้ำกับข้อมูลที่มีอยู่แล้วได้" ,
) ;
}
2024-04-26 11:04:15 +07:00
// }
2024-10-04 18:12:23 +07:00
const before = structuredClone ( posMaster ) ;
2024-04-19 12:14:17 +07:00
posMaster . createdUserId = request . user . sub ;
2024-01-31 11:00:51 +07:00
posMaster . createdFullName = request . user . name ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
2024-08-30 21:02:14 +07:00
posMaster . createdAt = new Date ( ) ;
posMaster . lastUpdatedAt = new Date ( ) ;
2024-10-04 18:12:23 +07:00
await this . posMasterRepository . save ( posMaster , { data : request } ) ;
2024-10-07 17:00:54 +07:00
setLogDataDiff ( request , { before , after : posMaster } ) ;
2024-01-31 15:09:22 +07:00
await this . positionRepository . delete ( { posMasterId : posMaster.id } ) ;
2024-02-21 17:10:30 +07:00
await Promise . all (
requestBody . positions . map ( async ( x : any ) = > {
const position = Object . assign ( new Position ( ) ) ;
position . positionName = x . posDictName ;
position . positionField = x . posDictField ;
position . posTypeId = x . posTypeId == "" ? null : x . posTypeId ;
position . posLevelId = x . posLevelId == "" ? null : x . posLevelId ;
position . posExecutiveId = x . posExecutiveId == "" ? null : x . posExecutiveId ;
position . positionExecutiveField = x . posDictExecutiveField ;
position . positionArea = x . posDictArea ;
position . isSpecial = x . isSpecial ;
2024-06-12 14:41:16 +07:00
position . isOfficer = x . isOfficer ;
2024-09-12 22:53:58 +07:00
position . isStaff = x . isStaff ;
2024-06-12 14:41:16 +07:00
position . isDirector = x . isDirector ;
2024-09-12 22:53:58 +07:00
position . positionSign = x . positionSign ;
2024-04-24 14:56:20 +07:00
position . positionIsSelected = x . positionIsSelected ;
2024-02-21 17:10:30 +07:00
position . posMasterId = posMaster . id ;
position . createdUserId = request . user . sub ;
position . createdFullName = request . user . name ;
position . lastUpdateUserId = request . user . sub ;
position . lastUpdateFullName = request . user . name ;
2024-08-30 21:02:14 +07:00
position . createdAt = new Date ( ) ;
position . lastUpdatedAt = new Date ( ) ;
2024-10-04 18:12:23 +07:00
await this . positionRepository . save ( position , { data : request } ) ;
2024-02-21 17:10:30 +07:00
} ) ,
) ;
2024-01-31 11:00:51 +07:00
return new HttpSuccess ( posMaster . id ) ;
}
2024-01-31 14:29:39 +07:00
/ * *
* API ร า ย ล ะ เ อ ี ย ด อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_037 - ร า ย ล ะ เ อ ี ย ด อ ั ต ร า ก ำ ล ั ง ( ADMIN ) # 36
*
* /
2024-01-31 16:11:49 +07:00
@Get ( "position/{id}" )
2024-01-31 16:12:07 +07:00
async detailPosition ( @Path ( ) id : string ) {
2024-02-28 11:31:01 +07:00
const posMaster = await this . posMasterRepository . findOne ( {
where : { id } ,
} ) ;
if ( ! posMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล" ) ;
2024-01-31 14:29:39 +07:00
}
2024-02-28 11:31:01 +07:00
const positions = await this . positionRepository . find ( {
where : { posMasterId : posMaster.id } ,
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
order : { lastUpdatedAt : "ASC" } ,
} ) ;
const formattedData = {
id : posMaster.id ,
posMasterNoPrefix : posMaster.posMasterNoPrefix ,
posMasterNo : posMaster.posMasterNo ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ,
2024-04-09 12:18:52 +07:00
reason : posMaster.reason ,
2024-09-13 10:29:08 +07:00
// isOfficer: posMaster.isOfficer,
2024-09-12 22:53:58 +07:00
isStaff : posMaster.isStaff ,
2024-06-12 21:55:27 +07:00
isDirector : posMaster.isDirector ,
2024-09-12 22:53:58 +07:00
positionSign : posMaster.positionSign ,
2024-02-28 11:31:01 +07:00
positions : positions.map ( ( position ) = > ( {
id : position.id ,
positionName : position.positionName ,
positionField : position.positionField ,
posTypeId : position.posTypeId ,
posTypeName : position.posType == null ? null : position . posType . posTypeName ,
posLevelId : position.posLevelId ,
posLevelName : position.posLevel == null ? null : position . posLevel . posLevelName ,
posExecutiveId : position.posExecutiveId ,
posExecutiveName :
position . posExecutive == null ? null : position . posExecutive . posExecutiveName ,
positionExecutiveField : position.positionExecutiveField ,
positionArea : position.positionArea ,
positionIsSelected : position.positionIsSelected ,
isSpecial : position.isSpecial ,
} ) ) ,
} ;
return new HttpSuccess ( formattedData ) ;
2024-01-31 14:29:39 +07:00
}
2024-01-31 15:09:22 +07:00
/ * *
* API ล บ อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_035 - ล บ อ ั ต ร า ก ำ ล ั ง ( ADMIN ) # 38
*
* @param { string } id Id ต ำ แ ห น ่ ง
* /
@Delete ( "master/{id}" )
2024-08-16 17:36:08 +07:00
async deletePosMaster ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
await new permission ( ) . PermissionUpdate ( request , "SYS_ORG" ) ;
2024-01-31 15:09:22 +07:00
const delPosMaster = await this . posMasterRepository . findOne ( {
where : { id } ,
// relations: ["position"],
} ) ;
if ( ! delPosMaster ) {
2024-02-28 14:00:38 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งในสายงานนี้" ) ;
2024-01-31 15:09:22 +07:00
}
2024-02-28 11:31:01 +07:00
await this . positionRepository . delete ( { posMasterId : id } ) ;
await this . posMasterRepository . delete ( { id } ) ;
return new HttpSuccess ( ) ;
2024-01-31 15:09:22 +07:00
}
2024-01-31 17:22:45 +07:00
2024-09-19 10:51:31 +07:00
/ * *
* API ร า ย ก า ร อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_070 - ร า ย ก า ร อ ั ต ร า ก ำ ล ั ง ( ADMIN Menu ) # 56
*
* /
@Post ( "admin/master/list" )
async listForAdmin (
@Request ( ) request : RequestWithUser ,
@Body ( )
body : {
id : string ;
revisionId : string ;
type : number ;
isAll : boolean ;
2024-10-03 15:37:28 +07:00
isBlank : boolean ;
2024-09-19 10:51:31 +07:00
page : number ;
pageSize : number ;
keyword? : string ;
} ,
) {
let typeCondition : any = { } ;
let checkChildConditions : any = { } ;
let keywordAsInt : any ;
let searchShortName = "" ;
let labelName = "" ;
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 {
}
2024-10-03 15:37:28 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-09-19 10:51:31 +07:00
} 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 {
}
2024-10-03 15:37:28 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-09-19 10:51:31 +07:00
} 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 {
}
2024-10-03 15:37:28 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-09-19 10:51:31 +07:00
} 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 {
}
2024-10-03 15:37:28 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-09-19 10:51:31 +07:00
} else if ( body . type === 4 ) {
typeCondition = {
orgChild4Id : body.id ,
} ;
searchShortName = ` CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
2024-10-03 15:37:28 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-09-19 10:51:31 +07:00
}
let findPosition : any ;
let masterId = new Array ( ) ;
if ( body . keyword != null && body . keyword != "" ) {
const findTypes : PosType [ ] = await this . posTypeRepository . find ( {
where : { posTypeName : Like ( ` % ${ body . keyword } % ` ) } ,
select : [ "id" ] ,
} ) ;
findPosition = await this . positionRepository . find ( {
where : { posTypeId : In ( findTypes . map ( ( x ) = > x . id ) ) } ,
select : [ "posMasterId" ] ,
} ) ;
masterId = masterId . concat ( findPosition . map ( ( position : any ) = > position . posMasterId ) ) ;
const findLevel : PosLevel [ ] = await this . posLevelRepository . find ( {
where : { posLevelName : Like ( ` % ${ body . keyword } % ` ) } ,
select : [ "id" ] ,
} ) ;
findPosition = await this . positionRepository . find ( {
where : { posLevelId : In ( findLevel . map ( ( x ) = > x . id ) ) } ,
select : [ "posMasterId" ] ,
} ) ;
masterId = masterId . concat ( findPosition . map ( ( position : any ) = > position . posMasterId ) ) ;
const findExecutive : PosExecutive [ ] = await this . posExecutiveRepository . find ( {
where : { posExecutiveName : Like ( ` % ${ body . keyword } % ` ) } ,
select : [ "id" ] ,
} ) ;
findPosition = await this . positionRepository . find ( {
where : { posExecutiveId : In ( findExecutive . map ( ( x ) = > x . id ) ) } ,
select : [ "posMasterId" ] ,
} ) ;
masterId = masterId . concat ( findPosition . map ( ( position : any ) = > position . posMasterId ) ) ;
findPosition = await this . positionRepository . 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 } % ` ) } ) ) ,
} ,
] ;
let [ posMaster , total ] = await AppDataSource . getRepository ( PosMaster )
. 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" )
2024-10-07 17:00:54 +07:00
. leftJoinAndSelect ( "posMaster.posMasterAssigns" , "posMasterAssigns" )
. leftJoinAndSelect ( "posMasterAssigns.assign" , "assign" )
2024-09-19 10:51:31 +07:00
. where ( conditions )
. andWhere (
new Brackets ( ( qb ) = > {
qb . 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 ( ) ;
//แก้ค้นหา
let _position : any [ ] = [ ] ;
let x : any = null ;
let y : any = null ;
if ( body . keyword != null && body . keyword != "" ) {
const position = await this . positionRepository . find ( {
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
where : { posMasterId : In ( posMaster . map ( ( x ) = > x . id ) ) } ,
order : { createdAt : "ASC" } ,
} ) ;
for ( let data of position ) {
x = data . posMasterId ;
if ( y != x ) {
if (
data . positionName . includes ( body . keyword ) ||
data . posType . posTypeName . includes ( body . keyword ) ||
data . posLevel . posLevelName . includes ( body . keyword )
) {
_position . push ( data ) ;
}
}
y = x ;
}
}
if ( _position . length > 0 ) {
posMaster = posMaster . filter ( ( x ) = > _position . some ( ( y ) = > y . posMasterId === x . id ) ) ;
}
const formattedData = await Promise . all (
posMaster . map ( async ( posMaster ) = > {
const positions = await this . positionRepository . find ( {
where : {
posMasterId : posMaster.id ,
} ,
relations : [ "posLevel" , "posType" , "posExecutive" ] ,
order : {
createdAt : "ASC" ,
} ,
} ) ;
const authRoleName = await this . authRoleRepo . findOne ( {
where : { id : String ( posMaster . authRoleId ) } ,
} ) ;
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 . posTypeRepository . findOne ( {
where : { id : String ( profile ? . posTypeId ) } ,
} ) ;
const level = await this . posLevelRepository . 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 ? posMaster.posMasterNoPrefix : null ,
posMasterNo : posMaster.posMasterNo ? posMaster.posMasterNo : null ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ? posMaster.posMasterNoSuffix : null ,
reason : posMaster.reason ,
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 ,
authRoleId : posMaster.authRoleId ,
authRoleName :
authRoleName == null || authRoleName . roleName == null ? null : authRoleName . roleName ,
2024-10-07 17:00:54 +07:00
isPosMasterAssign : posMaster.posMasterAssigns.length > 0 ? true : false ,
posMasterAssigns : posMaster.posMasterAssigns.map ( ( x ) = > ( {
id : x.id ,
assignId : x.assignId ,
commandSysId : x.assign.commandSysId ,
name : x.assign.name ,
description : x.assign.description ,
} ) ) ,
2024-09-19 10:51:31 +07:00
positions : positions.map ( ( position : any ) = > ( {
id : position.id ,
positionName : position.positionName ,
positionField : position.positionField ,
posTypeId : position.posTypeId ,
posTypeName : position.posType == null ? null : position . posType . posTypeName ,
posLevelId : position.posLevelId ,
posLevelName : position.posLevel == null ? null : position . posLevel . posLevelName ,
posExecutiveId : position.posExecutiveId ,
posExecutiveName :
position . posExecutive == null ? null : position . posExecutive . posExecutiveName ,
positionExecutiveField : position.positionExecutiveField ,
positionArea : position.positionArea ,
positionIsSelected : position.positionIsSelected ,
isSpecial : position.isSpecial ,
} ) ) ,
} ;
} ) ,
) ;
return new HttpSuccess ( { data : formattedData , total } ) ;
}
2024-01-31 17:22:45 +07:00
/ * *
* API ร า ย ก า ร อ ั ต ร า ก ำ ล ั ง
*
2024-02-20 16:01:05 +07:00
* @summary ORG_070 - ร า ย ก า ร อ ั ต ร า ก ำ ล ั ง ( ADMIN ) # 56
2024-01-31 17:22:45 +07:00
*
* /
@Post ( "master/list" )
async list (
2024-09-16 18:00:44 +07:00
@Request ( ) request : RequestWithUser ,
2024-01-31 17:22:45 +07:00
@Body ( )
body : {
id : string ;
2024-02-20 15:42:10 +07:00
revisionId : string ;
2024-01-31 17:22:45 +07:00
type : number ;
2024-01-31 17:51:49 +07:00
isAll : boolean ;
page : number ;
pageSize : number ;
keyword? : string ;
2024-01-31 17:22:45 +07:00
} ,
) {
2024-02-28 11:31:01 +07:00
let typeCondition : any = { } ;
let checkChildConditions : any = { } ;
let keywordAsInt : any ;
let searchShortName = "" ;
2024-03-04 09:43:46 +07:00
let labelName = "" ;
2024-02-28 11:31:01 +07:00
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) ` ;
2024-09-16 18:00:44 +07:00
let _data = await new permission ( ) . PermissionOrgList ( request , "SYS_ORG" ) ;
2024-02-28 11:31:01 +07:00
if ( body . type === 0 ) {
typeCondition = {
orgRootId : body.id ,
} ;
if ( ! body . isAll ) {
checkChildConditions = {
orgChild1Id : IsNull ( ) ,
2024-02-01 20:05:26 +07:00
} ;
2024-02-28 11:31:01 +07:00
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 ( ) ,
2024-02-01 20:05:26 +07:00
} ;
2024-02-28 11:31:01 +07:00
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 ( ) ,
2024-02-01 20:05:26 +07:00
} ;
2024-02-28 11:31:01 +07:00
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 ) {
2024-04-25 18:45:22 +07:00
checkChildConditions = {
2024-02-28 11:31:01 +07:00
orgChild4Id : IsNull ( ) ,
2024-02-01 20:05:26 +07:00
} ;
2024-02-28 11:31:01 +07:00
searchShortName = ` CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
} else {
2024-02-01 20:05:26 +07:00
}
2024-02-28 11:31:01 +07:00
} 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 : PosType [ ] = await this . posTypeRepository . find ( {
where : { posTypeName : Like ( ` % ${ body . keyword } % ` ) } ,
select : [ "id" ] ,
} ) ;
findPosition = await this . positionRepository . find ( {
where : { posTypeId : In ( findTypes . map ( ( x ) = > x . id ) ) } ,
select : [ "posMasterId" ] ,
} ) ;
masterId = masterId . concat ( findPosition . map ( ( position : any ) = > position . posMasterId ) ) ;
const findLevel : PosLevel [ ] = await this . posLevelRepository . find ( {
where : { posLevelName : Like ( ` % ${ body . keyword } % ` ) } ,
select : [ "id" ] ,
} ) ;
findPosition = await this . positionRepository . find ( {
where : { posLevelId : In ( findLevel . map ( ( x ) = > x . id ) ) } ,
select : [ "posMasterId" ] ,
} ) ;
masterId = masterId . concat ( findPosition . map ( ( position : any ) = > position . posMasterId ) ) ;
const findExecutive : PosExecutive [ ] = await this . posExecutiveRepository . find ( {
where : { posExecutiveName : Like ( ` % ${ body . keyword } % ` ) } ,
select : [ "id" ] ,
} ) ;
findPosition = await this . positionRepository . find ( {
where : { posExecutiveId : In ( findExecutive . map ( ( x ) = > x . id ) ) } ,
select : [ "posMasterId" ] ,
} ) ;
masterId = masterId . concat ( findPosition . map ( ( position : any ) = > position . posMasterId ) ) ;
findPosition = await this . positionRepository . 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" ;
2024-02-03 13:09:44 +07:00
}
2024-02-28 11:31:01 +07:00
masterId = [ . . . new Set ( masterId ) ] ;
}
2024-11-04 13:29:30 +07:00
2024-02-28 11:31:01 +07:00
const revisionCondition = {
orgRevisionId : body.revisionId ,
} ;
2024-09-18 17:58:34 +07:00
const chkRevision = await this . orgRevisionRepository . findOne ( {
where : { id : body.revisionId } ,
} ) ;
if ( chkRevision != null && chkRevision . orgRevisionIsDraft == true )
_data = {
root : null ,
child1 : null ,
child2 : null ,
child3 : null ,
child4 : null ,
privilege : "OWNER" ,
} ;
2024-11-04 13:29:30 +07:00
const conditions = [
{
. . . checkChildConditions ,
. . . typeCondition ,
. . . revisionCondition ,
. . . ( body . keyword &&
( masterId . length > 0
? { id : In ( masterId ) }
: { posMasterNo : Like ( ` % ${ body . keyword } % ` ) } ) ) ,
} ,
] ;
let [ posMaster , total ] = await AppDataSource . getRepository ( PosMaster )
. 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 )
. andWhere (
_data . root != undefined && _data . root != null
? _data . root [ 0 ] != null
? ` posMaster.orgRootId IN (:...root) `
: ` posMaster.orgRootId is null `
: "1=1" ,
2024-09-16 18:00:44 +07:00
{
2024-11-04 13:29:30 +07:00
root : _data.root ,
2024-09-16 18:00:44 +07:00
} ,
2024-11-04 13:29:30 +07:00
)
. andWhere (
_data . child1 != undefined && _data . child1 != null
? _data . child1 [ 0 ] != null
? ` posMaster.orgChild1Id IN (:...child1) `
: ` posMaster.orgChild1Id is null `
: "1=1" ,
{
child1 : _data.child1 ,
} ,
)
. andWhere (
_data . child2 != undefined && _data . child2 != null
? _data . child2 [ 0 ] != null
? ` posMaster.orgChild2Id IN (:...child2) `
: ` posMaster.orgChild2Id is null `
: "1=1" ,
{
child2 : _data.child2 ,
} ,
)
. andWhere (
_data . child3 != undefined && _data . child3 != null
? _data . child3 [ 0 ] != null
? ` posMaster.orgChild3Id IN (:...child3) `
: ` posMaster.orgChild3Id is null `
: "1=1" ,
{
child3 : _data.child3 ,
} ,
)
. andWhere (
_data . child4 != undefined && _data . child4 != null
? _data . child4 [ 0 ] != null
? ` posMaster.orgChild4Id IN (:...child4) `
: ` posMaster.orgChild4Id is null `
: "1=1" ,
{
child4 : _data.child4 ,
} ,
)
. 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 ( ) ;
//แก้ค้นหา
let _position : any [ ] = [ ] ;
let x : any = null ;
let y : any = null ;
if ( body . keyword != null && body . keyword != "" ) {
const position = await this . positionRepository . find ( {
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
where : { posMasterId : In ( posMaster . map ( ( x ) = > x . id ) ) } ,
order : { createdAt : "ASC" } ,
} ) ;
for ( let data of position ) {
x = data . posMasterId ;
if ( y != x ) {
if (
data . positionName . includes ( body . keyword ) ||
data . posType . posTypeName . includes ( body . keyword ) ||
data . posLevel . posLevelName . includes ( body . keyword )
) {
_position . push ( data ) ;
2024-05-09 18:01:47 +07:00
}
}
2024-11-04 13:29:30 +07:00
y = x ;
2024-05-09 18:01:47 +07:00
}
2024-11-04 13:29:30 +07:00
}
if ( _position . length > 0 ) {
posMaster = posMaster . filter ( ( x ) = > _position . some ( ( y ) = > y . posMasterId === x . id ) ) ;
}
2024-05-09 18:01:47 +07:00
2024-02-28 11:31:01 +07:00
const formattedData = await Promise . all (
posMaster . map ( async ( posMaster ) = > {
const positions = await this . positionRepository . find ( {
where : {
posMasterId : posMaster.id ,
} ,
relations : [ "posLevel" , "posType" , "posExecutive" ] ,
2024-04-29 16:01:47 +07:00
order : {
createdAt : "ASC" ,
2024-04-26 17:51:53 +07:00
} ,
2024-02-28 11:31:01 +07:00
} ) ;
2024-01-31 18:24:38 +07:00
2024-06-14 12:28:09 +07:00
const authRoleName = await this . authRoleRepo . findOne ( {
2024-06-19 23:03:54 +07:00
where : { id : String ( posMaster . authRoleId ) } ,
2024-06-14 12:28:09 +07:00
} ) ;
2024-02-28 11:31:01 +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 ) } ,
2024-02-15 10:31:47 +07:00
} ) ;
2024-02-28 11:31:01 +07:00
} else if ( ! chkRevision ? . orgRevisionIsCurrent && chkRevision ? . orgRevisionIsDraft ) {
profile = await this . profileRepository . findOne ( {
where : { id : String ( posMaster . next_holderId ) } ,
2024-02-15 10:31:47 +07:00
} ) ;
2024-02-28 11:31:01 +07:00
}
const type = await this . posTypeRepository . findOne ( {
where : { id : String ( profile ? . posTypeId ) } ,
} ) ;
const level = await this . posLevelRepository . findOne ( {
where : { id : String ( profile ? . posLevelId ) } ,
} ) ;
2024-01-31 18:24:38 +07:00
2024-02-28 11:31:01 +07:00
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 ;
}
2024-02-12 10:57:35 +07:00
2024-02-28 11:31:01 +07:00
return {
id : posMaster.id ,
orgRootId : posMaster.orgRootId ,
orgChild1Id : posMaster.orgChild1Id ,
orgChild2Id : posMaster.orgChild2Id ,
orgChild3Id : posMaster.orgChild3Id ,
orgChild4Id : posMaster.orgChild4Id ,
2024-04-25 10:00:12 +07:00
posMasterNoPrefix : posMaster.posMasterNoPrefix ? posMaster.posMasterNoPrefix : null ,
posMasterNo : posMaster.posMasterNo ? posMaster.posMasterNo : null ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ? posMaster.posMasterNoSuffix : null ,
2024-04-09 12:18:52 +07:00
reason : posMaster.reason ,
2024-02-28 11:31:01 +07:00
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 ,
2024-06-14 12:28:09 +07:00
authRoleId : posMaster.authRoleId ,
2024-06-19 23:03:54 +07:00
authRoleName :
authRoleName == null || authRoleName . roleName == null ? null : authRoleName . roleName ,
2024-05-20 15:25:18 +07:00
positions : positions.map ( ( position : any ) = > ( {
2024-02-28 11:31:01 +07:00
id : position.id ,
positionName : position.positionName ,
positionField : position.positionField ,
posTypeId : position.posTypeId ,
posTypeName : position.posType == null ? null : position . posType . posTypeName ,
posLevelId : position.posLevelId ,
posLevelName : position.posLevel == null ? null : position . posLevel . posLevelName ,
posExecutiveId : position.posExecutiveId ,
posExecutiveName :
position . posExecutive == null ? null : position . posExecutive . posExecutiveName ,
positionExecutiveField : position.positionExecutiveField ,
positionArea : position.positionArea ,
positionIsSelected : position.positionIsSelected ,
isSpecial : position.isSpecial ,
} ) ) ,
} ;
} ) ,
) ;
return new HttpSuccess ( { data : formattedData , total } ) ;
2024-01-31 17:22:45 +07:00
}
2024-02-01 13:07:54 +07:00
/ * *
2024-02-01 15:06:28 +07:00
* API จ ั ด ล ำ ด ั บ ต ำ แ ห น ่ ง
*
* @summary ORG_040 - จ ั ด ล ำ ด ั บ ต ำ แ ห น ่ ง ( ADMIN ) # 43
*
* /
2024-02-01 13:07:54 +07:00
@Post ( "sort" )
2024-08-30 21:02:14 +07:00
async Sort (
@Body ( ) requestBody : { id : string ; type : number ; sortId : string [ ] } ,
@Request ( ) request : RequestWithUser ,
) {
2024-08-16 17:36:08 +07:00
await new permission ( ) . PermissionUpdate ( request , "SYS_ORG" ) ;
2024-10-04 18:12:23 +07:00
const before = null ;
2024-02-28 11:31:01 +07:00
switch ( requestBody . type ) {
case 0 : {
const rootId = await this . posMasterRepository . findOne ( {
where : { orgRootId : requestBody.id } ,
} ) ;
if ( ! rootId ? . id ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found rootId: " + requestBody . id ) ;
2024-02-01 13:07:54 +07:00
}
2024-02-28 11:31:01 +07:00
const listPosMasterId_0 = await this . posMasterRepository . 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." ) ;
2024-02-01 13:07:54 +07:00
}
2024-02-28 11:31:01 +07:00
const sortData_0 = listPosMasterId_0 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
2024-10-04 18:12:23 +07:00
await this . posMasterRepository . save ( sortData_0 , { data : request } ) ;
setLogDataDiff ( request , { before , after : sortData_0 } ) ;
2024-02-28 11:31:01 +07:00
break ;
}
2024-02-01 13:07:54 +07:00
2024-02-28 11:31:01 +07:00
case 1 : {
const child1Id = await this . posMasterRepository . findOne ( {
where : { orgChild1Id : requestBody.id } ,
} ) ;
if ( ! child1Id ? . id ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found child1Id: " + requestBody . id ) ;
}
const listPosMasterId_1 = await this . posMasterRepository . 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." ) ;
2024-02-01 13:07:54 +07:00
}
2024-02-28 11:31:01 +07:00
const sortData_1 = listPosMasterId_1 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
2024-10-04 18:12:23 +07:00
await this . posMasterRepository . save ( sortData_1 , { data : request } ) ;
setLogDataDiff ( request , { before , after : sortData_1 } ) ;
2024-02-28 11:31:01 +07:00
break ;
}
2024-02-01 13:07:54 +07:00
2024-02-28 11:31:01 +07:00
case 2 : {
const child2Id = await this . posMasterRepository . findOne ( {
where : { orgChild2Id : requestBody.id } ,
} ) ;
if ( ! child2Id ? . id ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found child2Id: " + requestBody . id ) ;
}
const listPosMasterId_2 = await this . posMasterRepository . 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." ) ;
2024-02-01 13:07:54 +07:00
}
2024-02-28 11:31:01 +07:00
const sortData_2 = listPosMasterId_2 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
2024-10-04 18:12:23 +07:00
await this . posMasterRepository . save ( sortData_2 , { data : request } ) ;
setLogDataDiff ( request , { before , after : sortData_2 } ) ;
2024-02-28 11:31:01 +07:00
break ;
}
2024-02-01 13:07:54 +07:00
2024-02-28 11:31:01 +07:00
case 3 : {
const child3Id = await this . posMasterRepository . findOne ( {
where : { orgChild3Id : requestBody.id } ,
} ) ;
if ( ! child3Id ? . id ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found chil3Id: " + requestBody . id ) ;
}
const listPosMasterId_3 = await this . posMasterRepository . 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." ) ;
2024-02-01 13:07:54 +07:00
}
2024-02-28 11:31:01 +07:00
const sortData_3 = listPosMasterId_3 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
2024-10-04 18:12:23 +07:00
await this . posMasterRepository . save ( sortData_3 , { data : request } ) ;
setLogDataDiff ( request , { before , after : sortData_3 } ) ;
2024-02-28 11:31:01 +07:00
break ;
}
2024-02-01 13:07:54 +07:00
2024-02-28 11:31:01 +07:00
case 4 : {
const child4Id = await this . posMasterRepository . findOne ( {
where : { orgChild4Id : requestBody.id } ,
} ) ;
if ( ! child4Id ? . id ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found child4Id: " + requestBody . id ) ;
}
const listPosMasterId_4 = await this . posMasterRepository . 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 ,
} ) ) ;
2024-10-04 18:12:23 +07:00
await this . posMasterRepository . save ( sortData_4 , { data : request } ) ;
setLogDataDiff ( request , { before , after : sortData_4 } ) ;
2024-02-28 11:31:01 +07:00
break ;
2024-02-01 13:07:54 +07:00
}
2024-02-28 11:31:01 +07:00
default :
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found type: " + requestBody . type ) ;
2024-02-01 13:07:54 +07:00
}
2024-02-28 11:31:01 +07:00
return new HttpSuccess ( ) ;
2024-02-01 13:07:54 +07:00
}
2024-02-01 15:21:23 +07:00
2024-02-01 20:05:26 +07:00
/ * *
* API ด ู ป ร ะ ว ั ต ิ อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_054 - ด ู ป ร ะ ว ั ต ิ อ ั ต ร า ก ำ ล ั ง ( ADMIN ) # 58
*
* @param { string } id Id อ ั ต ร า ก ำ ล ั ง
* /
@Get ( "history/{id}" )
2024-09-13 10:29:08 +07:00
async getHistoryPosMater ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
2024-10-22 08:20:45 +07:00
let _workflow = await new permission ( ) . Workflow ( request , id , "SYS_ORG" ) ;
if ( _workflow == false ) await new permission ( ) . PermissionGet ( request , "SYS_ORG" ) ;
2024-02-01 20:05:26 +07:00
const posMaster = await this . posMasterRepository . findOne ( {
where : { id } ,
} ) ;
if ( ! posMaster ) {
2024-02-28 14:00:38 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
2024-02-01 20:05:26 +07:00
}
const posMasters = await this . posMasterRepository . find ( {
2024-02-13 15:36:23 +07:00
where : {
ancestorDNA :
posMaster . ancestorDNA == null || posMaster . ancestorDNA == ""
? "123"
: posMaster . ancestorDNA ,
} ,
2024-02-01 20:05:26 +07:00
order : { lastUpdatedAt : "DESC" } ,
2024-07-08 11:06:40 +07:00
relations : [
"orgRoot" ,
"orgChild1" ,
"orgChild2" ,
"orgChild3" ,
"orgChild4" ,
"current_holder" ,
"positions" ,
"positions.posLevel" ,
"positions.posType" ,
"positions.posExecutive" ,
] ,
2024-02-01 20:05:26 +07:00
} ) ;
const _data = posMasters . map ( ( item ) = > ( {
id : item.id ,
orgShortName :
item . orgRoot == null
2024-02-12 12:23:37 +07:00
? null
2024-02-01 20:05:26 +07:00
: item . orgChild1 == null
2024-02-02 16:15:51 +07:00
? item . orgRoot . orgRootShortName
2024-02-01 20:05:26 +07:00
: item . orgChild2 == null
? item . orgChild1 . orgChild1ShortName
: item . orgChild3 == null
? item . orgChild2 . orgChild2ShortName
: item . orgChild4 == null
? item . orgChild3 . orgChild3ShortName
: item . orgChild4 . orgChild4ShortName ,
2024-02-13 17:09:06 +07:00
lastUpdatedAt : item.lastUpdatedAt ? item.lastUpdatedAt : null ,
2024-02-13 15:36:23 +07:00
posMasterNoPrefix : item.posMasterNoPrefix ? item.posMasterNoPrefix : null ,
posMasterNo : item.posMasterNo ? item.posMasterNo : null ,
posMasterNoSuffix : item.posMasterNoSuffix ? item.posMasterNoSuffix : null ,
2024-04-09 12:18:52 +07:00
reason : item.reason ? item.reason : null ,
2024-07-08 11:06:40 +07:00
position : item.positions.map ( ( x ) = > x . positionName ) . join ( "/" ) ,
2024-07-08 20:37:55 +07:00
posExecutive : item.positions
. filter ( ( x ) = > x . posExecutive != null )
. map ( ( x ) = > x . posExecutive ? . posExecutiveName ? ? null )
. join ( "/" ) ,
2024-07-08 11:06:40 +07:00
posLevel : item.positions.map ( ( x ) = > x . posLevel . posLevelName ) . join ( "/" ) ,
posType : item.positions.map ( ( x ) = > x . posType . posTypeName ) . join ( "/" ) ,
fullname :
2024-07-08 16:32:02 +07:00
( item ? . current_holder ? . prefix ? ? "" ) +
"" +
( item ? . current_holder ? . firstName ? ? "" ) +
2024-07-08 11:06:40 +07:00
" " +
2024-07-08 16:32:02 +07:00
( item ? . current_holder ? . lastName ? ? "" ) ,
2024-02-01 20:05:26 +07:00
} ) ) ;
return new HttpSuccess ( _data ) ;
}
/ * *
* API ย ้ า ย อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_054 - ย ้ า ย อ ั ต ร า ก ำ ล ั ง ( ADMIN ) # 59
*
* /
@Post ( "move" )
async movePosMaster (
@Body ( ) requestBody : { id : string ; type : number ; positionMaster : string [ ] } ,
2024-08-16 17:36:08 +07:00
@Request ( ) request : RequestWithUser ,
2024-02-01 20:05:26 +07:00
) {
2024-08-16 17:36:08 +07:00
await new permission ( ) . PermissionUpdate ( request , "SYS_ORG" ) ;
2024-02-28 11:31:01 +07:00
const posMasters = await this . posMasterRepository . find ( {
where : { id : In ( requestBody . positionMaster ) } ,
} ) ;
2024-02-01 20:05:26 +07:00
2024-03-04 15:34:38 +07:00
const type0LastPosMasterNo =
requestBody . type == 0
? await this . posMasterRepository . find ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) ,
} ,
} )
: [ ] ;
const type1LastPosMasterNo =
requestBody . type == 1
? await this . posMasterRepository . find ( {
where : {
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) ,
} ,
} )
: [ ] ;
const type2LastPosMasterNo =
requestBody . type == 2
? await this . posMasterRepository . find ( {
where : {
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) ,
} ,
} )
: [ ] ;
const type3LastPosMasterNo =
requestBody . type == 3
? await this . posMasterRepository . find ( {
where : {
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) ,
} ,
} )
: [ ] ;
const type4LastPosMasterNo =
requestBody . type == 4
? await this . posMasterRepository . find ( {
where : {
orgChild4Id : requestBody.id ,
} ,
} )
: [ ] ;
const allLastPosMasterNo = [
. . . type0LastPosMasterNo ,
. . . type1LastPosMasterNo ,
. . . type2LastPosMasterNo ,
. . . type3LastPosMasterNo ,
. . . type4LastPosMasterNo ,
] ;
2024-04-09 12:18:52 +07:00
// let maxPosMasterNo = Math.max(...allLastPosMasterNo.map((pos) => pos.posMasterNo), 0);
2024-04-29 16:01:47 +07:00
let _shortName : string ;
2024-03-04 15:34:38 +07:00
let maxPosMasterOrder = Math . max ( . . . allLastPosMasterNo . map ( ( pos ) = > pos . posMasterOrder ) , 0 ) ;
2024-04-09 13:08:15 +07:00
await Promise . all (
posMasters . map ( async ( posMaster : any ) = > {
let change = true ;
2024-02-01 20:05:26 +07:00
2024-04-09 13:08:15 +07:00
if ( requestBody . type == 0 ) {
const org = await this . orgRootRepository . findOne ( {
where : { id : requestBody.id } ,
2024-04-09 12:18:52 +07:00
} ) ;
2024-04-09 13:08:15 +07:00
if ( org != null ) {
2024-04-29 16:01:47 +07:00
_shortName = org . orgRootShortName ;
2024-04-09 13:08:15 +07:00
const _posMaster = await this . posMasterRepository . findOne ( {
2024-04-19 12:14:17 +07:00
where : {
orgRootId : org.id ,
posMasterNo : posMaster.posMasterNo ,
orgChild1Id : IsNull ( ) || "" ,
2024-05-02 17:39:07 +07:00
id : Not ( In ( requestBody . positionMaster ) ) ,
2024-04-19 12:14:17 +07:00
} ,
2024-04-09 13:08:15 +07:00
} ) ;
if ( _posMaster != null )
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
` เลขที่ตำแหน่ง ${ org . orgRootShortName } ${ posMaster . posMasterNo } มีอยู่ในระบบอยู่แล้ว ` ,
) ;
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 ;
}
2024-02-01 20:05:26 +07:00
}
2024-04-09 13:08:15 +07:00
if ( requestBody . type == 1 ) {
const org = await this . child1Repository . findOne ( {
where : { id : requestBody.id } ,
2024-04-09 12:18:52 +07:00
} ) ;
2024-04-09 13:08:15 +07:00
if ( org != null ) {
2024-04-29 16:01:47 +07:00
_shortName = org . orgChild1ShortName ;
2024-04-09 13:08:15 +07:00
const _posMaster = await this . posMasterRepository . findOne ( {
2024-04-19 12:14:17 +07:00
where : {
orgChild1Id : org.id ,
posMasterNo : posMaster.posMasterNo ,
orgChild2Id : IsNull ( ) || "" ,
2024-05-02 17:39:07 +07:00
id : Not ( In ( requestBody . positionMaster ) ) ,
2024-04-19 12:14:17 +07:00
} ,
2024-04-09 13:08:15 +07:00
} ) ;
if ( _posMaster != null )
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
` เลขที่ตำแหน่ง ${ org . orgChild1ShortName } ${ posMaster . posMasterNo } มีอยู่ในระบบอยู่แล้ว ` ,
) ;
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 ;
}
2024-02-01 20:05:26 +07:00
}
2024-04-09 13:08:15 +07:00
if ( requestBody . type == 2 ) {
const org = await this . child2Repository . findOne ( {
where : { id : requestBody.id } ,
2024-04-09 12:18:52 +07:00
} ) ;
2024-04-09 13:08:15 +07:00
if ( org != null ) {
2024-04-29 16:01:47 +07:00
_shortName = org . orgChild2ShortName ;
2024-04-09 13:08:15 +07:00
const _posMaster = await this . posMasterRepository . findOne ( {
2024-04-19 12:14:17 +07:00
where : {
orgChild2Id : org.id ,
posMasterNo : posMaster.posMasterNo ,
orgChild3Id : IsNull ( ) || "" ,
2024-05-02 17:39:07 +07:00
id : Not ( In ( requestBody . positionMaster ) ) ,
2024-04-19 12:14:17 +07:00
} ,
2024-04-09 13:08:15 +07:00
} ) ;
if ( _posMaster != null )
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
` เลขที่ตำแหน่ง ${ org . orgChild2ShortName } ${ posMaster . posMasterNo } มีอยู่ในระบบอยู่แล้ว ` ,
) ;
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 ;
}
2024-02-01 20:05:26 +07:00
}
2024-04-09 13:08:15 +07:00
if ( requestBody . type == 3 ) {
const org = await this . child3Repository . findOne ( {
where : { id : requestBody.id } ,
2024-04-09 12:18:52 +07:00
} ) ;
2024-04-09 13:08:15 +07:00
if ( org != null ) {
2024-04-29 16:01:47 +07:00
_shortName = org . orgChild3ShortName ;
2024-04-09 13:08:15 +07:00
const _posMaster = await this . posMasterRepository . findOne ( {
2024-04-19 12:14:17 +07:00
where : {
orgChild3Id : org.id ,
posMasterNo : posMaster.posMasterNo ,
orgChild4Id : IsNull ( ) || "" ,
2024-05-02 17:39:07 +07:00
id : Not ( In ( requestBody . positionMaster ) ) ,
2024-04-19 12:14:17 +07:00
} ,
2024-04-09 13:08:15 +07:00
} ) ;
if ( _posMaster != null )
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
` เลขที่ตำแหน่ง ${ org . orgChild3ShortName } ${ posMaster . posMasterNo } มีอยู่ในระบบอยู่แล้ว ` ,
) ;
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 ;
}
2024-02-01 20:05:26 +07:00
}
2024-04-09 13:08:15 +07:00
if ( requestBody . type == 4 ) {
const org = await this . child4Repository . findOne ( {
where : { id : requestBody.id } ,
2024-04-09 12:18:52 +07:00
} ) ;
2024-04-09 13:08:15 +07:00
if ( org != null ) {
2024-04-29 16:01:47 +07:00
_shortName = org . orgChild4ShortName ;
2024-04-09 13:08:15 +07:00
const _posMaster = await this . posMasterRepository . findOne ( {
2024-04-19 12:14:17 +07:00
where : {
orgChild4Id : org.id ,
posMasterNo : posMaster.posMasterNo ,
2024-05-02 17:39:07 +07:00
id : Not ( In ( requestBody . positionMaster ) ) ,
2024-04-19 12:14:17 +07:00
} ,
2024-04-09 13:08:15 +07:00
} ) ;
if ( _posMaster != null )
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
` เลขที่ตำแหน่ง ${ org . orgChild4ShortName } ${ posMaster . posMasterNo } มีอยู่ในระบบอยู่แล้ว ` ,
) ;
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 ;
}
2024-02-01 20:05:26 +07:00
}
2024-04-26 17:14:24 +07:00
2024-04-30 15:21:20 +07:00
//Check All Branch
2024-04-26 17:14:24 +07:00
const orgRevision = await this . orgRevisionRepository . findOne ( {
where : {
2024-04-29 16:01:47 +07:00
orgRevisionIsDraft : true ,
2024-04-26 17:14:24 +07:00
orgRevisionIsCurrent : false ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
2024-04-26 17:14:24 +07:00
const _orgRoot = await this . orgRootRepository . find ( {
where : {
orgRevisionId : orgRevision?.id ,
orgRootShortName : _shortName ,
id : Not ( requestBody . id ) ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( _orgRoot . length > 0 ) {
for ( const r of _orgRoot ) {
2024-04-26 17:14:24 +07:00
const _posMasterRoot = await this . posMasterRepository . findOne ( {
where : {
orgRevisionId : orgRevision?.id ,
orgRootId : r.id ,
orgChild1Id : IsNull ( ) || "" ,
2024-04-29 16:01:47 +07:00
posMasterNo : posMaster.posMasterNo ,
2024-05-02 17:39:07 +07:00
id : Not ( In ( requestBody . positionMaster ) ) ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( _posMasterRoot != null ) {
2024-04-26 17:14:24 +07:00
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
` เลขที่ตำแหน่ง ${ r . orgRootShortName } ${ posMaster . posMasterNo } มีอยู่ในระบบอยู่แล้ว ` ,
) ;
}
}
}
//child1
const _orgChild1 = await this . child1Repository . find ( {
where : {
orgRevisionId : orgRevision?.id ,
orgChild1ShortName : _shortName ,
id : Not ( requestBody . id ) ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( _orgChild1 . length > 0 ) {
for ( const c1 of _orgChild1 ) {
2024-04-26 17:14:24 +07:00
const _posMasterChild1 = await this . posMasterRepository . findOne ( {
where : {
orgRevisionId : orgRevision?.id ,
orgChild1Id : c1.id ,
orgChild2Id : IsNull ( ) || "" ,
2024-04-29 16:01:47 +07:00
posMasterNo : posMaster.posMasterNo ,
2024-05-02 17:39:07 +07:00
id : Not ( In ( requestBody . positionMaster ) ) ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( _posMasterChild1 != null ) {
2024-04-26 17:14:24 +07:00
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
` เลขที่ตำแหน่ง ${ c1 . orgChild1ShortName } ${ posMaster . posMasterNo } มีอยู่ในระบบอยู่แล้ว ` ,
) ;
}
}
}
//child2
const _orgChild2 = await this . child2Repository . find ( {
where : {
orgRevisionId : orgRevision?.id ,
orgChild2ShortName : _shortName ,
id : Not ( requestBody . id ) ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( _orgChild2 . length > 0 ) {
for ( const c2 of _orgChild2 ) {
2024-04-26 17:14:24 +07:00
const _posMasterChild2 = await this . posMasterRepository . findOne ( {
where : {
orgRevisionId : orgRevision?.id ,
orgChild2Id : c2.id ,
orgChild3Id : IsNull ( ) || "" ,
2024-04-29 16:01:47 +07:00
posMasterNo : posMaster.posMasterNo ,
2024-05-02 17:39:07 +07:00
id : Not ( In ( requestBody . positionMaster ) ) ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( _posMasterChild2 != null ) {
2024-04-26 17:14:24 +07:00
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
` เลขที่ตำแหน่ง ${ c2 . orgChild2ShortName } ${ posMaster . posMasterNo } มีอยู่ในระบบอยู่แล้ว ` ,
) ;
}
}
}
//child3
const _orgChild3 = await this . child3Repository . find ( {
where : {
orgRevisionId : orgRevision?.id ,
orgChild3ShortName : _shortName ,
id : Not ( requestBody . id ) ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( _orgChild3 . length > 0 ) {
for ( const c3 of _orgChild3 ) {
2024-04-26 17:14:24 +07:00
const _posMasterChild3 = await this . posMasterRepository . findOne ( {
where : {
orgRevisionId : orgRevision?.id ,
orgChild3Id : c3.id ,
orgChild4Id : IsNull ( ) || "" ,
2024-04-29 16:01:47 +07:00
posMasterNo : posMaster.posMasterNo ,
2024-05-02 17:39:07 +07:00
id : Not ( In ( requestBody . positionMaster ) ) ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( _posMasterChild3 != null ) {
2024-04-26 17:14:24 +07:00
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
` เลขที่ตำแหน่ง ${ c3 . orgChild3ShortName } ${ posMaster . posMasterNo } มีอยู่ในระบบอยู่แล้ว ` ,
) ;
}
}
}
//child4
const _orgChild4 = await this . child4Repository . find ( {
where : {
orgRevisionId : orgRevision?.id ,
orgChild4ShortName : _shortName ,
id : Not ( requestBody . id ) ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( _orgChild4 . length > 0 ) {
for ( const c4 of _orgChild4 ) {
2024-04-26 17:14:24 +07:00
const _posMasterChild4 = await this . posMasterRepository . findOne ( {
where : {
orgRevisionId : orgRevision?.id ,
orgChild4Id : c4.id ,
2024-04-29 16:01:47 +07:00
posMasterNo : posMaster.posMasterNo ,
2024-05-02 17:39:07 +07:00
id : Not ( In ( requestBody . positionMaster ) ) ,
2024-04-29 16:01:47 +07:00
} ,
} ) ;
if ( _posMasterChild4 != null ) {
2024-04-26 17:14:24 +07:00
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
` เลขที่ตำแหน่ง ${ c4 . orgChild4ShortName } ${ posMaster . posMasterNo } มีอยู่ในระบบอยู่แล้ว ` ,
) ;
}
}
}
2024-10-04 18:12:23 +07:00
const before = null ;
2024-04-09 13:08:15 +07:00
if ( change == true ) {
posMaster . posMasterOrder = maxPosMasterOrder += 1 ;
posMaster . createdUserId = request . user . sub ;
posMaster . createdFullName = request . user . name ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
2024-08-30 21:02:14 +07:00
posMaster . createdAt = new Date ( ) ;
posMaster . lastUpdatedAt = new Date ( ) ;
2024-10-04 18:12:23 +07:00
await this . posMasterRepository . save ( posMaster , { data : request } ) ;
setLogDataDiff ( request , { before , after : posMaster } ) ;
2024-04-09 13:08:15 +07:00
}
} ) ,
) ;
2024-02-28 11:31:01 +07:00
return new HttpSuccess ( ) ;
2024-02-01 20:05:26 +07:00
}
2024-02-02 16:15:51 +07:00
/ * *
* API ต ำ แ ห น ่ ง ท ั ้ ง ห ม ด
*
* @summary ORG_055 - ต ำ แ ห น ่ ง ท ั ้ ง ห ม ด ( ADMIN ) # 60
*
* /
@Post ( "summary" )
2024-02-12 16:27:10 +07:00
async PositionSummary ( @Body ( ) requestBody : { id : string ; type : number ; isNode : boolean } ) {
2024-02-28 11:31:01 +07:00
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 . posMasterRepository . count ( {
where : { orgRootId : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
2024-02-12 10:57:35 +07:00
}
2024-02-28 11:31:01 +07:00
case 1 : {
totalPosition = await this . posMasterRepository . count ( {
where : { orgChild1Id : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgChild1Id : requestBody.id ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgChild1Id : requestBody.id ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgChild1Id : requestBody.id ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgChild1Id : requestBody.id ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
case 2 : {
totalPosition = await this . posMasterRepository . count ( {
where : { orgChild2Id : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgChild2Id : requestBody.id ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgChild2Id : requestBody.id ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgChild2Id : requestBody.id ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgChild2Id : requestBody.id ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
2024-02-02 16:15:51 +07:00
}
2024-02-28 11:31:01 +07:00
case 3 : {
totalPosition = await this . posMasterRepository . count ( {
where : { orgChild3Id : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgChild3Id : requestBody.id ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgChild3Id : requestBody.id ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgChild3Id : requestBody.id ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgChild3Id : requestBody.id ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
case 4 : {
totalPosition = await this . posMasterRepository . count ( {
where : { orgChild4Id : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgChild4Id : requestBody.id ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgChild4Id : requestBody.id ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgChild4Id : requestBody.id ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgChild4Id : requestBody.id ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
default :
break ;
}
} else {
switch ( requestBody . type ) {
case 0 : {
totalPosition = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
case 1 : {
totalPosition = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
}
case 2 : {
totalPosition = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . 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 . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . 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 . posMasterRepository . 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 . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . 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 . posMasterRepository . 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 . posMasterRepository . 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 . posMasterRepository . 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 . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild4Id : requestBody.id ,
} ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . 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 . posMasterRepository . 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 . posMasterRepository . 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 . posMasterRepository . 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 ;
2024-02-02 16:15:51 +07:00
}
}
2024-02-28 11:31:01 +07:00
summary = {
totalPosition : totalPosition ,
totalPositionCurrentUse : totalPositionCurrentUse ,
totalPositionCurrentVacant : totalPositionCurrentVacant ,
totalPositionNextUse : totalPositionNextUse ,
totalPositionNextVacant : totalPositionNextVacant ,
} ;
return new HttpSuccess ( summary ) ;
2024-02-02 16:15:51 +07:00
}
2024-02-08 15:47:22 +07:00
/ * *
2024-02-09 09:27:11 +07:00
* API ส ร ้ า ง ค น ค ร อ ง ต ำ แ ห น ่ ง
2024-02-08 15:47:22 +07:00
*
2024-02-09 09:27:11 +07:00
* @summary ORG_064 - ส ร ้ า ง ค น ค ร อ ง ต ำ แ ห น ่ ง ( ADMIN ) # 70
2024-02-08 15:47:22 +07:00
*
* /
@Post ( "profile" )
async createHolder (
@Body ( ) requestBody : { posMaster : string ; position : string ; profileId : string ; isSit : boolean } ,
2024-08-30 21:02:14 +07:00
@Request ( ) request : RequestWithUser ,
2024-02-08 15:47:22 +07:00
) {
2024-08-16 17:36:08 +07:00
await new permission ( ) . PermissionUpdate ( request , "SYS_ORG" ) ;
2024-02-08 15:47:22 +07:00
const dataMaster = await this . posMasterRepository . findOne ( {
where : { id : requestBody.posMaster } ,
relations : [ "positions" ] ,
} ) ;
if ( ! dataMaster ) {
2024-02-29 11:29:16 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
2024-02-08 15:47:22 +07:00
}
2024-02-28 11:31:01 +07:00
dataMaster . positions . forEach ( async ( position ) = > {
if ( position . id === requestBody . position ) {
position . positionIsSelected = true ;
} else {
position . positionIsSelected = false ;
}
await this . positionRepository . save ( position ) ;
} ) ;
2024-10-04 18:12:23 +07:00
const before = null ;
2024-02-28 11:31:01 +07:00
dataMaster . isSit = requestBody . isSit ;
dataMaster . next_holderId = requestBody . profileId ;
2024-10-04 18:12:23 +07:00
await this . posMasterRepository . save ( dataMaster , { data : request } ) ;
2024-10-07 17:00:54 +07:00
setLogDataDiff ( request , { before , after : dataMaster } ) ;
2024-02-08 15:47:22 +07:00
2024-02-28 11:31:01 +07:00
return new HttpSuccess ( ) ;
2024-02-08 15:47:22 +07:00
}
/ * *
2024-03-13 18:59:35 +07:00
* API ล บ ค น ค ร อ ง ต ำ แ ห น ่ ง
2024-02-08 15:47:22 +07:00
*
* @summary ORG_066 - ล บ ค น ค ร อ ง ต ำ แ ห น ่ ง ( ADMIN ) # 71
*
* @param { string } id * Id posMaster
* /
@Post ( "profile/delete/{id}" )
2024-08-16 17:36:08 +07:00
async deleteHolder ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
2024-08-30 21:02:14 +07:00
await new permission ( ) . PermissionDelete ( request , "SYS_ORG" ) ;
2024-02-08 15:47:22 +07:00
const dataMaster = await this . posMasterRepository . findOne ( {
where : { id : id } ,
relations : [ "positions" ] ,
} ) ;
if ( ! dataMaster ) {
2024-02-28 14:00:38 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
2024-02-08 15:47:22 +07:00
}
2024-02-28 11:31:01 +07:00
await this . posMasterRepository . update ( id , {
isSit : false ,
next_holderId : null ,
2024-11-13 11:32:45 +07:00
current_holderId : null ,
statusReport : "PENDING" ,
2024-02-28 11:31:01 +07:00
} ) ;
2024-02-08 15:47:22 +07:00
2024-02-28 11:31:01 +07:00
dataMaster . positions . forEach ( async ( position ) = > {
await this . positionRepository . update ( position . id , {
positionIsSelected : false ,
2024-02-08 15:47:22 +07:00
} ) ;
2024-02-28 11:31:01 +07:00
} ) ;
2024-02-08 15:47:22 +07:00
2024-02-28 11:31:01 +07:00
return new HttpSuccess ( ) ;
2024-02-08 15:47:22 +07:00
}
2024-02-12 17:06:31 +07:00
/ * *
* API ส ื บ ท อ ด ต ำ แ ห น ่ ง
*
* @summary ORG_068 - ส ื บ ท อ ด ต ำ แ ห น ่ ง ( ADMIN ) # 74
*
* /
@Post ( "dna" )
2024-08-30 21:02:14 +07:00
async dna (
@Body ( ) requestBody : { draftPositionId : string ; publishPositionId : string } ,
@Request ( ) request : RequestWithUser ,
) {
await new permission ( ) . PermissionUpdate ( request , "SYS_ORG" ) ;
2024-02-13 14:31:36 +07:00
const findDraft = await this . orgRevisionRepository . findOne ( {
2024-02-13 15:37:41 +07:00
where : {
orgRevisionIsDraft : true ,
} ,
2024-02-12 17:06:31 +07:00
} ) ;
2024-02-13 17:14:54 +07:00
if ( ! findDraft ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลโครงสร้างที่เผยแพร่" ) ;
}
2024-02-12 17:06:31 +07:00
const dataPublish = await this . posMasterRepository . findOne ( {
2024-02-13 15:37:41 +07:00
where : {
id : requestBody.publishPositionId ,
2024-02-13 14:31:36 +07:00
} ,
2024-02-12 17:06:31 +07:00
} ) ;
if ( ! dataPublish ) {
2024-02-29 11:29:16 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
2024-02-13 15:37:41 +07:00
}
2024-02-13 14:31:36 +07:00
2024-02-12 17:06:31 +07:00
const dataDraft = await this . posMasterRepository . findOne ( {
2024-02-13 15:37:41 +07:00
where : {
id : requestBody.draftPositionId ,
2024-02-13 14:31:36 +07:00
} ,
2024-02-12 17:06:31 +07:00
} ) ;
if ( ! dataDraft ) {
2024-02-29 11:29:16 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
2024-02-12 17:06:31 +07:00
}
2024-02-28 11:31:01 +07:00
await this . posMasterRepository . update (
{ orgRevisionId : findDraft.id , ancestorDNA : dataPublish.ancestorDNA } ,
{ ancestorDNA : "" } ,
) ;
if ( dataPublish . ancestorDNA == null || dataPublish . ancestorDNA == "" )
dataPublish . ancestorDNA = dataPublish . id ;
dataDraft . ancestorDNA = dataPublish . ancestorDNA ;
await this . posMasterRepository . save ( dataDraft ) ;
await this . posMasterRepository . save ( dataPublish ) ;
return new HttpSuccess ( ) ;
2024-02-12 17:06:31 +07:00
}
2024-02-20 13:35:11 +07:00
2024-10-24 19:28:53 +07:00
/ * *
* API ค ้ น ห า ต ำ แ ห น ่ ง ใ น ร ะ บ บ ส ม ั ค ร ส อ บ ข ร ก .
*
* @summary ค ้ น ห า ต ำ แ ห น ่ ง ใ น ร ะ บ บ ส ม ั ค ร ส อ บ ข ร ก .
*
* /
@Post ( "placement/search-all" )
async searchPlacementAll (
@Body ( )
body : {
node : number ;
nodeId : string ;
position? : string | null ;
posType? : string | null ;
posLevel? : string | null ;
isAll : boolean ;
isBlank : boolean ;
} ,
) {
let typeCondition : any = { } ;
2024-10-28 14:18:06 +07:00
let conditionA =
2024-11-01 10:43:41 +07:00
"positions.posTypeId LIKE :posType AND positions.posLevelId LIKE :posLevel AND positions.positionName LIKE :position" ;
2024-10-24 19:28:53 +07:00
let posType = await this . posTypeRepository . findOne ( {
where : { id : String ( body . posType ) } ,
} ) ;
let posLevel = await this . posLevelRepository . findOne ( {
where : { id : String ( body . posLevel ) } ,
} ) ;
if ( body . isAll == false ) {
if ( body . node === 0 ) {
typeCondition = {
orgRootId : body.nodeId ,
orgChild1Id : IsNull ( ) ,
} ;
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
} else if ( body . node === 1 ) {
typeCondition = {
orgChild1Id : body.nodeId ,
orgChild2Id : IsNull ( ) ,
} ;
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
} else if ( body . node === 2 ) {
typeCondition = {
orgChild2Id : body.nodeId ,
orgChild3Id : IsNull ( ) ,
} ;
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
} else if ( body . node === 3 ) {
typeCondition = {
orgChild3Id : body.nodeId ,
orgChild4Id : IsNull ( ) ,
} ;
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
} else if ( body . node === 4 ) {
typeCondition = {
orgChild4Id : body.nodeId ,
} ;
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
}
} else {
if ( body . node === 0 ) {
typeCondition = {
orgRootId : body.nodeId ,
} ;
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
} else if ( body . node === 1 ) {
typeCondition = {
orgChild1Id : body.nodeId ,
} ;
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
} else if ( body . node === 2 ) {
typeCondition = {
orgChild2Id : body.nodeId ,
} ;
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
} else if ( body . node === 3 ) {
typeCondition = {
orgChild3Id : body.nodeId ,
} ;
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
} else if ( body . node === 4 ) {
typeCondition = {
orgChild4Id : body.nodeId ,
} ;
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
}
}
const [ posMaster , total ] = await AppDataSource . getRepository ( PosMaster )
. 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.positions" , "positions" )
. leftJoinAndSelect ( "positions.posType" , "posType" )
. leftJoinAndSelect ( "positions.posLevel" , "posLevel" )
. leftJoinAndSelect ( "positions.posExecutive" , "posExecutive" )
. andWhere (
new Brackets ( ( qb ) = > {
qb . andWhere ( typeCondition ) . andWhere ( conditionA == null ? "1=1" : conditionA , {
posType : posType == null ? ` %% ` : ` ${ posType . id } ` ,
posLevel : posLevel == null ? ` %% ` : ` ${ posLevel . id } ` ,
2024-10-28 14:18:06 +07:00
position : body.position == null ? ` %% ` : ` ${ body . position } ` ,
2024-10-24 19:28:53 +07:00
} ) ;
} ) ,
)
. orderBy ( "posMaster.posMasterOrder" , "ASC" )
. getManyAndCount ( ) ;
2024-11-01 14:42:55 +07:00
const _posType = await this . posTypeRepository . find ( {
2024-11-04 13:29:30 +07:00
where : { posTypeName : In ( [ "ทั่วไป" , "วิชาการ" ] ) } ,
2024-11-01 14:42:55 +07:00
} ) ;
const _posLevel = await this . posLevelRepository . find ( {
2024-11-04 13:29:30 +07:00
where : { posLevelName : In ( [ "ปฏิบัติงาน" , "ปฏิบัติการ" ] ) } ,
2024-11-01 14:42:55 +07:00
} ) ;
2024-10-24 19:28:53 +07:00
const formattedData = await Promise . all (
posMaster . map ( async ( posMaster ) = > {
let shortName = "" ;
if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id == null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null
) {
body . node = 0 ;
shortName = posMaster . orgRoot . orgRootShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null
) {
body . node = 1 ;
shortName = posMaster . orgChild1 . orgChild1ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id == null
) {
body . node = 2 ;
shortName = posMaster . orgChild2 . orgChild2ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id !== null
) {
body . node = 3 ;
shortName = posMaster . orgChild3 . orgChild3ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id !== null
) {
body . node = 4 ;
shortName = posMaster . orgChild4 . orgChild4ShortName ;
}
let node : any = null ;
let nodeId : any = null ;
if ( posMaster . orgChild4Id != null ) {
node = 4 ;
nodeId = posMaster . orgChild4Id ;
} else if ( posMaster . orgChild3Id != null ) {
node = 3 ;
nodeId = posMaster . orgChild3Id ;
} else if ( posMaster . orgChild2Id != null ) {
node = 2 ;
nodeId = posMaster . orgChild2Id ;
} else if ( posMaster . orgChild1Id != null ) {
node = 1 ;
nodeId = posMaster . orgChild1Id ;
} else if ( posMaster . orgRootId != null ) {
node = 0 ;
nodeId = posMaster . orgRootId ;
}
2024-11-04 13:29:30 +07:00
let _position : any ;
if ( posLevel == null && posType == null && body . position != null ) {
2024-11-01 14:42:55 +07:00
_position = posMaster . positions
2024-11-04 13:29:30 +07:00
. filter (
( x : any ) = >
_posType . some ( ( y : any ) = > y . id == x . posTypeId ) &&
_posLevel . some ( ( z : any ) = > z . id == x . posLevelId ) ,
2024-11-01 14:42:55 +07:00
)
. map ( ( position ) = > ( {
id : position.id ,
positionName : position.positionName ,
positionField : position.positionField ,
posTypeId : position.posTypeId ,
posTypeName : position.posType == null ? null : position . posType . posTypeName ,
posLevelId : position.posLevelId ,
posLevelName : position.posLevel == null ? null : position . posLevel . posLevelName ,
posExecutiveId : position.posExecutiveId ,
posExecutiveName :
position . posExecutive == null ? null : position . posExecutive . posExecutiveName ,
positionExecutiveField : position.positionExecutiveField ,
positionArea : position.positionArea ,
positionIsSelected : position.positionIsSelected ,
isSpecial : position.isSpecial ,
} ) ) ;
2024-11-04 13:29:30 +07:00
} else {
2024-11-01 14:42:55 +07:00
_position = posMaster . positions . map ( ( position ) = > ( {
id : position.id ,
positionName : position.positionName ,
positionField : position.positionField ,
posTypeId : position.posTypeId ,
posTypeName : position.posType == null ? null : position . posType . posTypeName ,
posLevelId : position.posLevelId ,
posLevelName : position.posLevel == null ? null : position . posLevel . posLevelName ,
posExecutiveId : position.posExecutiveId ,
posExecutiveName :
position . posExecutive == null ? null : position . posExecutive . posExecutiveName ,
positionExecutiveField : position.positionExecutiveField ,
positionArea : position.positionArea ,
positionIsSelected : position.positionIsSelected ,
isSpecial : position.isSpecial ,
} ) ) ;
}
2024-10-24 19:28:53 +07:00
return {
id : posMaster.id ,
node : node ,
nodeId : nodeId ,
orgRootId : posMaster.orgRootId ,
orgChild1Id : posMaster.orgChild1Id ,
orgChild2Id : posMaster.orgChild2Id ,
orgChild3Id : posMaster.orgChild3Id ,
orgChild4Id : posMaster.orgChild4Id ,
2024-11-14 11:17:41 +07:00
isCondition : posMaster.isCondition ,
2024-10-24 19:28:53 +07:00
posMasterNoPrefix : posMaster.posMasterNoPrefix ,
posMasterNo : posMaster.posMasterNo ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ,
orgShortname : shortName ,
isSit : posMaster.isSit ,
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 } ` ,
isPosition : posMaster.positions.filter ( ( x ) = > x . positionName == body . position ) . length > 0 ,
2024-11-01 14:42:55 +07:00
positions : _position ,
2024-10-24 19:28:53 +07:00
} ;
} ) ,
) ;
return new HttpSuccess ( { data : formattedData , total } ) ;
}
2024-02-20 13:35:11 +07:00
/ * *
2024-05-17 11:16:39 +07:00
* API ค ้ น ห า ต ำ แ ห น ่ ง ใ น ร ะ บ บ ส ม ั ค ร ส อ บ ข ร ก .
2024-02-20 13:35:11 +07:00
*
2024-05-17 11:16:39 +07:00
* @summary ค ้ น ห า ต ำ แ ห น ่ ง ใ น ร ะ บ บ ส ม ั ค ร ส อ บ ข ร ก .
2024-02-20 13:35:11 +07:00
*
* /
@Post ( "placement/search" )
2024-04-29 16:01:47 +07:00
async searchPlacement (
@Body ( )
body : {
node : number ;
nodeId : string ;
position : string ;
2024-05-02 17:39:07 +07:00
typeCommand : string | null ;
2024-05-24 18:06:54 +07:00
posType? : string | null ;
posLevel? : string | null ;
2024-04-30 14:14:43 +07:00
isAll : boolean ;
isBlank : boolean ;
2024-04-29 16:01:47 +07:00
} ,
) {
2024-02-20 13:35:11 +07:00
let typeCondition : any = { } ;
2024-04-30 14:14:43 +07:00
let conditionA : any = null ;
2024-04-29 20:12:09 +07:00
2024-04-30 00:09:32 +07:00
let posType = await this . posTypeRepository . findOne ( {
where : { id : String ( body . posType ) } ,
2024-04-29 20:12:09 +07:00
} ) ;
2024-04-30 00:09:32 +07:00
let posLevel = await this . posLevelRepository . findOne ( {
where : { id : String ( body . posLevel ) } ,
} ) ;
2024-06-09 22:17:14 +07:00
if ( body . typeCommand == "APPOINTED" || body . typeCommand == "MOVE" ) {
conditionA = "positions.posTypeId LIKE :posType AND positions.posLevelId LIKE :posLevel" ;
} else if ( body . typeCommand == "APPOINT" ) {
conditionA = "posType.posTypeRank > :posTypeRank" ;
} else if ( body . typeCommand == "SLIP" ) {
conditionA = "positions.posTypeId LIKE :posType AND posLevel.posLevelRank > :posLevelRank" ;
}
2024-04-29 20:12:09 +07:00
2024-04-30 14:14:43 +07:00
if ( body . isAll == false ) {
if ( body . node === 0 ) {
typeCondition = {
orgRootId : body.nodeId ,
orgChild1Id : IsNull ( ) ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-04-30 14:14:43 +07:00
} else if ( body . node === 1 ) {
typeCondition = {
orgChild1Id : body.nodeId ,
orgChild2Id : IsNull ( ) ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-04-30 14:14:43 +07:00
} else if ( body . node === 2 ) {
typeCondition = {
orgChild2Id : body.nodeId ,
orgChild3Id : IsNull ( ) ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-04-30 14:14:43 +07:00
} else if ( body . node === 3 ) {
typeCondition = {
orgChild3Id : body.nodeId ,
orgChild4Id : IsNull ( ) ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-04-30 14:14:43 +07:00
} else if ( body . node === 4 ) {
typeCondition = {
orgChild4Id : body.nodeId ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-04-30 14:14:43 +07:00
}
} else {
if ( body . node === 0 ) {
typeCondition = {
orgRootId : body.nodeId ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-04-30 14:14:43 +07:00
} else if ( body . node === 1 ) {
typeCondition = {
orgChild1Id : body.nodeId ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-04-30 14:14:43 +07:00
} else if ( body . node === 2 ) {
typeCondition = {
orgChild2Id : body.nodeId ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-04-30 14:14:43 +07:00
} else if ( body . node === 3 ) {
typeCondition = {
orgChild3Id : body.nodeId ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-04-30 14:14:43 +07:00
} else if ( body . node === 4 ) {
typeCondition = {
orgChild4Id : body.nodeId ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-04-30 14:14:43 +07:00
}
2024-04-29 20:12:09 +07:00
}
2024-04-30 00:09:32 +07:00
const [ posMaster , total ] = await AppDataSource . getRepository ( PosMaster )
. 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" )
2024-04-30 14:14:43 +07:00
. leftJoinAndSelect ( "posMaster.positions" , "positions" )
. leftJoinAndSelect ( "positions.posType" , "posType" )
. leftJoinAndSelect ( "positions.posLevel" , "posLevel" )
. leftJoinAndSelect ( "positions.posExecutive" , "posExecutive" )
2024-11-14 18:03:56 +07:00
. andWhere ( "posMaster.next_holderId IS NULL" )
2024-04-30 14:14:43 +07:00
. andWhere (
new Brackets ( ( qb ) = > {
qb . andWhere ( typeCondition ) . andWhere ( conditionA == null ? "1=1" : conditionA , {
2024-07-18 20:39:23 +07:00
posType : posType == null ? ` %% ` : ` ${ posType . id } ` ,
posLevel : posLevel == null ? ` %% ` : ` ${ posLevel . id } ` ,
posTypeRank : posType == null ? 0 : posType.posTypeRank ,
posLevelRank : posLevel == null ? 0 : posLevel.posLevelRank ,
2024-04-30 14:14:43 +07:00
} ) ;
} ) ,
)
2024-04-30 00:09:32 +07:00
. orderBy ( "posMaster.posMasterOrder" , "ASC" )
. getManyAndCount ( ) ;
const formattedData = await Promise . all (
posMaster . map ( async ( posMaster ) = > {
let shortName = "" ;
if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id == null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null
) {
body . node = 0 ;
shortName = posMaster . orgRoot . orgRootShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null
) {
body . node = 1 ;
shortName = posMaster . orgChild1 . orgChild1ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id == null
) {
body . node = 2 ;
shortName = posMaster . orgChild2 . orgChild2ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id !== null
) {
body . node = 3 ;
shortName = posMaster . orgChild3 . orgChild3ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id !== null
) {
body . node = 4 ;
shortName = posMaster . orgChild4 . orgChild4ShortName ;
}
2024-04-30 18:00:32 +07:00
let node : any = null ;
let nodeId : any = null ;
if ( posMaster . orgChild4Id != null ) {
node = 4 ;
nodeId = posMaster . orgChild4Id ;
} else if ( posMaster . orgChild3Id != null ) {
node = 3 ;
nodeId = posMaster . orgChild3Id ;
} else if ( posMaster . orgChild2Id != null ) {
node = 2 ;
nodeId = posMaster . orgChild2Id ;
} else if ( posMaster . orgChild1Id != null ) {
node = 1 ;
nodeId = posMaster . orgChild1Id ;
} else if ( posMaster . orgRootId != null ) {
node = 0 ;
nodeId = posMaster . orgRootId ;
}
2024-04-30 00:09:32 +07:00
return {
id : posMaster.id ,
2024-04-30 18:00:32 +07:00
node : node ,
nodeId : nodeId ,
2024-04-30 00:09:32 +07:00
orgRootId : posMaster.orgRootId ,
orgChild1Id : posMaster.orgChild1Id ,
orgChild2Id : posMaster.orgChild2Id ,
orgChild3Id : posMaster.orgChild3Id ,
orgChild4Id : posMaster.orgChild4Id ,
2024-11-14 11:17:41 +07:00
isCondition : posMaster.isCondition ,
2024-04-30 00:09:32 +07:00
posMasterNoPrefix : posMaster.posMasterNoPrefix ,
posMasterNo : posMaster.posMasterNo ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ,
orgShortname : shortName ,
isSit : posMaster.isSit ,
2024-06-19 23:03:54 +07:00
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 } ` ,
2024-04-30 14:14:43 +07:00
isPosition : posMaster.positions.filter ( ( x ) = > x . positionName == body . position ) . length > 0 ,
positions : posMaster.positions.map ( ( position ) = > ( {
2024-04-30 00:09:32 +07:00
id : position.id ,
positionName : position.positionName ,
positionField : position.positionField ,
posTypeId : position.posTypeId ,
posTypeName : position.posType == null ? null : position . posType . posTypeName ,
posLevelId : position.posLevelId ,
posLevelName : position.posLevel == null ? null : position . posLevel . posLevelName ,
posExecutiveId : position.posExecutiveId ,
posExecutiveName :
position . posExecutive == null ? null : position . posExecutive . posExecutiveName ,
positionExecutiveField : position.positionExecutiveField ,
positionArea : position.positionArea ,
positionIsSelected : position.positionIsSelected ,
isSpecial : position.isSpecial ,
} ) ) ,
} ;
} ) ,
) ;
return new HttpSuccess ( { data : formattedData , total } ) ;
}
2024-05-17 11:16:39 +07:00
/ * *
* API ค ้ น ห า ต ำ แ ห น ่ ง ใ น ร ะ บ บ ส ม ั ค ร ส อ บ ล ู ก จ ้ า ง
*
* @summary ค ้ น ห า ต ำ แ ห น ่ ง ใ น ร ะ บ บ ส ม ั ค ร ส อ บ ล ู ก จ ้ า ง
*
* /
@Post ( "placementemp/search" )
async searchPlacementEmp (
@Body ( )
body : {
node : number ;
nodeId : string ;
position : string ;
typeCommand : string | null ;
2024-05-24 18:06:54 +07:00
posType? : string | null ;
posLevel? : string | null ;
2024-05-17 11:16:39 +07:00
isAll : boolean ;
isBlank : boolean ;
} ,
) {
let typeCondition : any = { } ;
let conditionA : any = null ;
2024-05-23 15:37:13 +07:00
let posType = await this . posTypeEmployeeRepository . findOne ( {
2024-05-17 11:16:39 +07:00
where : { id : String ( body . posType ) } ,
} ) ;
2024-05-23 15:37:13 +07:00
let posLevel = await this . posLevelEmployeeRepository . findOne ( {
2024-05-17 11:16:39 +07:00
where : { id : String ( body . posLevel ) } ,
} ) ;
if ( body . typeCommand == "APPOINTED" || body . typeCommand == "MOVE" ) {
conditionA = "positions.posTypeId LIKE :posType AND positions.posLevelId LIKE :posLevel" ;
} else if ( body . typeCommand == "APPOINT" ) {
conditionA = "posType.posTypeRank > :posTypeRank" ;
} else if ( body . typeCommand == "SLIP" ) {
conditionA = "positions.posTypeId LIKE :posType AND posLevel.posLevelRank > :posLevelRank" ;
}
if ( body . isAll == false ) {
if ( body . node === 0 ) {
typeCondition = {
orgRootId : body.nodeId ,
orgChild1Id : IsNull ( ) ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-05-17 11:16:39 +07:00
} else if ( body . node === 1 ) {
typeCondition = {
orgChild1Id : body.nodeId ,
orgChild2Id : IsNull ( ) ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-05-17 11:16:39 +07:00
} else if ( body . node === 2 ) {
typeCondition = {
orgChild2Id : body.nodeId ,
orgChild3Id : IsNull ( ) ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-05-17 11:16:39 +07:00
} else if ( body . node === 3 ) {
typeCondition = {
orgChild3Id : body.nodeId ,
orgChild4Id : IsNull ( ) ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-05-17 11:16:39 +07:00
} else if ( body . node === 4 ) {
typeCondition = {
orgChild4Id : body.nodeId ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-05-17 11:16:39 +07:00
}
} else {
if ( body . node === 0 ) {
typeCondition = {
orgRootId : body.nodeId ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-05-17 11:16:39 +07:00
} else if ( body . node === 1 ) {
typeCondition = {
orgChild1Id : body.nodeId ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-05-17 11:16:39 +07:00
} else if ( body . node === 2 ) {
typeCondition = {
orgChild2Id : body.nodeId ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-05-17 11:16:39 +07:00
} else if ( body . node === 3 ) {
typeCondition = {
orgChild3Id : body.nodeId ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-05-17 11:16:39 +07:00
} else if ( body . node === 4 ) {
typeCondition = {
orgChild4Id : body.nodeId ,
} ;
2024-06-26 09:37:31 +07:00
if ( body . isBlank == true ) {
typeCondition . current_holderId = IsNull ( ) ;
}
2024-05-17 11:16:39 +07:00
}
}
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.positions" , "positions" )
. leftJoinAndSelect ( "positions.posType" , "posType" )
. leftJoinAndSelect ( "positions.posLevel" , "posLevel" )
// .leftJoinAndSelect("positions.posExecutive", "posExecutive")
2024-11-14 18:03:56 +07:00
. andWhere ( "posMaster.next_holderId IS NULL" )
2024-05-17 11:16:39 +07:00
. andWhere (
new Brackets ( ( qb ) = > {
qb . andWhere ( typeCondition ) . andWhere ( conditionA == null ? "1=1" : conditionA , {
2024-05-24 18:06:54 +07:00
posType : posType == null ? ` %% ` : ` % ${ posType . id } % ` ,
posLevel : posLevel == null ? ` %% ` : ` % ${ posLevel . id } % ` ,
posTypeRank : posType == null ? "" : posType . posTypeRank ,
posLevelRank : posLevel == null ? "" : posLevel . posLevelRank ,
2024-05-17 11:16:39 +07:00
} ) ;
} ) ,
)
. orderBy ( "posMaster.posMasterOrder" , "ASC" )
. getManyAndCount ( ) ;
const formattedData = await Promise . all (
posMaster . map ( async ( posMaster ) = > {
let shortName = "" ;
if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id == null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null
) {
body . node = 0 ;
shortName = posMaster . orgRoot . orgRootShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null
) {
body . node = 1 ;
shortName = posMaster . orgChild1 . orgChild1ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id == null
) {
body . node = 2 ;
shortName = posMaster . orgChild2 . orgChild2ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id !== null
) {
body . node = 3 ;
shortName = posMaster . orgChild3 . orgChild3ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id !== null
) {
body . node = 4 ;
shortName = posMaster . orgChild4 . orgChild4ShortName ;
}
let node : any = null ;
let nodeId : any = null ;
if ( posMaster . orgChild4Id != null ) {
node = 4 ;
nodeId = posMaster . orgChild4Id ;
} else if ( posMaster . orgChild3Id != null ) {
node = 3 ;
nodeId = posMaster . orgChild3Id ;
} else if ( posMaster . orgChild2Id != null ) {
node = 2 ;
nodeId = posMaster . orgChild2Id ;
} else if ( posMaster . orgChild1Id != null ) {
node = 1 ;
nodeId = posMaster . orgChild1Id ;
} else if ( posMaster . orgRootId != null ) {
node = 0 ;
nodeId = posMaster . orgRootId ;
}
return {
id : posMaster.id ,
node : node ,
nodeId : nodeId ,
orgRootId : posMaster.orgRootId ,
orgChild1Id : posMaster.orgChild1Id ,
orgChild2Id : posMaster.orgChild2Id ,
orgChild3Id : posMaster.orgChild3Id ,
orgChild4Id : posMaster.orgChild4Id ,
posMasterNoPrefix : posMaster.posMasterNoPrefix ,
posMasterNo : posMaster.posMasterNo ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ,
orgShortname : shortName ,
isSit : posMaster.isSit ,
isPosition : posMaster.positions.filter ( ( x ) = > x . positionName == body . position ) . length > 0 ,
2024-08-19 16:48:54 +07:00
// current_holder: posMaster.current_holderId ? posMaster.current_holderId : null,
// next_holder: posMaster.next_holderId ? posMaster.next_holderId : null,
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 } ` ,
2024-05-17 11:16:39 +07:00
positions : posMaster.positions.map ( ( position ) = > ( {
id : position.id ,
positionName : position.positionName ,
// positionField: position.positionField,
posTypeId : position.posTypeId ,
posTypeName : position.posType == null ? null : position . posType . posTypeName ,
posLevelId : position.posLevelId ,
posLevelName : position.posLevel == null ? null : position . posLevel . posLevelName ,
// posExecutiveId: position.posExecutiveId,
// posExecutiveName:
// position.posExecutive == null ? null : position.posExecutive.posExecutiveName,
// positionExecutiveField: position.positionExecutiveField,
// positionArea: position.positionArea,
positionIsSelected : position.positionIsSelected ,
// isSpecial: position.isSpecial,
} ) ) ,
} ;
} ) ,
) ;
return new HttpSuccess ( { data : formattedData , total } ) ;
}
2024-05-21 13:23:42 +07:00
2024-05-21 15:00:55 +07:00
/ * *
* API ค ้ น ห า ค น ต า ม โ ค ร ง ส ร ้ า ง
2024-05-21 13:23:42 +07:00
*
* @summary ค ้ น ห า ค น ต า ม โ ค ร ง ส ร ้ า ง
*
* /
2024-05-21 15:00:55 +07:00
@Post ( "profile/search" )
async searchProfile (
@Body ( )
body : {
isAll : boolean ;
node : number ;
nodeId : string ;
} ,
) {
let typeCondition : any = { } ;
if ( body . isAll == false ) {
if ( body . node === 0 ) {
typeCondition = {
orgRootId : body.nodeId ,
orgChild1Id : IsNull ( ) ,
} ;
} else if ( body . node === 1 ) {
typeCondition = {
orgChild1Id : body.nodeId ,
orgChild2Id : IsNull ( ) ,
} ;
} else if ( body . node === 2 ) {
typeCondition = {
orgChild2Id : body.nodeId ,
orgChild3Id : IsNull ( ) ,
} ;
} else if ( body . node === 3 ) {
typeCondition = {
orgChild3Id : body.nodeId ,
orgChild4Id : IsNull ( ) ,
} ;
} else if ( body . node === 4 ) {
typeCondition = {
orgChild4Id : body.nodeId ,
} ;
}
} else {
if ( body . node === 0 ) {
typeCondition = {
orgRootId : body.nodeId ,
} ;
} else if ( body . node === 1 ) {
typeCondition = {
orgChild1Id : body.nodeId ,
} ;
} else if ( body . node === 2 ) {
typeCondition = {
orgChild2Id : body.nodeId ,
} ;
} else if ( body . node === 3 ) {
typeCondition = {
orgChild3Id : body.nodeId ,
} ;
} else if ( body . node === 4 ) {
typeCondition = {
orgChild4Id : body.nodeId ,
} ;
}
}
const [ posMaster , total ] = await AppDataSource . getRepository ( PosMaster )
. 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.positions" , "positions" )
. leftJoinAndSelect ( "positions.posType" , "posType" )
. leftJoinAndSelect ( "positions.posLevel" , "posLevel" )
. where ( "posMaster.current_holderId IS NOT NULL" )
. andWhere (
2024-05-21 13:23:42 +07:00
new Brackets ( ( qb ) = > {
qb . orWhere ( typeCondition ) ;
} ) ,
)
2024-05-21 15:00:55 +07:00
. orderBy ( "posMaster.posMasterOrder" , "ASC" )
. getManyAndCount ( ) ;
const formattedData = await Promise . all (
posMaster . map ( async ( posMaster ) = > {
let shortName = "" ;
if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id == null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null
) {
body . node = 0 ;
shortName = posMaster . orgRoot . orgRootShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null
) {
body . node = 1 ;
shortName = posMaster . orgChild1 . orgChild1ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id == null
) {
body . node = 2 ;
shortName = posMaster . orgChild2 . orgChild2ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id !== null
) {
body . node = 3 ;
shortName = posMaster . orgChild3 . orgChild3ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id !== null
) {
body . node = 4 ;
shortName = posMaster . orgChild4 . orgChild4ShortName ;
}
let node : any = null ;
let nodeId : any = null ;
if ( posMaster . orgChild4Id != null ) {
node = 4 ;
nodeId = posMaster . orgChild4Id ;
} else if ( posMaster . orgChild3Id != null ) {
node = 3 ;
nodeId = posMaster . orgChild3Id ;
} else if ( posMaster . orgChild2Id != null ) {
node = 2 ;
nodeId = posMaster . orgChild2Id ;
} else if ( posMaster . orgChild1Id != null ) {
node = 1 ;
nodeId = posMaster . orgChild1Id ;
} else if ( posMaster . orgRootId != null ) {
node = 0 ;
nodeId = posMaster . orgRootId ;
}
const fullname =
posMaster . current_holder . prefix +
" " +
posMaster . current_holder . firstName +
" " +
posMaster . current_holder . lastName ;
2024-06-09 22:17:14 +07:00
// Construct org path
const _root = posMaster . orgRoot ? ` ${ posMaster . orgRoot . orgRootName } ` : "" ;
const _child1 = posMaster . orgChild1 ? ` ${ posMaster . orgChild1 . orgChild1Name } / ` : "" ;
const _child2 = posMaster . orgChild2 ? ` ${ posMaster . orgChild2 . orgChild2Name } / ` : "" ;
const _child3 = posMaster . orgChild3 ? ` ${ posMaster . orgChild3 . orgChild3Name } / ` : "" ;
const _child4 = posMaster . orgChild4 ? ` ${ posMaster . orgChild4 . orgChild4Name } / ` : "" ;
2024-05-21 15:00:55 +07:00
return {
id : posMaster.id ,
node : node ,
nodeId : nodeId ,
orgRootId : posMaster.orgRootId ,
orgChild1Id : posMaster.orgChild1Id ,
orgChild2Id : posMaster.orgChild2Id ,
orgChild3Id : posMaster.orgChild3Id ,
orgChild4Id : posMaster.orgChild4Id ,
posMasterNoPrefix : posMaster.posMasterNoPrefix ,
posMasterNo : posMaster.posMasterNo ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ,
orgShortname : shortName ,
isSit : posMaster.isSit ,
name : fullname ,
2024-06-09 22:17:14 +07:00
prefix : posMaster.current_holder.prefix ,
firstName : posMaster.current_holder.firstName ,
lastName : posMaster.current_holder.lastName ,
2024-05-21 15:00:55 +07:00
profileId : posMaster.current_holder.id ,
citizenId : posMaster.current_holder.citizenId ,
position : posMaster.current_holder.position ,
2024-06-09 22:17:14 +07:00
organizationName : ` ${ _child4 } ${ _child3 } ${ _child2 } ${ _child1 } ${ _root } ` ,
2024-05-21 15:00:55 +07:00
} ;
} ) ,
) ;
return new HttpSuccess ( { data : formattedData , total } ) ;
}
2024-06-19 23:03:54 +07:00
/ * *
* API ค ้ น ห า ต ำ แ ห น ่ ง ใ น ร ะ บ บ ส ม ั ค ร ส อ บ ข ร ก .
*
* @summary ค ้ น ห า ต ำ แ ห น ่ ง ใ น ร ะ บ บ ส ม ั ค ร ส อ บ ข ร ก .
*
* /
@Post ( "act/search" )
async searchAct (
2024-09-05 11:57:38 +07:00
@Request ( ) request : RequestWithUser ,
2024-06-19 23:03:54 +07:00
@Body ( )
body : {
posmasterId : string ;
2024-06-26 00:06:51 +07:00
isAll : boolean ;
2024-06-19 23:03:54 +07:00
} ,
) {
2024-09-13 10:29:08 +07:00
await new permission ( ) . PermissionGet ( request , "SYS_ACTING" ) ;
2024-06-19 23:03:54 +07:00
const posMasterMain = await this . posMasterRepository . findOne ( {
where : { id : body.posmasterId } ,
relations : [ "posMasterActs" ] ,
} ) ;
if ( posMasterMain == null ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลประเภทตำแหน่งนี้" ) ;
}
let posId : any = posMasterMain . posMasterActs . map ( ( x ) = > x . posMasterChildId ) ;
posId . push ( body . posmasterId ) ;
2024-06-26 00:06:51 +07:00
let typeCondition : any = { } ;
if ( body . isAll == true ) {
2024-06-26 15:14:12 +07:00
typeCondition = {
orgRootId : posMasterMain.orgRootId ,
orgChild1Id : posMasterMain.orgChild1Id ,
orgChild2Id : posMasterMain.orgChild2Id ,
orgChild3Id : posMasterMain.orgChild3Id ,
orgChild4Id : posMasterMain.orgChild4Id ,
current_holderId : Not ( IsNull ( ) ) ,
id : Not ( In ( posId ) ) ,
} ;
2024-06-26 00:06:51 +07:00
} else {
typeCondition = {
orgRootId : posMasterMain.orgRootId == null ? IsNull ( ) : posMasterMain . orgRootId ,
orgChild1Id : posMasterMain.orgChild1Id == null ? IsNull ( ) : posMasterMain . orgChild1Id ,
orgChild2Id : posMasterMain.orgChild2Id == null ? IsNull ( ) : posMasterMain . orgChild2Id ,
orgChild3Id : posMasterMain.orgChild3Id == null ? IsNull ( ) : posMasterMain . orgChild3Id ,
orgChild4Id : posMasterMain.orgChild4Id == null ? IsNull ( ) : posMasterMain . orgChild4Id ,
current_holderId : Not ( IsNull ( ) ) ,
id : Not ( In ( posId ) ) ,
} ;
}
2024-09-05 11:57:38 +07:00
2024-06-19 23:03:54 +07:00
const posMaster = await this . posMasterRepository . find ( {
where : typeCondition ,
relations : [
"orgRoot" ,
"orgChild1" ,
"orgChild2" ,
"orgChild3" ,
"orgChild4" ,
"current_holder" ,
"current_holder.posLevel" ,
"current_holder.posType" ,
] ,
} ) ;
const data = await Promise . all (
posMaster
. sort ( ( a , b ) = > a . posMasterOrder - b . posMasterOrder )
. map ( ( item ) = > {
const shortName =
item . orgChild4 != null
? ` ${ item . orgChild4 . orgChild4ShortName } ${ item . posMasterNo } `
: item ? . orgChild3 != null
? ` ${ item . orgChild3 . orgChild3ShortName } ${ item . posMasterNo } `
: item ? . orgChild2 != null
? ` ${ item . orgChild2 . orgChild2ShortName } ${ item . posMasterNo } `
: item ? . orgChild1 != null
? ` ${ item . orgChild1 . orgChild1ShortName } ${ item . posMasterNo } `
: item ? . orgRoot != null
? ` ${ item . orgRoot . orgRootShortName } ${ item . posMasterNo } `
: null ;
return {
id : item.id ,
citizenId : item.current_holder?.citizenId ? ? null ,
prefix : item.current_holder?.prefix ? ? null ,
firstName : item.current_holder?.firstName ? ? null ,
lastName : item.current_holder?.lastName ? ? null ,
posLevel : item.current_holder?.posLevel?.posLevelName ? ? null ,
posType : item.current_holder?.posType?.posTypeName ? ? null ,
position : item.current_holder?.position ? ? null ,
posNo : shortName ,
} ;
} ) ,
) ;
return new HttpSuccess ( data ) ;
}
2024-06-24 10:52:40 +07:00
/ * *
* API บ ั น ท ึ ก ต ำ แ ห น ่ ง ใ ห ม ่
*
* @summary บ ั น ท ึ ก ต ำ แ ห น ่ ง ใ ห ม ่
*
* /
@Post ( "report/current" )
async reportApproveCurrent (
@Body ( )
body : {
posmasterId : string ;
positionId : string ;
profileId : string ;
} ,
) {
const posMaster = await this . posMasterRepository . findOne ( {
where : { id : body.posmasterId } ,
} ) ;
if ( posMaster == null ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
const posMasterOld = await this . posMasterRepository . findOne ( {
where : {
current_holderId : body.profileId ,
orgRevisionId : posMaster.orgRevisionId ,
} ,
} ) ;
if ( posMasterOld != null ) posMasterOld . current_holderId = null ;
2024-07-18 14:16:46 +07:00
2024-07-16 18:16:42 +07:00
const positionOld = await this . positionRepository . findOne ( {
where : {
posMasterId : posMasterOld?.id ,
2024-07-18 14:16:46 +07:00
positionIsSelected : true ,
} ,
} ) ;
2024-07-16 18:16:42 +07:00
if ( positionOld != null ) {
2024-07-18 14:16:46 +07:00
positionOld . positionIsSelected = false ;
2024-07-16 18:16:42 +07:00
await this . positionRepository . save ( positionOld ) ;
}
const checkPosition = await this . positionRepository . find ( {
where : {
posMasterId : body.posmasterId ,
2024-07-18 14:16:46 +07:00
positionIsSelected : true ,
} ,
} ) ;
2024-07-16 18:16:42 +07:00
if ( checkPosition . length > 0 ) {
2024-07-18 14:16:46 +07:00
const clearPosition = checkPosition . map ( ( positions ) = > ( {
2024-07-16 18:16:42 +07:00
. . . positions ,
2024-07-18 14:16:46 +07:00
positionIsSelected : false ,
2024-07-16 18:16:42 +07:00
} ) ) ;
await this . positionRepository . save ( clearPosition ) ;
}
2024-06-24 10:52:40 +07:00
const profile = await this . profileRepository . findOne ( {
where : { id : body.profileId } ,
} ) ;
if ( profile == null )
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลทะเบียนประวัตินี้" ) ;
posMaster . current_holderId = body . profileId ;
if ( posMasterOld != null ) await this . posMasterRepository . save ( posMasterOld ) ;
await this . posMasterRepository . save ( posMaster ) ;
2024-07-16 18:16:42 +07:00
const positionNew = await this . positionRepository . findOne ( {
where : {
id : body.positionId ,
2024-07-18 14:16:46 +07:00
posMasterId : body.posmasterId ,
} ,
} ) ;
if ( positionNew != null ) {
positionNew . positionIsSelected = true ;
2024-08-08 17:27:33 +07:00
profile . posLevelId = positionNew . posLevelId ;
profile . posTypeId = positionNew . posTypeId ;
2024-07-19 11:52:43 +07:00
profile . position = positionNew . positionName ;
await this . profileRepository . save ( profile ) ;
2024-07-16 18:16:42 +07:00
await this . positionRepository . save ( positionNew ) ;
}
2024-06-24 10:52:40 +07:00
return new HttpSuccess ( ) ;
}
2024-10-17 22:11:54 +07:00
/ * *
* API ร า ย ช ื ่ อ ต า ม ก ล ุ ่ ม ใ น โ ค ร ง ส ร ้ า ง
*
* @summary ร า ย ช ื ่ อ ต า ม ก ล ุ ่ ม ใ น โ ค ร ง ส ร ้ า ง
*
* /
@Post ( "report/draft" )
async reportDraft (
@Body ( )
body : {
type : string ;
rootId : string ;
} ,
) {
let conditionGroup = "" ;
2024-10-17 22:24:25 +07:00
if ( body . type . trim ( ) . toUpperCase ( ) == "GROUP1.1" ) {
2024-10-17 22:11:54 +07:00
conditionGroup =
"(posType.posTypeName = 'ทั่วไป' AND posLevel.posLevelName = 'ชำนาญงาน') OR (posType.posTypeName = 'ทั่วไป' AND posLevel.posLevelName = 'ปฏิบัติงาน') OR (posType.posTypeName = 'วิชาการ' AND posLevel.posLevelName = 'ปฏิบัติการ') OR (posType.posTypeName = 'วิชาการ' AND posLevel.posLevelName = 'ชำนาญการ')" ;
2024-10-17 22:13:44 +07:00
} else if ( body . type . trim ( ) . toUpperCase ( ) == "GROUP1.2" ) {
2024-10-17 22:11:54 +07:00
conditionGroup =
"(posType.posTypeName = 'ทั่วไป' AND posLevel.posLevelName = 'อาวุโส') OR (posType.posTypeName = 'วิชาการ' AND posLevel.posLevelName = 'ชำนาญการพิเศษ') OR (posType.posTypeName = 'อำนวยการ' AND posLevel.posLevelName = 'ต้น')" ;
2024-10-17 22:13:44 +07:00
} else if ( body . type . trim ( ) . toUpperCase ( ) == "GROUP2" ) {
2024-10-17 22:11:54 +07:00
conditionGroup =
"(posType.posTypeName = 'ทั่วไป' AND posLevel.posLevelName = 'ทักษะพิเศษ') OR (posType.posTypeName = 'วิชาการ' AND posLevel.posLevelName = 'เชี่ยวชาญ') OR (posType.posTypeName = 'วิชาการ' AND posLevel.posLevelName = 'ทรงคุณวุฒิ') OR (posType.posTypeName = 'อำนวยการ' AND posLevel.posLevelName = 'สูง') OR (posType.posTypeName = 'บริหาร' AND posLevel.posLevelName = 'ต้น') OR (posType.posTypeName = 'บริหาร' AND posLevel.posLevelName = 'สูง')" ;
} else {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "กลุ่มเป้าหมายไม่ถูกต้อง" ) ;
}
let posMaster = await AppDataSource . getRepository ( PosMaster )
. createQueryBuilder ( "posMaster" )
. leftJoinAndSelect ( "posMaster.orgRoot" , "orgRoot" )
. leftJoinAndSelect ( "posMaster.orgChild1" , "orgChild1" )
. leftJoinAndSelect ( "posMaster.orgChild2" , "orgChild2" )
. leftJoinAndSelect ( "posMaster.orgChild3" , "orgChild3" )
. leftJoinAndSelect ( "posMaster.orgChild4" , "orgChild4" )
. leftJoinAndSelect ( "posMaster.next_holder" , "next_holder" )
. leftJoinAndSelect ( "posMaster.positions" , "positions" )
. leftJoinAndSelect ( "positions.posType" , "posType" )
. leftJoinAndSelect ( "positions.posLevel" , "posLevel" )
. leftJoinAndSelect ( "posMaster.orgRevision" , "orgRevision" )
2024-10-17 22:24:25 +07:00
. andWhere ( "posMaster.orgRootId LIKE :orgRootId" , {
2024-10-23 00:31:00 +07:00
orgRootId : body.type.trim ( ) . toUpperCase ( ) == "GROUP1.1" ? body . rootId : ` %% ` ,
2024-10-17 22:24:25 +07:00
} )
2024-10-17 22:11:54 +07:00
. andWhere ( "posMaster.statusReport = :statusReport" , { statusReport : "PENDING" } )
. andWhere ( "posMaster.next_holderId IS NOT NULL" )
. andWhere ( "orgRevision.orgRevisionIsCurrent = :orgRevisionIsCurrent" , {
orgRevisionIsCurrent : false ,
} )
. andWhere ( "orgRevision.orgRevisionIsDraft = :orgRevisionIsDraft" , {
orgRevisionIsDraft : true ,
} )
. andWhere ( "positions.positionIsSelected = :isSelected" , { isSelected : true } )
. andWhere (
new Brackets ( ( qb ) = > {
qb . andWhere ( conditionGroup ) ;
} ) ,
)
. orderBy ( "posMaster.posMasterOrder" , "ASC" )
. select ( [
"posMaster.id" ,
"posMaster.posMasterNo" ,
"positions.positionName" ,
"positions.positionIsSelected" ,
"posType.posTypeName" ,
"posLevel.posLevelName" ,
"orgRoot.orgRootShortName" ,
"orgChild1.orgChild1ShortName" ,
"orgChild2.orgChild2ShortName" ,
"orgChild3.orgChild3ShortName" ,
"orgChild4.orgChild4ShortName" ,
"next_holder.prefix" ,
"next_holder.firstName" ,
"next_holder.lastName" ,
2024-10-22 15:14:42 +07:00
"next_holder.id" ,
"next_holder.citizenId" ,
2024-10-17 22:11:54 +07:00
] )
. getMany ( ) ;
const _posMaster = posMaster . map ( ( x ) = > {
const posMasterNo =
x . orgChild4 != null
? ` ${ x . orgChild4 . orgChild4ShortName } ${ x . posMasterNo } `
: x != null && x ? . orgChild3 != null
? ` ${ x . orgChild3 . orgChild3ShortName } ${ x . posMasterNo } `
: x != null && x ? . orgChild2 != null
? ` ${ x . orgChild2 . orgChild2ShortName } ${ x . posMasterNo } `
: x != null && x ? . orgChild1 != null
? ` ${ x . orgChild1 . orgChild1ShortName } ${ x . posMasterNo } `
: x != null && x ? . orgRoot != null
? ` ${ x . orgRoot . orgRootShortName } ${ x . posMasterNo } `
: null ;
const position =
x . positions . filter ( ( x ) = > x . positionIsSelected == true ) . length > 0
? x . positions . filter ( ( x ) = > x . positionIsSelected == true ) [ 0 ]
: null ;
return {
id : x.id ,
posMasterNo : posMasterNo ,
positionName : position?.positionName || null ,
posType : position?.posType?.posTypeName || null ,
posLevel : position?.posLevel?.posLevelName || null ,
2024-10-18 11:33:04 +07:00
profileId : x.next_holder?.id || null ,
2024-10-17 22:11:54 +07:00
prefix : x.next_holder?.prefix || null ,
firstName : x.next_holder?.firstName || null ,
lastName : x.next_holder?.lastName || null ,
2024-10-18 11:06:26 +07:00
citizenId : x.next_holder?.citizenId || null ,
2024-10-17 22:11:54 +07:00
} ;
} ) ;
return new HttpSuccess ( _posMaster ) ;
}
2024-10-31 20:59:14 +07:00
/ * *
* API ร า ย ก า ร อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_070 - ร า ย ก า ร อ ั ต ร า ก ำ ล ั ง ( ADMIN ) # 56
*
* /
@Post ( "master/position-condition" )
2024-11-01 09:47:45 +07:00
async listPositionCondition (
2024-10-31 20:59:14 +07:00
@Request ( ) request : RequestWithUser ,
@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) ` ;
let _data = await new permission ( ) . PermissionOrgList ( request , "SYS_POS_CONDITION" ) ;
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 : PosType [ ] = await this . posTypeRepository . find ( {
where : { posTypeName : Like ( ` % ${ body . keyword } % ` ) } ,
select : [ "id" ] ,
} ) ;
findPosition = await this . positionRepository . find ( {
where : { posTypeId : In ( findTypes . map ( ( x ) = > x . id ) ) } ,
select : [ "posMasterId" ] ,
} ) ;
masterId = masterId . concat ( findPosition . map ( ( position : any ) = > position . posMasterId ) ) ;
const findLevel : PosLevel [ ] = await this . posLevelRepository . find ( {
where : { posLevelName : Like ( ` % ${ body . keyword } % ` ) } ,
select : [ "id" ] ,
} ) ;
findPosition = await this . positionRepository . find ( {
where : { posLevelId : In ( findLevel . map ( ( x ) = > x . id ) ) } ,
select : [ "posMasterId" ] ,
} ) ;
masterId = masterId . concat ( findPosition . map ( ( position : any ) = > position . posMasterId ) ) ;
const findExecutive : PosExecutive [ ] = await this . posExecutiveRepository . find ( {
where : { posExecutiveName : Like ( ` % ${ body . keyword } % ` ) } ,
select : [ "id" ] ,
} ) ;
findPosition = await this . positionRepository . find ( {
where : { posExecutiveId : In ( findExecutive . map ( ( x ) = > x . id ) ) } ,
select : [ "posMasterId" ] ,
} ) ;
masterId = masterId . concat ( findPosition . map ( ( position : any ) = > position . posMasterId ) ) ;
findPosition = await this . positionRepository . 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 chkRevision = await this . orgRevisionRepository . findOne ( {
where : { id : body.revisionId } ,
} ) ;
if ( chkRevision != null && chkRevision . orgRevisionIsDraft == true )
_data = {
root : null ,
child1 : null ,
child2 : null ,
child3 : null ,
child4 : null ,
privilege : "OWNER" ,
} ;
const conditions = [
{
. . . checkChildConditions ,
. . . typeCondition ,
. . . revisionCondition ,
. . . ( body . keyword &&
( masterId . length > 0
? { id : In ( masterId ) }
: { posMasterNo : Like ( ` % ${ body . keyword } % ` ) } ) ) ,
} ,
] ;
let [ posMaster , total ] = await AppDataSource . getRepository ( PosMaster )
. createQueryBuilder ( "posMaster" )
. leftJoinAndSelect ( "posMaster.orgRoot" , "orgRoot" )
. leftJoinAndSelect ( "posMaster.orgChild1" , "orgChild1" )
. leftJoinAndSelect ( "posMaster.orgChild2" , "orgChild2" )
. leftJoinAndSelect ( "posMaster.orgChild3" , "orgChild3" )
. leftJoinAndSelect ( "posMaster.orgChild4" , "orgChild4" )
. where ( conditions )
. andWhere ( {
2024-11-04 13:29:30 +07:00
current_holderId : IsNull ( ) ,
2024-10-31 20:59:14 +07:00
} )
. andWhere (
_data . root != undefined && _data . root != null
? _data . root [ 0 ] != null
? ` posMaster.orgRootId IN (:...root) `
: ` posMaster.orgRootId is null `
: "1=1" ,
{
root : _data.root ,
} ,
)
. andWhere (
_data . child1 != undefined && _data . child1 != null
? _data . child1 [ 0 ] != null
? ` posMaster.orgChild1Id IN (:...child1) `
: ` posMaster.orgChild1Id is null `
: "1=1" ,
{
child1 : _data.child1 ,
} ,
)
. andWhere (
_data . child2 != undefined && _data . child2 != null
? _data . child2 [ 0 ] != null
? ` posMaster.orgChild2Id IN (:...child2) `
: ` posMaster.orgChild2Id is null `
: "1=1" ,
{
child2 : _data.child2 ,
} ,
)
. andWhere (
_data . child3 != undefined && _data . child3 != null
? _data . child3 [ 0 ] != null
? ` posMaster.orgChild3Id IN (:...child3) `
: ` posMaster.orgChild3Id is null `
: "1=1" ,
{
child3 : _data.child3 ,
} ,
)
. andWhere (
_data . child4 != undefined && _data . child4 != null
? _data . child4 [ 0 ] != null
? ` posMaster.orgChild4Id IN (:...child4) `
: ` posMaster.orgChild4Id is null `
: "1=1" ,
{
child4 : _data.child4 ,
} ,
)
. 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" ,
)
. orderBy ( "posMaster.posMasterOrder" , "ASC" )
. skip ( ( body . page - 1 ) * body . pageSize )
. take ( body . pageSize )
. getManyAndCount ( ) ;
//แก้ค้นหา
let _position : any [ ] = [ ] ;
let x : any = null ;
let y : any = null ;
if ( body . keyword != null && body . keyword != "" ) {
const position = await this . positionRepository . find ( {
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
where : { posMasterId : In ( posMaster . map ( ( x ) = > x . id ) ) } ,
order : { createdAt : "ASC" } ,
} ) ;
for ( let data of position ) {
x = data . posMasterId ;
if ( y != x ) {
if (
data . positionName . includes ( body . keyword ) ||
data . posType . posTypeName . includes ( body . keyword ) ||
data . posLevel . posLevelName . includes ( body . keyword )
) {
_position . push ( data ) ;
}
}
y = x ;
}
}
if ( _position . length > 0 ) {
posMaster = posMaster . filter ( ( x ) = > _position . some ( ( y ) = > y . posMasterId === x . id ) ) ;
}
const formattedData = await Promise . all (
posMaster . map ( async ( posMaster ) = > {
const positions = await this . positionRepository . find ( {
where : {
posMasterId : posMaster.id ,
} ,
relations : [ "posLevel" , "posType" , "posExecutive" ] ,
order : {
createdAt : "ASC" ,
} ,
} ) ;
let shortName = "" ;
if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id == null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null
) {
shortName = posMaster . orgRoot . orgRootShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id == null &&
posMaster . orgChild3Id == null
) {
shortName = posMaster . orgChild1 . orgChild1ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id == null
) {
shortName = posMaster . orgChild2 . orgChild2ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id !== null
) {
shortName = posMaster . orgChild3 . orgChild3ShortName ;
} else if (
posMaster . orgRootId !== null &&
posMaster . orgChild1Id !== null &&
posMaster . orgChild2Id !== null &&
posMaster . orgChild3Id !== null
) {
shortName = posMaster . orgChild4 . orgChild4ShortName ;
}
return {
id : posMaster.id ,
isCondition : posMaster.isCondition ,
conditionReason : posMaster.conditionReason ,
2024-11-01 09:47:45 +07:00
posMasterNoPrefix : posMaster.posMasterNoPrefix ? posMaster.posMasterNoPrefix : null ,
posMasterNo : posMaster.posMasterNo ? posMaster.posMasterNo : null ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ? posMaster.posMasterNoSuffix : null ,
2024-10-31 20:59:14 +07:00
orgShortname : shortName ,
profilePosition : positions [ 0 ] ? . positionName ,
profilePostype : positions [ 0 ] ? . posType ? . posTypeName ? ? null ,
profilePoslevel : positions [ 0 ] ? . posLevel ? . posLevelName ? ? null ,
positions : positions.map ( ( position : any ) = > ( {
id : position.id ,
positionName : position.positionName ,
positionField : position.positionField ,
posTypeName : position.posType == null ? null : position . posType . posTypeName ,
posLevelName : position.posLevel == null ? null : position . posLevel . posLevelName ,
posExecutiveName :
position . posExecutive == null ? null : position . posExecutive . posExecutiveName ,
positionExecutiveField : position.positionExecutiveField ,
positionArea : position.positionArea ,
} ) ) ,
} ;
} ) ,
) ;
return new HttpSuccess ( { data : formattedData , total } ) ;
}
/ * *
* API แ ก ้ ไ ข ต ำ แ ห น ่ ง เ ง ื ่ อ น ไ ข
*
* @summary แ ก ้ ไ ข ต ำ แ ห น ่ ง เ ง ื ่ อ น ไ ข ( ADMIN )
*
* /
@Put ( "master/position-condition/{id}" )
async updatePositionCondition (
@Path ( ) id : string ,
@Body ( )
requestBody : {
isCondition : boolean | null ;
conditionReason : string | null ;
} ,
@Request ( ) request : RequestWithUser ,
) {
await new permission ( ) . PermissionUpdate ( request , "SYS_POS_CONDITION" ) ;
const posMaster = await this . posMasterRepository . findOne ( {
where : { id : id } ,
} ) ;
if ( ! posMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล" ) ;
}
Object . assign ( posMaster , requestBody ) ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
posMaster . lastUpdatedAt = new Date ( ) ;
await this . posMasterRepository . save ( posMaster ) ;
return new HttpSuccess ( ) ;
}
2024-11-14 18:03:56 +07:00
/ * *
* API แ ก ้ ไ ข ต ำ แ ห น ่ ง
*
* @summary แ ก ้ ไ ข ต ำ แ ห น ่ ง ( ADMIN )
*
* /
@Post ( "officer/master/book" )
async posMasterBookOfficer (
@Body ( )
requestBody : {
posMasterId : string ;
profileId : string ;
} ,
@Request ( ) request : RequestWithUser ,
) {
const posMaster = await this . posMasterRepository . findOne ( {
where : { id : requestBody.posMasterId } ,
} ) ;
if ( ! posMaster ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่ง" ) ;
const profile = await this . profileRepository . findOne ( {
where : { id : requestBody.profileId } ,
} ) ;
if ( ! profile ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลผู้ใช้งาน" ) ;
posMaster . next_holderId = requestBody . profileId ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
posMaster . lastUpdatedAt = new Date ( ) ;
await this . posMasterRepository . save ( posMaster ) ;
return new HttpSuccess ( ) ;
}
/ * *
* API แ ก ้ ไ ข ต ำ แ ห น ่ ง
*
* @summary แ ก ้ ไ ข ต ำ แ ห น ่ ง ( ADMIN )
*
* /
@Post ( "officer/master/clear" )
async posMasterClearOfficer (
@Body ( )
requestBody : {
posMasterId : string ;
} ,
@Request ( ) request : RequestWithUser ,
) {
const posMaster = await this . posMasterRepository . findOne ( {
where : { id : requestBody.posMasterId } ,
} ) ;
if ( ! posMaster ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่ง" ) ;
posMaster . next_holderId = null ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
posMaster . lastUpdatedAt = new Date ( ) ;
await this . posMasterRepository . save ( posMaster ) ;
return new HttpSuccess ( ) ;
}
/ * *
* API แ ก ้ ไ ข ต ำ แ ห น ่ ง
*
* @summary แ ก ้ ไ ข ต ำ แ ห น ่ ง ( ADMIN )
*
* /
@Post ( "employee/master/book" )
async posMasterBookEmployee (
@Body ( )
requestBody : {
posMasterId : string ;
profileId : string ;
} ,
@Request ( ) request : RequestWithUser ,
) {
const posMaster = await this . employeePosMasterRepository . findOne ( {
where : { id : requestBody.posMasterId } ,
} ) ;
if ( ! posMaster ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่ง" ) ;
const profile = await this . profileEmployeeRepository . findOne ( {
where : { id : requestBody.profileId } ,
} ) ;
if ( ! profile ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลผู้ใช้งาน" ) ;
posMaster . next_holderId = requestBody . profileId ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
posMaster . lastUpdatedAt = new Date ( ) ;
await this . employeePosMasterRepository . save ( posMaster ) ;
return new HttpSuccess ( ) ;
}
/ * *
* API แ ก ้ ไ ข ต ำ แ ห น ่ ง
*
* @summary แ ก ้ ไ ข ต ำ แ ห น ่ ง ( ADMIN )
*
* /
@Post ( "employee/master/clear" )
async posMasterClearEmployee (
@Body ( )
requestBody : {
posMasterId : string ;
} ,
@Request ( ) request : RequestWithUser ,
) {
const posMaster = await this . employeePosMasterRepository . findOne ( {
where : { id : requestBody.posMasterId } ,
} ) ;
if ( ! posMaster ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่ง" ) ;
posMaster . next_holderId = null ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
posMaster . lastUpdatedAt = new Date ( ) ;
await this . employeePosMasterRepository . save ( posMaster ) ;
return new HttpSuccess ( ) ;
}
/ * *
* API แ ก ้ ไ ข ต ำ แ ห น ่ ง
*
* @summary แ ก ้ ไ ข ต ำ แ ห น ่ ง ( ADMIN )
*
* /
@Post ( "officer/master-old/book" )
async posMasterOldBookOfficer (
@Body ( )
requestBody : {
posMasterId : string ;
posMasterOldId : string ;
profileId : string ;
} ,
@Request ( ) request : RequestWithUser ,
) {
const posMaster = await this . posMasterRepository . findOne ( {
where : { id : requestBody.posMasterId } ,
} ) ;
if ( ! posMaster ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่ง" ) ;
const profile = await this . profileRepository . findOne ( {
where : { id : requestBody.profileId } ,
} ) ;
if ( ! profile ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลผู้ใช้งาน" ) ;
posMaster . next_holderId = requestBody . profileId ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
posMaster . lastUpdatedAt = new Date ( ) ;
await this . posMasterRepository . save ( posMaster ) ;
if ( requestBody . posMasterOldId != null ) {
const posMasterOld = await this . posMasterRepository . findOne ( {
where : { id : requestBody.posMasterOldId } ,
} ) ;
if ( ! posMasterOld ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่ง" ) ;
posMasterOld . next_holderId = null ;
posMasterOld . lastUpdateUserId = request . user . sub ;
posMasterOld . lastUpdateFullName = request . user . name ;
posMasterOld . lastUpdatedAt = new Date ( ) ;
await this . posMasterRepository . save ( posMasterOld ) ;
}
return new HttpSuccess ( ) ;
}
/ * *
* API แ ก ้ ไ ข ต ำ แ ห น ่ ง
*
* @summary แ ก ้ ไ ข ต ำ แ ห น ่ ง ( ADMIN )
*
* /
@Post ( "employee/master-old/book" )
async posMasterOldBookEmployee (
@Body ( )
requestBody : {
posMasterId : string ;
posMasterOldId : string ;
profileId : string ;
} ,
@Request ( ) request : RequestWithUser ,
) {
const posMaster = await this . employeePosMasterRepository . findOne ( {
where : { id : requestBody.posMasterId } ,
} ) ;
if ( ! posMaster ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่ง" ) ;
const profile = await this . profileEmployeeRepository . findOne ( {
where : { id : requestBody.profileId } ,
} ) ;
if ( ! profile ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลผู้ใช้งาน" ) ;
posMaster . next_holderId = requestBody . profileId ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
posMaster . lastUpdatedAt = new Date ( ) ;
await this . employeePosMasterRepository . save ( posMaster ) ;
if ( requestBody . posMasterOldId != null ) {
const posMasterOld = await this . employeePosMasterRepository . findOne ( {
where : { id : requestBody.posMasterOldId } ,
} ) ;
if ( ! posMasterOld ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่ง" ) ;
posMasterOld . next_holderId = null ;
posMasterOld . lastUpdateUserId = request . user . sub ;
posMasterOld . lastUpdateFullName = request . user . name ;
posMasterOld . lastUpdatedAt = new Date ( ) ;
await this . employeePosMasterRepository . save ( posMasterOld ) ;
}
return new HttpSuccess ( ) ;
}
2024-04-30 00:09:32 +07:00
}