2024-01-30 15:10:11 +07:00
import {
Controller ,
Get ,
Post ,
Put ,
Delete ,
Patch ,
Route ,
Security ,
Tags ,
Body ,
Path ,
Request ,
Example ,
SuccessResponse ,
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-02-20 18:04:13 +07:00
import { CreatePosDict , PosDict , UpdatePosDict } from "../entities/PosDict" ;
2024-01-30 16:02:34 +07:00
import HttpError from "../interfaces/http-error" ;
2024-02-15 09:41:27 +07:00
import { Equal , ILike , 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-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 ,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง" ,
)
@SuccessResponse ( HttpStatusCode . OK , "สำเร็จ" )
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-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-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-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-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 ,
@Request ( ) request : { user : Record < string , any > } ,
) {
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 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล PosTypeId" ) ;
}
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-01-30 16:02:34 +07:00
try {
2024-01-30 16:47:39 +07:00
posDict . createdUserId = request . user . sub ;
posDict . createdFullName = request . user . name ;
posDict . lastUpdateUserId = request . user . sub ;
posDict . lastUpdateFullName = request . user . name ;
await this . posDictRepository . save ( posDict ) ;
return new HttpSuccess ( posDict . id ) ;
2024-01-30 16:02:34 +07:00
} catch ( error ) {
return error ;
}
}
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 ,
@Request ( ) request : { user : Record < string , any > } ,
) {
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 ;
}
if ( requestBody . posExecutiveId != null ) {
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 ,
posExecutiveId : String ( requestBody . posExecutiveId ) ,
posDictExecutiveField : requestBody.posDictExecutiveField ,
posDictArea : requestBody.posDictArea ,
isSpecial : requestBody.isSpecial ,
} ,
} ) ;
if ( rowRepeated ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ข้อมูล Row นี้มีอยู่ในระบบแล้ว" ) ;
}
try {
posDict . lastUpdateUserId = request . user . sub ;
posDict . lastUpdateFullName = request . user . name ;
this . posDictRepository . merge ( posDict , requestBody ) ;
await this . posDictRepository . save ( posDict ) ;
return new HttpSuccess ( ) ;
} catch ( error ) {
return error ;
}
}
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-01-30 16:47:39 +07:00
async delete ( @Path ( ) id : string ) {
const delPosDict = await this . posDictRepository . findOne ( { where : { id } } ) ;
if ( ! delPosDict ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id ) ;
}
try {
await this . posDictRepository . remove ( delPosDict ) ;
return new HttpSuccess ( ) ;
} catch ( error ) {
return error ;
}
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-01-31 11:00:51 +07:00
try {
2024-01-30 18:01:58 +07:00
let findPosDict : any ;
2024-01-31 14:29:39 +07:00
switch ( type ) {
2024-01-30 18:01:58 +07:00
case "positionName" :
2024-01-31 14:29:39 +07:00
findPosDict = await this . posDictRepository . find ( {
where : { posDictName : Like ( ` % ${ keyword } % ` ) } ,
2024-01-31 15:02:54 +07:00
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-01-31 14:29:39 +07:00
} ) ;
2024-02-16 12:07:37 +07:00
// if (!findPosDict) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
// }
2024-01-31 13:16:22 +07:00
break ;
2024-01-30 18:01:58 +07:00
case "positionField" :
2024-01-31 14:29:39 +07:00
findPosDict = await this . posDictRepository . find ( {
where : { posDictField : Like ( ` % ${ keyword } % ` ) } ,
2024-01-31 15:02:54 +07:00
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-01-31 14:29:39 +07:00
} ) ;
2024-02-16 12:07:37 +07:00
// if (!findPosDict) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
// }
2024-01-31 13:16:22 +07:00
break ;
2024-01-30 18:01:58 +07:00
case "positionType" :
2024-01-31 14:29:39 +07:00
const findTypes : PosType [ ] = await this . posTypeRepository . find ( {
where : { posTypeName : Like ( ` % ${ keyword } % ` ) } ,
2024-01-31 15:02:54 +07:00
select : [ "id" ] ,
} ) ;
findPosDict = await this . posDictRepository . find ( {
where : { posTypeId : In ( findTypes . map ( ( x ) = > x . id ) ) } ,
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-01-31 14:29:39 +07:00
} ) ;
2024-01-31 13:16:22 +07:00
break ;
2024-01-30 18:01:58 +07:00
case "positionLevel" :
2024-01-31 14:29:39 +07:00
const findLevel : PosLevel [ ] = await this . posLevelRepository . find ( {
where : { posLevelName : Like ( ` % ${ keyword } % ` ) } ,
2024-01-31 15:02:54 +07:00
select : [ "id" ] ,
} ) ;
findPosDict = await this . posDictRepository . find ( {
where : { posLevelId : In ( findLevel . map ( ( x ) = > x . id ) ) } ,
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-01-31 14:29:39 +07:00
} ) ;
2024-01-31 13:16:22 +07:00
break ;
2024-01-30 18:01:58 +07:00
case "positionExecutive" :
2024-01-31 14:29:39 +07:00
const findExecutive : PosExecutive [ ] = await this . posExecutiveRepository . find ( {
where : { posExecutiveName : Like ( ` % ${ keyword } % ` ) } ,
2024-01-31 15:02:54 +07:00
select : [ "id" ] ,
} ) ;
findPosDict = await this . posDictRepository . find ( {
where : { posExecutiveId : In ( findExecutive . map ( ( x ) = > x . id ) ) } ,
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-01-31 14:29:39 +07:00
} ) ;
2024-01-31 13:16:22 +07:00
break ;
2024-01-30 18:01:58 +07:00
case "positionExecutiveField" :
2024-01-31 14:29:39 +07:00
findPosDict = await this . posDictRepository . find ( {
where : { posDictExecutiveField : Like ( ` % ${ keyword } % ` ) } ,
2024-01-31 15:02:54 +07:00
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-01-31 14:29:39 +07:00
} ) ;
2024-02-16 12:07:37 +07:00
// if (!findPosDict) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
// }
2024-01-31 13:16:22 +07:00
break ;
2024-01-30 18:01:58 +07:00
case "positionArea" :
2024-01-31 14:29:39 +07:00
findPosDict = await this . posDictRepository . find ( {
where : { posDictArea : Like ( ` % ${ keyword } % ` ) } ,
2024-01-31 15:02:54 +07:00
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-01-31 14:29:39 +07:00
} ) ;
2024-02-16 12:07:37 +07:00
// if (!findPosDict) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล " + keyword);
// }
2024-01-31 13:16:22 +07:00
break ;
default :
2024-01-31 15:02:54 +07:00
findPosDict = await this . posDictRepository . find ( {
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
} ) ;
2024-02-16 12:07:37 +07:00
// if (!findPosDict) {
// throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
// }
2024-01-31 13:16:22 +07:00
break ;
2024-01-30 18:01:58 +07:00
}
2024-01-31 13:16:22 +07:00
2024-01-31 14:29:39 +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 ,
2024-01-31 15:02:54 +07:00
posTypeName : item.posType == null ? null : item . posType . posTypeName ,
2024-01-31 14:29:39 +07:00
posLevelId : item.posLevelId ,
2024-01-31 15:02:54 +07:00
posLevelName : item.posLevel == null ? null : item . posLevel . posLevelName ,
2024-01-31 14:29:39 +07:00
posExecutiveId : item.posExecutiveId ,
2024-01-31 15:02:54 +07:00
posExecutiveName : item.posExecutive == null ? null : item . posExecutive . posExecutiveName ,
2024-01-31 14:29:39 +07:00
positionExecutiveField : item.posDictExecutiveField ,
positionArea : item.posDictArea ,
2024-02-20 17:12:28 +07:00
isSpecial : item.isSpecial ,
2024-01-31 14:29:39 +07:00
positionIsSelected : false ,
} ;
} ) ,
) ;
2024-01-31 13:16:22 +07:00
return new HttpSuccess ( mapDataPosDict ) ;
2024-01-31 14:29:39 +07:00
} catch ( error ) {
2024-01-30 18:01:58 +07:00
return error ;
}
}
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" ,
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 ,
@Request ( ) request : { user : Record < string , any > } ,
) {
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-02-21 17:10:30 +07:00
SName = orgRoot . shortName ;
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-02-21 17:10:30 +07:00
SName = orgChild1 . shortName ;
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-02-21 17:10:30 +07:00
SName = orgChild2 . shortName ;
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-02-21 17:10:30 +07:00
SName = orgChild3 . shortName ;
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-02-21 17:10:30 +07:00
SName = orgChild4 . shortName ;
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
} ) ;
2024-02-20 13:35:11 +07:00
const dataDup : any = await this . posMasterRepository . findOne ( {
where : {
orgChild4Id : orgChild4.id ,
posMasterNo : requestBody.posMasterNo ,
} ,
} ) ;
if ( dataDup != null )
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
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 ;
}
} 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-20 13:35:11 +07:00
const dataDup : any = await this . posMasterRepository . findOne ( {
where : {
orgChild3Id : orgChild3.id ,
orgChild4Id : IsNull ( ) || "" ,
posMasterNo : requestBody.posMasterNo ,
} ,
} ) ;
if ( dataDup != null )
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
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 ;
}
} 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-20 13:35:11 +07:00
const dataDup : any = await this . posMasterRepository . findOne ( {
where : {
orgChild2Id : orgChild2.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
posMasterNo : requestBody.posMasterNo ,
} ,
} ) ;
if ( dataDup != null )
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
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 ;
}
} 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-20 13:35:11 +07:00
const dataDup : any = await this . posMasterRepository . findOne ( {
where : {
orgChild1Id : orgChild1.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
posMasterNo : requestBody.posMasterNo ,
} ,
} ) ;
if ( dataDup != null )
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
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 ;
}
} 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-20 13:35:11 +07:00
const dataDup : any = await this . posMasterRepository . findOne ( {
where : {
orgRootId : orgRoot.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
posMasterNo : requestBody.posMasterNo ,
} ,
} ) ;
if ( dataDup != null )
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
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-21 16:13:23 +07:00
const chk_SName0 = await this . posMasterRepository . findOne ( {
where : {
orgRevisionId : posMaster.orgRevisionId ,
orgRoot : { orgRootShortName : SName } ,
orgChild1Id : IsNull ( ) ,
posMasterNo : requestBody.posMasterNo ,
} ,
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-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 ;
await this . posMasterRepository . save ( 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 ;
await this . positionRepository . save ( position ) ;
} ) ,
) ;
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" ,
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 ,
@Request ( ) request : { user : Record < string , any > } ,
) {
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-02-19 16:00:19 +07:00
posMaster . posMasterNo = requestBody . posMasterNo ;
posMaster . posMasterNoPrefix = requestBody . posMasterNoPrefix ;
posMaster . posMasterNoSuffix = requestBody . posMasterNoSuffix ;
2024-02-01 20:05:26 +07:00
posMaster . orgRootId = null ;
posMaster . orgChild1Id = null ;
posMaster . orgChild2Id = null ;
posMaster . orgChild3Id = null ;
posMaster . orgChild4Id = null ;
2024-01-31 11:00:51 +07:00
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-02-21 17:10:30 +07:00
SName = orgRoot . shortName ;
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-02-21 17:10:30 +07:00
SName = orgChild1 . shortName ;
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-02-21 17:10:30 +07:00
SName = orgChild2 . shortName ;
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-02-21 17:10:30 +07:00
SName = orgChild3 . shortName ;
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-02-21 17:10:30 +07:00
SName = orgChild4 . shortName ;
2024-01-31 11:00:51 +07:00
if ( ! orgChild4 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลโครงสร้าง" ) ;
} else {
2024-02-20 13:35:11 +07:00
const dataDup : any = await this . posMasterRepository . findOne ( {
where : {
2024-02-20 14:02:19 +07:00
id : Not ( posMaster . id ) ,
orgChild4Id : orgChild4.id ,
2024-02-21 16:13:23 +07:00
// posMasterNoPrefix: requestBody.posMasterNoPrefix,
2024-02-20 13:35:11 +07:00
posMasterNo : requestBody.posMasterNo ,
2024-02-21 16:13:23 +07:00
// posMasterNoSuffix: requestBody.posMasterNoSuffix,
2024-02-20 13:35:11 +07:00
} ,
} ) ;
if ( dataDup != null )
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
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 ;
}
} else {
2024-02-20 13:35:11 +07:00
const dataDup : any = await this . posMasterRepository . findOne ( {
where : {
2024-02-20 14:02:19 +07:00
id : Not ( posMaster . id ) ,
orgChild3Id : orgChild3.id ,
2024-02-20 13:35:11 +07:00
orgChild4Id : IsNull ( ) || "" ,
2024-02-21 16:13:23 +07:00
// posMasterNoPrefix: requestBody.posMasterNoPrefix,
2024-02-20 13:35:11 +07:00
posMasterNo : requestBody.posMasterNo ,
2024-02-21 16:13:23 +07:00
// posMasterNoSuffix: requestBody.posMasterNoSuffix,
2024-02-20 13:35:11 +07:00
} ,
} ) ;
if ( dataDup != null )
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
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 ;
}
} else {
2024-02-20 13:35:11 +07:00
const dataDup : any = await this . posMasterRepository . findOne ( {
where : {
2024-02-20 14:02:19 +07:00
id : Not ( posMaster . id ) ,
orgChild2Id : orgChild2.id ,
2024-02-20 13:35:11 +07:00
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-21 16:13:23 +07:00
// posMasterNoPrefix: requestBody.posMasterNoPrefix,
2024-02-20 13:35:11 +07:00
posMasterNo : requestBody.posMasterNo ,
2024-02-21 16:13:23 +07:00
// posMasterNoSuffix: requestBody.posMasterNoSuffix,
2024-02-20 13:35:11 +07:00
} ,
} ) ;
if ( dataDup != null )
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
2024-01-31 11:00:51 +07:00
posMaster . orgRootId = orgChild2 . orgRootId ;
posMaster . orgChild1Id = orgChild2 . orgChild1Id ;
posMaster . orgChild2Id = orgChild2 . id ;
posMaster . orgRevisionId = orgChild2 . orgRevisionId ;
}
} else {
2024-02-20 13:35:11 +07:00
const dataDup : any = await this . posMasterRepository . findOne ( {
where : {
2024-02-20 14:02:19 +07:00
id : Not ( posMaster . id ) ,
orgChild1Id : orgChild1.id ,
2024-02-20 13:35:11 +07:00
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-21 16:13:23 +07:00
// posMasterNoPrefix: requestBody.posMasterNoPrefix,
2024-02-20 13:35:11 +07:00
posMasterNo : requestBody.posMasterNo ,
2024-02-21 16:13:23 +07:00
// posMasterNoSuffix: requestBody.posMasterNoSuffix,
2024-02-20 13:35:11 +07:00
} ,
} ) ;
if ( dataDup != null )
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
2024-01-31 11:00:51 +07:00
posMaster . orgRootId = orgChild1 . orgRootId ;
posMaster . orgChild1Id = orgChild1 . id ;
posMaster . orgRevisionId = orgChild1 . orgRevisionId ;
}
} else {
2024-02-20 13:35:11 +07:00
const dataDup : any = await this . posMasterRepository . findOne ( {
where : {
2024-02-20 14:02:19 +07:00
id : Not ( posMaster . id ) ,
2024-02-20 13:35:11 +07:00
orgRootId : orgRoot.id ,
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-21 16:13:23 +07:00
// posMasterNoPrefix: requestBody.posMasterNoPrefix,
2024-02-20 13:35:11 +07:00
posMasterNo : requestBody.posMasterNo ,
2024-02-21 16:13:23 +07:00
// posMasterNoSuffix: requestBody.posMasterNoSuffix,
2024-02-20 13:35:11 +07:00
} ,
} ) ;
if ( dataDup != null )
throw new HttpError (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้" ,
) ;
2024-01-31 11:00:51 +07:00
posMaster . orgRootId = orgRoot . id ;
posMaster . orgRevisionId = orgRoot . orgRevisionId ;
}
2024-02-21 16:13:23 +07:00
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
id : Not ( posMaster . id ) ,
2024-02-21 16:13:23 +07:00
} ,
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
id : Not ( posMaster . id ) ,
2024-02-21 16:13:23 +07:00
} ,
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
id : Not ( posMaster . id ) ,
2024-02-21 16:13:23 +07:00
} ,
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
id : Not ( posMaster . id ) ,
2024-02-21 16:13:23 +07:00
} ,
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-02-07 11:03:12 +07:00
posMaster . createdUserId = request . user . sub ; //สงสัยว่าทำให้ bug แก้ไขไม่ได้
2024-01-31 11:00:51 +07:00
posMaster . createdFullName = request . user . name ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
await this . posMasterRepository . save ( 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 ;
position . positionIsSelected = false ;
position . posMasterId = posMaster . id ;
position . createdUserId = request . user . sub ;
position . createdFullName = request . user . name ;
position . lastUpdateUserId = request . user . sub ;
position . lastUpdateFullName = request . user . name ;
await this . positionRepository . save ( position ) ;
} ) ,
) ;
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-01-31 14:29:39 +07:00
try {
const posMaster = await this . posMasterRepository . findOne ( {
where : { id } ,
} ) ;
if ( ! posMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูล" ) ;
}
2024-01-31 15:09:22 +07:00
const positions = await this . positionRepository . find ( {
2024-01-31 14:29:39 +07:00
where : { posMasterId : posMaster.id } ,
2024-02-01 15:44:32 +07:00
relations : [ "posType" , "posLevel" , "posExecutive" ] ,
2024-02-15 09:41:27 +07:00
order : { lastUpdatedAt : "ASC" } ,
2024-01-31 14:29:39 +07:00
} ) ;
const formattedData = {
id : posMaster.id ,
posMasterNoPrefix : posMaster.posMasterNoPrefix ,
posMasterNo : posMaster.posMasterNo ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ,
positions : positions.map ( ( position ) = > ( {
id : position.id ,
positionName : position.positionName ,
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 ,
2024-02-20 17:12:28 +07:00
isSpecial : position.isSpecial ,
2024-01-31 14:29:39 +07:00
} ) ) ,
} ;
return new HttpSuccess ( formattedData ) ;
} catch ( error ) {
return error ;
}
}
2024-01-31 15:09:22 +07:00
/ * *
* API ล บ อ ั ต ร า ก ำ ล ั ง
*
* @summary ORG_035 - ล บ อ ั ต ร า ก ำ ล ั ง ( ADMIN ) # 38
*
* @param { string } id Id ต ำ แ ห น ่ ง
* /
@Delete ( "master/{id}" )
async deletePosMaster ( @Path ( ) id : string ) {
const delPosMaster = await this . posMasterRepository . findOne ( {
where : { id } ,
// relations: ["position"],
} ) ;
if ( ! delPosMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id ) ;
}
try {
await this . positionRepository . delete ( { posMasterId : id } ) ;
await this . posMasterRepository . delete ( { id } ) ;
return new HttpSuccess ( ) ;
} catch ( error ) {
return error ;
}
}
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 (
@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
} ,
) {
try {
let typeCondition : any = { } ;
2024-02-01 11:12:44 +07:00
let checkChildConditions : any = { } ;
2024-02-13 14:31:36 +07:00
let keywordAsInt : any ;
2024-02-20 13:51:05 +07:00
let searchShortName = "" ;
2024-02-13 14:31:36 +07:00
2024-02-01 20:05:26 +07:00
if ( body . type === 0 ) {
typeCondition = {
orgRootId : body.id ,
} ;
if ( ! body . isAll ) {
checkChildConditions = {
orgChild1Id : IsNull ( ) ,
} ;
2024-02-20 13:51:05 +07:00
searchShortName = ` CONCAT(orgRoot.orgRootShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
} else {
searchShortName = ` CONCAT(orgRoot.orgRootShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' or CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' or CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' or CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' or CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
2024-02-01 20:05:26 +07:00
}
} else if ( body . type === 1 ) {
typeCondition = {
orgChild1Id : body.id ,
} ;
if ( ! body . isAll ) {
checkChildConditions = {
orgChild2Id : IsNull ( ) ,
} ;
2024-02-20 13:51:05 +07:00
searchShortName = ` CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
} else {
searchShortName = ` CONCAT(orgChild1.orgChild1ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' or CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' or CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' or CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
2024-02-01 20:05:26 +07:00
}
} else if ( body . type === 2 ) {
typeCondition = {
orgChild2Id : body.id ,
} ;
if ( ! body . isAll ) {
checkChildConditions = {
orgChild3Id : IsNull ( ) ,
} ;
2024-02-20 13:51:05 +07:00
searchShortName = ` CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
} else {
searchShortName = ` CONCAT(orgChild2.orgChild2ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' or CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' or CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
2024-02-01 20:05:26 +07:00
}
} else if ( body . type === 3 ) {
typeCondition = {
orgChild3Id : body.id ,
} ;
if ( ! body . isAll ) {
checkChildConditions = {
orgChild4Id : IsNull ( ) ,
} ;
2024-02-20 13:51:05 +07:00
searchShortName = ` CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
} else {
searchShortName = ` CONCAT(orgChild3.orgChild3ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' or CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
2024-02-01 20:05:26 +07:00
}
} else if ( body . type === 4 ) {
typeCondition = {
orgChild4Id : body.id ,
} ;
2024-02-20 13:51:05 +07:00
searchShortName = ` CONCAT(orgChild4.orgChild4ShortName,posMaster.posMasterNoPrefix,posMaster.posMasterNo,posMaster.posMasterNoSuffix) like '% ${ body . keyword } %' ` ;
2024-02-01 20:05:26 +07:00
}
let findPosition : any ;
let masterId = new Array ( ) ;
2024-02-06 16:23:33 +07:00
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 ) ] ;
2024-02-03 13:09:44 +07:00
}
2024-02-15 09:41:27 +07:00
2024-02-20 17:00:04 +07:00
const revisionCondition = {
orgRevisionId : body.revisionId ,
} ;
2024-02-14 17:42:17 +07:00
const conditions = [
2024-02-05 11:35:35 +07:00
{
. . . checkChildConditions ,
. . . typeCondition ,
2024-02-20 17:00:04 +07:00
. . . revisionCondition ,
2024-02-15 09:41:27 +07:00
. . . ( body . keyword &&
( masterId . length > 0
? { id : In ( masterId ) }
: { posMasterNo : Like ( ` % ${ body . keyword } % ` ) } ) ) ,
2024-02-05 11:35:35 +07:00
} ,
2024-02-15 09:41:27 +07:00
] ;
2024-02-14 17:42:17 +07:00
// if (body.keyword) {
// conditions.push({
// id: masterId.length > 0 ? In(masterId) : false,
// posMasterNo: Like(`%${keywordAsInt}%`),
// });
// }
// const [posMaster, total] = await this.posMasterRepository.findAndCount({
// where: conditions,
2024-02-15 09:41:27 +07:00
// order: { posMasterOrder: "ASC" },
2024-02-14 17:42:17 +07:00
// relations: [
// "orgRoot",
// "orgChild1",
// "orgChild2",
// "orgChild3",
// "orgChild4",
// "current_holder",
// "next_holder",
// ],
// skip: (body.page - 1) * body.pageSize,
// take: body.pageSize,
// });
const [ posMaster , total ] = await AppDataSource . getRepository ( PosMaster )
2024-02-16 12:07:37 +07:00
. 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" )
. where ( conditions )
2024-02-20 17:50:02 +07:00
// // .where("posMaster.orgRevisionId = :revisionId", { revisionId: body.revisionId })
2024-02-20 15:21:26 +07:00
. orWhere (
new Brackets ( ( qb ) = > {
2024-02-20 17:50:02 +07:00
qb . andWhere ( body . keyword != null && body . keyword != "" ? searchShortName : "1=1" )
2024-02-20 15:21:26 +07:00
. andWhere ( checkChildConditions )
. andWhere ( typeCondition )
2024-02-20 17:00:04 +07:00
. andWhere ( revisionCondition ) ;
2024-02-20 15:21:26 +07:00
} ) ,
)
2024-02-16 12:07:37 +07:00
. orWhere (
new Brackets ( ( qb ) = > {
2024-02-20 17:50:02 +07:00
qb . andWhere (
2024-02-16 12:07:37 +07:00
body . keyword != null && body . keyword != ""
? "CONCAT(current_holder.prefix, current_holder.firstName, current_holder.lastName) LIKE :keyword OR :keyword LIKE current_holder.prefix OR :keyword LIKE current_holder.firstName OR :keyword LIKE current_holder.lastName "
: "1=1" ,
{
keyword : ` % ${ body . keyword } % ` ,
} ,
)
. andWhere ( checkChildConditions )
2024-02-20 15:21:26 +07:00
. andWhere ( typeCondition )
2024-02-20 17:00:04 +07:00
. andWhere ( revisionCondition ) ;
2024-02-16 12:07:37 +07:00
} ) ,
)
. orderBy ( "posMaster.posMasterOrder" , "ASC" )
. skip ( ( body . page - 1 ) * body . pageSize )
. take ( body . pageSize )
. getManyAndCount ( ) ;
2024-02-01 17:28:47 +07:00
2024-02-01 20:05:26 +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-01-31 18:24:38 +07:00
2024-02-15 12:13:34 +07:00
let profile : any ;
const chkRevision = await this . orgRevisionRepository . findOne ( {
2024-02-16 12:07:37 +07:00
where : { id : posMaster.orgRevisionId } ,
} ) ;
if ( chkRevision ? . orgRevisionIsCurrent && ! chkRevision ? . orgRevisionIsDraft ) {
2024-02-15 12:13:34 +07:00
profile = await this . profileRepository . findOne ( {
where : { id : String ( posMaster . current_holderId ) } ,
} ) ;
2024-02-16 12:07:37 +07:00
} else if ( ! chkRevision ? . orgRevisionIsCurrent && chkRevision ? . orgRevisionIsDraft ) {
2024-02-15 12:13:34 +07:00
profile = await this . profileRepository . findOne ( {
where : { id : String ( posMaster . next_holderId ) } ,
} ) ;
}
2024-02-15 09:39:13 +07:00
const type = await this . posTypeRepository . findOne ( {
2024-02-15 10:31:47 +07:00
where : { id : String ( profile ? . posTypeId ) } ,
} ) ;
2024-02-15 09:39:13 +07:00
const level = await this . posLevelRepository . findOne ( {
2024-02-15 10:31:47 +07:00
where : { id : String ( profile ? . posLevelId ) } ,
} ) ;
2024-01-31 18:24:38 +07:00
2024-02-12 12:23:37 +07:00
let shortName = "" ;
2024-02-12 10:57:35 +07:00
2024-02-12 15:44:34 +07:00
// if (body.isAll === true) {
2024-02-13 15:36:23 +07:00
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 15:44:34 +07:00
// }
2024-02-12 10:57:35 +07:00
2024-02-01 20:05:26 +07:00
return {
id : posMaster.id ,
2024-02-03 13:09:44 +07:00
orgRootId : posMaster.orgRootId ,
orgChild1Id : posMaster.orgChild1Id ,
orgChild2Id : posMaster.orgChild2Id ,
orgChild3Id : posMaster.orgChild3Id ,
orgChild4Id : posMaster.orgChild4Id ,
2024-02-01 20:05:26 +07:00
posMasterNoPrefix : posMaster.posMasterNoPrefix ,
posMasterNo : posMaster.posMasterNo ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ,
2024-02-09 09:27:11 +07:00
fullNameCurrentHolder :
2024-02-06 12:04:42 +07:00
posMaster . current_holder == null
? null
: ` ${ posMaster . current_holder . prefix } ${ posMaster . current_holder . firstName } ${ posMaster . current_holder . lastName } ` ,
2024-02-09 09:27:11 +07:00
fullNameNextHolder :
2024-02-06 12:04:42 +07:00
posMaster . next_holder == null
? null
: ` ${ posMaster . next_holder . prefix } ${ posMaster . next_holder . firstName } ${ posMaster . next_holder . lastName } ` ,
2024-02-12 10:57:35 +07:00
orgShortname : shortName ,
2024-02-15 09:39:13 +07:00
isSit : posMaster.isSit ,
2024-02-16 12:07:37 +07:00
profilePosition : profile == null || profile . position == null ? null : profile . position ,
profilePostype : type == null || type . posTypeName == null ? null : type . posTypeName ,
2024-02-15 12:13:34 +07:00
profilePoslevel :
2024-02-15 10:31:47 +07:00
level == null || level . posLevelName == null ? null : level . posLevelName ,
2024-02-01 20:05:26 +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 ,
2024-02-20 17:12:28 +07:00
isSpecial : position.isSpecial ,
2024-02-01 20:05:26 +07:00
} ) ) ,
} ;
} ) ,
) ;
return new HttpSuccess ( { data : formattedData , total } ) ;
2024-01-31 17:22:45 +07:00
} catch ( error ) {
return error ;
}
}
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" )
async Sort ( @Body ( ) requestBody : { id : string ; type : number ; sortId : string [ ] } ) {
try {
switch ( requestBody . type ) {
case 0 : {
const rootId = await this . posMasterRepository . findOne ( {
where : { orgRootId : requestBody.id } ,
} ) ;
if ( ! rootId ? . id ) {
2024-02-01 15:06:28 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found rootId: " + requestBody . id ) ;
2024-02-01 13:07:54 +07:00
}
const listPosMasterId_0 = await this . posMasterRepository . find ( {
2024-02-01 15:06:28 +07:00
where : {
orgRootId : requestBody.id ,
orgChild1Id : IsNull ( ) ,
orgChild2Id : IsNull ( ) ,
orgChild3Id : IsNull ( ) ,
orgChild4Id : IsNull ( ) ,
2024-02-01 13:07:54 +07:00
} ,
select : [ "id" , "posMasterOrder" ] ,
} ) ;
if ( ! listPosMasterId_0 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found masterId type 0." ) ;
}
const sortData_0 = listPosMasterId_0 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
await this . posMasterRepository . save ( sortData_0 ) ;
break ;
}
case 1 : {
const child1Id = await this . posMasterRepository . findOne ( {
where : { orgChild1Id : requestBody.id } ,
} ) ;
if ( ! child1Id ? . id ) {
2024-02-01 15:06:28 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found child1Id: " + requestBody . id ) ;
2024-02-01 13:07:54 +07:00
}
const listPosMasterId_1 = await this . posMasterRepository . find ( {
2024-02-01 15:06:28 +07:00
where : {
orgRootId : Not ( IsNull ( ) ) ,
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) ,
orgChild3Id : IsNull ( ) ,
orgChild4Id : IsNull ( ) ,
2024-02-01 13:07:54 +07:00
} ,
select : [ "id" , "posMasterOrder" ] ,
} ) ;
if ( ! listPosMasterId_1 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found masterId type 1." ) ;
}
const sortData_1 = listPosMasterId_1 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
await this . posMasterRepository . save ( sortData_1 ) ;
break ;
}
case 2 : {
const child2Id = await this . posMasterRepository . findOne ( {
where : { orgChild2Id : requestBody.id } ,
} ) ;
if ( ! child2Id ? . id ) {
2024-02-01 15:06:28 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found child2Id: " + requestBody . id ) ;
2024-02-01 13:07:54 +07:00
}
const listPosMasterId_2 = await this . posMasterRepository . find ( {
2024-02-01 15:06:28 +07:00
where : {
orgRootId : Not ( IsNull ( ) ) ,
orgChild1Id : Not ( IsNull ( ) ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) ,
orgChild4Id : IsNull ( ) ,
2024-02-01 13:07:54 +07:00
} ,
select : [ "id" , "posMasterOrder" ] ,
} ) ;
if ( ! listPosMasterId_2 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found masterId type 2." ) ;
}
const sortData_2 = listPosMasterId_2 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
await this . posMasterRepository . save ( sortData_2 ) ;
break ;
}
case 3 : {
const child3Id = await this . posMasterRepository . findOne ( {
where : { orgChild3Id : requestBody.id } ,
} ) ;
if ( ! child3Id ? . id ) {
2024-02-01 15:06:28 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found chil3Id: " + requestBody . id ) ;
2024-02-01 13:07:54 +07:00
}
const listPosMasterId_3 = await this . posMasterRepository . find ( {
2024-02-01 15:06:28 +07:00
where : {
orgRootId : Not ( IsNull ( ) ) ,
orgChild1Id : Not ( IsNull ( ) ) ,
orgChild2Id : Not ( IsNull ( ) ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) ,
2024-02-01 13:07:54 +07:00
} ,
select : [ "id" , "posMasterOrder" ] ,
} ) ;
if ( ! listPosMasterId_3 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found masterId type 3." ) ;
}
const sortData_3 = listPosMasterId_3 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
await this . posMasterRepository . save ( sortData_3 ) ;
break ;
}
case 4 : {
const child4Id = await this . posMasterRepository . findOne ( {
where : { orgChild4Id : requestBody.id } ,
} ) ;
if ( ! child4Id ? . id ) {
2024-02-01 15:06:28 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found child4Id: " + requestBody . id ) ;
2024-02-01 13:07:54 +07:00
}
const listPosMasterId_4 = await this . posMasterRepository . find ( {
2024-02-01 15:06:28 +07:00
where : {
orgRootId : Not ( IsNull ( ) ) ,
orgChild1Id : Not ( IsNull ( ) ) ,
orgChild2Id : Not ( IsNull ( ) ) ,
orgChild3Id : Not ( IsNull ( ) ) ,
orgChild4Id : requestBody.id ,
2024-02-01 13:07:54 +07:00
} ,
select : [ "id" , "posMasterOrder" ] ,
} ) ;
if ( ! listPosMasterId_4 ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found masterId type 4." ) ;
}
const sortData_4 = listPosMasterId_4 . map ( ( data ) = > ( {
id : data.id ,
posMasterOrder : requestBody.sortId.indexOf ( data . id ) + 1 ,
} ) ) ;
await this . posMasterRepository . save ( sortData_4 ) ;
break ;
}
default :
throw new HttpError ( HttpStatusCode . NOT_FOUND , "not found type: " + requestBody . type ) ;
}
return new HttpSuccess ( ) ;
} catch ( error ) {
return error ;
}
}
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}" )
async getHistoryPosMater ( @Path ( ) id : string ) {
const posMaster = await this . posMasterRepository . findOne ( {
where : { id } ,
} ) ;
if ( ! posMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id ) ;
}
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" } ,
relations : [ "orgRoot" , "orgChild1" , "orgChild2" , "orgChild3" , "orgChild4" ] ,
} ) ;
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-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 [ ] } ,
@Request ( ) request : { user : Record < string , any > } ,
) {
try {
const posMasters = await this . posMasterRepository . find ( {
where : { id : In ( requestBody . positionMaster ) } ,
} ) ;
posMasters . forEach ( async ( posMaster : any ) = > {
posMaster . orgRootId = null ;
posMaster . orgChild1Id = null ;
posMaster . orgChild2Id = null ;
posMaster . orgChild3Id = null ;
posMaster . orgChild4Id = null ;
if ( requestBody . type == 0 ) {
const org = await this . orgRootRepository . findOne ( {
where : { id : requestBody.id } ,
} ) ;
if ( org != null ) {
posMaster . orgRootId = org . id ;
posMaster . orgRevisionId = org . orgRevisionId ;
}
}
if ( requestBody . type == 1 ) {
const org = await this . child1Repository . findOne ( {
where : { id : requestBody.id } ,
} ) ;
if ( org != null ) {
posMaster . orgRootId = org . orgRootId ;
posMaster . orgChild1Id = org . id ;
posMaster . orgRevisionId = org . orgRevisionId ;
}
}
if ( requestBody . type == 2 ) {
const org = await this . child2Repository . findOne ( {
where : { id : requestBody.id } ,
} ) ;
if ( org != null ) {
posMaster . orgRootId = org . orgRootId ;
posMaster . orgChild1Id = org . orgChild1Id ;
posMaster . orgChild2Id = org . id ;
posMaster . orgRevisionId = org . orgRevisionId ;
}
}
if ( requestBody . type == 3 ) {
const org = await this . child3Repository . findOne ( {
where : { id : requestBody.id } ,
} ) ;
if ( org != null ) {
posMaster . orgRootId = org . orgRootId ;
posMaster . orgChild1Id = org . orgChild1Id ;
posMaster . orgChild2Id = org . orgChild2Id ;
posMaster . orgChild3Id = org . id ;
posMaster . orgRevisionId = org . orgRevisionId ;
}
}
if ( requestBody . type == 4 ) {
const org = await this . child4Repository . findOne ( {
where : { id : requestBody.id } ,
} ) ;
if ( org != null ) {
posMaster . orgRootId = org . orgRootId ;
posMaster . orgChild1Id = org . orgChild1Id ;
posMaster . orgChild2Id = org . orgChild2Id ;
posMaster . orgChild3Id = org . orgChild3Id ;
posMaster . orgChild4Id = org . id ;
posMaster . orgRevisionId = org . orgRevisionId ;
}
}
posMaster . createdUserId = request . user . sub ;
posMaster . createdFullName = request . user . name ;
posMaster . lastUpdateUserId = request . user . sub ;
posMaster . lastUpdateFullName = request . user . name ;
await this . posMasterRepository . save ( posMaster ) ;
} ) ;
return new HttpSuccess ( ) ;
} catch ( error ) {
return error ;
}
}
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-02 16:15:51 +07:00
try {
let summary : any ;
let totalPosition : any ;
let totalPositionCurrentUse : any ;
let totalPositionCurrentVacant : any ;
let totalPositionNextUse : any ;
let totalPositionNextVacant : any ;
2024-02-13 15:36:23 +07:00
if ( requestBody . isNode === true ) {
2024-02-12 16:27:10 +07:00
switch ( requestBody . type ) {
case 0 : {
2024-02-13 18:37:58 +07:00
// const NodeId = await this.posMasterRepository.findOne({
// where: { orgRootId: requestBody.id },
// });
// if (!NodeId) {
// throw new HttpError(
// HttpStatusCode.NOT_FOUND,
// "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id,
// );
// }
2024-02-12 16:27:10 +07:00
totalPosition = await this . posMasterRepository . count ( {
where : { orgRootId : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
2024-02-13 18:37:58 +07:00
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
2024-02-13 18:37:58 +07:00
current_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
2024-02-13 18:37:58 +07:00
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
2024-02-13 18:37:58 +07:00
next_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
break ;
2024-02-09 16:35:01 +07:00
}
2024-02-12 16:27:10 +07:00
case 1 : {
2024-02-13 18:37:58 +07:00
// const NodeId = await this.posMasterRepository.findOne({
// where: { orgChild1Id: requestBody.id },
// });
// if (!NodeId) {
// throw new HttpError(
// HttpStatusCode.NOT_FOUND,
// "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id,
// );
// }
2024-02-12 16:27:10 +07:00
totalPosition = await this . posMasterRepository . count ( {
where : { orgChild1Id : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgChild1Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgChild1Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
current_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgChild1Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgChild1Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
next_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
break ;
2024-02-09 16:35:01 +07:00
}
2024-02-12 16:27:10 +07:00
case 2 : {
2024-02-13 18:37:58 +07:00
// const NodeId = await this.posMasterRepository.findOne({
// where: { orgChild2Id: requestBody.id },
// });
// if (!NodeId) {
// throw new HttpError(
// HttpStatusCode.NOT_FOUND,
// "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id,
// );
// }
2024-02-12 16:27:10 +07:00
totalPosition = await this . posMasterRepository . count ( {
where : { orgChild2Id : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgChild2Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgChild2Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
current_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgChild2Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgChild2Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
next_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
break ;
2024-02-09 16:35:01 +07:00
}
2024-02-12 16:27:10 +07:00
case 3 : {
2024-02-13 18:37:58 +07:00
// const NodeId = await this.posMasterRepository.findOne({
// where: { orgChild3Id: requestBody.id },
// });
// if (!NodeId) {
// throw new HttpError(
// HttpStatusCode.NOT_FOUND,
// "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id,
// );
// }
2024-02-12 16:27:10 +07:00
totalPosition = await this . posMasterRepository . count ( {
where : { orgChild3Id : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgChild3Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgChild3Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
current_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgChild3Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgChild3Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
next_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
break ;
2024-02-09 16:35:01 +07:00
}
2024-02-12 16:27:10 +07:00
case 4 : {
2024-02-13 18:37:58 +07:00
// const NodeId = await this.posMasterRepository.findOne({
// where: { orgChild4Id: requestBody.id },
// });
// if (!NodeId) {
// throw new HttpError(
// HttpStatusCode.NOT_FOUND,
// "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id,
// );
// }
2024-02-12 16:27:10 +07:00
totalPosition = await this . posMasterRepository . count ( {
where : { orgChild4Id : requestBody.id } ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgChild4Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgChild4Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
current_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgChild4Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgChild4Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
next_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
break ;
}
default :
break ;
2024-02-12 10:57:35 +07:00
}
2024-02-13 15:36:23 +07:00
} else {
2024-02-12 16:27:10 +07:00
switch ( requestBody . type ) {
case 0 : {
2024-02-13 18:37:58 +07:00
// const NodeId = await this.posMasterRepository.findOne({
// where: { orgRootId: requestBody.id },
// });
// if (!NodeId) {
// throw new HttpError(
// HttpStatusCode.NOT_FOUND,
// "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id,
// );
// }
2024-02-12 16:27:10 +07:00
totalPosition = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
2024-02-13 15:36:23 +07:00
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
2024-02-13 15:36:23 +07:00
where : {
2024-02-12 16:27:10 +07:00
orgRootId : requestBody.id ,
2024-02-13 15:36:23 +07:00
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
2024-02-13 15:36:23 +07:00
where : {
2024-02-12 16:27:10 +07:00
orgRootId : requestBody.id ,
2024-02-13 15:36:23 +07:00
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
current_holderId : IsNull ( ) || "" ,
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgRootId : requestBody.id ,
2024-02-13 15:36:23 +07:00
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
2024-02-13 15:36:23 +07:00
where : {
2024-02-12 16:27:10 +07:00
orgRootId : requestBody.id ,
2024-02-13 15:36:23 +07:00
orgChild1Id : IsNull ( ) || "" ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
next_holderId : IsNull ( ) || "" ,
} ,
} ) ;
break ;
2024-02-09 16:35:01 +07:00
}
2024-02-12 16:27:10 +07:00
case 1 : {
2024-02-13 18:37:58 +07:00
// const NodeId = await this.posMasterRepository.findOne({
// where: { orgChild1Id: requestBody.id },
// });
// if (!NodeId) {
// throw new HttpError(
// HttpStatusCode.NOT_FOUND,
// "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id,
// );
// }
2024-02-12 16:27:10 +07:00
totalPosition = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-13 18:37:58 +07:00
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-13 18:37:58 +07:00
current_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-13 18:37:58 +07:00
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : requestBody.id ,
orgChild2Id : IsNull ( ) || "" ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-13 18:37:58 +07:00
next_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
break ;
}
case 2 : {
2024-02-13 18:37:58 +07:00
// const NodeId = await this.posMasterRepository.findOne({
// where: { orgChild2Id: requestBody.id },
// });
// if (!NodeId) {
// throw new HttpError(
// HttpStatusCode.NOT_FOUND,
// "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id,
// );
// }
2024-02-12 16:27:10 +07:00
totalPosition = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-13 18:37:58 +07:00
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-13 18:37:58 +07:00
current_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-13 18:37:58 +07:00
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : requestBody.id ,
orgChild3Id : IsNull ( ) || "" ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-13 18:37:58 +07:00
next_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
break ;
}
case 3 : {
2024-02-13 18:37:58 +07:00
// const NodeId = await this.posMasterRepository.findOne({
// where: { orgChild3Id: requestBody.id },
// });
// if (!NodeId) {
// throw new HttpError(
// HttpStatusCode.NOT_FOUND,
// "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id,
// );
// }
2024-02-12 16:27:10 +07:00
totalPosition = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-13 18:37:58 +07:00
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-13 18:37:58 +07:00
current_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-13 18:37:58 +07:00
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-13 15:36:23 +07:00
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : requestBody.id ,
orgChild4Id : IsNull ( ) || "" ,
2024-02-13 18:37:58 +07:00
next_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
break ;
}
case 4 : {
2024-02-13 18:37:58 +07:00
// const NodeId = await this.posMasterRepository.findOne({
// where: { orgChild4Id: requestBody.id },
// });
// if (!NodeId) {
// throw new HttpError(
// HttpStatusCode.NOT_FOUND,
// "ไม่พบข้อมูลตามไอดีนี้ : " + requestBody.id,
// );
// }
2024-02-12 16:27:10 +07:00
totalPosition = await this . posMasterRepository . count ( {
where : {
2024-02-13 15:36:23 +07:00
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild4Id : requestBody.id ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentUse = await this . posMasterRepository . count ( {
where : {
2024-02-13 15:36:23 +07:00
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild4Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
current_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionCurrentVacant = await this . posMasterRepository . count ( {
where : {
2024-02-13 15:36:23 +07:00
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild4Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
current_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextUse = await this . posMasterRepository . count ( {
where : {
2024-02-13 15:36:23 +07:00
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild4Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
next_holderId : Not ( IsNull ( ) ) || Not ( "" ) ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
totalPositionNextVacant = await this . posMasterRepository . count ( {
where : {
2024-02-13 15:36:23 +07:00
orgRootId : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild1Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild2Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild3Id : Not ( IsNull ( ) ) || Not ( "" ) ,
orgChild4Id : requestBody.id ,
2024-02-13 18:37:58 +07:00
next_holderId : IsNull ( ) || "" ,
2024-02-12 16:27:10 +07:00
} ,
} ) ;
break ;
}
default :
break ;
2024-02-02 16:15:51 +07:00
}
}
2024-02-03 13:09:44 +07:00
summary = {
2024-02-02 16:15:51 +07:00
totalPosition : totalPosition ,
totalPositionCurrentUse : totalPositionCurrentUse ,
totalPositionCurrentVacant : totalPositionCurrentVacant ,
totalPositionNextUse : totalPositionNextUse ,
2024-02-03 13:09:44 +07:00
totalPositionNextVacant : totalPositionNextVacant ,
} ;
2024-02-02 16:15:51 +07:00
return new HttpSuccess ( summary ) ;
} catch ( error ) {
return error ;
}
}
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 } ,
) {
const dataMaster = await this . posMasterRepository . findOne ( {
where : { id : requestBody.posMaster } ,
relations : [ "positions" ] ,
} ) ;
if ( ! dataMaster ) {
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
"ไม่พบข้อมูลไอดีนี้ : " + requestBody . posMaster ,
) ;
}
try {
dataMaster . positions . forEach ( async ( position ) = > {
if ( position . id === requestBody . position ) {
position . positionIsSelected = true ;
} else {
position . positionIsSelected = false ;
}
await this . positionRepository . save ( position ) ;
} ) ;
dataMaster . isSit = requestBody . isSit ;
dataMaster . next_holderId = requestBody . profileId ;
await this . posMasterRepository . save ( dataMaster ) ;
return new HttpSuccess ( ) ;
} catch ( error ) {
return error ;
}
}
/ * *
* API ส ร ้ า ง ท ะ เ บ ี ย น ป ร ะ ว ั ต ิ
*
* @summary ORG_066 - ล บ ค น ค ร อ ง ต ำ แ ห น ่ ง ( ADMIN ) # 71
*
* @param { string } id * Id posMaster
* /
@Post ( "profile/delete/{id}" )
async deleteHolder ( @Path ( ) id : string ) {
const dataMaster = await this . posMasterRepository . findOne ( {
where : { id : id } ,
relations : [ "positions" ] ,
} ) ;
if ( ! dataMaster ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลไอดีนี้ : " + id ) ;
}
try {
await this . posMasterRepository . update ( id , {
isSit : false ,
next_holderId : null ,
} ) ;
dataMaster . positions . forEach ( async ( position ) = > {
await this . positionRepository . update ( position . id , {
positionIsSelected : false ,
} ) ;
} ) ;
return new HttpSuccess ( ) ;
} catch ( error ) {
return error ;
}
}
2024-02-12 17:06:31 +07:00
/ * *
* API ส ื บ ท อ ด ต ำ แ ห น ่ ง
*
* @summary ORG_068 - ส ื บ ท อ ด ต ำ แ ห น ่ ง ( ADMIN ) # 74
*
* /
@Post ( "dna" )
2024-02-13 15:36:23 +07:00
async dna ( @Body ( ) requestBody : { draftPositionId : string ; publishPositionId : string } ) {
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-13 15:37:41 +07:00
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
"ไม่พบข้อมูลไอดีนี้(publishPositionId) : " + requestBody . publishPositionId ,
) ;
}
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-13 15:36:23 +07:00
throw new HttpError (
HttpStatusCode . NOT_FOUND ,
"ไม่พบข้อมูลไอดีนี้(draftPositionId) : " + requestBody . draftPositionId ,
) ;
2024-02-12 17:06:31 +07:00
}
try {
2024-02-13 14:31:36 +07:00
await this . posMasterRepository . update (
2024-02-13 17:14:54 +07:00
{ orgRevisionId : findDraft.id , ancestorDNA : dataPublish.ancestorDNA } ,
2024-02-13 15:37:41 +07:00
{ ancestorDNA : "" } ,
2024-02-13 14:31:36 +07:00
) ;
2024-02-13 19:03:13 +07:00
if ( dataPublish . ancestorDNA == null || dataPublish . ancestorDNA == "" )
dataPublish . ancestorDNA = dataPublish . id ;
2024-02-12 17:06:31 +07:00
dataDraft . ancestorDNA = dataPublish . ancestorDNA ;
await this . posMasterRepository . save ( dataDraft ) ;
2024-02-13 19:03:13 +07:00
await this . posMasterRepository . save ( dataPublish ) ;
2024-02-12 17:06:31 +07:00
return new HttpSuccess ( ) ;
} catch ( error ) {
return error ;
}
}
2024-02-20 13:35:11 +07:00
/ * *
* API ค ้ น ห า ต ำ แ ห น ่ ง ใ น ร ะ บ บ ส ม ั ค ร ส อ บ
*
* @summary ค ้ น ห า ต ำ แ ห น ่ ง ใ น ร ะ บ บ ส ม ั ค ร ส อ บ
*
* /
@Post ( "placement/search" )
async searchPlacement ( @Body ( ) body : { node : number ; nodeId : string ; position : string } ) {
let typeCondition : any = { } ;
if ( body . node === 0 ) {
typeCondition = {
orgRootId : body.nodeId ,
orgChild1Id : IsNull ( ) ,
next_holderId : IsNull ( ) ,
} ;
} else if ( body . node === 1 ) {
typeCondition = {
orgChild1Id : body.nodeId ,
orgChild2Id : IsNull ( ) ,
next_holderId : IsNull ( ) ,
} ;
} else if ( body . node === 2 ) {
typeCondition = {
orgChild2Id : body.nodeId ,
orgChild3Id : IsNull ( ) ,
next_holderId : IsNull ( ) ,
} ;
} else if ( body . node === 3 ) {
typeCondition = {
orgChild3Id : body.nodeId ,
orgChild4Id : IsNull ( ) ,
next_holderId : IsNull ( ) ,
} ;
} else if ( body . node === 4 ) {
typeCondition = {
orgChild4Id : body.nodeId ,
next_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" )
. where ( typeCondition )
. orderBy ( "posMaster.posMasterOrder" , "ASC" )
. getManyAndCount ( ) ;
const formattedData = await Promise . all (
posMaster . map ( async ( posMaster ) = > {
const positions = await this . positionRepository . find ( {
where : {
posMasterId : posMaster.id ,
} ,
relations : [ "posLevel" , "posType" , "posExecutive" ] ,
} ) ;
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 . 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 ;
}
return {
id : posMaster.id ,
orgRootId : posMaster.orgRootId ,
orgChild1Id : posMaster.orgChild1Id ,
orgChild2Id : posMaster.orgChild2Id ,
orgChild3Id : posMaster.orgChild3Id ,
orgChild4Id : posMaster.orgChild4Id ,
posMasterNoPrefix : posMaster.posMasterNoPrefix ,
posMasterNo : posMaster.posMasterNo ,
posMasterNoSuffix : posMaster.posMasterNoSuffix ,
fullNameCurrentHolder :
posMaster . current_holder == null
? null
: ` ${ posMaster . current_holder . prefix } ${ posMaster . current_holder . firstName } ${ posMaster . current_holder . lastName } ` ,
fullNameNextHolder :
posMaster . next_holder == null
? null
: ` ${ posMaster . next_holder . prefix } ${ posMaster . next_holder . firstName } ${ posMaster . next_holder . lastName } ` ,
orgShortname : shortName ,
isSit : posMaster.isSit ,
profilePosition : profile == null || profile . position == null ? null : profile . position ,
profilePostype : type == null || type . posTypeName == null ? null : type . posTypeName ,
profilePoslevel : level == null || level . posLevelName == null ? null : level . posLevelName ,
isPosition : positions.filter ( ( x ) = > x . positionName == body . position ) . length > 0 ,
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 ,
2024-02-20 17:12:28 +07:00
isSpecial : position.isSpecial ,
2024-02-20 13:35:11 +07:00
} ) ) ,
} ;
} ) ,
) ;
return new HttpSuccess ( { data : formattedData , total } ) ;
}
2024-01-30 15:10:11 +07:00
}