2024-09-11 17:29:33 +07:00
import {
Controller ,
Post ,
Put ,
Delete ,
Route ,
Security ,
Tags ,
Body ,
Path ,
Request ,
Response ,
Get ,
Query ,
} from "tsoa" ;
import { AppDataSource } from "../database/data-source" ;
import HttpSuccess from "../interfaces/http-success" ;
import HttpStatusCode from "../interfaces/http-status" ;
import HttpError from "../interfaces/http-error" ;
import { Command } from "../entities/Command" ;
2024-11-12 10:46:33 +07:00
import { Brackets , LessThan , MoreThan , Double , In , Between , IsNull , Not , Like } from "typeorm" ;
2024-09-11 17:29:33 +07:00
import { CommandType } from "../entities/CommandType" ;
2024-09-12 22:53:58 +07:00
import { CommandSend } from "../entities/CommandSend" ;
2024-10-08 16:35:08 +07:00
import { Profile , CreateProfileAllFields } from "../entities/Profile" ;
2024-09-12 22:53:58 +07:00
import { RequestWithUser } from "../middlewares/user" ;
import { OrgRevision } from "../entities/OrgRevision" ;
2024-09-13 15:34:01 +07:00
import { CommandSendCC } from "../entities/CommandSendCC" ;
2024-09-25 11:17:38 +07:00
import { CommandSalary } from "../entities/CommandSalary" ;
import { CommandRecive } from "../entities/CommandRecive" ;
import HttpStatus from "../interfaces/http-status" ;
2024-09-26 16:05:59 +07:00
import Extension from "../interfaces/extension" ;
2024-10-01 17:51:48 +07:00
import { ProfileEmployee } from "../entities/ProfileEmployee" ;
import CallAPI from "../interfaces/call-api" ;
2024-10-08 16:35:08 +07:00
import { ProfileSalary , CreateProfileSalary } from "../entities/ProfileSalary" ;
2024-10-04 15:35:14 +07:00
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory" ;
2024-10-08 16:35:08 +07:00
import {
calculateRetireDate ,
calculateRetireLaw ,
2024-10-18 17:35:04 +07:00
commandTypePath ,
2024-10-08 16:35:08 +07:00
removeProfileInOrganize ,
setLogDataDiff ,
2024-10-28 20:59:31 +07:00
checkReturnCommandType ,
2024-10-28 18:01:36 +07:00
checkExceptCommandType ,
2024-10-28 11:48:00 +07:00
checkCommandType ,
2024-10-08 16:35:08 +07:00
} from "../interfaces/utils" ;
2024-10-04 15:35:14 +07:00
import { Position } from "../entities/Position" ;
import { PosMaster } from "../entities/PosMaster" ;
import { EmployeePosition } from "../entities/EmployeePosition" ;
import { EmployeePosMaster } from "../entities/EmployeePosMaster" ;
import { ProfileDiscipline } from "../entities/ProfileDiscipline" ;
import { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory" ;
2024-10-04 22:36:34 +07:00
import { PosMasterAct } from "../entities/PosMasterAct" ;
2024-10-11 11:05:31 +07:00
import { sendToQueue } from "../services/rabbitmq" ;
2024-10-08 16:35:08 +07:00
import { PosLevel } from "../entities/PosLevel" ;
import { PosType } from "../entities/PosType" ;
2024-11-14 14:42:32 +07:00
import { addUserRoles , createUser , getRoles , deleteUser , enableStatus } from "../keycloak" ;
2024-10-08 16:35:08 +07:00
import { ProfileEducation , CreateProfileEducation } from "../entities/ProfileEducation" ;
import { ProfileEducationHistory } from "../entities/ProfileEducationHistory" ;
2024-10-10 11:12:31 +07:00
import { CreateProfileCertificate , ProfileCertificate } from "../entities/ProfileCertificate" ;
2024-10-08 16:35:08 +07:00
import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory" ;
2024-10-10 11:12:31 +07:00
import permission from "../interfaces/permission" ;
2024-11-01 19:53:26 +07:00
import { CommandSign } from "../entities/CommandSign" ;
2024-11-12 10:46:33 +07:00
import { RoleKeycloak } from "../entities/RoleKeycloak" ;
2024-09-11 17:29:33 +07:00
@Route ( "api/v1/org/command" )
@Tags ( "Command" )
@Security ( "bearerAuth" )
@Response (
HttpStatusCode . INTERNAL_SERVER_ERROR ,
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง" ,
)
export class CommandController extends Controller {
private commandRepository = AppDataSource . getRepository ( Command ) ;
private commandTypeRepository = AppDataSource . getRepository ( CommandType ) ;
2024-09-12 22:53:58 +07:00
private commandSendRepository = AppDataSource . getRepository ( CommandSend ) ;
2024-09-13 15:34:01 +07:00
private commandSendCCRepository = AppDataSource . getRepository ( CommandSendCC ) ;
2024-09-25 11:17:38 +07:00
private commandSalaryRepository = AppDataSource . getRepository ( CommandSalary ) ;
private commandReciveRepository = AppDataSource . getRepository ( CommandRecive ) ;
2024-09-12 22:53:58 +07:00
private profileRepository = AppDataSource . getRepository ( Profile ) ;
2024-10-01 17:51:48 +07:00
private profileEmployeeRepository = AppDataSource . getRepository ( ProfileEmployee ) ;
2024-09-12 22:53:58 +07:00
private orgRevisionRepo = AppDataSource . getRepository ( OrgRevision ) ;
2024-10-04 15:35:14 +07:00
private salaryRepo = AppDataSource . getRepository ( ProfileSalary ) ;
private salaryHistoryRepo = AppDataSource . getRepository ( ProfileSalaryHistory ) ;
private posMasterRepository = AppDataSource . getRepository ( PosMaster ) ;
private positionRepository = AppDataSource . getRepository ( Position ) ;
private employeePosMasterRepository = AppDataSource . getRepository ( EmployeePosMaster ) ;
private employeePositionRepository = AppDataSource . getRepository ( EmployeePosition ) ;
private disciplineRepository = AppDataSource . getRepository ( ProfileDiscipline ) ;
private disciplineHistoryRepository = AppDataSource . getRepository ( ProfileDisciplineHistory ) ;
2024-10-04 22:36:34 +07:00
private posMasterActRepository = AppDataSource . getRepository ( PosMasterAct ) ;
2024-10-08 16:35:08 +07:00
private posLevelRepo = AppDataSource . getRepository ( PosLevel ) ;
private posTypeRepo = AppDataSource . getRepository ( PosType ) ;
private profileEducationRepo = AppDataSource . getRepository ( ProfileEducation ) ;
private profileEducationHistoryRepo = AppDataSource . getRepository ( ProfileEducationHistory ) ;
private certificateRepo = AppDataSource . getRepository ( ProfileCertificate ) ;
private certificateHistoryRepo = AppDataSource . getRepository ( ProfileCertificateHistory ) ;
2024-10-10 17:42:20 +07:00
private orgRevisionRepository = AppDataSource . getRepository ( OrgRevision ) ;
2024-11-01 19:53:26 +07:00
private commandSignRepository = AppDataSource . getRepository ( CommandSign ) ;
2024-11-12 10:46:33 +07:00
private roleKeycloakRepo = AppDataSource . getRepository ( RoleKeycloak ) ;
2024-09-11 17:29:33 +07:00
/ * *
* API list ร า ย ก า ร ค ำ ส ั ่ ง
*
* @summary API list ร า ย ก า ร ค ำ ส ั ่ ง
*
* /
@Get ( "list" )
async GetResult (
2024-10-09 16:20:43 +07:00
@Request ( ) request : RequestWithUser ,
2024-09-11 17:29:33 +07:00
@Query ( "page" ) page : number = 1 ,
@Query ( "pageSize" ) pageSize : number = 10 ,
@Query ( ) keyword : string = "" ,
@Query ( ) commandTypeId? : string | null ,
@Query ( ) year? : number ,
@Query ( ) status? : string | null ,
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionList ( request , "COMMAND" ) ;
2024-10-10 11:12:31 +07:00
let profilekArray : any = [ ] ;
let _profile = await this . profileRepository . findOne ( {
where : { keycloak : request.user.sub } ,
relations : [ "current_holders" , "current_holders.orgRevision" ] ,
} ) ;
let isDirector =
_profile ? . current_holders ? . filter (
( x ) = >
x . orgRevision ? . orgRevisionIsCurrent == true && x . orgRevision ? . orgRevisionIsDraft == false ,
) [ 0 ] ? . isDirector || false ;
if ( isDirector ) {
let _data : any = {
root : null ,
child1 : null ,
child2 : null ,
child3 : null ,
child4 : null ,
} ;
if ( ! request . user . role . includes ( "SUPER_ADMIN" ) ) {
2024-10-10 11:15:33 +07:00
_data = await new permission ( ) . PermissionOrgList ( request , "COMMAND" ) ;
2024-10-10 11:12:31 +07:00
}
const profiles = await this . profileRepository
. createQueryBuilder ( "profile" )
. leftJoinAndSelect ( "profile.current_holders" , "current_holders" )
. leftJoinAndSelect ( "current_holders.orgRoot" , "orgRoot" )
. leftJoinAndSelect ( "current_holders.orgChild1" , "orgChild1" )
. leftJoinAndSelect ( "current_holders.orgChild2" , "orgChild2" )
. leftJoinAndSelect ( "current_holders.orgChild3" , "orgChild3" )
. leftJoinAndSelect ( "current_holders.orgChild4" , "orgChild4" )
. andWhere (
_data . root != undefined && _data . root != null
? _data . root [ 0 ] != null
? ` current_holders.orgRootId IN (:...root) `
: ` current_holders.orgRootId is null `
: "1=1" ,
{
root : _data.root ,
} ,
)
. andWhere (
_data . child1 != undefined && _data . child1 != null
? _data . child1 [ 0 ] != null
? ` current_holders.orgChild1Id IN (:...child1) `
: ` current_holders.orgChild1Id is null `
: "1=1" ,
{
child1 : _data.child1 ,
} ,
)
. andWhere (
_data . child2 != undefined && _data . child2 != null
? _data . child2 [ 0 ] != null
? ` current_holders.orgChild2Id IN (:...child2) `
: ` current_holders.orgChild2Id is null `
: "1=1" ,
{
child2 : _data.child2 ,
} ,
)
. andWhere (
_data . child3 != undefined && _data . child3 != null
? _data . child3 [ 0 ] != null
? ` current_holders.orgChild3Id IN (:...child3) `
: ` current_holders.orgChild3Id is null `
: "1=1" ,
{
child3 : _data.child3 ,
} ,
)
. andWhere (
_data . child4 != undefined && _data . child4 != null
? _data . child4 [ 0 ] != null
? ` current_holders.orgChild4Id IN (:...child4) `
: ` current_holders.orgChild4Id is null `
: "1=1" ,
{
child4 : _data.child4 ,
} ,
)
. select ( "profile.keycloak" , "keycloak" )
. getRawMany ( ) ;
profilekArray = profiles . map ( ( p ) = > p . keycloak ) ;
}
2024-09-11 17:29:33 +07:00
const [ commands , total ] = await this . commandRepository
. createQueryBuilder ( "command" )
2024-10-10 11:12:31 +07:00
. andWhere (
new Brackets ( ( qb ) = > {
qb . orWhere (
profilekArray . length > 0
? "command.createdUserId IN (:...profilekArray)"
: "command.createdUserId='1'" ,
{
profilekArray : profilekArray ,
} ,
) . orWhere ( "command.createdUserId = :createdUserId" , {
createdUserId : request.user.sub ,
} ) ;
} ) ,
)
2024-09-11 17:29:33 +07:00
. andWhere (
status != null && status != undefined && status != ""
2024-09-24 11:31:08 +07:00
? "command.status IN (:...status)"
2024-09-11 17:29:33 +07:00
: "1=1" ,
{
status :
status == null || status == undefined || status == ""
? null
2024-09-24 09:45:43 +07:00
: status . trim ( ) . toLocaleUpperCase ( ) == "NEW" ||
status . trim ( ) . toLocaleUpperCase ( ) == "DRAFT"
? [ "NEW" , "DRAFT" ]
: [ status . trim ( ) . toLocaleUpperCase ( ) ] ,
2024-09-11 17:29:33 +07:00
} ,
)
2024-09-12 22:53:58 +07:00
. andWhere (
year != null && year != undefined && year != 0
? "command.commandYear = :commandYear"
: "1=1" ,
{
commandYear : year == null || year == undefined || year == 0 ? null : ` ${ year } ` ,
} ,
)
2024-09-11 17:29:33 +07:00
. andWhere (
commandTypeId != null && commandTypeId != undefined && commandTypeId != ""
? "command.commandTypeId = :commandTypeId"
: "1=1" ,
{
commandTypeId :
commandTypeId == null || commandTypeId == undefined || commandTypeId == ""
? null
: ` ${ commandTypeId } ` ,
} ,
)
2024-10-08 10:09:56 +07:00
. andWhere (
new Brackets ( ( qb ) = > {
qb . where ( keyword != null && keyword != "" ? "command.commandNo LIKE :keyword" : "1=1" , {
keyword : ` % ${ keyword } % ` ,
2024-11-01 19:53:26 +07:00
} )
. orWhere ( keyword != null && keyword != "" ? "command.issue LIKE :keyword" : "1=1" , {
keyword : ` % ${ keyword } % ` ,
} )
. orWhere (
keyword != null && keyword != "" ? "command.createdFullName LIKE :keyword" : "1=1" ,
{
keyword : ` % ${ keyword } % ` ,
} ,
) ;
2024-10-08 10:09:56 +07:00
} ) ,
)
2024-09-26 19:46:09 +07:00
. orderBy ( "command.createdAt" , "DESC" )
2024-09-11 17:29:33 +07:00
. skip ( ( page - 1 ) * pageSize )
. take ( pageSize )
. getManyAndCount ( ) ;
const data = commands . map ( ( _data ) = > ( {
id : _data.id ,
commandNo : _data.commandNo ,
commandYear : _data.commandYear ,
commandAffectDate : _data.commandAffectDate ,
commandExcecuteDate : _data.commandExcecuteDate ,
2024-10-17 13:51:57 +07:00
assignFullName : null , //xxxxxxxxxxxxxxx
2024-09-11 17:29:33 +07:00
createdFullName : _data.createdFullName ,
status : _data.status ,
2024-09-25 16:17:06 +07:00
issue : _data.issue ,
2024-09-11 17:29:33 +07:00
} ) ) ;
return new HttpSuccess ( { data , total } ) ;
}
/ * *
* API ส ร ้ า ง ร า ย ก า ร body ค ำ ส ั ่ ง
*
* @summary API ส ร ้ า ง ร า ย ก า ร body ค ำ ส ั ่ ง
*
* /
@Post ( )
async Post (
@Body ( )
requestBody : {
commandTypeId : string ;
commandNo : string | null ;
commandYear : number | null ;
} ,
2024-09-12 22:53:58 +07:00
@Request ( ) request : RequestWithUser ,
2024-09-11 17:29:33 +07:00
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionCreate ( request , "COMMAND" ) ;
2024-09-11 17:29:33 +07:00
const command = Object . assign ( new Command ( ) , requestBody ) ;
const commandType = await this . commandTypeRepository . findOne ( {
where : { id : requestBody.commandTypeId } ,
} ) ;
if ( ! commandType ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบประเภทคำสั่งนี้ในระบบ" ) ;
}
2024-09-23 16:16:59 +07:00
command . detailHeader = commandType . detailHeader ;
command . detailBody = commandType . detailBody ;
command . detailFooter = commandType . detailFooter ;
command . isAttachment = commandType . isAttachment ;
2024-09-24 09:45:43 +07:00
command . status = "NEW" ;
2024-09-12 22:53:58 +07:00
command . issue = commandType . name ;
2024-09-11 17:29:33 +07:00
command . createdUserId = request . user . sub ;
command . createdFullName = request . user . name ;
command . createdAt = new Date ( ) ;
command . lastUpdateUserId = request . user . sub ;
command . lastUpdateFullName = request . user . name ;
command . lastUpdatedAt = new Date ( ) ;
await this . commandRepository . save ( command ) ;
return new HttpSuccess ( command . id ) ;
}
/ * *
* API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab1
*
* @summary API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab1
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Get ( "tab1/{id}" )
2024-10-24 10:21:24 +07:00
async GetByIdTab1 ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
await new permission ( ) . PermissionGet ( request , "COMMAND" ) ;
2024-09-11 17:29:33 +07:00
const command = await this . commandRepository . findOne ( {
where : { id } ,
2024-09-12 22:53:58 +07:00
relations : [ "commandType" , "commandType.commandTypeSys" ] ,
2024-09-11 17:29:33 +07:00
} ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
2024-09-12 22:53:58 +07:00
const _command = {
id : command.id ,
status : command.status ,
commandNo : command.commandNo ,
commandYear : command.commandYear ,
issue : command.issue ,
detailHeader : command.detailHeader ,
detailBody : command.detailBody ,
detailFooter : command.detailFooter ,
2024-09-25 21:05:16 +07:00
isBangkok : command.isBangkok ,
isAttachment : command.isAttachment ,
2024-09-19 15:57:01 +07:00
commandAffectDate : command.commandAffectDate ,
commandExcecuteDate : command.commandExcecuteDate ,
2024-09-12 22:53:58 +07:00
commandTypeName : command.commandType?.name || null ,
2024-11-01 14:14:13 +07:00
commandCode : command.commandType?.code || null ,
2024-09-12 22:53:58 +07:00
commandSysId : command.commandType?.commandSysId || null ,
} ;
return new HttpSuccess ( _command ) ;
2024-09-11 17:29:33 +07:00
}
/ * *
* API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง Tab1
*
* @summary API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง Tab1
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "tab1/{id}" )
async PutTab1 (
@Path ( ) id : string ,
@Body ( )
requestBody : {
commandNo : string | null ;
commandYear : number | null ;
issue : string | null ;
detailHeader : string | null ;
detailBody : string | null ;
detailFooter : string | null ;
2024-09-19 15:57:01 +07:00
commandAffectDate : Date | null ;
commandExcecuteDate : Date | null ;
2024-10-09 12:53:13 +07:00
isBangkok : string | null ;
2024-09-11 17:29:33 +07:00
} ,
2024-09-12 22:53:58 +07:00
@Request ( ) request : RequestWithUser ,
2024-09-11 17:29:33 +07:00
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-11 17:29:33 +07:00
const command = await this . commandRepository . findOne ( { where : { id : id } } ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
const data = new Command ( ) ;
Object . assign ( data , { . . . command , . . . requestBody } ) ;
data . lastUpdateUserId = request . user . sub ;
data . lastUpdateFullName = request . user . name ;
data . lastUpdatedAt = new Date ( ) ;
await this . commandRepository . save ( data ) ;
return new HttpSuccess ( ) ;
}
2024-09-24 16:42:24 +07:00
/ * *
* API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab2
*
* @summary API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab2
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Get ( "tab2/{id}" )
2024-10-24 10:21:24 +07:00
async GetByIdTab2 ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
await new permission ( ) . PermissionGet ( request , "COMMAND" ) ;
2024-09-24 16:42:24 +07:00
const command = await this . commandRepository . findOne ( {
where : { id } ,
relations : [ "commandSalary" , "commandRecives" ] ,
} ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
const _command = {
id : command.id ,
commandSalaryId : command.commandSalaryId ,
commandSalary : command.commandSalary?.name || null ,
positionDetail : command.positionDetail ,
2024-09-25 16:17:06 +07:00
commandRecives : command.commandRecives
2024-09-24 16:42:24 +07:00
. sort ( ( a , b ) = > a . order - b . order )
. map ( ( x ) = > ( {
2024-09-25 16:17:06 +07:00
id : x.id ,
2024-09-24 16:42:24 +07:00
citizenId : x.citizenId ,
2024-10-02 14:08:40 +07:00
prefix : x.prefix ,
firstName : x.firstName ,
lastName : x.lastName ,
// profileId: x.profileId,
2024-09-24 16:42:24 +07:00
order : x.order ,
2024-09-25 16:17:06 +07:00
remarkVertical : x.remarkVertical ,
remarkHorizontal : x.remarkHorizontal ,
amount : x.amount ,
2024-12-10 12:06:15 +07:00
amountSpecial : x.amountSpecial ,
2024-09-25 16:17:06 +07:00
positionSalaryAmount : x.positionSalaryAmount ,
mouthSalaryAmount : x.mouthSalaryAmount ,
2024-09-24 16:42:24 +07:00
} ) ) ,
} ;
return new HttpSuccess ( _command ) ;
}
/ * *
* API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง Tab2
*
* @summary API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง Tab2
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "tab2/{id}" )
async PutTab2 (
@Path ( ) id : string ,
@Body ( )
requestBody : {
2024-09-25 11:17:38 +07:00
positionDetail : string | null ;
commandSalaryId : string | null ;
2024-09-24 16:42:24 +07:00
} ,
@Request ( ) request : RequestWithUser ,
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-24 16:42:24 +07:00
const command = await this . commandRepository . findOne ( { where : { id : id } } ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
2024-09-25 11:17:38 +07:00
if ( requestBody . commandSalaryId != undefined && requestBody . commandSalaryId != null ) {
const commandSalary = await this . commandSalaryRepository . findOne ( {
where : { id : requestBody.commandSalaryId } ,
} ) ;
if ( ! commandSalary ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลต้นแบบ" ) ;
}
}
2024-09-24 16:42:24 +07:00
const data = new Command ( ) ;
Object . assign ( data , { . . . command , . . . requestBody } ) ;
data . lastUpdateUserId = request . user . sub ;
data . lastUpdateFullName = request . user . name ;
data . lastUpdatedAt = new Date ( ) ;
await this . commandRepository . save ( data ) ;
return new HttpSuccess ( ) ;
}
2024-09-25 21:05:16 +07:00
@Get ( "tab2/swap/{direction}/{commandReciveId}" )
2024-09-25 11:17:38 +07:00
public async swapSalary (
@Path ( ) direction : string ,
commandReciveId : string ,
2024-10-24 10:21:24 +07:00
@Request ( ) request : RequestWithUser ,
2024-09-25 11:17:38 +07:00
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-25 11:17:38 +07:00
const source_item = await this . commandReciveRepository . findOne ( {
where : { id : commandReciveId } ,
} ) ;
if ( source_item == null ) throw new HttpError ( HttpStatus . NOT_FOUND , "ไม่พบข้อมูล" ) ;
const sourceOrder = source_item . order ;
if ( direction . trim ( ) . toUpperCase ( ) == "UP" ) {
const dest_item = await this . commandReciveRepository . findOne ( {
where : { commandId : source_item.commandId , order : LessThan ( sourceOrder ) } ,
order : { order : "DESC" } ,
} ) ;
if ( dest_item == null ) return new HttpSuccess ( ) ;
var destOrder = dest_item . order ;
dest_item . order = sourceOrder ;
source_item . order = destOrder ;
await Promise . all ( [
this . commandReciveRepository . save ( source_item ) ,
this . commandReciveRepository . save ( dest_item ) ,
] ) ;
} else {
const dest_item = await this . commandReciveRepository . findOne ( {
where : { commandId : source_item.commandId , order : MoreThan ( sourceOrder ) } ,
order : { order : "ASC" } ,
} ) ;
if ( dest_item == null ) return new HttpSuccess ( ) ;
var destOrder = dest_item . order ;
dest_item . order = sourceOrder ;
source_item . order = destOrder ;
await Promise . all ( [
this . commandReciveRepository . save ( source_item ) ,
this . commandReciveRepository . save ( dest_item ) ,
] ) ;
}
return new HttpSuccess ( ) ;
}
2024-09-25 16:17:06 +07:00
/ * *
* API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง Tab2
*
* @summary API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง Tab2
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "tab2/recive/{commandReciveId}" )
async PutTab2Recive (
@Path ( ) commandReciveId : string ,
@Body ( )
requestBody : {
remarkVertical : string | null ;
remarkHorizontal : string | null ;
amount : Double | null ;
2024-12-10 12:06:15 +07:00
amountSpecial : Double | null ;
2024-09-25 16:17:06 +07:00
positionSalaryAmount : Double | null ;
mouthSalaryAmount : Double | null ;
} ,
@Request ( ) request : RequestWithUser ,
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-25 16:17:06 +07:00
const commandRecive = await this . commandReciveRepository . findOne ( {
where : { id : commandReciveId } ,
} ) ;
if ( ! commandRecive ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลผู้ได้รับคำสั่ง" ) ;
}
const data = new CommandRecive ( ) ;
Object . assign ( data , { . . . commandRecive , . . . requestBody } ) ;
data . lastUpdateUserId = request . user . sub ;
data . lastUpdateFullName = request . user . name ;
data . lastUpdatedAt = new Date ( ) ;
await this . commandReciveRepository . save ( data ) ;
return new HttpSuccess ( ) ;
}
2024-09-25 11:17:38 +07:00
/ * *
* API ล บ ร า ย ก า ร ผ ู ้ ไ ด ้ ร ั บ ค ำ ส ั ่ ง
*
* @summary API ล บ ร า ย ก า ร ผ ู ้ ไ ด ้ ร ั บ ค ำ ส ั ่ ง
*
* @param { string } id Id ผ ู ้ ไ ด ้ ร ั บ ค ำ ส ั ่ ง
* /
2024-09-25 16:17:06 +07:00
@Delete ( "tab2/{commandReciveId}" )
2024-10-07 22:35:43 +07:00
async DeleteTab2 ( @Path ( ) commandReciveId : string , @Request ( ) request : RequestWithUser ) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-25 11:17:38 +07:00
const commandRecive = await this . commandReciveRepository . findOne ( {
2024-09-25 16:17:06 +07:00
where : { id : commandReciveId } ,
2024-10-07 22:35:43 +07:00
relations : [ "command" , "command.commandType" ] ,
2024-09-25 11:17:38 +07:00
} ) ;
if ( ! commandRecive ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลผู้ได้รับคำสั่ง" ) ;
}
2024-10-07 22:35:43 +07:00
2024-10-18 17:35:04 +07:00
const path = commandTypePath ( commandRecive . command . commandType . code ) ;
2024-10-07 22:35:43 +07:00
if ( path == null ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบประเภทคำสั่งนี้ในระบบ" ) ;
await new CallAPI ( )
. PostData ( request , path + "/delete" , {
refIds : [ commandRecive . refId ] ,
} )
. then ( async ( res ) = > { } )
. catch ( ( ) = > { } ) ;
2024-09-25 16:17:06 +07:00
const commandId = commandRecive . commandId ;
2024-09-26 11:18:28 +07:00
await this . commandReciveRepository . delete ( commandRecive . id ) ;
2024-09-25 16:17:06 +07:00
const commandReciveList = await this . commandReciveRepository . find ( {
where : {
commandId : commandId ,
} ,
order : { order : "ASC" } ,
} ) ;
commandReciveList . map ( async ( p , i ) = > {
p . order = i + 1 ;
await this . commandReciveRepository . save ( p ) ;
} ) ;
2024-10-07 22:35:43 +07:00
2024-09-25 11:17:38 +07:00
return new HttpSuccess ( ) ;
}
2024-09-12 22:53:58 +07:00
/ * *
* API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab3
*
* @summary API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab3
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Get ( "tab3/{id}" )
2024-10-24 10:21:24 +07:00
async GetByIdTab3 ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
await new permission ( ) . PermissionGet ( request , "COMMAND" ) ;
2024-09-12 22:53:58 +07:00
const command = await this . commandRepository . findOne ( {
where : { id } ,
2024-09-13 15:34:01 +07:00
relations : [ "commandSends" , "commandSends.commandSendCCs" ] ,
2024-09-12 22:53:58 +07:00
} ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
const _command = command . commandSends . map ( ( item ) = > ( {
id : item.id ,
citizenId : item.citizenId ,
prefix : item.prefix ,
2024-09-25 21:05:16 +07:00
firstName : item.firstName ,
2024-09-12 22:53:58 +07:00
lastName : item.lastName ,
position : item.position ,
org : item.org ,
2024-09-13 15:34:01 +07:00
sendCC : item.commandSendCCs.map ( ( x ) = > x . name ) ,
2024-09-12 22:53:58 +07:00
profileId : item.profileId ,
} ) ) ;
return new HttpSuccess ( _command ) ;
}
/ * *
* API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง Tab3
*
* @summary API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง Tab3
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "tab3-add/{id}" )
async PutTab3Add (
@Path ( ) id : string ,
@Body ( )
requestBody : {
profileId : string [ ] ;
} ,
@Request ( ) request : RequestWithUser ,
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-12 22:53:58 +07:00
const command = await this . commandRepository . findOne ( { where : { id : id } } ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
let _null : any = null ;
await Promise . all (
requestBody . profileId . map ( async ( item ) = > {
const commandSendCheck = await this . commandSendRepository . findOne ( {
where : { profileId : item , commandId : command.id } ,
} ) ;
if ( commandSendCheck ) return ;
let profile = await this . profileRepository . findOne ( {
where : { id : item } ,
relations : [ "current_holders" , "current_holders.orgRoot" ] ,
} ) ;
if ( ! profile ) return ;
const findRevision = await this . orgRevisionRepo . findOne ( {
where : { orgRevisionIsCurrent : true } ,
} ) ;
const commandSend = new CommandSend ( ) ;
commandSend . citizenId = profile . citizenId ;
commandSend . prefix =
profile . rank != null && profile . rank != "" ? profile.rank : profile.prefix ;
2024-09-25 21:05:16 +07:00
commandSend . firstName = profile . firstName ;
2024-09-12 22:53:58 +07:00
commandSend . lastName = profile . lastName ;
commandSend . position = profile . position ;
commandSend . org =
profile . current_holders ? . find ( ( x ) = > x . orgRevisionId == findRevision ? . id ) ? . orgRoot
? . orgRootName || _null ;
commandSend . profileId = profile . id ;
commandSend . commandId = command . id ;
commandSend . createdUserId = request . user . sub ;
commandSend . createdFullName = request . user . name ;
commandSend . createdAt = new Date ( ) ;
commandSend . lastUpdateUserId = request . user . sub ;
commandSend . lastUpdateFullName = request . user . name ;
commandSend . lastUpdatedAt = new Date ( ) ;
await this . commandSendRepository . save ( commandSend ) ;
2024-12-12 11:36:11 +07:00
if ( commandSend && commandSend . id ) {
2024-12-02 11:15:49 +07:00
let _ccName = new Array ( "EMAIL" , "INBOX" ) ;
2024-11-22 12:05:39 +07:00
let _dataSendCC = new Array ( ) ;
2024-12-12 11:36:11 +07:00
for ( let i = 0 ; i < _ccName . length ; i ++ ) {
2024-11-22 12:05:39 +07:00
_dataSendCC . push ( {
commandSendId : commandSend.id ,
name : _ccName [ i ] ,
createdUserId : request.user.sub ,
createdFullName : request.user.name ,
createdAt : new Date ( ) ,
lastUpdateUserId : request.user.sub ,
lastUpdateFullName : request.user.name ,
lastUpdatedAt : new Date ( ) ,
} ) ;
}
await this . commandSendCCRepository . save ( _dataSendCC ) ;
}
2024-09-12 22:53:58 +07:00
} ) ,
) ;
return new HttpSuccess ( ) ;
}
/ * *
* API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง Tab3
*
* @summary API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง Tab3
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "tab3/{id}" )
async PutTab3Update (
@Path ( ) id : string ,
@Body ( )
requestBody : {
commandSend : {
id : string ;
2024-09-13 15:34:01 +07:00
sendCC : string [ ] ;
2024-09-12 22:53:58 +07:00
} [ ] ;
} ,
@Request ( ) request : RequestWithUser ,
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-12 22:53:58 +07:00
const command = await this . commandRepository . findOne ( { where : { id : id } } ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
await Promise . all (
requestBody . commandSend . map ( async ( item ) = > {
2024-09-13 15:34:01 +07:00
const commandSendCC = await this . commandSendCCRepository . find ( {
where : { commandSendId : item.id } ,
2024-09-12 22:53:58 +07:00
} ) ;
2024-09-13 15:34:01 +07:00
await this . commandSendCCRepository . remove ( commandSendCC ) ;
2024-09-12 22:53:58 +07:00
2024-09-13 15:34:01 +07:00
await Promise . all (
item . sendCC . map ( async ( item1 ) = > {
const _commandSendCC = new CommandSendCC ( ) ;
_commandSendCC . name = item1 ;
_commandSendCC . commandSendId = item . id ;
_commandSendCC . createdUserId = request . user . sub ;
_commandSendCC . createdFullName = request . user . name ;
_commandSendCC . createdAt = new Date ( ) ;
_commandSendCC . lastUpdateUserId = request . user . sub ;
_commandSendCC . lastUpdateFullName = request . user . name ;
_commandSendCC . lastUpdatedAt = new Date ( ) ;
await this . commandSendCCRepository . save ( _commandSendCC ) ;
} ) ,
) ;
2024-09-12 22:53:58 +07:00
} ) ,
) ;
return new HttpSuccess ( ) ;
}
/ * *
* API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง Tab3
*
* @summary API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง Tab3
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Delete ( "tab3/{commandSendId}" )
async DeleteTab3Update (
@Path ( ) commandSendId : string ,
@Body ( )
requestBody : { reason? : string | null } ,
@Request ( ) request : RequestWithUser ,
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-12 22:53:58 +07:00
const command = await this . commandSendRepository . findOne ( { where : { id : commandSendId } } ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลผู้ได้รับสำเนาคำสั่ง" ) ;
}
2024-09-27 11:10:17 +07:00
await this . commandSendCCRepository . delete ( { commandSendId : commandSendId } ) ;
2024-09-12 22:53:58 +07:00
await this . commandSendRepository . delete ( commandSendId ) ;
return new HttpSuccess ( ) ;
}
2024-09-11 17:29:33 +07:00
/ * *
* API ค ั ด ล อ ก
*
* @summary API ค ั ด ล อ ก
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "copy/{id}" )
async PutCopy (
@Path ( ) id : string ,
@Body ( )
2024-09-12 22:53:58 +07:00
requestBody : { commandNo? : string | null ; commandYear? : number | null } ,
@Request ( ) request : RequestWithUser ,
2024-09-11 17:29:33 +07:00
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-27 10:12:54 +07:00
const command = await this . commandRepository . findOne ( {
where : { id : id } ,
} ) ;
2024-09-11 17:29:33 +07:00
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
const copy = new Command ( ) ;
Object . assign ( copy , { . . . command , . . . requestBody , id : undefined } ) ;
2024-09-27 17:14:15 +07:00
const _null : any = null ;
2024-09-27 10:12:54 +07:00
copy . status = "NEW" ;
2024-09-27 17:14:15 +07:00
copy . commandAffectDate = _null ;
copy . commandExcecuteDate = _null ;
copy . isSignature = _null ;
2024-10-29 14:35:02 +07:00
copy . isDraft = false ;
copy . isSign = false ;
2024-09-11 17:29:33 +07:00
copy . createdUserId = request . user . sub ;
copy . createdFullName = request . user . name ;
copy . createdAt = new Date ( ) ;
copy . lastUpdateUserId = request . user . sub ;
copy . lastUpdateFullName = request . user . name ;
copy . lastUpdatedAt = new Date ( ) ;
await this . commandRepository . save ( copy ) ;
2024-09-27 10:12:54 +07:00
2024-09-11 17:29:33 +07:00
return new HttpSuccess ( copy . id ) ;
}
/ * *
* API ย ก เ ล ิ ก
*
* @summary API ย ก เ ล ิ ก
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "cancel/{id}" )
async PutCancel (
@Path ( ) id : string ,
@Body ( )
requestBody : { reason? : string | null } ,
2024-09-12 22:53:58 +07:00
@Request ( ) request : RequestWithUser ,
2024-09-11 17:29:33 +07:00
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-11 17:29:33 +07:00
const command = await this . commandRepository . findOne ( { where : { id : id } } ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
command . status = "CANCEL" ;
command . lastUpdateUserId = request . user . sub ;
command . lastUpdateFullName = request . user . name ;
command . lastUpdatedAt = new Date ( ) ;
await this . commandRepository . save ( command ) ;
return new HttpSuccess ( ) ;
}
2024-09-12 22:53:58 +07:00
/ * *
* API ท ำ ค ำ ส ั ่ ง ใ ห ม ่
*
* @summary API ท ำ ค ำ ส ั ่ ง ใ ห ม ่
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "resume/{id}" )
async PutDraft (
@Path ( ) id : string ,
@Body ( )
requestBody : { reason? : string | null } ,
@Request ( ) request : RequestWithUser ,
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-12 22:53:58 +07:00
const command = await this . commandRepository . findOne ( { where : { id : id } } ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
command . status = "DRAFT" ;
command . lastUpdateUserId = request . user . sub ;
command . lastUpdateFullName = request . user . name ;
command . lastUpdatedAt = new Date ( ) ;
await this . commandRepository . save ( command ) ;
return new HttpSuccess ( ) ;
}
2024-09-11 17:29:33 +07:00
/ * *
2024-11-13 17:04:28 +07:00
* API ล บ ร า ย ก า ร ค ำ ส ั ่ ง ถ า ว ร
2024-09-11 17:29:33 +07:00
*
2024-11-13 17:04:28 +07:00
* @summary API ล บ ร า ย ก า ร ค ำ ส ั ่ ง ถ า ว ร
2024-09-11 17:29:33 +07:00
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Delete ( "{id}" )
2024-10-07 22:35:43 +07:00
async Delete ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionDelete ( request , "COMMAND" ) ;
2024-09-11 17:29:33 +07:00
const command = await this . commandRepository . findOne ( {
where : { id : id } ,
2024-10-07 22:35:43 +07:00
relations : [ "commandType" , "commandRecives" ] ,
2024-09-11 17:29:33 +07:00
} ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
2024-09-26 19:46:09 +07:00
const commandSend = await this . commandSendRepository . find ( {
where : { commandId : id } ,
} ) ;
2024-10-07 22:35:43 +07:00
2024-10-18 17:35:04 +07:00
const path = commandTypePath ( command . commandType . code ) ;
2024-10-07 22:35:43 +07:00
if ( path == null ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบประเภทคำสั่งนี้ในระบบ" ) ;
await new CallAPI ( )
. PostData ( request , path + "/delete" , {
refIds : command.commandRecives.map ( ( x ) = > x . refId ) ,
} )
. then ( async ( res ) = > { } )
. catch ( ( ) = > { } ) ;
2024-09-26 19:46:09 +07:00
await this . commandSendCCRepository . delete ( { commandSendId : In ( commandSend . map ( ( x ) = > x . id ) ) } ) ;
2024-09-26 16:05:59 +07:00
await this . commandReciveRepository . delete ( { commandId : command.id } ) ;
await this . commandSendRepository . delete ( { commandId : command.id } ) ;
2024-09-11 17:29:33 +07:00
await this . commandRepository . delete ( command . id ) ;
return new HttpSuccess ( ) ;
}
2024-09-24 11:30:07 +07:00
/ * *
* API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab0
*
* @summary API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab0
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Get ( "tab0/{id}" )
2024-10-24 10:21:24 +07:00
async GetByIdTab0 ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
await new permission ( ) . PermissionGet ( request , "COMMAND" ) ;
2024-09-24 11:30:07 +07:00
const command = await this . commandRepository . findOne ( {
where : { id } ,
2024-12-12 11:36:11 +07:00
relations : [ "commandType" ] ,
2024-09-24 11:30:07 +07:00
} ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
const _command = {
id : command.id ,
isSignature : command.isSignature ,
status : command.status ,
isDraft : command.isDraft ,
isSign : command.isSign ,
2024-09-24 16:42:24 +07:00
isAttachment : command.isAttachment ,
2024-12-10 16:03:40 +07:00
isSalary : command.commandType ? command.commandType.isSalary : null ,
2024-09-24 11:30:07 +07:00
} ;
return new HttpSuccess ( _command ) ;
}
/ * *
* API ท ำ ค ำ ส ั ่ ง ใ ห ม ่
*
* @summary API ท ำ ค ำ ส ั ่ ง ใ ห ม ่
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "sign/{id}" )
async PutSelectSign (
@Path ( ) id : string ,
@Body ( )
requestBody : { sign : boolean } ,
@Request ( ) request : RequestWithUser ,
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-24 11:30:07 +07:00
const command = await this . commandRepository . findOne ( { where : { id : id } } ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
command . isSignature = requestBody . sign ;
command . status = "DRAFT" ;
command . lastUpdateUserId = request . user . sub ;
command . lastUpdateFullName = request . user . name ;
command . lastUpdatedAt = new Date ( ) ;
await this . commandRepository . save ( command ) ;
return new HttpSuccess ( ) ;
}
/ * *
* API ท ำ ค ำ ส ั ่ ง ใ ห ม ่
*
* @summary API ท ำ ค ำ ส ั ่ ง ใ ห ม ่
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "draft/{id}" )
async PutSelectDraft (
@Path ( ) id : string ,
@Body ( )
2024-09-24 16:42:24 +07:00
requestBody : { sign : boolean } ,
2024-09-24 11:30:07 +07:00
@Request ( ) request : RequestWithUser ,
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-24 11:30:07 +07:00
const command = await this . commandRepository . findOne ( { where : { id : id } } ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
2024-09-24 16:42:24 +07:00
command . isDraft = requestBody . sign ;
2024-09-24 11:30:07 +07:00
command . status = "PENDING" ;
command . lastUpdateUserId = request . user . sub ;
command . lastUpdateFullName = request . user . name ;
command . lastUpdatedAt = new Date ( ) ;
await this . commandRepository . save ( command ) ;
return new HttpSuccess ( ) ;
}
/ * *
* API ท ำ ค ำ ส ั ่ ง ใ ห ม ่
*
* @summary API ท ำ ค ำ ส ั ่ ง ใ ห ม ่
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "pending-check/{id}" )
async PutSelectPending_Check (
@Path ( ) id : string ,
@Body ( )
requestBody : { sign? : boolean } ,
@Request ( ) request : RequestWithUser ,
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-09-24 11:30:07 +07:00
const command = await this . commandRepository . findOne ( { where : { id : id } } ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
command . isSign = true ;
command . lastUpdateUserId = request . user . sub ;
command . lastUpdateFullName = request . user . name ;
command . lastUpdatedAt = new Date ( ) ;
await this . commandRepository . save ( command ) ;
return new HttpSuccess ( ) ;
}
/ * *
2024-10-01 23:33:45 +07:00
* API อ อ ก ค ำ ส ั ่ ง
2024-09-24 11:30:07 +07:00
*
2024-10-01 23:33:45 +07:00
* @summary API อ อ ก ค ำ ส ั ่ ง
2024-09-24 11:30:07 +07:00
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "pending/{id}" )
async PutSelectPending (
@Path ( ) id : string ,
@Body ( )
requestBody : { sign? : boolean } ,
@Request ( ) request : RequestWithUser ,
) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
2024-10-01 23:33:45 +07:00
const command = await this . commandRepository . findOne ( {
where : { id : id } ,
2024-10-18 18:08:08 +07:00
relations : [ "commandType" , "commandRecives" , "commandSends" , "commandSends.commandSendCCs" ] ,
2024-10-01 23:33:45 +07:00
} ) ;
2024-09-24 11:30:07 +07:00
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
command . isSign = true ;
2024-09-27 12:58:24 +07:00
if ( command . commandExcecuteDate == null )
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบวันที่คำสั่งมีผล" ) ;
2024-10-07 23:33:55 +07:00
2024-10-17 10:38:32 +07:00
let profiles =
command && command . commandRecives . length > 0
2024-10-18 17:35:04 +07:00
? command . commandRecives
. filter ( ( x ) = > x . profileId != null )
. map ( ( x ) = > ( {
2024-10-18 17:52:25 +07:00
receiverUserId : x.profileId ,
2024-10-21 10:22:39 +07:00
notiLink : "" ,
2024-10-18 17:35:04 +07:00
} ) )
2024-10-17 10:38:32 +07:00
: [ ] ;
2024-10-07 23:33:55 +07:00
2024-10-09 12:53:13 +07:00
await new CallAPI ( )
2024-10-07 23:33:55 +07:00
. PostData ( request , "/placement/noti/profiles" , {
subject : ` ${ command . issue } ` ,
body : ` ${ command . issue } ` ,
2024-10-11 13:39:41 +07:00
receiverUserIds : profiles ,
2024-10-09 12:53:13 +07:00
payload : "" , //แนบไฟล์
2024-10-07 23:33:55 +07:00
isSendMail : true ,
isSendInbox : true ,
2024-10-10 20:50:21 +07:00
isSendNotification : true ,
2024-10-07 23:33:55 +07:00
} )
. catch ( ( error ) = > {
console . error ( "Error calling API:" , error ) ;
} ) ;
2024-10-19 22:29:37 +07:00
let profilesSend =
2024-10-18 18:08:08 +07:00
command && command . commandSends . length > 0
? command . commandSends
. filter ( ( x ) = > x . profileId != null )
. map ( ( x ) = > ( {
receiverUserId : x.profileId ,
notiLink : "" ,
isSendMail : x.commandSendCCs.map ( ( x ) = > x . name == "EMAIL" ) . length > 0 ? true : false ,
isSendInbox : x.commandSendCCs.map ( ( x ) = > x . name == "INBOX" ) . length > 0 ? true : false ,
isSendNotification : true ,
} ) )
: [ ] ;
await new CallAPI ( )
2024-10-19 22:29:37 +07:00
. PostData ( request , "/placement/noti/profiles-send" , {
2024-10-18 18:08:08 +07:00
subject : ` ${ command . issue } ` ,
body : ` ${ command . issue } ` ,
2024-10-19 22:29:37 +07:00
receiverUserIds : profilesSend ,
2024-10-18 18:08:08 +07:00
payload : "" , //แนบไฟล์
} )
. catch ( ( error ) = > {
console . error ( "Error calling API:" , error ) ;
} ) ;
2024-09-27 12:58:24 +07:00
if (
new Date ( new Date ( ) . getFullYear ( ) , new Date ( ) . getMonth ( ) , new Date ( ) . getDate ( ) ) <
new Date (
command . commandExcecuteDate . getFullYear ( ) ,
command . commandExcecuteDate . getMonth ( ) ,
command . commandExcecuteDate . getDate ( ) ,
)
) {
2024-09-24 11:30:07 +07:00
command . status = "WAITING" ;
2024-10-11 11:05:31 +07:00
command . lastUpdateUserId = request . user . sub ;
command . lastUpdateFullName = request . user . name ;
command . lastUpdatedAt = new Date ( ) ;
await this . commandRepository . save ( command ) ;
2024-09-24 11:30:07 +07:00
} else {
2024-10-18 17:35:04 +07:00
const path = commandTypePath ( command . commandType . code ) ;
2024-10-01 17:51:48 +07:00
if ( path == null ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบประเภทคำสั่งนี้ในระบบ" ) ;
2024-10-11 11:05:31 +07:00
const msg = {
data : {
id : command.id ,
status : "REPORTED" ,
lastUpdateUserId : request.user.sub ,
lastUpdateFullName : request.user.name ,
lastUpdatedAt : new Date ( ) ,
} ,
user : request.user ,
token : request.headers [ "authorization" ] ,
} ;
sendToQueue ( msg ) ;
}
return new HttpSuccess ( ) ;
}
2024-12-12 11:36:11 +07:00
/ * *
2024-11-15 15:10:34 +07:00
* API อ อ ก ค ำ ส ั ่ ง
*
* @summary API อ อ ก ค ำ ส ั ่ ง
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
2024-12-12 11:36:11 +07:00
@Put ( "testRabbit/{id}" )
async testRabbit (
@Path ( ) id : string ,
@Body ( )
requestBody : { sign? : boolean } ,
@Request ( ) request : RequestWithUser ,
) {
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
const command = await this . commandRepository . findOne ( {
where : { id : id } ,
relations : [ "commandType" , "commandRecives" ] ,
} ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
command . isSign = true ;
if ( command . commandExcecuteDate == null )
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบวันที่คำสั่งมีผล" ) ;
let profiles =
command && command . commandRecives . length > 0
? command . commandRecives
. filter ( ( x ) = > x . profileId != null )
. map ( ( x ) = > ( {
receiverUserId : x.profileId ,
notiLink : "" ,
} ) )
: [ ] ;
await new CallAPI ( )
. PostData ( request , "/placement/noti/profiles" , {
subject : ` ${ command . issue } ` ,
body : ` ${ command . issue } ` ,
receiverUserIds : profiles ,
payload : "" , //แนบไฟล์
isSendMail : true ,
isSendInbox : true ,
isSendNotification : true ,
} )
. catch ( ( error ) = > {
console . error ( "Error calling API:" , error ) ;
} ) ;
if (
new Date ( new Date ( ) . getFullYear ( ) , new Date ( ) . getMonth ( ) , new Date ( ) . getDate ( ) ) <
new Date (
command . commandExcecuteDate . getFullYear ( ) ,
command . commandExcecuteDate . getMonth ( ) ,
command . commandExcecuteDate . getDate ( ) ,
)
) {
command . status = "WAITING" ;
command . lastUpdateUserId = request . user . sub ;
command . lastUpdateFullName = request . user . name ;
command . lastUpdatedAt = new Date ( ) ;
await this . commandRepository . save ( command ) ;
} else {
const path = commandTypePath ( command . commandType . code ) ;
if ( path == null ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบประเภทคำสั่งนี้ในระบบ" ) ;
const msg = {
data : {
id : command.id ,
status : "REPORTED" ,
lastUpdateUserId : request.user.sub ,
lastUpdateFullName : request.user.name ,
lastUpdatedAt : new Date ( ) ,
} ,
user : request.user ,
token : request.headers [ "authorization" ] ,
} ;
sendToQueue ( msg ) ;
}
return new HttpSuccess ( ) ;
}
2024-10-11 11:05:31 +07:00
async cronjobCommand ( @Request ( ) request? : RequestWithUser ) {
const today = new Date ( ) ;
2024-10-22 10:28:21 +07:00
today . setUTCHours ( 0 , 0 , 0 , 0 ) ;
2024-10-11 11:05:31 +07:00
const tomorrow = new Date ( today ) ;
tomorrow . setDate ( tomorrow . getDate ( ) + 1 ) ;
2024-10-01 17:51:48 +07:00
2024-10-11 11:05:31 +07:00
const command = await this . commandRepository . find ( {
relations : [ "commandType" , "commandRecives" ] ,
2024-10-17 10:38:32 +07:00
where : {
2024-10-11 11:05:31 +07:00
commandExcecuteDate : Between ( today , tomorrow ) ,
2024-10-17 10:38:32 +07:00
status : "WAITING" ,
} ,
2024-10-11 11:05:31 +07:00
} ) ;
2024-10-17 10:38:32 +07:00
2024-10-11 11:05:31 +07:00
const data = {
client_id : "gettoken" ,
client_secret : process.env.AUTH_ACCOUNT_SECRET ,
grant_type : "password" ,
requested_token_type : "urn:ietf:params:oauth:token-type:refresh_token" ,
username : process.env.USERNAME_ ,
password : process.env.PASSWORD_ ,
} ;
let _data : any = null ;
await Promise . all ( [
2024-10-03 11:54:55 +07:00
await new CallAPI ( )
2024-12-18 10:52:40 +07:00
. PostDataKeycloak ( ` /realms/ ${ process . env . KC_REALMS } /protocol/openid-connect/token ` , data )
2024-10-11 11:05:31 +07:00
. then ( async ( x ) = > {
_data = x ;
2024-10-03 11:54:55 +07:00
} )
2024-10-11 11:05:31 +07:00
. catch ( async ( x ) = > {
throw new HttpError ( HttpStatus . UNAUTHORIZED , "ชื่อผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง" ) ;
} ) ,
] ) ;
if ( _data == null ) {
return new HttpError ( HttpStatus . UNAUTHORIZED , "ชื่อผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง" ) ;
2024-10-17 10:38:32 +07:00
}
2024-10-11 11:05:31 +07:00
command . forEach ( async ( x ) = > {
2024-10-18 17:35:04 +07:00
const path = commandTypePath ( x . commandType . code ) ;
2024-10-17 10:38:32 +07:00
if ( path == null ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบประเภทคำสั่งนี้ในระบบ" ) ;
const msg = {
data : {
id : x.id ,
status : "REPORTED" ,
lastUpdateUserId : "system" ,
lastUpdateFullName : "system" ,
// lastUpdateUserId: _data.user.sub,
// lastUpdateFullName: _data.user.name,
lastUpdatedAt : new Date ( ) ,
} ,
user : _data.user ,
token : _data.access_token ,
} ;
sendToQueue ( msg ) ;
} ) ;
2024-09-24 11:30:07 +07:00
return new HttpSuccess ( ) ;
}
2024-09-26 16:05:59 +07:00
/ * *
* API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab4 ค ำ ส ั ่ ง
*
* @summary API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab4 ค ำ ส ั ่ ง
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Get ( "tab4/cover/{id}" )
2024-10-01 17:51:48 +07:00
async GetByIdTab4Cover ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionGet ( request , "COMMAND" ) ;
2024-09-26 16:05:59 +07:00
const command = await this . commandRepository . findOne ( {
where : { id } ,
2024-11-01 16:57:18 +07:00
relations : [ "commandType" , "commandRecives" ] ,
2024-09-26 16:05:59 +07:00
} ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
2024-10-10 17:42:20 +07:00
let issue =
command . isBangkok == "OFFICE"
? "สำนักปลัดกรุงเทพมหานคร"
: command . isBangkok == "BANGKOK"
? "กรุงเทพมหานคร"
: null ;
if ( issue == null ) {
const orgRevisionActive = await this . orgRevisionRepository . findOne ( {
2024-10-10 20:05:25 +07:00
where : { orgRevisionIsCurrent : true , orgRevisionIsDraft : false } ,
2024-10-10 17:42:20 +07:00
relations : [ "posMasters" , "posMasters.orgRoot" ] ,
} ) ;
if ( orgRevisionActive != null ) {
const profile = await this . profileRepository . findOne ( {
where : {
keycloak : command.createdUserId.toString ( ) ,
} ,
} ) ;
if ( profile != null ) {
issue =
2024-10-10 20:05:25 +07:00
orgRevisionActive ? . posMasters ? . filter ( ( x ) = > x . current_holderId == profile . id ) [ 0 ]
? . orgRoot ? . orgRootName || null ;
2024-10-10 17:42:20 +07:00
}
}
}
if ( issue == null ) issue = "..................................." ;
2024-11-01 16:57:18 +07:00
let res : any [ ] = [ ] ;
2024-11-01 19:53:26 +07:00
if ( command . commandRecives . length > 0 ) {
2024-11-01 16:57:18 +07:00
await new CallAPI ( )
2024-11-01 19:53:26 +07:00
. GetData ( request , ` /probation/report/command10/appoints/ ${ command . commandRecives [ 0 ] . refId } ` )
. then ( ( x ) = > {
res = x ;
} )
. catch ( ( x ) = > { } ) ;
2024-11-01 16:57:18 +07:00
}
2024-11-01 19:53:26 +07:00
2024-11-01 16:57:18 +07:00
let _command = {
2024-10-10 17:42:20 +07:00
issue : issue ,
2024-10-11 13:39:41 +07:00
commandNo : command.commandNo == null ? "" : Extension . ToThaiNumber ( command . commandNo ) ,
commandYear :
command . commandYear == null
? ""
: Extension . ToThaiNumber ( Extension . ToThaiYear ( command . commandYear ) . toString ( ) ) ,
2024-09-26 16:05:59 +07:00
commandTitle : command.issue ,
detailHeader : command.detailHeader ,
detailBody : command.detailBody ,
detailFooter : command.detailFooter ,
2024-09-26 17:37:20 +07:00
commandDate :
command . commandAffectDate == null
2024-09-27 16:29:28 +07:00
? ""
2024-09-26 17:37:20 +07:00
: Extension . ToThaiNumber ( Extension . ToThaiFullDate2 ( command . commandAffectDate ) ) ,
commandExcecuteDate :
command . commandExcecuteDate == null
2024-09-27 16:29:28 +07:00
? ""
2024-09-26 17:37:20 +07:00
: Extension . ToThaiNumber ( Extension . ToThaiFullDate2 ( command . commandExcecuteDate ) ) ,
2024-09-26 16:05:59 +07:00
name : "..................................." ,
position : "..................................." ,
2024-10-10 17:42:20 +07:00
authorizedUserFullName : "..................................." ,
authorizedPosition : "..................................." ,
commandAffectDate : "..................................." ,
2024-11-01 19:53:26 +07:00
name1 :
res && res . length > 0
? ` ๑. ${ res [ 0 ] . name } ......... ${ res [ 0 ] . role } `
: "๑. ..........................ประธาน" ,
name2 :
res && res . length > 1
? ` ๒. ${ res [ 1 ] . name } ......... ${ res [ 1 ] . role } `
: "๒. ..........................กรรมการ" ,
name3 :
res && res . length > 2
? ` ๓. ${ res [ 2 ] . name } ......... ${ res [ 2 ] . role } `
: "๓. ..........................กรรมการ" ,
name4 :
res && res . length > 3
? ` ๔. ${ res [ 3 ] . name } ......... ${ res [ 3 ] . role } `
: "๔. ..........................กรรมการ" ,
2024-09-26 16:05:59 +07:00
} ;
2024-11-01 16:57:18 +07:00
2024-09-26 16:05:59 +07:00
return new HttpSuccess ( {
template : command.commandType.fileCover ,
reportName : "docx-report" ,
2024-09-26 17:25:33 +07:00
data : _command ,
2024-09-26 16:05:59 +07:00
} ) ;
}
/ * *
* API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab4 แ น บ ท ้ า ย
*
* @summary API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab4 แ น บ ท ้ า ย
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Get ( "tab4/attachment/{id}" )
2024-10-01 17:51:48 +07:00
async GetByIdTab4Attachment ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
2024-10-24 10:21:24 +07:00
await new permission ( ) . PermissionGet ( request , "COMMAND" ) ;
2024-09-26 16:05:59 +07:00
const command = await this . commandRepository . findOne ( {
where : { id } ,
2024-10-01 17:51:48 +07:00
relations : [ "commandType" , "commandRecives" ] ,
2024-09-26 16:05:59 +07:00
} ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
2024-10-01 17:51:48 +07:00
let _command : any = [ ] ;
2024-10-18 17:35:04 +07:00
const path = commandTypePath ( command . commandType . code ) ;
2024-10-01 17:51:48 +07:00
if ( path == null ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบประเภทคำสั่งนี้ในระบบ" ) ;
2024-10-03 11:54:55 +07:00
await new CallAPI ( )
. PostData ( request , path + "/attachment" , {
2024-10-07 22:35:43 +07:00
refIds : command.commandRecives
. filter ( ( x ) = > x . refId != null )
. map ( ( x ) = > ( {
refId : x.refId ,
Sequence : x.order ,
CitizenId : x.citizenId ,
Prefix : x.prefix ,
FirstName : x.firstName ,
LastName : x.lastName ,
Amount : x.amount ,
PositionSalaryAmount : x.positionSalaryAmount ,
MouthSalaryAmount : x.mouthSalaryAmount ,
RemarkHorizontal : x.remarkHorizontal ,
RemarkVertical : x.remarkVertical ,
CommandYear : command.commandYear ,
2024-11-14 18:03:56 +07:00
CommandExcecuteDate : command.commandExcecuteDate ,
2024-10-07 22:35:43 +07:00
} ) ) ,
2024-10-01 17:51:48 +07:00
} )
. then ( async ( res ) = > {
_command = res ;
} )
. catch ( ( ) = > { } ) ;
2024-10-10 20:05:25 +07:00
let issue =
command . isBangkok == "OFFICE"
? "สำนักปลัดกรุงเทพมหานคร"
: command . isBangkok == "BANGKOK"
? "กรุงเทพมหานคร"
: null ;
if ( issue == null ) {
const orgRevisionActive = await this . orgRevisionRepository . findOne ( {
where : { orgRevisionIsCurrent : true , orgRevisionIsDraft : false } ,
relations : [ "posMasters" , "posMasters.orgRoot" ] ,
} ) ;
if ( orgRevisionActive != null ) {
const profile = await this . profileRepository . findOne ( {
where : {
keycloak : command.createdUserId.toString ( ) ,
} ,
} ) ;
if ( profile != null ) {
issue =
orgRevisionActive ? . posMasters ? . filter ( ( x ) = > x . current_holderId == profile . id ) [ 0 ]
? . orgRoot ? . orgRootName || null ;
}
}
}
if ( issue == null ) issue = "..................................." ;
2024-09-26 16:05:59 +07:00
return new HttpSuccess ( {
template : command.commandType.fileAttachment ,
reportName : "xlsx-report" ,
2024-10-10 20:05:25 +07:00
data : {
data : _command ,
issuerOrganizationName : issue ,
commandNo : command.commandNo == null ? "" : Extension . ToThaiNumber ( command . commandNo ) ,
commandYear :
command . commandYear == null
? ""
: Extension . ToThaiNumber ( Extension . ToThaiYear ( command . commandYear ) . toString ( ) ) ,
commandExcecuteDate :
command . commandExcecuteDate == null
? ""
: Extension . ToThaiNumber ( Extension . ToThaiFullDate2 ( command . commandExcecuteDate ) ) ,
} ,
2024-09-26 16:05:59 +07:00
} ) ;
}
2024-10-01 17:51:48 +07:00
2024-11-01 19:53:26 +07:00
/ * *
* API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง step
*
* @summary API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง step
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Get ( "step/{id}" )
async GetByIdStep ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
await new permission ( ) . PermissionGet ( request , "COMMAND" ) ;
const command = await this . commandRepository . findOne ( {
where : { id } ,
relations : [ "commandSigns" ] ,
2024-11-04 12:15:16 +07:00
order : {
2024-11-04 17:12:00 +07:00
commandSigns : {
createdAt : "ASC" ,
} ,
} ,
2024-11-01 19:53:26 +07:00
} ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
const _command = command . commandSigns . map ( ( item ) = > ( {
id : item.id ,
prefix : item.prefix ,
firstName : item.firstName ,
lastName : item.lastName ,
position : item.position ,
profileId : item.profileId ,
2024-11-04 11:16:37 +07:00
comment : item.comment ,
isSignatory : item.isSignatory ,
2024-11-01 19:53:26 +07:00
} ) ) ;
return new HttpSuccess ( _command ) ;
}
/ * *
* API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง step
*
* @summary API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง step
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "step-add/{id}" )
async PutStepAdd (
@Path ( ) id : string ,
@Body ( )
requestBody : {
profileId : string ;
isSignatory : boolean ;
} ,
@Request ( ) request : RequestWithUser ,
) {
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
const command = await this . commandRepository . findOne ( { where : { id : id } } ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
const profile = await this . profileRepository . findOne ( { where : { id : requestBody.profileId } } ) ;
if ( ! profile ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลผู้ใช้งานนี้" ) ;
}
command . status = "PENDING" ;
command . isDraft = true ;
const commandSign = new CommandSign ( ) ;
commandSign . prefix = profile . prefix ;
commandSign . firstName = profile . firstName ;
commandSign . lastName = profile . lastName ;
commandSign . position = profile . position ;
commandSign . isSignatory = requestBody . isSignatory ;
commandSign . profileId = requestBody . profileId ;
commandSign . commandId = command . id ;
commandSign . createdUserId = request . user . sub ;
commandSign . createdFullName = request . user . name ;
commandSign . createdAt = new Date ( ) ;
commandSign . lastUpdateUserId = request . user . sub ;
commandSign . lastUpdateFullName = request . user . name ;
commandSign . lastUpdatedAt = new Date ( ) ;
await this . commandSignRepository . save ( commandSign ) ;
await this . commandRepository . save ( command ) ;
return new HttpSuccess ( ) ;
}
/ * *
* API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง step - comment
*
* @summary API แ ก ้ ไ ข ร า ย ก า ร body ค ำ ส ั ่ ง step - comment
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Put ( "step-comment/{id}" )
async PutStepComment (
@Path ( ) id : string ,
@Body ( )
requestBody : {
comment : string ;
} ,
@Request ( ) request : RequestWithUser ,
) {
await new permission ( ) . PermissionUpdate ( request , "COMMAND" ) ;
const commandSign = await this . commandSignRepository . findOne ( {
where : { id : id } ,
relations : [ "command" ] ,
} ) ;
if ( ! commandSign ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
commandSign . comment = requestBody . comment ;
commandSign . lastUpdateUserId = request . user . sub ;
commandSign . lastUpdateFullName = request . user . name ;
commandSign . lastUpdatedAt = new Date ( ) ;
await this . commandSignRepository . save ( commandSign ) ;
2024-11-04 17:12:00 +07:00
if ( commandSign . isSignatory == true )
await this . PutSelectPending ( commandSign . commandId , { sign : true } , request ) ;
2024-11-01 19:53:26 +07:00
return new HttpSuccess ( ) ;
}
2024-10-01 17:51:48 +07:00
/ * *
* API ส ร ้ า ง ร า ย ก า ร body ค ำ ส ั ่ ง
*
* @summary API ส ร ้ า ง ร า ย ก า ร body ค ำ ส ั ่ ง
*
* /
@Post ( "person" )
async PostPerson (
@Body ( )
requestBody : {
2024-10-02 11:12:28 +07:00
commandTypeId? : string | null ;
2024-10-01 17:51:48 +07:00
commandNo? : string | null ;
commandYear? : number | null ;
commandId? : string | null ;
2024-10-07 12:34:04 +07:00
commandAffectDate? : Date | null ;
commandExcecuteDate? : Date | null ;
2024-10-01 17:51:48 +07:00
persons : {
refId : string ;
2024-10-09 12:53:13 +07:00
profileId? : string | null ;
2024-12-03 09:25:17 +07:00
citizenId? : string | null ;
prefix? : string | null ;
firstName? : string | null ;
lastName? : string | null ;
2024-11-04 17:12:00 +07:00
remarkVertical? : string | null ;
remarkHorizontal? : string | null ;
2024-11-08 14:51:28 +07:00
rootId? : string | null ;
2024-12-06 18:43:07 +07:00
amount? : Double | null ;
amountSpecial? : Double | null ;
positionSalaryAmount? : Double | null ;
mouthSalaryAmount? : Double | null ;
2024-10-01 17:51:48 +07:00
} [ ] ;
} ,
@Request ( ) request : RequestWithUser ,
) {
let command = new Command ( ) ;
2024-12-12 11:36:11 +07:00
let commandCode : string = "" ;
2024-10-07 22:35:43 +07:00
let null_ : any = null ;
2024-10-01 17:51:48 +07:00
if (
requestBody . commandId != undefined &&
requestBody . commandId != null &&
requestBody . commandId != ""
) {
const _command = await this . commandRepository . findOne ( {
where : { id : requestBody.commandId } ,
2024-10-01 23:33:45 +07:00
relations : [ "commandRecives" , "commandType" ] ,
2024-10-01 17:51:48 +07:00
order : {
commandRecives : {
order : "DESC" ,
} ,
} ,
} ) ;
if ( ! _command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบคำสั่งนี้ในระบบ" ) ;
}
2024-10-01 23:33:45 +07:00
commandCode = _command . commandType . code ;
2024-10-01 17:51:48 +07:00
command = _command ;
} else {
command = Object . assign ( new Command ( ) , requestBody ) ;
2024-10-02 11:12:28 +07:00
if ( ! requestBody . commandTypeId ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบประเภทคำสั่ง" ) ;
}
2024-10-01 17:51:48 +07:00
const commandType = await this . commandTypeRepository . findOne ( {
where : { id : requestBody.commandTypeId } ,
} ) ;
if ( ! commandType ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบประเภทคำสั่งนี้ในระบบ" ) ;
}
2024-10-01 23:33:45 +07:00
commandCode = commandType . code ;
2024-10-01 17:51:48 +07:00
command . detailHeader = commandType . detailHeader ;
command . detailBody = commandType . detailBody ;
command . detailFooter = commandType . detailFooter ;
command . isAttachment = commandType . isAttachment ;
command . status = "NEW" ;
command . issue = commandType . name ;
2024-10-07 22:35:43 +07:00
( command . commandAffectDate = requestBody . commandAffectDate
2024-10-07 12:34:04 +07:00
? new Date ( requestBody . commandAffectDate )
2024-10-07 22:35:43 +07:00
: null_ ) ,
( command . commandExcecuteDate = requestBody . commandExcecuteDate
? new Date ( requestBody . commandExcecuteDate )
: null_ ) ,
( command . createdUserId = request . user . sub ) ;
2024-10-01 17:51:48 +07:00
command . createdFullName = request . user . name ;
command . createdAt = new Date ( ) ;
command . lastUpdateUserId = request . user . sub ;
command . lastUpdateFullName = request . user . name ;
command . lastUpdatedAt = new Date ( ) ;
await this . commandRepository . save ( command ) ;
}
2024-10-18 17:35:04 +07:00
const path = commandTypePath ( commandCode ) ;
2024-10-01 17:51:48 +07:00
if ( path == null ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบประเภทคำสั่งนี้ในระบบ" ) ;
2024-10-03 11:54:55 +07:00
await new CallAPI ( )
2024-10-01 17:51:48 +07:00
. PostData ( request , path , {
2024-10-07 22:35:43 +07:00
refIds : requestBody.persons.filter ( ( x ) = > x . refId != null ) . map ( ( x ) = > x . refId ) ,
2024-10-01 17:51:48 +07:00
} )
. then ( async ( res ) = > {
let order =
command . commandRecives == null || command . commandRecives . length <= 0
? 0
: command . commandRecives [ 0 ] . order ;
await Promise . all (
requestBody . persons . map ( async ( item ) = > {
const _commandRecive = await this . commandReciveRepository . findOne ( {
where : {
commandId : command.id ,
refId : item.refId ,
} ,
} ) ;
if ( _commandRecive ) return ;
order = order + 1 ;
let commandRecive = new CommandRecive ( ) ;
commandRecive = Object . assign ( new CommandRecive ( ) , item ) ;
commandRecive . order = order ;
2024-11-05 10:00:18 +07:00
let salaryData = null_ ;
2024-12-06 18:43:07 +07:00
const excludedCommands = [ "C-PM-33" , "C-PM-34" , "C-PM-35" , "C-PM-36" , "C-PM-37" ] ;
if ( ! excludedCommands . includes ( commandCode ) ) {
if ( item . profileId ) {
salaryData = await this . profileRepository . findOne ( {
2024-11-05 10:00:18 +07:00
where : {
2024-11-05 17:26:42 +07:00
id : item.profileId ,
} ,
} ) ;
2024-12-06 18:43:07 +07:00
let null_ : any = 0 ;
if ( ! salaryData ) {
salaryData = await this . profileEmployeeRepository . findOne ( {
where : {
id : item.profileId ,
} ,
} ) ;
}
commandRecive . amount = salaryData ? salaryData.amount : null_ ;
commandRecive . positionSalaryAmount = salaryData
? salaryData . positionSalaryAmount
: null_ ;
commandRecive . mouthSalaryAmount = salaryData ? salaryData.mouthSalaryAmount : null_ ;
} else {
commandRecive . amount = null_ ;
commandRecive . positionSalaryAmount = null_ ;
commandRecive . mouthSalaryAmount = null_ ;
2024-11-05 10:00:18 +07:00
}
2024-11-05 17:26:42 +07:00
} else {
2024-12-06 18:43:07 +07:00
commandRecive . amount = item . amount ? ? null_ ;
commandRecive . amountSpecial = item . amountSpecial ? ? null_ ;
commandRecive . positionSalaryAmount = item . positionSalaryAmount ? ? null_ ;
commandRecive . mouthSalaryAmount = item . mouthSalaryAmount ? ? null_ ;
2024-11-05 10:00:18 +07:00
}
2024-12-06 18:43:07 +07:00
2024-11-04 17:12:00 +07:00
commandRecive . remarkVertical =
item . remarkVertical == null ? null_ : item.remarkVertical ;
commandRecive . remarkHorizontal =
item . remarkHorizontal == null ? null_ : item.remarkHorizontal ;
commandRecive . order = order ;
2024-10-01 17:51:48 +07:00
commandRecive . commandId = command . id ;
commandRecive . createdUserId = request . user . sub ;
commandRecive . createdFullName = request . user . name ;
commandRecive . createdAt = new Date ( ) ;
commandRecive . lastUpdateUserId = request . user . sub ;
commandRecive . lastUpdateFullName = request . user . name ;
commandRecive . lastUpdatedAt = new Date ( ) ;
await this . commandReciveRepository . save ( commandRecive ) ;
} ) ,
) ;
} )
. catch ( ( ) = > { } ) ;
2024-10-22 16:09:46 +07:00
if ( requestBody . persons != undefined && requestBody . persons . length > 0 ) {
const posMaster = await this . posMasterRepository . find ( {
where : {
current_holderId : In ( requestBody . persons . map ( ( x ) = > x . profileId ) ) ,
orgRevision : { orgRevisionIsDraft : false , orgRevisionIsCurrent : true } ,
} ,
select : [ "orgRootId" ] ,
} ) ;
2024-11-14 18:03:56 +07:00
let _posMaster : any ;
if ( [ "C-PM-38" , "C-PM-40" ] . includes ( commandCode ) ) {
2024-11-08 15:29:39 +07:00
_posMaster = await this . posMasterRepository . find ( {
where : {
orgRootId : In ( posMaster . map ( ( x ) = > x . orgRootId ) ) ,
orgChild1 : IsNull ( ) ,
orgChild2 : IsNull ( ) ,
orgChild3 : IsNull ( ) ,
orgChild4 : IsNull ( ) ,
orgRevision : { orgRevisionIsDraft : false , orgRevisionIsCurrent : true } ,
isDirector : true ,
current_holderId : Not ( IsNull ( ) ) ,
} ,
relations : [ "current_holder" , "orgRoot" ] ,
} ) ;
2024-11-14 18:03:56 +07:00
} else {
2024-11-08 15:29:39 +07:00
_posMaster = await this . posMasterRepository . find ( {
where : {
orgRootId : In ( posMaster . map ( ( x ) = > x . orgRootId ) ) ,
orgRevision : { orgRevisionIsDraft : false , orgRevisionIsCurrent : true } ,
isDirector : true ,
current_holderId : Not ( IsNull ( ) ) ,
} ,
relations : [ "current_holder" , "orgRoot" ] ,
} ) ;
}
2024-10-22 16:09:46 +07:00
await Promise . all (
2024-11-14 18:03:56 +07:00
_posMaster . map ( async ( item : any ) = > {
2024-10-22 16:09:46 +07:00
const _commandSend = await this . commandSendRepository . findOne ( {
where : {
commandId : command.id ,
profileId : item.current_holder.id ,
} ,
} ) ;
2024-11-08 14:51:28 +07:00
if ( _commandSend ) return ;
let commandSend = new CommandSend ( ) ;
commandSend . citizenId = item . current_holder . citizenId ;
commandSend . prefix = item . current_holder . prefix ;
commandSend . firstName = item . current_holder . firstName ;
commandSend . lastName = item . current_holder . lastName ;
commandSend . position = item . current_holder . position ;
commandSend . org = item . orgRoot . orgRootName ;
commandSend . profileId = item . current_holder . id ;
commandSend . commandId = command . id ;
commandSend . createdUserId = request . user . sub ;
commandSend . createdFullName = request . user . name ;
commandSend . createdAt = new Date ( ) ;
commandSend . lastUpdateUserId = request . user . sub ;
commandSend . lastUpdateFullName = request . user . name ;
commandSend . lastUpdatedAt = new Date ( ) ;
await this . commandSendRepository . save ( commandSend ) ;
2024-12-12 11:36:11 +07:00
if ( commandSend && commandSend . id ) {
2024-12-02 11:15:49 +07:00
let _ccName = new Array ( "EMAIL" , "INBOX" ) ;
let _dataSendCC = new Array ( ) ;
2024-12-12 11:36:11 +07:00
for ( let i = 0 ; i < _ccName . length ; i ++ ) {
2024-12-02 11:15:49 +07:00
_dataSendCC . push ( {
commandSendId : commandSend.id ,
name : _ccName [ i ] ,
createdUserId : request.user.sub ,
createdFullName : request.user.name ,
createdAt : new Date ( ) ,
lastUpdateUserId : request.user.sub ,
lastUpdateFullName : request.user.name ,
lastUpdatedAt : new Date ( ) ,
} ) ;
}
await this . commandSendCCRepository . save ( _dataSendCC ) ;
2024-12-12 11:36:11 +07:00
}
2024-11-14 18:03:56 +07:00
} ) ,
2024-11-08 14:51:28 +07:00
) ;
const _posMasterNext = await this . posMasterRepository . find ( {
where : {
2024-11-14 18:03:56 +07:00
orgRootId : In (
requestBody . persons
. filter ( ( x ) = > x . rootId != undefined && x . rootId != null && x . rootId != "" )
. map ( ( x ) = > x . rootId ) ,
) ,
2024-11-08 14:51:28 +07:00
orgRevision : { orgRevisionIsDraft : false , orgRevisionIsCurrent : true } ,
isDirector : true ,
current_holderId : Not ( IsNull ( ) ) ,
} ,
relations : [ "current_holder" , "orgRoot" ] ,
} ) ;
await Promise . all (
_posMasterNext . map ( async ( item ) = > {
const _commandSend = await this . commandSendRepository . findOne ( {
where : {
commandId : command.id ,
profileId : item.current_holder.id ,
} ,
} ) ;
2024-10-22 16:09:46 +07:00
if ( _commandSend ) return ;
let commandSend = new CommandSend ( ) ;
commandSend . citizenId = item . current_holder . citizenId ;
commandSend . prefix = item . current_holder . prefix ;
commandSend . firstName = item . current_holder . firstName ;
commandSend . lastName = item . current_holder . lastName ;
commandSend . position = item . current_holder . position ;
commandSend . org = item . orgRoot . orgRootName ;
commandSend . profileId = item . current_holder . id ;
commandSend . commandId = command . id ;
commandSend . createdUserId = request . user . sub ;
commandSend . createdFullName = request . user . name ;
commandSend . createdAt = new Date ( ) ;
commandSend . lastUpdateUserId = request . user . sub ;
commandSend . lastUpdateFullName = request . user . name ;
commandSend . lastUpdatedAt = new Date ( ) ;
await this . commandSendRepository . save ( commandSend ) ;
2024-12-12 11:36:11 +07:00
if ( commandSend && commandSend . id ) {
2024-12-02 11:15:49 +07:00
let _ccName = new Array ( "EMAIL" , "INBOX" ) ;
let _dataSendCC = new Array ( ) ;
2024-12-12 11:36:11 +07:00
for ( let i = 0 ; i < _ccName . length ; i ++ ) {
2024-12-02 11:15:49 +07:00
_dataSendCC . push ( {
commandSendId : commandSend.id ,
name : _ccName [ i ] ,
createdUserId : request.user.sub ,
createdFullName : request.user.name ,
createdAt : new Date ( ) ,
lastUpdateUserId : request.user.sub ,
lastUpdateFullName : request.user.name ,
lastUpdatedAt : new Date ( ) ,
} ) ;
}
await this . commandSendCCRepository . save ( _dataSendCC ) ;
}
2024-10-22 16:09:46 +07:00
} ) ,
) ;
}
2024-10-01 17:51:48 +07:00
return new HttpSuccess ( command . id ) ;
}
2024-10-04 15:35:14 +07:00
@Post ( "excexute/salary-current" )
public async newSalaryAndUpdateCurrent (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
data : {
profileId : string ;
date? : Date | null ;
amount? : Double | null ;
2024-12-06 18:43:07 +07:00
amountSpecial? : Double | null ;
2024-10-04 15:35:14 +07:00
positionSalaryAmount? : Double | null ;
mouthSalaryAmount? : Double | null ;
posNo : string | null ;
position : string | null ;
positionLine : string | null ;
positionPathSide : string | null ;
positionExecutive : string | null ;
positionType : string | null ;
positionLevel : string | null ;
refCommandNo : string | null ;
templateDoc : string | null ;
posmasterId : string ;
positionId : string ;
2024-10-21 14:21:15 +07:00
commandId? : string | null ;
2024-10-04 15:35:14 +07:00
} [ ] ;
} ,
) {
await Promise . all (
body . data . map ( async ( item ) = > {
2024-12-12 11:36:11 +07:00
const profile : any = await this . profileRepository . findOneBy ( { id : item.profileId } ) ;
2024-10-04 15:35:14 +07:00
if ( ! profile ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลทะเบียนประวัตินี้" ) ;
}
2024-10-21 14:21:15 +07:00
let _null : any = null ;
2024-10-04 15:35:14 +07:00
const dest_item = await this . salaryRepo . findOne ( {
where : { profileId : item.profileId } ,
order : { order : "DESC" } ,
} ) ;
const before = null ;
const data = new ProfileSalary ( ) ;
const meta = {
order : dest_item == null ? 1 : dest_item.order + 1 ,
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
} ;
Object . assign ( data , { . . . item , . . . meta } ) ;
const history = new ProfileSalaryHistory ( ) ;
Object . assign ( history , { . . . data , id : undefined } ) ;
await this . salaryRepo . save ( data , { data : req } ) ;
setLogDataDiff ( req , { before , after : data } ) ;
2024-10-22 16:09:46 +07:00
history . commandId = item . commandId ? ? _null ;
2024-10-24 11:58:29 +07:00
history . profileSalaryId = data . id ;
2024-10-04 15:35:14 +07:00
await this . salaryHistoryRepo . save ( history , { data : req } ) ;
const posMaster = await this . posMasterRepository . findOne ( {
where : { id : item.posmasterId } ,
} ) ;
if ( posMaster == null )
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
const posMasterOld = await this . posMasterRepository . findOne ( {
where : {
current_holderId : item.profileId ,
orgRevisionId : posMaster.orgRevisionId ,
} ,
} ) ;
if ( posMasterOld != null ) posMasterOld . current_holderId = null ;
const positionOld = await this . positionRepository . findOne ( {
where : {
posMasterId : posMasterOld?.id ,
positionIsSelected : true ,
} ,
} ) ;
if ( positionOld != null ) {
positionOld . positionIsSelected = false ;
await this . positionRepository . save ( positionOld ) ;
}
const checkPosition = await this . positionRepository . find ( {
where : {
posMasterId : item.posmasterId ,
positionIsSelected : true ,
} ,
} ) ;
if ( checkPosition . length > 0 ) {
const clearPosition = checkPosition . map ( ( positions ) = > ( {
. . . positions ,
positionIsSelected : false ,
} ) ) ;
await this . positionRepository . save ( clearPosition ) ;
}
posMaster . current_holderId = item . profileId ;
if ( posMasterOld != null ) await this . posMasterRepository . save ( posMasterOld ) ;
await this . posMasterRepository . save ( posMaster ) ;
const positionNew = await this . positionRepository . findOne ( {
where : {
id : item.positionId ,
posMasterId : item.posmasterId ,
} ,
} ) ;
if ( positionNew != null ) {
positionNew . positionIsSelected = true ;
profile . posLevelId = positionNew . posLevelId ;
profile . posTypeId = positionNew . posTypeId ;
profile . position = positionNew . positionName ;
2024-12-06 18:43:07 +07:00
profile . amount = item . amount ? ? null ;
profile . amountSpecial = item . amountSpecial ? ? null ;
2024-10-04 15:35:14 +07:00
await this . profileRepository . save ( profile ) ;
await this . positionRepository . save ( positionNew ) ;
}
} ) ,
) ;
return new HttpSuccess ( ) ;
}
@Post ( "excexute/salary-employee-current" )
public async newSalaryEmployeeAndUpdateCurrent (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
data : {
profileId : string ;
date? : Date | null ;
amount? : Double | null ;
2024-12-06 18:43:07 +07:00
amountSpecial? : Double | null ;
2024-10-04 15:35:14 +07:00
positionSalaryAmount? : Double | null ;
mouthSalaryAmount? : Double | null ;
posNo : string | null ;
position : string | null ;
positionType : string | null ;
positionLevel : string | null ;
refCommandNo : string | null ;
templateDoc : string | null ;
posmasterId : string ;
positionId : string ;
2024-10-21 14:21:15 +07:00
commandId? : string | null ;
2024-10-04 15:35:14 +07:00
} [ ] ;
} ,
) {
await Promise . all (
body . data . map ( async ( item ) = > {
2024-12-12 11:36:11 +07:00
const profile : any = await this . profileEmployeeRepository . findOneBy ( { id : item.profileId } ) ;
2024-10-04 15:35:14 +07:00
if ( ! profile ) {
throw new HttpError ( HttpStatus . BAD_REQUEST , "ไม่พบ profile ดังกล่าว" ) ;
}
const dest_item = await this . salaryRepo . findOne ( {
where : { profileEmployeeId : item.profileId } ,
order : { order : "DESC" } ,
} ) ;
const before = null ;
const data = new ProfileSalary ( ) ;
const meta = {
order : dest_item == null ? 1 : dest_item.order + 1 ,
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
} ;
2024-10-04 22:36:34 +07:00
Object . assign ( data , {
. . . item ,
. . . meta ,
profileEmployeeId : item.profileId ,
profileId : undefined ,
} ) ;
2024-10-04 15:35:14 +07:00
const history = new ProfileSalaryHistory ( ) ;
Object . assign ( history , { . . . data , id : undefined } ) ;
await this . salaryRepo . save ( data , { data : req } ) ;
setLogDataDiff ( req , { before , after : data } ) ;
history . profileSalaryId = data . id ;
await this . salaryHistoryRepo . save ( history , { data : req } ) ;
const posMaster = await this . employeePosMasterRepository . findOne ( {
where : { id : item.posmasterId } ,
relations : [ "orgRoot" ] ,
} ) ;
if ( posMaster == null )
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
const posMasterOld = await this . employeePosMasterRepository . findOne ( {
where : {
current_holderId : item.profileId ,
orgRevisionId : posMaster.orgRevisionId ,
} ,
} ) ;
if ( posMasterOld != null ) posMasterOld . current_holderId = null ;
2024-11-14 18:03:56 +07:00
// if (posMasterOld != null) posMasterOld.next_holderId = null;
2024-10-04 15:35:14 +07:00
const positionOld = await this . employeePositionRepository . findOne ( {
where : {
posMasterId : posMasterOld?.id ,
positionIsSelected : true ,
} ,
} ) ;
if ( positionOld != null ) {
positionOld . positionIsSelected = false ;
await this . employeePositionRepository . save ( positionOld ) ;
}
const checkPosition = await this . employeePositionRepository . find ( {
where : {
posMasterId : item.posmasterId ,
positionIsSelected : true ,
} ,
} ) ;
if ( checkPosition . length > 0 ) {
const clearPosition = checkPosition . map ( ( positions ) = > ( {
. . . positions ,
positionIsSelected : false ,
} ) ) ;
await this . employeePositionRepository . save ( clearPosition ) ;
}
posMaster . current_holderId = item . profileId ;
2024-11-14 18:03:56 +07:00
posMaster . next_holderId = null ;
2024-10-04 15:35:14 +07:00
if ( posMasterOld != null ) await this . employeePosMasterRepository . save ( posMasterOld ) ;
await this . employeePosMasterRepository . save ( posMaster ) ;
const positionNew = await this . employeePositionRepository . findOne ( {
where : {
id : item.positionId ,
posMasterId : item.posmasterId ,
} ,
} ) ;
if ( positionNew != null ) {
positionNew . positionIsSelected = true ;
profile . posLevelId = positionNew . posLevelId ;
profile . posTypeId = positionNew . posTypeId ;
profile . position = positionNew . positionName ;
profile . employeeOc = posMaster ? . orgRoot ? . orgRootName ? ? null ;
profile . positionEmployeePositionId = positionNew . positionName ;
2024-12-06 18:43:07 +07:00
profile . amount = item . amount ? ? null ;
profile . amountSpecial = item . amountSpecial ? ? null ;
2024-10-04 15:35:14 +07:00
await this . profileEmployeeRepository . save ( profile ) ;
await this . employeePositionRepository . save ( positionNew ) ;
}
} ) ,
) ;
return new HttpSuccess ( ) ;
}
@Post ( "excexute/salary-leave" )
public async newSalaryAndUpdateLeave (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
data : {
profileId : string ;
date? : Date | null ;
amount? : Double | null ;
2024-12-06 18:43:07 +07:00
amountSpecial? : Double | null ;
2024-10-04 15:35:14 +07:00
positionSalaryAmount? : Double | null ;
mouthSalaryAmount? : Double | null ;
posNo : string | null ;
position : string | null ;
positionLine : string | null ;
positionPathSide : string | null ;
positionExecutive : string | null ;
positionType : string | null ;
positionLevel : string | null ;
refCommandNo : string | null ;
templateDoc : string | null ;
isLeave : boolean ;
leaveReason? : string | null ;
dateLeave? : Date | null ;
2024-10-21 14:21:15 +07:00
commandId? : string | null ;
2024-10-22 14:23:28 +07:00
isGovernment? : boolean | null ;
2024-10-04 15:35:14 +07:00
} [ ] ;
} ,
) {
2024-11-12 10:46:33 +07:00
const roleKeycloak = await this . roleKeycloakRepo . findOne ( {
where : { name : Like ( "USER" ) } ,
} ) ;
2024-10-04 15:35:14 +07:00
await Promise . all (
body . data . map ( async ( item ) = > {
2024-11-12 10:46:33 +07:00
const profile = await this . profileRepository . findOne ( {
where : { id : item.profileId } ,
2024-11-14 18:03:56 +07:00
relations : [ "roleKeycloaks" ] ,
} ) ;
2024-10-04 15:35:14 +07:00
if ( ! profile ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลทะเบียนประวัตินี้" ) ;
}
const dest_item = await this . salaryRepo . findOne ( {
where : { profileId : item.profileId } ,
order : { order : "DESC" } ,
} ) ;
const before = null ;
const data = new ProfileSalary ( ) ;
const meta = {
order : dest_item == null ? 1 : dest_item.order + 1 ,
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
} ;
Object . assign ( data , { . . . item , . . . meta } ) ;
const history = new ProfileSalaryHistory ( ) ;
Object . assign ( history , { . . . data , id : undefined } ) ;
2024-10-22 14:23:28 +07:00
data . dateGovernment = meta . createdAt ;
2024-10-04 15:35:14 +07:00
await this . salaryRepo . save ( data , { data : req } ) ;
setLogDataDiff ( req , { before , after : data } ) ;
history . profileSalaryId = data . id ;
await this . salaryHistoryRepo . save ( history , { data : req } ) ;
const _null : any = null ;
profile . isLeave = item . isLeave ;
profile . leaveReason = item . leaveReason ? ? _null ;
profile . dateLeave = item . dateLeave ? ? _null ;
profile . lastUpdateUserId = req . user . sub ;
profile . lastUpdateFullName = req . user . name ;
profile . lastUpdatedAt = new Date ( ) ;
if ( item . isLeave == true ) {
await removeProfileInOrganize ( profile . id , "OFFICER" ) ;
}
2024-10-25 14:36:32 +07:00
const clearProfile = await checkCommandType ( String ( item . commandId ) ) ;
2024-10-28 11:48:00 +07:00
if ( clearProfile ) {
if ( profile . keycloak != null ) {
2024-10-25 14:36:32 +07:00
const delUserKeycloak = await deleteUser ( profile . keycloak ) ;
2024-11-22 11:34:11 +07:00
if ( delUserKeycloak ) {
profile . keycloak = _null ;
profile . roleKeycloaks = [ ] ;
profile . isActive = false ;
}
2024-10-25 14:36:32 +07:00
}
profile . position = _null ;
profile . posTypeId = _null ;
profile . posLevelId = _null ;
}
2024-10-28 20:59:31 +07:00
const returnWork = await checkReturnCommandType ( String ( item . commandId ) ) ;
2024-11-22 11:34:11 +07:00
//คำสั่งบรรจุกลับเข้ารับราชการ หรือ ผู้ออกไปรับราชการทหารกลับเข้ารับราชการ solutionเดิม ให้ enable user เปลี่ยนเป็นสร้าง user ใหม่เลยเพราะยังไงตอนถูกพักก็ถูกลบ user
2024-10-29 12:23:37 +07:00
if ( returnWork && item . isGovernment ) {
2024-12-02 18:14:07 +07:00
let userKeycloakId ;
userKeycloakId = await createUser ( profile . citizenId , profile . citizenId , {
firstName : profile.firstName ,
lastName : profile.lastName ,
} ) ;
2024-12-04 15:46:20 +07:00
// กรณี Keycloak ไม่ถูกลบ ให้ลบซ้ำอีกรอบแล้วสร้างใหม่ และหากยังไม่สามารถลบได้ให้แสดง Error
2024-12-12 11:36:11 +07:00
if (
profile . keycloak != null &&
userKeycloakId &&
userKeycloakId . errorMessage === "User exists with same username"
) {
2024-12-02 18:14:07 +07:00
const delUserKeycloak = await deleteUser ( profile . keycloak ) ;
2024-12-12 11:36:11 +07:00
if ( delUserKeycloak ) {
2024-12-02 18:14:07 +07:00
userKeycloakId = await createUser ( profile . citizenId , profile . citizenId , {
firstName : profile.firstName ,
lastName : profile.lastName ,
} ) ;
2024-12-12 11:36:11 +07:00
} else {
throw new HttpError (
HttpStatus . BAD_REQUEST ,
"พบข้อผิดพลาด ไม่สามารถจัดการผู้ใช้งานได้" ,
) ;
2024-12-04 15:46:20 +07:00
}
2024-12-02 18:14:07 +07:00
}
const list = await getRoles ( ) ;
let result = false ;
if ( Array . isArray ( list ) && userKeycloakId ) {
result = await addUserRoles (
2024-11-14 14:42:32 +07:00
userKeycloakId ,
list
. filter ( ( v ) = > v . name === "USER" )
. map ( ( x ) = > ( {
id : x.id ,
name : x.name ,
} ) ) ,
) ;
2024-12-02 18:14:07 +07:00
}
2024-12-06 18:43:07 +07:00
profile . amount = item . amount ? ? _null ;
profile . amountSpecial = item . amountSpecial ? ? _null ;
2024-11-14 14:42:32 +07:00
profile . isActive = true ;
2024-12-02 18:14:07 +07:00
profile . keycloak = typeof userKeycloakId === "string" ? userKeycloakId : "" ;
2024-11-22 11:34:11 +07:00
profile . roleKeycloaks = result && roleKeycloak ? [ roleKeycloak ] : [ ] ;
2024-10-28 20:59:31 +07:00
}
2024-10-04 15:35:14 +07:00
await this . profileRepository . save ( profile ) ;
} ) ,
) ;
return new HttpSuccess ( ) ;
}
@Post ( "excexute/salary-employee-leave" )
public async newSalaryEmployeeAndUpdateLeave (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
data : {
profileId : string ;
date? : Date | null ;
amount? : Double | null ;
positionSalaryAmount? : Double | null ;
mouthSalaryAmount? : Double | null ;
posNo : string | null ;
position : string | null ;
positionType : string | null ;
positionLevel : string | null ;
refCommandNo : string | null ;
templateDoc : string | null ;
isLeave : boolean ;
leaveReason? : string | null ;
dateLeave? : Date | null ;
2024-10-22 14:23:28 +07:00
isGovernment? : boolean | null ;
2024-10-25 14:36:32 +07:00
commandId? : string | null ;
2024-10-04 15:35:14 +07:00
} [ ] ;
} ,
) {
await Promise . all (
body . data . map ( async ( item ) = > {
2024-11-12 10:46:33 +07:00
const profile = await this . profileEmployeeRepository . findOne ( {
2024-11-14 18:03:56 +07:00
where : { id : item.profileId } ,
relations : [ "roleKeycloaks" ] ,
2024-11-12 10:46:33 +07:00
} ) ;
2024-10-04 15:35:14 +07:00
if ( ! profile ) {
throw new HttpError ( HttpStatus . BAD_REQUEST , "ไม่พบ profile ดังกล่าว" ) ;
}
const dest_item = await this . salaryRepo . findOne ( {
where : { profileEmployeeId : item.profileId } ,
order : { order : "DESC" } ,
} ) ;
const before = null ;
const data = new ProfileSalary ( ) ;
const meta = {
order : dest_item == null ? 1 : dest_item.order + 1 ,
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
} ;
2024-10-04 22:36:34 +07:00
Object . assign ( data , {
. . . item ,
. . . meta ,
profileEmployeeId : item.profileId ,
profileId : undefined ,
} ) ;
2024-10-04 15:35:14 +07:00
const history = new ProfileSalaryHistory ( ) ;
Object . assign ( history , { . . . data , id : undefined } ) ;
2024-10-22 14:23:28 +07:00
data . dateGovernment = meta . createdAt ;
2024-10-04 15:35:14 +07:00
await this . salaryRepo . save ( data , { data : req } ) ;
setLogDataDiff ( req , { before , after : data } ) ;
history . profileSalaryId = data . id ;
await this . salaryHistoryRepo . save ( history , { data : req } ) ;
const _null : any = null ;
profile . isLeave = item . isLeave ;
profile . leaveReason = item . leaveReason ? ? _null ;
profile . dateLeave = item . dateLeave ? ? _null ;
profile . lastUpdateUserId = req . user . sub ;
profile . lastUpdateFullName = req . user . name ;
profile . lastUpdatedAt = new Date ( ) ;
if ( item . isLeave == true ) {
await removeProfileInOrganize ( profile . id , "EMPLOYEE" ) ;
}
2024-10-25 14:36:32 +07:00
const clearProfile = await checkCommandType ( String ( item . commandId ) ) ;
2024-10-28 11:48:00 +07:00
if ( clearProfile ) {
if ( profile . keycloak != null ) {
2024-10-25 14:36:32 +07:00
const delUserKeycloak = await deleteUser ( profile . keycloak ) ;
2024-11-22 11:34:11 +07:00
if ( delUserKeycloak ) {
profile . keycloak = _null ;
profile . roleKeycloaks = [ ] ;
profile . isActive = false ;
}
2024-10-25 14:36:32 +07:00
}
profile . position = _null ;
profile . posTypeId = _null ;
profile . posLevelId = _null ;
}
2024-10-04 15:35:14 +07:00
await this . profileEmployeeRepository . save ( profile ) ;
} ) ,
) ;
return new HttpSuccess ( ) ;
}
@Post ( "excexute/salary" )
public async newSalaryAndUpdate (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
data : {
profileId : string ;
date? : Date | null ;
amount? : Double | null ;
2024-12-06 18:43:07 +07:00
amountSpecial? : Double | null ;
2024-10-04 15:35:14 +07:00
positionSalaryAmount? : Double | null ;
mouthSalaryAmount? : Double | null ;
posNo : string | null ;
position : string | null ;
positionLine : string | null ;
positionPathSide : string | null ;
positionExecutive : string | null ;
positionType : string | null ;
positionLevel : string | null ;
refCommandNo : string | null ;
templateDoc : string | null ;
2024-10-21 14:21:15 +07:00
commandId? : string | null ;
2024-11-07 14:03:40 +07:00
leaveReason? : string | null ;
dateLeave? : Date | null ;
2024-11-07 13:53:52 +07:00
isLeave? : boolean ;
2024-10-04 15:35:14 +07:00
} [ ] ;
} ,
) {
await Promise . all (
body . data . map ( async ( item ) = > {
2024-12-12 11:36:11 +07:00
const profile : any = await this . profileRepository . findOne ( {
2024-11-12 10:46:33 +07:00
where : { id : item.profileId } ,
2024-11-14 18:03:56 +07:00
relations : [ "roleKeycloaks" ] ,
} ) ;
2024-10-04 15:35:14 +07:00
if ( ! profile ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลทะเบียนประวัตินี้" ) ;
}
const dest_item = await this . salaryRepo . findOne ( {
where : { profileId : item.profileId } ,
order : { order : "DESC" } ,
} ) ;
const before = null ;
const data = new ProfileSalary ( ) ;
const meta = {
order : dest_item == null ? 1 : dest_item.order + 1 ,
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
} ;
2024-11-14 18:03:56 +07:00
if ( item . isLeave != undefined && item . isLeave == true ) {
2024-11-07 13:53:52 +07:00
await removeProfileInOrganize ( profile . id , "OFFICER" ) ;
}
2024-10-25 14:36:32 +07:00
const clearProfile = await checkCommandType ( String ( item . commandId ) ) ;
const _null : any = null ;
2024-10-28 11:48:00 +07:00
if ( clearProfile ) {
if ( profile . keycloak != null ) {
2024-10-25 14:36:32 +07:00
const delUserKeycloak = await deleteUser ( profile . keycloak ) ;
2024-11-22 11:34:11 +07:00
if ( delUserKeycloak ) {
profile . keycloak = _null ;
profile . roleKeycloaks = [ ] ;
profile . isActive = false ;
}
2024-10-25 14:36:32 +07:00
}
profile . position = _null ;
profile . posTypeId = _null ;
profile . posLevelId = _null ;
2024-11-07 14:03:40 +07:00
profile . leaveReason = item . leaveReason ? ? _null ;
profile . dateLeave = item . dateLeave ? ? _null ;
2024-12-06 18:43:07 +07:00
profile . amount = item . amount ? ? _null ;
profile . amountSpecial = item . amountSpecial ? ? _null ;
2024-10-28 11:48:00 +07:00
await this . profileRepository . save ( profile , { data : req } ) ;
2024-10-25 14:36:32 +07:00
}
2024-10-04 15:35:14 +07:00
Object . assign ( data , { . . . item , . . . meta } ) ;
const history = new ProfileSalaryHistory ( ) ;
Object . assign ( history , { . . . data , id : undefined } ) ;
await this . salaryRepo . save ( data , { data : req } ) ;
setLogDataDiff ( req , { before , after : data } ) ;
history . profileSalaryId = data . id ;
await this . salaryHistoryRepo . save ( history , { data : req } ) ;
} ) ,
) ;
return new HttpSuccess ( ) ;
}
@Post ( "excexute/salary-employee" )
public async newSalaryEmployeeAndUpdate (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
data : {
profileId : string ;
date? : Date | null ;
amount? : Double | null ;
positionSalaryAmount? : Double | null ;
mouthSalaryAmount? : Double | null ;
posNo : string | null ;
position : string | null ;
positionType : string | null ;
positionLevel : string | null ;
refCommandNo : string | null ;
templateDoc : string | null ;
} [ ] ;
} ,
) {
await Promise . all (
body . data . map ( async ( item ) = > {
const profile = await this . profileEmployeeRepository . findOneBy ( { id : item.profileId } ) ;
if ( ! profile ) {
throw new HttpError ( HttpStatus . BAD_REQUEST , "ไม่พบ profile ดังกล่าว" ) ;
}
const dest_item = await this . salaryRepo . findOne ( {
where : { profileEmployeeId : item.profileId } ,
order : { order : "DESC" } ,
} ) ;
const before = null ;
const data = new ProfileSalary ( ) ;
const meta = {
order : dest_item == null ? 1 : dest_item.order + 1 ,
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
} ;
2024-10-04 22:36:34 +07:00
Object . assign ( data , {
. . . item ,
. . . meta ,
profileEmployeeId : item.profileId ,
profileId : undefined ,
} ) ;
2024-10-04 15:35:14 +07:00
const history = new ProfileSalaryHistory ( ) ;
Object . assign ( history , { . . . data , id : undefined } ) ;
await this . salaryRepo . save ( data , { data : req } ) ;
setLogDataDiff ( req , { before , after : data } ) ;
history . profileSalaryId = data . id ;
await this . salaryHistoryRepo . save ( history , { data : req } ) ;
} ) ,
) ;
return new HttpSuccess ( ) ;
}
@Post ( "excexute/salary-leave-discipline" )
public async newSalaryAndUpdateLeaveDiscipline (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
data : {
profileId : string ;
date? : Date | null ;
refCommandNo? : string | null ;
salaryRef? : string | null ;
isLeave : boolean | null ;
leaveReason? : string | null ;
dateLeave? : Date | null ;
refCommandDate? : Date | null ;
detail? : string | null ;
level? : string | null ;
unStigma? : string | null ;
2024-10-21 14:21:15 +07:00
commandId? : string | null ;
2024-10-22 10:52:26 +07:00
amount? : Double | null ;
positionSalaryAmount? : Double | null ;
mouthSalaryAmount? : Double | null ;
2024-10-22 14:23:28 +07:00
isGovernment? : boolean | null ;
2024-10-04 15:35:14 +07:00
} [ ] ;
} ,
) {
await Promise . all (
body . data . map ( async ( item ) = > {
const profile = await this . profileRepository . findOne ( {
2024-11-14 18:03:56 +07:00
relations : [ "profileSalary" , "posLevel" , "posType" , "current_holders" , "roleKeycloaks" ] ,
2024-10-04 15:35:14 +07:00
where : { id : item.profileId } ,
order : {
profileSalary : {
order : "DESC" ,
} ,
} ,
} ) ;
if ( ! profile ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลทะเบียนประวัตินี้" ) ;
}
2024-11-07 13:27:29 +07:00
const orgRevision = await this . orgRevisionRepo . findOne ( {
2024-11-14 18:03:56 +07:00
where : {
2024-11-07 13:27:29 +07:00
orgRevisionIsCurrent : true ,
2024-11-14 18:03:56 +07:00
orgRevisionIsDraft : false ,
} ,
} ) ;
2024-11-07 13:27:29 +07:00
const shortName =
2024-11-14 18:03:56 +07:00
! profile . current_holders || profile . current_holders . length == 0
2024-11-07 13:27:29 +07:00
? null
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) != null &&
2024-11-14 18:03:56 +07:00
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgChild4 != null
2024-11-07 13:27:29 +07:00
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgChild4 . orgChild4ShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) != null &&
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgChild3 != null
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgChild3 . orgChild3ShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) != null &&
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgChild2 != null
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgChild2 . orgChild2ShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
2024-11-14 18:03:56 +07:00
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) !=
null &&
2024-11-07 13:27:29 +07:00
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgChild1 != null
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgChild1 . orgChild1ShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) !=
null &&
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgRoot != null
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgRoot . orgRootShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
: null ;
2024-11-14 18:03:56 +07:00
let position =
profile . current_holders
. filter ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) [ 0 ]
? . positions ? . filter ( ( pos ) = > pos . positionIsSelected === true ) [ 0 ] ? ? null ;
2024-10-04 15:35:14 +07:00
// ประวัติตำแหน่ง
const data = new ProfileSalary ( ) ;
const meta = {
2024-10-28 18:01:36 +07:00
profileId : profile.id ,
2024-10-29 14:09:57 +07:00
commandId : item.commandId ,
2024-10-04 15:35:14 +07:00
date : item.date ,
refCommandNo : item.refCommandNo ,
templateDoc : item.salaryRef ,
2024-11-07 13:27:29 +07:00
position : profile.position ,
positionType : profile.posType.posTypeName ,
positionLevel : profile.posLevel.posLevelName ,
2024-11-14 18:03:56 +07:00
posNo : shortName ? shortName : "-" ,
2024-11-07 13:27:29 +07:00
positionLine : position?.positionField ? ? "-" ,
positionPathSide : position?.positionArea ? ? "-" ,
positionExecutive : position?.posExecutive?.posExecutiveName ? ? "-" ,
2024-10-22 10:52:26 +07:00
amount : item.amount ? item.amount : null ,
positionSalaryAmount : item.positionSalaryAmount ? item.positionSalaryAmount : null ,
mouthSalaryAmount : item.mouthSalaryAmount ? item.mouthSalaryAmount : null ,
2024-10-04 15:35:14 +07:00
order :
profile . profileSalary . length >= 0
? profile . profileSalary . length > 0
? profile . profileSalary [ 0 ] . order + 1
: 1
: null ,
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
2024-10-22 14:23:28 +07:00
dateGovernment : new Date ( ) ,
2024-10-22 16:09:46 +07:00
isGovernment : item.isGovernment ,
2024-10-04 15:35:14 +07:00
} ;
Object . assign ( data , meta ) ;
const history = new ProfileSalaryHistory ( ) ;
Object . assign ( history , { . . . data , id : undefined } ) ;
await this . salaryRepo . save ( data ) ;
history . profileSalaryId = data . id ;
await this . salaryHistoryRepo . save ( history ) ;
// ประวัติวินัย
const dataDis = new ProfileDiscipline ( ) ;
const metaDis = {
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
} ;
Object . assign ( dataDis , { . . . body , . . . metaDis } ) ;
const historyDis = new ProfileDisciplineHistory ( ) ;
Object . assign ( historyDis , { . . . dataDis , id : undefined } ) ;
await this . disciplineRepository . save ( dataDis ) ;
historyDis . profileDisciplineId = dataDis . id ;
await this . disciplineHistoryRepository . save ( historyDis ) ;
// ทะเบียนประวัติ
if ( item . isLeave != null ) {
2024-10-29 12:23:37 +07:00
const _profile = await this . profileRepository . findOne ( {
where : { id : item.profileId } ,
} ) ;
if ( ! _profile ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลทะเบียนประวัตินี้" ) ;
}
2024-10-04 15:35:14 +07:00
const _null : any = null ;
2024-10-29 12:23:37 +07:00
_profile . isLeave = item . isLeave ;
_profile . leaveReason = item . leaveReason ? ? _null ;
_profile . dateLeave = item . dateLeave ? ? _null ;
_profile . lastUpdateUserId = req . user . sub ;
_profile . lastUpdateFullName = req . user . name ;
_profile . lastUpdatedAt = new Date ( ) ;
2024-10-28 18:01:36 +07:00
const exceptClear = await checkExceptCommandType ( String ( item . commandId ) ) ;
if ( item . isLeave == true && ! exceptClear ) {
2024-10-29 12:23:37 +07:00
await removeProfileInOrganize ( _profile . id , "OFFICER" ) ;
2024-12-12 11:36:11 +07:00
}
2024-11-22 11:34:11 +07:00
//คำสั่งพักราชการ หรือ ให้ออกจากราชการไว้ก่อน solutionเดิม ให้ disable user ไว้แต่ยังไม่ลบ เปลี่ยนเป็นลบ user ออกเลย
else if ( item . isLeave == true && exceptClear && _profile . keycloak != null ) {
// const enableActive = await enableStatus(_profile.keycloak, false);
// if (!enableActive) throw new Error("Failed. Cannot change enable status.");
const delUserKeycloak = await deleteUser ( _profile . keycloak ) ;
if ( delUserKeycloak ) {
_profile . keycloak = _null ;
_profile . roleKeycloaks = [ ] ;
_profile . isActive = false ;
}
2024-11-14 14:42:32 +07:00
}
2024-10-25 14:36:32 +07:00
const clearProfile = await checkCommandType ( String ( item . commandId ) ) ;
2024-10-28 11:48:00 +07:00
if ( clearProfile ) {
2024-10-29 12:23:37 +07:00
if ( _profile . keycloak != null ) {
const delUserKeycloak = await deleteUser ( _profile . keycloak ) ;
2024-11-22 11:34:11 +07:00
if ( delUserKeycloak ) {
_profile . keycloak = _null ;
_profile . roleKeycloaks = [ ] ;
_profile . isActive = false ;
}
2024-10-25 14:36:32 +07:00
}
2024-10-29 12:23:37 +07:00
_profile . position = _null ;
_profile . posTypeId = _null ;
_profile . posLevelId = _null ;
2024-10-25 14:36:32 +07:00
}
2024-10-29 14:14:14 +07:00
await this . profileRepository . save ( _profile ) ;
2024-10-04 15:35:14 +07:00
}
} ) ,
) ;
return new HttpSuccess ( ) ;
}
2024-10-04 22:36:34 +07:00
@Post ( "excexute/salary-probation" )
public async newSalaryAndUpdateLeaveDisciplinefgh (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
data : {
profileId : string ;
date? : Date | null ;
refCommandNo? : string | null ;
salaryRef? : string | null ;
2024-10-21 14:21:15 +07:00
commandId? : string | null ;
2024-10-21 17:16:36 +07:00
amount? : Double | null ;
positionSalaryAmount? : Double | null ;
mouthSalaryAmount? : Double | null ;
2024-10-04 22:36:34 +07:00
} [ ] ;
} ,
) {
await Promise . all (
body . data . map ( async ( item ) = > {
const profile = await this . profileRepository . findOne ( {
2024-11-13 18:39:16 +07:00
relations : [
"profileSalary" ,
"posType" ,
"posLevel" ,
"current_holders" ,
"current_holders.orgRoot" ,
"current_holders.orgChild1" ,
"current_holders.orgChild2" ,
"current_holders.orgChild3" ,
"current_holders.orgChild4" ,
] ,
2024-10-04 22:36:34 +07:00
where : { id : item.profileId } ,
order : {
profileSalary : {
order : "DESC" ,
} ,
} ,
} ) ;
if ( ! profile ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลทะเบียนประวัตินี้" ) ;
}
2024-11-07 13:27:29 +07:00
const orgRevision = await this . orgRevisionRepo . findOne ( {
2024-11-14 18:03:56 +07:00
where : {
2024-11-07 13:27:29 +07:00
orgRevisionIsCurrent : true ,
2024-11-14 18:03:56 +07:00
orgRevisionIsDraft : false ,
} ,
} ) ;
2024-11-07 13:27:29 +07:00
const shortName =
2024-11-14 18:03:56 +07:00
! profile . current_holders || profile . current_holders . length == 0
2024-11-07 13:27:29 +07:00
? null
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) != null &&
2024-11-14 18:03:56 +07:00
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgChild4 != null
2024-11-07 13:27:29 +07:00
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgChild4 . orgChild4ShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) != null &&
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgChild3 != null
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgChild3 . orgChild3ShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) != null &&
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgChild2 != null
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgChild2 . orgChild2ShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
2024-11-14 18:03:56 +07:00
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) !=
null &&
2024-11-07 13:27:29 +07:00
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgChild1 != null
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgChild1 . orgChild1ShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) !=
null &&
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgRoot != null
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgRoot . orgRootShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
: null ;
2024-11-14 18:03:56 +07:00
let position =
profile . current_holders
. filter ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) [ 0 ]
? . positions ? . filter ( ( pos ) = > pos . positionIsSelected === true ) [ 0 ] ? ? null ;
2024-10-04 22:36:34 +07:00
// ประวัติตำแหน่ง
const data = new ProfileSalary ( ) ;
const meta = {
profileId : item.profileId ,
date : item.date ,
refCommandNo : item.refCommandNo ,
templateDoc : item.salaryRef ,
2024-11-06 16:20:53 +07:00
commandId : item.commandId ,
2024-11-07 13:27:29 +07:00
position : profile.position ,
positionType : profile.posType.posTypeName ,
positionLevel : profile.posLevel.posLevelName ,
2024-11-14 18:03:56 +07:00
posNo : shortName ? shortName : "-" ,
2024-11-07 13:27:29 +07:00
positionLine : position?.positionField ? ? "-" ,
positionPathSide : position?.positionArea ? ? "-" ,
positionExecutive : position?.posExecutive?.posExecutiveName ? ? "-" ,
2024-10-21 17:16:36 +07:00
amount : item.amount ? item.amount : null ,
positionSalaryAmount : item.positionSalaryAmount ? item.positionSalaryAmount : null ,
mouthSalaryAmount : item.mouthSalaryAmount ? item.mouthSalaryAmount : null ,
2024-10-04 22:36:34 +07:00
order :
profile . profileSalary . length >= 0
? profile . profileSalary . length > 0
? profile . profileSalary [ 0 ] . order + 1
: 1
: null ,
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
} ;
Object . assign ( data , meta ) ;
const history = new ProfileSalaryHistory ( ) ;
Object . assign ( history , { . . . data , id : undefined } ) ;
await this . salaryRepo . save ( data ) ;
history . profileSalaryId = data . id ;
await this . salaryHistoryRepo . save ( history ) ;
} ) ,
) ;
2024-11-06 16:20:53 +07:00
const checkCommandType = await this . commandRepository . findOne ( {
2024-11-14 18:03:56 +07:00
where : { id : body.data.length > 0 ? body . data [ 0 ] . commandId ? . toString ( ) : "" } ,
relations : [ "commandType" ] ,
2024-11-06 16:20:53 +07:00
} ) ;
2024-11-14 18:03:56 +07:00
if ( checkCommandType ? . commandType . code == "C-PM-11" ) {
const profile = await this . profileRepository . find ( {
where : { id : In ( body . data . map ( ( x ) = > x . profileId ) ) } ,
} ) ;
2024-11-06 16:20:53 +07:00
const data = profile . map ( ( x ) = > ( {
. . . x ,
isProbation : false ,
} ) ) ;
await this . profileRepository . save ( data ) ;
}
2024-10-04 22:36:34 +07:00
return new HttpSuccess ( ) ;
}
@Post ( "excexute/salary-probation-leave" )
async ExecuteCommand12Async (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
data : {
profileId : string ;
date? : Date | null ;
refCommandNo? : string | null ;
salaryRef? : string | null ;
2024-10-21 14:21:15 +07:00
commandId? : string | null ;
2024-10-21 17:20:39 +07:00
amount? : Double | null ;
positionSalaryAmount? : Double | null ;
mouthSalaryAmount? : Double | null ;
2024-10-22 14:23:28 +07:00
isGovernment? : boolean | null ;
2024-10-04 22:36:34 +07:00
} [ ] ;
} ,
) {
await Promise . all (
body . data . map ( async ( item ) = > {
const profile = await this . profileRepository . findOne ( {
2024-11-14 18:03:56 +07:00
relations : [
"profileSalary" ,
"posType" ,
"posLevel" ,
"current_holders" ,
"current_holders.positions" ,
"current_holders.positions.posExecutive" ,
] ,
2024-10-04 22:36:34 +07:00
where : { id : item.profileId } ,
order : {
profileSalary : {
order : "DESC" ,
} ,
} ,
} ) ;
if ( ! profile ) {
throw new HttpError ( HttpStatus . NOT_FOUND , "ไม่พบข้อมูลโปรไฟล์" ) ;
}
const _profile = await this . profileRepository . findOne ( {
where : { id : item.profileId } ,
2024-11-14 18:03:56 +07:00
relations : [ "roleKeycloaks" ] ,
2024-10-04 22:36:34 +07:00
} ) ;
if ( ! _profile ) {
throw new HttpError ( HttpStatus . NOT_FOUND , "ไม่พบข้อมูลโปรไฟล์" ) ;
}
let dateLeave_ : any = item . date ;
_profile . isLeave = true ;
_profile . leaveReason =
"คำสั่งให้ข้าราชการออกจากราชการเพราะผลการทดลองปฏิบัติหน้าที่ราชการต่ำกว่ามาตรฐานที่กำหนด" ;
_profile . dateLeave = dateLeave_ ;
_profile . lastUpdateUserId = req . user . sub ;
_profile . lastUpdateFullName = req . user . name ;
_profile . lastUpdatedAt = new Date ( ) ;
2024-11-07 13:27:29 +07:00
const orgRevision = await this . orgRevisionRepo . findOne ( {
2024-11-14 18:03:56 +07:00
where : {
2024-11-07 13:27:29 +07:00
orgRevisionIsCurrent : true ,
2024-11-14 18:03:56 +07:00
orgRevisionIsDraft : false ,
} ,
} ) ;
2024-11-07 13:27:29 +07:00
const shortName =
2024-11-14 18:03:56 +07:00
! profile . current_holders || profile . current_holders . length == 0
2024-11-07 13:27:29 +07:00
? null
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) != null &&
2024-11-14 18:03:56 +07:00
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgChild4 != null
2024-11-07 13:27:29 +07:00
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgChild4 . orgChild4ShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) != null &&
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgChild3 != null
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgChild3 . orgChild3ShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) != null &&
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgChild2 != null
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgChild2 . orgChild2ShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
2024-11-14 18:03:56 +07:00
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) !=
null &&
2024-11-07 13:27:29 +07:00
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgChild1 != null
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgChild1 . orgChild1ShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
: profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) !=
null &&
profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id )
? . orgRoot != null
? ` ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . orgRoot . orgRootShortName } ${ profile . current_holders . find ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) ? . posMasterNo } `
: null ;
2024-11-14 18:03:56 +07:00
let position =
profile . current_holders
. filter ( ( x ) = > x . orgRevisionId == orgRevision ? . id ) [ 0 ]
? . positions ? . filter ( ( pos ) = > pos . positionIsSelected === true ) [ 0 ] ? ? null ;
2024-10-04 22:36:34 +07:00
const profileSalary : ProfileSalary = Object . assign ( new ProfileSalary ( ) , {
profileId : item.profileId ,
date : item.date ,
refCommandNo : item.refCommandNo ,
templateDoc : item.salaryRef ,
2024-10-21 14:21:15 +07:00
commandId : item.commandId ,
2024-11-07 13:27:29 +07:00
position : profile.position ,
positionType : profile.posType.posTypeName ,
positionLevel : profile.posLevel.posLevelName ,
posNo : shortName ,
positionLine : position?.positionField ? ? "-" ,
positionPathSide : position?.positionArea ? ? "-" ,
positionExecutive : position?.posExecutive?.posExecutiveName ? ? "-" ,
2024-10-21 17:20:39 +07:00
amount : item.amount ? item.amount : null ,
positionSalaryAmount : item.positionSalaryAmount ? item.positionSalaryAmount : null ,
mouthSalaryAmount : item.mouthSalaryAmount ? item.mouthSalaryAmount : null ,
2024-10-04 22:36:34 +07:00
order :
profile . profileSalary . length >= 0
? profile . profileSalary . length > 0
? profile . profileSalary [ 0 ] . order + 1
: 1
: null ,
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
2024-10-22 14:23:28 +07:00
dateGovernment : new Date ( ) ,
2024-10-22 16:09:46 +07:00
isGovernment : item.isGovernment ,
2024-10-04 22:36:34 +07:00
} ) ;
2024-11-07 13:27:29 +07:00
await removeProfileInOrganize ( profile . id , "OFFICER" ) ;
2024-10-25 14:36:32 +07:00
const clearProfile = await checkCommandType ( String ( item . commandId ) ) ;
const _null : any = null ;
2024-10-28 11:48:00 +07:00
if ( clearProfile ) {
2024-11-14 18:03:56 +07:00
if ( _profile . keycloak != null ) {
const delUserKeycloak = await deleteUser ( _profile . keycloak ) ;
2024-11-22 11:34:11 +07:00
if ( delUserKeycloak ) {
_profile . keycloak = _null ;
_profile . roleKeycloaks = [ ] ;
_profile . isActive = false ;
}
2024-11-14 18:03:56 +07:00
}
_profile . position = _null ;
_profile . posTypeId = _null ;
_profile . posLevelId = _null ;
}
await Promise . all ( [
this . profileRepository . save ( _profile ) ,
this . salaryRepo . save ( profileSalary ) ,
] ) ;
const history = new ProfileSalaryHistory ( ) ;
Object . assign ( history , { . . . profileSalary , id : undefined } ) ;
history . profileSalaryId = profileSalary . id ;
await this . salaryHistoryRepo . save ( history ) ;
2024-10-04 22:36:34 +07:00
} ) ,
) ;
return new HttpSuccess ( ) ;
}
2024-10-08 16:35:08 +07:00
@Post ( "excexute/create-officer-profile" )
public async CreateOfficeProfileExcecute (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
data : {
2024-10-09 12:53:13 +07:00
bodyProfile : CreateProfileAllFields ;
2024-10-10 13:07:10 +07:00
bodyEducations? : CreateProfileEducation [ ] ;
bodyCertificates? : CreateProfileCertificate [ ] ;
bodySalarys? : CreateProfileSalary | null ;
bodyPosition ? : {
2024-10-08 16:35:08 +07:00
posmasterId : string ;
positionId : string ;
2024-10-10 13:07:10 +07:00
} | null ;
2024-10-08 16:35:08 +07:00
} [ ] ;
} ,
) {
2024-11-12 10:46:33 +07:00
const roleKeycloak = await this . roleKeycloakRepo . findOne ( {
where : { name : Like ( "USER" ) } ,
2024-11-14 18:03:56 +07:00
} ) ;
2024-10-08 16:35:08 +07:00
await Promise . all (
body . data . map ( async ( item ) = > {
const before = null ;
const meta = {
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
} ;
2024-10-09 15:45:42 +07:00
const _null : any = null ;
2024-10-08 16:35:08 +07:00
if ( item . bodyProfile . posLevelId === "" ) item . bodyProfile . posLevelId = null ;
if ( item . bodyProfile . posTypeId === "" ) item . bodyProfile . posTypeId = null ;
2024-10-10 11:12:31 +07:00
if (
item . bodyProfile . posLevelId &&
! ( await this . posLevelRepo . findOneBy ( { id : item.bodyProfile.posLevelId } ) )
) {
2024-10-08 16:35:08 +07:00
throw new HttpError ( HttpStatus . NOT_FOUND , "ไม่พบข้อมูลระดับตำแหน่งนี้" ) ;
}
2024-10-10 11:12:31 +07:00
if (
item . bodyProfile . posTypeId &&
! ( await this . posTypeRepo . findOneBy ( { id : item.bodyProfile.posTypeId } ) )
) {
2024-10-08 16:35:08 +07:00
throw new HttpError ( HttpStatus . NOT_FOUND , "ไม่พบข้อมูลประเภทตำแหน่งนี้" ) ;
}
2024-11-12 10:46:33 +07:00
let profile : any = await this . profileRepository . findOne ( {
where : { citizenId : item.bodyProfile.citizenId } ,
2024-11-14 18:03:56 +07:00
relations : [ "roleKeycloaks" ] ,
2024-10-10 11:12:31 +07:00
} ) ;
if ( ! profile ) {
2024-10-09 15:45:42 +07:00
profile = Object . assign ( { . . . item . bodyProfile , . . . meta } ) ;
2024-10-10 11:12:31 +07:00
profile . dateRetire =
item . bodyProfile . birthDate == null
? _null
: calculateRetireDate ( item . bodyProfile . birthDate ) ;
profile . dateRetireLaw =
item . bodyProfile . birthDate == null
? _null
: calculateRetireLaw ( item . bodyProfile . birthDate ) ;
2024-10-09 15:45:42 +07:00
const userKeycloakId = await createUser ( profile . citizenId , profile . citizenId , {
firstName : profile.firstName ,
lastName : profile.lastName ,
2024-10-08 16:35:08 +07:00
} ) ;
2024-11-22 11:34:11 +07:00
// if (typeof userKeycloakId !== "string") {
// throw new Error(userKeycloakId.errorMessage);
// }
2024-10-08 16:35:08 +07:00
const list = await getRoles ( ) ;
2024-10-09 12:53:13 +07:00
if ( ! Array . isArray ( list ) )
throw new Error ( "Failed. Cannot get role(s) data from the server." ) ;
2024-10-08 16:35:08 +07:00
const result = await addUserRoles (
userKeycloakId ,
list
. filter ( ( v ) = > v . name === "USER" )
. map ( ( x ) = > ( {
id : x.id ,
name : x.name ,
2024-10-09 12:53:13 +07:00
} ) ) ,
2024-10-08 16:35:08 +07:00
) ;
2024-11-22 11:34:11 +07:00
// if (!result) throw new Error("Failed. Cannot set user's role.");
profile . keycloak = typeof userKeycloakId === "string" ? userKeycloakId : "" ;
profile . roleKeycloaks = result && roleKeycloak ? [ roleKeycloak ] : [ ] ;
2024-10-16 16:34:36 +07:00
profile . email = item . bodyProfile . email ;
2024-11-15 16:26:26 +07:00
profile . dateStart = item . bodyProfile . dateStart ;
2024-12-06 18:43:07 +07:00
profile . amount = item . bodyProfile . amount ? ? null ;
profile . amountSpecial = item . bodyProfile . amountSpecial ? ? null ;
2024-10-09 15:45:42 +07:00
await this . profileRepository . save ( profile ) ;
setLogDataDiff ( req , { before , after : profile } ) ;
}
2024-10-10 14:35:26 +07:00
if ( profile && profile . id ) {
2024-10-08 16:35:08 +07:00
//Educations
2024-10-10 14:35:26 +07:00
if ( item . bodyEducations && item . bodyEducations . length > 0 ) {
2024-10-10 13:07:10 +07:00
await Promise . all (
item . bodyEducations . map ( async ( education ) = > {
const profileEdu = new ProfileEducation ( ) ;
Object . assign ( profileEdu , { . . . education , . . . meta } ) ;
const eduHistory = new ProfileEducationHistory ( ) ;
Object . assign ( eduHistory , { . . . profileEdu , id : undefined } ) ;
profileEdu . profileId = profile . id ;
await this . profileEducationRepo . save ( profileEdu , { data : req } ) ;
setLogDataDiff ( req , { before , after : profileEdu } ) ;
eduHistory . profileEducationId = profileEdu . id ;
await this . profileEducationHistoryRepo . save ( eduHistory , { data : req } ) ;
} ) ,
) ;
}
2024-10-10 14:35:26 +07:00
2024-10-08 16:35:08 +07:00
//Certificates
2024-10-10 14:35:26 +07:00
if ( item . bodyCertificates && item . bodyCertificates . length > 0 ) {
2024-10-10 13:07:10 +07:00
await Promise . all (
item . bodyCertificates . map ( async ( cer ) = > {
const profileCer = new ProfileCertificate ( ) ;
Object . assign ( profileCer , { . . . cer , . . . meta } ) ;
const cerHistory = new ProfileCertificateHistory ( ) ;
Object . assign ( cerHistory , { . . . profileCer , id : undefined } ) ;
2024-10-10 13:28:56 +07:00
profileCer . profileId = profile . id ;
2024-10-10 13:07:10 +07:00
await this . certificateRepo . save ( profileCer , { data : req } ) ;
setLogDataDiff ( req , { before , after : profileCer } ) ;
cerHistory . profileCertificateId = profileCer . id ;
await this . certificateHistoryRepo . save ( cerHistory , { data : req } ) ;
} ) ,
) ;
2024-10-08 16:35:08 +07:00
}
2024-10-10 13:07:10 +07:00
//Salary
2024-10-10 14:35:26 +07:00
if ( item . bodySalarys && item . bodySalarys != null ) {
2024-10-10 13:07:10 +07:00
const dest_item = await this . salaryRepo . findOne ( {
where : { profileId : profile.id } ,
order : { order : "DESC" } ,
} ) ;
2024-12-12 11:36:11 +07:00
const profileSal : any = new ProfileSalary ( ) ;
2024-10-10 13:07:10 +07:00
Object . assign ( profileSal , { . . . item . bodySalarys , . . . meta } ) ;
const salaryHistory = new ProfileSalaryHistory ( ) ;
Object . assign ( salaryHistory , { . . . profileSal , id : undefined } ) ;
2024-10-10 14:35:26 +07:00
profileSal . order = dest_item == null ? 1 : dest_item.order + 1 ;
2024-10-10 13:48:20 +07:00
profileSal . profileId = profile . id ;
2024-10-22 14:23:28 +07:00
profileSal . dateGovernment = meta . createdAt ;
2024-12-06 18:43:07 +07:00
profileSal . amount = item . bodySalarys . amount ? ? null ;
profileSal . amountSpecial = item . bodySalarys . amountSpecial ? ? null ;
profileSal . positionSalaryAmount = item . bodySalarys . positionSalaryAmount ? ? null ;
profileSal . mouthSalaryAmount = item . bodySalarys . mouthSalaryAmount ? ? null ;
2024-10-10 13:07:10 +07:00
await this . salaryRepo . save ( profileSal , { data : req } ) ;
setLogDataDiff ( req , { before , after : profileSal } ) ;
salaryHistory . profileSalaryId = profileSal . id ;
await this . salaryHistoryRepo . save ( salaryHistory , { data : req } ) ;
2024-10-08 16:35:08 +07:00
}
2024-10-10 13:07:10 +07:00
//Position
2024-10-10 14:35:26 +07:00
if ( item . bodyPosition && item . bodyPosition != null ) {
2024-10-10 13:07:10 +07:00
const posMaster = await this . posMasterRepository . findOne ( {
where : { id : item.bodyPosition.posmasterId } ,
} ) ;
2024-10-10 14:35:26 +07:00
if ( posMaster == null )
2024-10-10 13:07:10 +07:00
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
2024-10-09 12:53:13 +07:00
2024-10-10 13:07:10 +07:00
const posMasterOld = await this . posMasterRepository . findOne ( {
where : {
current_holderId : profile.id ,
orgRevisionId : posMaster.orgRevisionId ,
} ,
} ) ;
if ( posMasterOld != null ) posMasterOld . current_holderId = null ;
2024-10-09 12:53:13 +07:00
2024-10-10 13:07:10 +07:00
const positionOld = await this . positionRepository . findOne ( {
where : {
posMasterId : posMasterOld?.id ,
positionIsSelected : true ,
} ,
} ) ;
if ( positionOld != null ) {
positionOld . positionIsSelected = false ;
await this . positionRepository . save ( positionOld ) ;
}
2024-10-09 12:53:13 +07:00
2024-10-10 13:07:10 +07:00
const checkPosition = await this . positionRepository . find ( {
where : {
posMasterId : item.bodyPosition.posmasterId ,
positionIsSelected : true ,
} ,
} ) ;
if ( checkPosition . length > 0 ) {
const clearPosition = checkPosition . map ( ( positions ) = > ( {
. . . positions ,
positionIsSelected : false ,
} ) ) ;
await this . positionRepository . save ( clearPosition ) ;
}
2024-10-10 13:28:56 +07:00
2024-10-10 13:07:10 +07:00
posMaster . current_holderId = profile . id ;
if ( posMasterOld != null ) await this . posMasterRepository . save ( posMasterOld ) ;
await this . posMasterRepository . save ( posMaster ) ;
2024-10-10 13:28:56 +07:00
2024-10-10 13:07:10 +07:00
const positionNew = await this . positionRepository . findOne ( {
where : {
id : item.bodyPosition.positionId ,
posMasterId : item.bodyPosition.posmasterId ,
} ,
} ) ;
if ( positionNew != null ) {
positionNew . positionIsSelected = true ;
profile . posLevelId = positionNew . posLevelId ;
profile . posTypeId = positionNew . posTypeId ;
profile . position = positionNew . positionName ;
2024-11-07 16:16:46 +07:00
profile . dateStart = new Date ( ) ;
2024-10-10 14:35:26 +07:00
await this . profileRepository . save ( profile , { data : req } ) ;
2024-10-10 13:07:10 +07:00
setLogDataDiff ( req , { before , after : profile } ) ;
await this . positionRepository . save ( positionNew , { data : req } ) ;
}
2024-10-08 16:35:08 +07:00
}
}
2024-10-09 12:53:13 +07:00
} ) ,
2024-10-08 16:35:08 +07:00
) ;
return new HttpSuccess ( ) ;
}
2024-10-30 17:46:35 +07:00
@Post ( "command21/employee/report" )
public async command21SalaryEmployee (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
refIds : string [ ] ;
} ,
) {
const profile = await this . profileEmployeeRepository . find ( { where : { id : In ( body . refIds ) } } ) ;
const data = profile . map ( ( _data ) = > ( {
. . . _data ,
statusTemp : "REPORT" ,
} ) ) ;
await this . profileEmployeeRepository . save ( data ) ;
return new HttpSuccess ( ) ;
}
@Post ( "command38/officer/report" )
public async command38SalaryOfficer (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
refIds : string [ ] ;
} ,
) {
const posMasters = await this . posMasterRepository . find ( { where : { id : In ( body . refIds ) } } ) ;
const data = posMasters . map ( ( _data ) = > ( {
. . . _data ,
statusReport : "REPORT" ,
} ) ) ;
await this . posMasterRepository . save ( data ) ;
2024-11-13 11:32:45 +07:00
if ( posMasters && posMasters . length > 0 ) {
const orgRevision = await this . orgRevisionRepo . findOne ( {
2024-11-14 18:03:56 +07:00
where : { id : posMasters [ 0 ] . orgRevisionId } ,
2024-11-13 11:32:45 +07:00
} ) ;
2024-11-13 13:31:41 +07:00
if ( orgRevision != null && orgRevision . isLock == false ) {
2024-11-13 11:32:45 +07:00
await this . orgRevisionRepo . update ( orgRevision . id , {
isLock : true ,
} ) ;
}
}
2024-10-30 17:46:35 +07:00
return new HttpSuccess ( ) ;
}
2024-11-01 14:14:13 +07:00
2024-10-30 17:46:35 +07:00
@Post ( "command40/officer/report" )
public async command40SalaryOfficer (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
refIds : string [ ] ;
} ,
) {
const posMasters = await this . posMasterActRepository . find ( { where : { id : In ( body . refIds ) } } ) ;
const data = posMasters . map ( ( _data ) = > ( {
. . . _data ,
statusReport : "REPORT" ,
} ) ) ;
await this . posMasterActRepository . save ( data ) ;
return new HttpSuccess ( ) ;
}
2024-10-04 22:36:34 +07:00
@Post ( "command21/employee/report/excecute" )
public async command21SalaryEmployeeExcecute (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
refIds : {
refId : string ;
commandAffectDate : Date | null ;
commandNo : string | null ;
2024-10-21 14:21:15 +07:00
commandId? : string | null ;
2024-10-04 22:36:34 +07:00
commandYear : number ;
templateDoc : string | null ;
amount : Double | null ;
2024-12-06 18:43:07 +07:00
amountSpecial : Double | null ;
2024-10-04 22:36:34 +07:00
positionSalaryAmount : Double | null ;
mouthSalaryAmount : Double | null ;
} [ ] ;
} ,
) {
2024-11-12 10:46:33 +07:00
const roleKeycloak = await this . roleKeycloakRepo . findOne ( {
where : { name : Like ( "USER" ) } ,
} ) ;
2024-10-04 22:36:34 +07:00
await Promise . all (
body . refIds . map ( async ( item ) = > {
2024-11-12 10:46:33 +07:00
const profile = await this . profileEmployeeRepository . findOne ( {
where : { id : item.refId } ,
2024-11-14 18:03:56 +07:00
relations : [ "roleKeycloaks" ] ,
} ) ;
2024-10-04 22:36:34 +07:00
if ( ! profile ) {
throw new HttpError ( HttpStatus . BAD_REQUEST , "ไม่พบ profile ดังกล่าว" ) ;
}
const dest_item = await this . salaryRepo . findOne ( {
where : { profileEmployeeId : item.refId } ,
order : { order : "DESC" } ,
} ) ;
const before = null ;
const data = new ProfileSalary ( ) ;
const meta = {
profileEmployeeId : profile.id ,
date : new Date ( ) ,
amount : item.amount ,
2024-12-06 18:43:07 +07:00
amountSpecial : item.amountSpecial ,
2024-10-22 16:09:46 +07:00
commandId : item.commandId ,
2024-10-04 22:36:34 +07:00
positionSalaryAmount : item.positionSalaryAmount ,
mouthSalaryAmount : item.mouthSalaryAmount ,
posNo : profile.posMasterNoTemp ,
position : profile.positionTemp ,
positionType : profile.posTypeNameTemp ,
positionLevel : profile.posLevelNameTemp ,
refCommandNo : ` ${ item . commandNo } / ${ Extension . ToThaiYear ( item . commandYear ) } ` ,
templateDoc : item.templateDoc ,
order : dest_item == null ? 1 : dest_item.order + 1 ,
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
} ;
Object . assign ( data , meta ) ;
const history = new ProfileSalaryHistory ( ) ;
Object . assign ( history , { . . . data , id : undefined } ) ;
await this . salaryRepo . save ( data , { data : req } ) ;
setLogDataDiff ( req , { before , after : data } ) ;
history . profileSalaryId = data . id ;
await this . salaryHistoryRepo . save ( history , { data : req } ) ;
const posMaster = await this . employeePosMasterRepository . findOne ( {
where : { id : profile.posmasterIdTemp } ,
relations : [ "orgRoot" ] ,
} ) ;
if ( posMaster == null )
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลตำแหน่งนี้" ) ;
const posMasterOld = await this . employeePosMasterRepository . findOne ( {
where : {
current_holderId : profile.id ,
orgRevisionId : posMaster.orgRevisionId ,
} ,
} ) ;
if ( posMasterOld != null ) posMasterOld . current_holderId = null ;
2024-11-14 18:03:56 +07:00
// if (posMasterOld != null) posMasterOld.next_holderId = null;
2024-10-04 22:36:34 +07:00
const positionOld = await this . employeePositionRepository . findOne ( {
where : {
posMasterId : posMasterOld?.id ,
positionIsSelected : true ,
} ,
} ) ;
if ( positionOld != null ) {
positionOld . positionIsSelected = false ;
await this . employeePositionRepository . save ( positionOld ) ;
}
const checkPosition = await this . employeePositionRepository . find ( {
where : {
posMasterId : profile.posmasterIdTemp ,
positionIsSelected : true ,
} ,
} ) ;
if ( checkPosition . length > 0 ) {
const clearPosition = checkPosition . map ( ( positions ) = > ( {
. . . positions ,
positionIsSelected : false ,
} ) ) ;
await this . employeePositionRepository . save ( clearPosition ) ;
}
posMaster . current_holderId = profile . id ;
2024-11-14 18:03:56 +07:00
posMaster . next_holderId = null ;
2024-10-04 22:36:34 +07:00
if ( posMasterOld != null ) await this . employeePosMasterRepository . save ( posMasterOld ) ;
await this . employeePosMasterRepository . save ( posMaster ) ;
const positionNew = await this . employeePositionRepository . findOne ( {
where : {
id : profile.positionIdTemp ,
posMasterId : profile.posmasterIdTemp ,
} ,
} ) ;
2024-11-05 17:26:42 +07:00
2024-10-04 22:36:34 +07:00
if ( positionNew != null ) {
2024-11-05 17:26:42 +07:00
// Create Keycloak
const userKeycloakId = await createUser ( profile . citizenId , profile . citizenId , {
firstName : profile.firstName ,
lastName : profile.lastName ,
// email: profile.email,
} ) ;
2024-11-22 11:34:11 +07:00
// if (typeof userKeycloakId !== "string") {
// throw new Error(userKeycloakId.errorMessage);
// }
2024-11-05 17:26:42 +07:00
const list = await getRoles ( ) ;
if ( ! Array . isArray ( list ) )
throw new Error ( "Failed. Cannot get role(s) data from the server." ) ;
const result = await addUserRoles (
userKeycloakId ,
list
. filter ( ( v ) = > v . name === "USER" )
. map ( ( x ) = > ( {
id : x.id ,
name : x.name ,
} ) ) ,
) ;
2024-11-22 11:34:11 +07:00
// if (!result) throw new Error("Failed. Cannot set user's role.");
profile . keycloak = typeof userKeycloakId == "string" ? userKeycloakId : "" ;
profile . roleKeycloaks = result && roleKeycloak ? [ roleKeycloak ] : [ ] ;
2024-11-05 17:26:42 +07:00
// End Create Keycloak
2024-10-04 22:36:34 +07:00
positionNew . positionIsSelected = true ;
profile . posLevelId = positionNew . posLevelId ;
profile . posTypeId = positionNew . posTypeId ;
profile . position = positionNew . positionName ;
profile . employeeOc = posMaster ? . orgRoot ? . orgRootName ? ? null ;
profile . positionEmployeePositionId = positionNew . positionName ;
profile . statusTemp = "DONE" ;
profile . employeeClass = "PERM" ;
const _null : any = null ;
profile . employeeWage = item . amount == null ? _null : item.amount.toString ( ) ;
2024-11-07 16:16:46 +07:00
profile . dateStart = new Date ( ) ;
2024-12-12 17:44:57 +07:00
profile . dateAppoint = new Date ( ) ;
2024-12-06 18:43:07 +07:00
profile . amount = item . amount == null ? _null : item.amount ;
profile . amountSpecial = item . amountSpecial == null ? _null : item.amountSpecial ;
2024-10-04 22:36:34 +07:00
await this . profileEmployeeRepository . save ( profile ) ;
await this . employeePositionRepository . save ( positionNew ) ;
}
} ) ,
) ;
return new HttpSuccess ( ) ;
}
2024-10-30 17:46:35 +07:00
2024-10-28 09:47:24 +07:00
@Post ( "command21/employee/report/delete" )
2024-10-11 10:10:50 +07:00
public async command21SalaryEmployeeDelete (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
refIds : string [ ] ;
} ,
) {
const profile = await this . profileEmployeeRepository . find ( { where : { id : In ( body . refIds ) } } ) ;
const data = profile . map ( ( _data ) = > ( {
. . . _data ,
2024-10-28 09:47:24 +07:00
statusTemp : "PENDING" ,
2024-10-11 10:10:50 +07:00
} ) ) ;
await this . profileEmployeeRepository . save ( data ) ;
return new HttpSuccess ( ) ;
}
2024-10-04 22:36:34 +07:00
@Post ( "command40/officer/report/excecute" )
public async command40SalaryOfficerExcecute (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
refIds : {
refId : string ;
commandAffectDate : Date | null ;
commandNo : string | null ;
2024-10-21 14:21:15 +07:00
commandId? : string | null ;
2024-10-04 22:36:34 +07:00
commandYear : number ;
templateDoc : string | null ;
amount : Double | null ;
positionSalaryAmount : Double | null ;
mouthSalaryAmount : Double | null ;
} [ ] ;
} ,
) {
2024-10-23 00:31:00 +07:00
const posMasters = await this . posMasterActRepository . find ( {
where : { id : In ( body . refIds . map ( ( x ) = > x . refId ) ) } ,
} ) ;
const data = posMasters . map ( ( _data ) = > ( {
. . . _data ,
statusReport : "PENDING" ,
} ) ) ;
await this . posMasterActRepository . save ( data ) ;
2024-10-04 22:36:34 +07:00
return new HttpSuccess ( ) ;
}
2024-10-30 17:46:35 +07:00
2024-10-28 11:48:00 +07:00
@Post ( "command40/officer/report/delete" )
2024-10-11 10:10:50 +07:00
public async command40SalaryOfficerDelete (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
refIds : string [ ] ;
} ,
) {
2024-10-23 00:31:00 +07:00
const posMasters = await this . posMasterActRepository . find ( { where : { id : In ( body . refIds ) } } ) ;
const data = posMasters . map ( ( _data ) = > ( {
. . . _data ,
statusReport : "PENDING" ,
} ) ) ;
await this . posMasterActRepository . save ( data ) ;
2024-10-04 22:36:34 +07:00
return new HttpSuccess ( ) ;
}
@Post ( "command40/officer/report/attachment" )
public async command40SalaryOfficerAttachment (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
2024-10-28 14:46:55 +07:00
refIds : {
refId? : string ;
2024-10-28 15:30:47 +07:00
Sequence? : any | null ;
CitizenId? : any | null ;
Prefix? : any | null ;
FirstName? : any | null ;
LastName? : any | null ;
Amount? : any | null ;
PositionSalaryAmount? : any | null ;
MouthSalaryAmount? : any | null ;
RemarkHorizontal? : any | null ;
RemarkVertical? : any | null ;
CommandYear? : any | null ;
2024-11-20 18:57:03 +07:00
CommandExcecuteDate? : Date | null ;
2024-10-29 12:23:37 +07:00
} [ ] ;
2024-10-04 22:36:34 +07:00
} ,
) {
let data : any = [ ] ;
await Promise . all (
body . refIds . map ( async ( item , i ) = > {
const posMasterAct = await this . posMasterActRepository . findOne ( {
relations : [
"posMaster" ,
"posMaster.orgRoot" ,
"posMaster.orgChild1" ,
"posMaster.orgChild2" ,
"posMaster.orgChild3" ,
"posMaster.orgChild4" ,
"posMaster.current_holder" ,
"posMasterChild" ,
"posMasterChild.orgRoot" ,
"posMasterChild.orgChild1" ,
"posMasterChild.orgChild2" ,
"posMasterChild.orgChild3" ,
"posMasterChild.orgChild4" ,
"posMasterChild.current_holder" ,
"posMasterChild.current_holder.posLevel" ,
"posMasterChild.current_holder.posType" ,
] ,
where : {
2024-10-28 14:46:55 +07:00
id : item.refId ,
2024-10-04 22:36:34 +07:00
} ,
} ) ;
if ( ! posMasterAct ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลประเภทตำแหน่งนี้" ) ;
}
const organization = [
posMasterAct . posMasterChild ? . current_holder ? . position ? ? null ,
posMasterAct . posMasterChild ? . orgChild4 ? . orgChild4Name ? ? null ,
posMasterAct . posMasterChild ? . orgChild3 ? . orgChild3Name ? ? null ,
posMasterAct . posMasterChild ? . orgChild2 ? . orgChild2Name ? ? null ,
posMasterAct . posMasterChild ? . orgChild1 ? . orgChild1Name ? ? null ,
posMasterAct . posMasterChild ? . orgRoot ? . orgRootName ? ? null ,
] ;
const _organization = organization
. filter ( ( part ) = > part !== undefined && part !== null )
. join ( "/" ) ;
const organizationNew = [
posMasterAct . posMaster ? . current_holder ? . position ? ? null ,
posMasterAct . posMaster ? . orgChild4 ? . orgChild4Name ? ? null ,
posMasterAct . posMaster ? . orgChild3 ? . orgChild3Name ? ? null ,
posMasterAct . posMaster ? . orgChild2 ? . orgChild2Name ? ? null ,
posMasterAct . posMaster ? . orgChild1 ? . orgChild1Name ? ? null ,
posMasterAct . posMaster ? . orgRoot ? . orgRootName ? ? null ,
] ;
const _organizationNew = organizationNew
. filter ( ( part ) = > part !== undefined && part !== null )
. join ( "/" ) ;
var _data = {
no : Extension.ToThaiNumber ( ( i + 1 ) . toString ( ) ) ,
2024-11-05 22:45:44 +07:00
fullName : ` ${ item . Prefix ? ? "" } ${ item . FirstName ? ? "" } ${ item . LastName ? ? "" } ` ,
2024-10-29 12:23:37 +07:00
oc :
2024-11-06 09:50:58 +07:00
/ * ( p o s M a s t e r A c t . p o s M a s t e r C h i l d ? . c u r r e n t _ h o l d e r ? . p o s i t i o n ? ? " - " ) +
"/" + * /
2024-11-14 18:03:56 +07:00
_organization ? ? "-" ,
2024-10-28 14:46:55 +07:00
postype : posMasterAct.posMasterChild?.current_holder?.posType?.posTypeName ? ? "-" ,
poslevel : posMasterAct.posMasterChild?.current_holder?.posLevel?.posLevelName ? ? "-" ,
2024-11-05 22:45:44 +07:00
organizationNew :
2024-11-06 09:50:58 +07:00
/ * ( p o s M a s t e r A c t . p o s M a s t e r ? . c u r r e n t _ h o l d e r ? . p o s i t i o n ? ? " - " ) +
"/" + * /
2024-11-14 18:03:56 +07:00
_organizationNew ? ? "-" ,
2024-10-04 22:36:34 +07:00
// date: Extension.ToThaiShortDate_noPrefix(new Date()),
2024-11-20 18:57:03 +07:00
dateStart : item.CommandExcecuteDate
? Extension . ToThaiShortDate ( item . CommandExcecuteDate )
: "-" ,
2024-10-28 14:46:55 +07:00
dateEnd : "-" ,
2024-10-04 22:36:34 +07:00
order :
posMasterAct . posMasterOrder == null
? "-"
: "ลำดับที่ " + Extension . ToThaiNumber ( posMasterAct . posMasterOrder . toString ( ) ) ,
} ;
data . push ( _data ) ;
} ) ,
) ;
return new HttpSuccess ( data ) ;
}
@Post ( "command38/officer/report/excecute" )
public async command38SalaryOfficerExcecute (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
refIds : {
refId : string ;
commandAffectDate : Date | null ;
commandNo : string | null ;
2024-10-21 14:21:15 +07:00
commandId? : string | null ;
2024-10-04 22:36:34 +07:00
commandYear : number ;
templateDoc : string | null ;
amount : Double | null ;
positionSalaryAmount : Double | null ;
mouthSalaryAmount : Double | null ;
} [ ] ;
} ,
) {
2024-10-17 22:11:54 +07:00
await Promise . all (
body . refIds . map ( async ( item ) = > {
2024-10-30 17:46:35 +07:00
const posMaster = await this . posMasterRepository . findOne ( {
2024-10-17 22:11:54 +07:00
where : { id : item.refId } ,
2024-10-30 17:46:35 +07:00
relations : [
"orgRoot" ,
"orgChild1" ,
"orgChild2" ,
"orgChild3" ,
"orgChild4" ,
"current_holder" ,
"current_holder.posLevel" ,
"current_holder.posType" ,
] ,
2024-10-17 22:11:54 +07:00
} ) ;
2024-10-30 17:46:35 +07:00
if ( ! posMaster ) {
throw new HttpError ( HttpStatus . BAD_REQUEST , "ไม่พบตำแหน่งดังกล่าว" ) ;
}
2024-11-01 14:14:13 +07:00
if ( posMaster . next_holderId != null ) {
2024-10-30 17:46:35 +07:00
const shortName =
2024-11-01 14:14:13 +07:00
posMaster != null && posMaster . orgChild4 != null
2024-10-30 17:46:35 +07:00
? ` ${ posMaster . orgChild4 . orgChild4ShortName } ${ posMaster . posMasterNo } `
: posMaster != null && posMaster . orgChild3 != null
? ` ${ posMaster . orgChild3 . orgChild3ShortName } ${ posMaster . posMasterNo } `
: posMaster != null && posMaster . orgChild2 != null
? ` ${ posMaster . orgChild2 . orgChild2ShortName } ${ posMaster . posMasterNo } `
: posMaster != null && posMaster . orgChild1 != null
? ` ${ posMaster . orgChild1 . orgChild1ShortName } ${ posMaster . posMasterNo } `
: posMaster != null && posMaster ? . orgRoot != null
? ` ${ posMaster . orgRoot . orgRootShortName } ${ posMaster . posMasterNo } `
: null ;
const profile = await this . profileRepository . findOne ( {
2024-11-01 14:14:13 +07:00
where : { id : posMaster.next_holderId } ,
2024-10-30 17:46:35 +07:00
} ) ;
const position = await this . positionRepository . findOne ( {
2024-11-01 14:14:13 +07:00
where : {
2024-10-30 17:46:35 +07:00
posMasterId : posMaster.id ,
2024-11-01 14:14:13 +07:00
positionIsSelected : true ,
} ,
2024-11-14 18:03:56 +07:00
relations : [ "posType" , "posLevel" ] ,
2024-10-30 17:46:35 +07:00
} ) ;
const dest_item = await this . salaryRepo . findOne ( {
where : { profileId : profile?.id } ,
order : { order : "DESC" } ,
} ) ;
const before = null ;
const data = new ProfileSalary ( ) ;
const meta = {
profileId : profile?.id ,
date : new Date ( ) ,
amount : item.amount ,
commandId : item.commandId ,
positionSalaryAmount : item.positionSalaryAmount ,
mouthSalaryAmount : item.mouthSalaryAmount ,
posNo : shortName ? ? null ,
position : position?.positionName ? ? null ,
2024-11-12 14:09:20 +07:00
positionType : position?.posType.posTypeName ? ? null ,
positionLevel : position?.posLevel.posLevelName ? ? null ,
2024-10-30 17:46:35 +07:00
refCommandNo : ` ${ item . commandNo } / ${ Extension . ToThaiYear ( item . commandYear ) } ` ,
templateDoc : item.templateDoc ,
order : dest_item == null ? 1 : dest_item.order + 1 ,
createdUserId : req.user.sub ,
createdFullName : req.user.name ,
lastUpdateUserId : req.user.sub ,
lastUpdateFullName : req.user.name ,
createdAt : new Date ( ) ,
lastUpdatedAt : new Date ( ) ,
} ;
Object . assign ( data , meta ) ;
const history = new ProfileSalaryHistory ( ) ;
Object . assign ( history , { . . . data , id : undefined } ) ;
await this . salaryRepo . save ( data , { data : req } ) ;
setLogDataDiff ( req , { before , after : data } ) ;
history . profileSalaryId = data . id ;
await this . salaryHistoryRepo . save ( history , { data : req } ) ;
if ( profile != null ) {
profile . position = position ? . positionName ? ? "" ;
profile . posTypeId = position ? . posTypeId ? ? "" ;
profile . posLevelId = position ? . posLevelId ? ? "" ;
profile . lastUpdateUserId = req . user . sub ;
profile . lastUpdateFullName = req . user . name ;
profile . lastUpdatedAt = new Date ( ) ;
await this . profileRepository . save ( profile ) ;
}
2024-10-17 22:11:54 +07:00
}
} ) ,
) ;
const posMasters = await this . posMasterRepository . find ( {
where : { id : In ( body . refIds . map ( ( x ) = > x . refId ) ) } ,
} ) ;
const data = posMasters . map ( ( _data ) = > ( {
. . . _data ,
2024-10-30 17:46:35 +07:00
current_holderId : _data.next_holderId ,
next_holderId : null ,
2024-10-17 22:11:54 +07:00
statusReport : "PENDING" ,
} ) ) ;
await this . posMasterRepository . save ( data ) ;
2024-10-04 22:36:34 +07:00
return new HttpSuccess ( ) ;
}
2024-10-30 17:46:35 +07:00
2024-10-17 19:26:33 +07:00
@Post ( "command38/officer/report/delete" )
public async command38SalaryOfficerDelete (
@Request ( ) req : RequestWithUser ,
@Body ( )
body : {
2024-11-12 17:39:14 +07:00
refIds : string [ ] ;
2024-10-17 19:26:33 +07:00
} ,
) {
2024-11-01 14:14:13 +07:00
const posMasters = await this . posMasterRepository . find ( {
2024-11-13 09:07:44 +07:00
where : { id : In ( body . refIds ) } ,
2024-11-01 14:14:13 +07:00
} ) ;
2024-10-17 22:11:54 +07:00
const data = posMasters . map ( ( _data ) = > ( {
. . . _data ,
statusReport : "PENDING" ,
} ) ) ;
await this . posMasterRepository . save ( data ) ;
2024-11-13 11:32:45 +07:00
if ( data && data . length > 0 ) {
const revisionId = data [ 0 ] . orgRevisionId ;
const orgRevision = await this . orgRevisionRepo . findOne ( {
where : { id : revisionId } ,
2024-11-14 18:03:56 +07:00
relations : [ "posMasters" ] ,
2024-11-13 11:32:45 +07:00
} ) ;
2024-11-14 18:03:56 +07:00
if (
orgRevision != null &&
2024-11-15 13:48:24 +07:00
! [ "REPORT" , "DONE" ] . includes (
2024-12-12 11:36:11 +07:00
orgRevision . posMasters . find (
( x ) = > x . statusReport === "REPORT" || x . statusReport === "DONE" ,
) ? . statusReport || "" ,
2024-11-14 18:03:56 +07:00
)
2024-11-13 11:32:45 +07:00
) {
await this . orgRevisionRepo . update ( orgRevision . id , {
isLock : false ,
} ) ;
}
}
2024-10-17 19:26:33 +07:00
return new HttpSuccess ( ) ;
}
2024-10-24 10:21:24 +07:00
/ * *
* API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab0
*
* @summary API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab0
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Get ( "register-tab0/{id}" )
async GetByIdTab0Register ( @Path ( ) id : string ) {
const command = await this . commandRepository . findOne ( {
where : { id } ,
} ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
const _command = {
id : command.id ,
isSignature : command.isSignature ,
status : command.status ,
isDraft : command.isDraft ,
isSign : command.isSign ,
isAttachment : command.isAttachment ,
} ;
return new HttpSuccess ( _command ) ;
}
/ * *
* API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab4 ค ำ ส ั ่ ง
*
* @summary API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab4 ค ำ ส ั ่ ง
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Get ( "register-tab4/cover/{id}" )
async GetByIdTab4CoverRegister ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
const command = await this . commandRepository . findOne ( {
where : { id } ,
relations : [ "commandType" ] ,
} ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
let issue =
command . isBangkok == "OFFICE"
? "สำนักปลัดกรุงเทพมหานคร"
: command . isBangkok == "BANGKOK"
? "กรุงเทพมหานคร"
: null ;
if ( issue == null ) {
const orgRevisionActive = await this . orgRevisionRepository . findOne ( {
where : { orgRevisionIsCurrent : true , orgRevisionIsDraft : false } ,
relations : [ "posMasters" , "posMasters.orgRoot" ] ,
} ) ;
if ( orgRevisionActive != null ) {
const profile = await this . profileRepository . findOne ( {
where : {
keycloak : command.createdUserId.toString ( ) ,
} ,
} ) ;
if ( profile != null ) {
issue =
orgRevisionActive ? . posMasters ? . filter ( ( x ) = > x . current_holderId == profile . id ) [ 0 ]
? . orgRoot ? . orgRootName || null ;
}
}
}
if ( issue == null ) issue = "..................................." ;
const _command = {
issue : issue ,
commandNo : command.commandNo == null ? "" : Extension . ToThaiNumber ( command . commandNo ) ,
commandYear :
command . commandYear == null
? ""
: Extension . ToThaiNumber ( Extension . ToThaiYear ( command . commandYear ) . toString ( ) ) ,
commandTitle : command.issue ,
detailHeader : command.detailHeader ,
detailBody : command.detailBody ,
detailFooter : command.detailFooter ,
commandDate :
command . commandAffectDate == null
? ""
: Extension . ToThaiNumber ( Extension . ToThaiFullDate2 ( command . commandAffectDate ) ) ,
commandExcecuteDate :
command . commandExcecuteDate == null
? ""
: Extension . ToThaiNumber ( Extension . ToThaiFullDate2 ( command . commandExcecuteDate ) ) ,
name : "..................................." ,
position : "..................................." ,
authorizedUserFullName : "..................................." ,
authorizedPosition : "..................................." ,
commandAffectDate : "..................................." ,
} ;
return new HttpSuccess ( {
template : command.commandType.fileCover ,
reportName : "docx-report" ,
data : _command ,
} ) ;
}
/ * *
* API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab4 แ น บ ท ้ า ย
*
* @summary API ร า ย ล ะ เ อ ี ย ด ร า ย ก า ร ค ำ ส ั ่ ง tab4 แ น บ ท ้ า ย
*
* @param { string } id Id ค ำ ส ั ่ ง
* /
@Get ( "register-tab4/attachment/{id}" )
async GetByIdTab4AttachmentRegister ( @Path ( ) id : string , @Request ( ) request : RequestWithUser ) {
const command = await this . commandRepository . findOne ( {
where : { id } ,
relations : [ "commandType" , "commandRecives" ] ,
} ) ;
if ( ! command ) {
throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบข้อมูลคำสั่งนี้" ) ;
}
let _command : any = [ ] ;
const path = commandTypePath ( command . commandType . code ) ;
if ( path == null ) throw new HttpError ( HttpStatusCode . NOT_FOUND , "ไม่พบประเภทคำสั่งนี้ในระบบ" ) ;
await new CallAPI ( )
. PostData ( request , path + "/attachment" , {
refIds : command.commandRecives
. filter ( ( x ) = > x . refId != null )
. map ( ( x ) = > ( {
refId : x.refId ,
Sequence : x.order ,
CitizenId : x.citizenId ,
Prefix : x.prefix ,
FirstName : x.firstName ,
LastName : x.lastName ,
Amount : x.amount ,
PositionSalaryAmount : x.positionSalaryAmount ,
MouthSalaryAmount : x.mouthSalaryAmount ,
RemarkHorizontal : x.remarkHorizontal ,
RemarkVertical : x.remarkVertical ,
CommandYear : command.commandYear ,
} ) ) ,
} )
. then ( async ( res ) = > {
_command = res ;
} )
. catch ( ( ) = > { } ) ;
let issue =
command . isBangkok == "OFFICE"
? "สำนักปลัดกรุงเทพมหานคร"
: command . isBangkok == "BANGKOK"
? "กรุงเทพมหานคร"
: null ;
if ( issue == null ) {
const orgRevisionActive = await this . orgRevisionRepository . findOne ( {
where : { orgRevisionIsCurrent : true , orgRevisionIsDraft : false } ,
relations : [ "posMasters" , "posMasters.orgRoot" ] ,
} ) ;
if ( orgRevisionActive != null ) {
const profile = await this . profileRepository . findOne ( {
where : {
keycloak : command.createdUserId.toString ( ) ,
} ,
} ) ;
if ( profile != null ) {
issue =
orgRevisionActive ? . posMasters ? . filter ( ( x ) = > x . current_holderId == profile . id ) [ 0 ]
? . orgRoot ? . orgRootName || null ;
}
}
}
if ( issue == null ) issue = "..................................." ;
return new HttpSuccess ( {
template : command.commandType.fileAttachment ,
reportName : "xlsx-report" ,
data : {
data : _command ,
issuerOrganizationName : issue ,
commandNo : command.commandNo == null ? "" : Extension . ToThaiNumber ( command . commandNo ) ,
commandYear :
command . commandYear == null
? ""
: Extension . ToThaiNumber ( Extension . ToThaiYear ( command . commandYear ) . toString ( ) ) ,
commandExcecuteDate :
command . commandExcecuteDate == null
? ""
: Extension . ToThaiNumber ( Extension . ToThaiFullDate2 ( command . commandExcecuteDate ) ) ,
} ,
} ) ;
}
2024-09-11 17:29:33 +07:00
}