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"; import { Brackets, LessThan, MoreThan, Double, In, Between, IsNull, Not, Like } from "typeorm"; import { CommandType } from "../entities/CommandType"; import { CommandSend } from "../entities/CommandSend"; import { Profile, CreateProfileAllFields } from "../entities/Profile"; import { RequestWithUser } from "../middlewares/user"; import { OrgRevision } from "../entities/OrgRevision"; import { CommandSendCC } from "../entities/CommandSendCC"; import { CommandSalary } from "../entities/CommandSalary"; import { CommandRecive } from "../entities/CommandRecive"; import HttpStatus from "../interfaces/http-status"; import Extension from "../interfaces/extension"; import { ProfileEmployee } from "../entities/ProfileEmployee"; import CallAPI from "../interfaces/call-api"; import { ProfileSalary, CreateProfileSalary } from "../entities/ProfileSalary"; import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory"; import { calculateRetireDate, calculateRetireLaw, commandTypePath, removeProfileInOrganize, setLogDataDiff, checkReturnCommandType, checkExceptCommandType, checkCommandType, } from "../interfaces/utils"; 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"; import { PosMasterAct } from "../entities/PosMasterAct"; import { sendToQueue } from "../services/rabbitmq"; import { PosLevel } from "../entities/PosLevel"; import { PosType } from "../entities/PosType"; import { addUserRoles, createUser, getRoles, deleteUser, enableStatus, getUserByUsername, getRoleMappings, removeUserRoles, } from "../keycloak"; import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation"; import { ProfileEducationHistory } from "../entities/ProfileEducationHistory"; import { CreateProfileCertificate, ProfileCertificate } from "../entities/ProfileCertificate"; import { ProfileCertificateHistory } from "../entities/ProfileCertificateHistory"; import permission from "../interfaces/permission"; import { CommandSign } from "../entities/CommandSign"; import { RoleKeycloak } from "../entities/RoleKeycloak"; import axios from "axios"; import querystring from "querystring"; import { SubDistrict } from "../entities/SubDistrict"; import { District } from "../entities/District"; import { Province } from "../entities/Province"; import { ProfileAssistance } from "../entities/ProfileAssistance"; import { ProfileAssistanceHistory } from "../entities/ProfileAssistanceHistory"; import { ProfileActposition } from "../entities/ProfileActposition"; import { ProfileActpositionHistory } from "../entities/ProfileActpositionHistory"; @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); private commandSendRepository = AppDataSource.getRepository(CommandSend); private commandSendCCRepository = AppDataSource.getRepository(CommandSendCC); private commandSalaryRepository = AppDataSource.getRepository(CommandSalary); private commandReciveRepository = AppDataSource.getRepository(CommandRecive); private profileRepository = AppDataSource.getRepository(Profile); private profileEmployeeRepository = AppDataSource.getRepository(ProfileEmployee); private orgRevisionRepo = AppDataSource.getRepository(OrgRevision); 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); private posMasterActRepository = AppDataSource.getRepository(PosMasterAct); 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); private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private commandSignRepository = AppDataSource.getRepository(CommandSign); private roleKeycloakRepo = AppDataSource.getRepository(RoleKeycloak); private provinceRepo = AppDataSource.getRepository(Province); private districtRepo = AppDataSource.getRepository(District); private subDistrictRepo = AppDataSource.getRepository(SubDistrict); private assistanceRepository = AppDataSource.getRepository(ProfileAssistance); private assistanceHistoryRepository = AppDataSource.getRepository(ProfileAssistanceHistory); private actpositionRepository = AppDataSource.getRepository(ProfileActposition); private actpositionHistoryRepository = AppDataSource.getRepository(ProfileActpositionHistory); /** * API list รายการคำสั่ง * * @summary API list รายการคำสั่ง * */ @Get("list") async GetResult( @Request() request: RequestWithUser, @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query() keyword: string = "", @Query() commandTypeId?: string | null, @Query() year?: number, @Query() status?: string | null, ) { await new permission().PermissionList(request, "COMMAND"); 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")) { _data = await new permission().PermissionOrgList(request, "COMMAND"); } 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); } //only search command name let baseKeyword = keyword; let yearKeyword = ""; const regex = /\/(\d{4})$/; const match = keyword.match(regex); if (match) { baseKeyword = keyword.substring(0, keyword.lastIndexOf("/")).trim(); yearKeyword = match[1].trim(); } let yearInBC = yearKeyword ? parseInt(yearKeyword) - 543 : null; // // console.log("k>>",keyword); // console.log("bk>>",baseKeyword); // console.log("yk>>",yearKeyword); // console.log("yi>>",yearInBC); const [commands, total] = await this.commandRepository .createQueryBuilder("command") .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, }); }), ) .andWhere( status != null && status != undefined && status != "" ? "command.status IN (:...status)" : "1=1", { status: status == null || status == undefined || status == "" ? null : status.trim().toLocaleUpperCase() == "NEW" || status.trim().toLocaleUpperCase() == "DRAFT" ? ["NEW", "DRAFT"] : [status.trim().toLocaleUpperCase()], }, ) .andWhere( year != null && year != undefined && year != 0 ? "command.commandYear = :commandYear" : "1=1", { commandYear: year == null || year == undefined || year == 0 ? null : `${year}`, }, ) .andWhere( commandTypeId != null && commandTypeId != undefined && commandTypeId != "" ? "command.commandTypeId = :commandTypeId" : "1=1", { commandTypeId: commandTypeId == null || commandTypeId == undefined || commandTypeId == "" ? null : `${commandTypeId}`, }, ) .andWhere( new Brackets((qb) => { qb.where( keyword != null && keyword != "" ? "command.commandNo LIKE :baseKeyword" : "1=1", { baseKeyword: `%${baseKeyword}%`, }, ) .orWhere(keyword != null && keyword != "" ? "command.issue LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, }) .orWhere( keyword != null && keyword != "" ? "command.createdFullName LIKE :keyword" : "1=1", { keyword: `%${keyword}%`, }, ); }), ) .orderBy("command.createdAt", "DESC") .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, assignFullName: null, //xxxxxxxxxxxxxxx createdFullName: _data.createdFullName, status: _data.status, issue: _data.issue, })); return new HttpSuccess({ data, total }); } /** * API สร้างรายการ body คำสั่ง * * @summary API สร้างรายการ body คำสั่ง * */ @Post() async Post( @Body() requestBody: { commandTypeId: string; commandNo: string | null; commandYear: number | null; }, @Request() request: RequestWithUser, ) { await new permission().PermissionCreate(request, "COMMAND"); 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, "ไม่พบประเภทคำสั่งนี้ในระบบ"); } command.detailHeader = commandType.detailHeader; command.detailBody = commandType.detailBody; command.detailFooter = commandType.detailFooter; command.isAttachment = commandType.isAttachment; command.isUploadAttachment = commandType.isUploadAttachment; command.status = "NEW"; command.issue = commandType.name; 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}") async GetByIdTab1(@Path() id: string, @Request() request: RequestWithUser) { await new permission().PermissionGet(request, "COMMAND"); const command = await this.commandRepository.findOne({ where: { id }, relations: ["commandType", "commandType.commandTypeSys"], }); if (!command) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้"); } 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, isBangkok: command.isBangkok, isAttachment: command.isAttachment, isUploadAttachment: command.isUploadAttachment, commandAffectDate: command.commandAffectDate, commandExcecuteDate: command.commandExcecuteDate, commandTypeName: command.commandType?.name || null, commandCode: command.commandType?.code || null, commandSysId: command.commandType?.commandSysId || null, }; return new HttpSuccess(_command); } /** * 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; commandAffectDate: Date | null; commandExcecuteDate: Date | null; isBangkok: string | null; }, @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 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(); } /** * API รายละเอียดรายการคำสั่ง tab2 * * @summary API รายละเอียดรายการคำสั่ง tab2 * * @param {string} id Id คำสั่ง */ @Get("tab2/{id}") async GetByIdTab2(@Path() id: string, @Request() request: RequestWithUser) { await new permission().PermissionGet(request, "COMMAND"); 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, commandRecives: command.commandRecives .sort((a, b) => a.order - b.order) .map((x) => ({ id: x.id, citizenId: x.citizenId, prefix: x.prefix, firstName: x.firstName, lastName: x.lastName, // profileId: x.profileId, order: x.order, remarkVertical: x.remarkVertical, remarkHorizontal: x.remarkHorizontal, amount: x.amount, amountSpecial: x.amountSpecial, positionSalaryAmount: x.positionSalaryAmount, mouthSalaryAmount: x.mouthSalaryAmount, position: x.position, posType: x.posType, posLevel: x.posLevel, })), }; 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: { positionDetail: string | null; commandSalaryId: string | null; }, @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, "ไม่พบข้อมูลคำสั่งนี้"); } 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, "ไม่พบข้อมูลต้นแบบ"); } } 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(); } @Get("tab2/swap/{direction}/{commandReciveId}") public async swapSalary( @Path() direction: string, commandReciveId: string, @Request() request: RequestWithUser, ) { await new permission().PermissionUpdate(request, "COMMAND"); 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(); } /** * 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; amountSpecial: Double | null; positionSalaryAmount: Double | null; mouthSalaryAmount: Double | null; }, @Request() request: RequestWithUser, ) { await new permission().PermissionUpdate(request, "COMMAND"); 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(); } /** * API ลบรายการผู้ได้รับคำสั่ง * * @summary API ลบรายการผู้ได้รับคำสั่ง * * @param {string} id Id ผู้ได้รับคำสั่ง */ @Delete("tab2/{commandReciveId}") async DeleteTab2(@Path() commandReciveId: string, @Request() request: RequestWithUser) { await new permission().PermissionUpdate(request, "COMMAND"); const commandRecive = await this.commandReciveRepository.findOne({ where: { id: commandReciveId }, relations: ["command", "command.commandType"], }); if (!commandRecive) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ได้รับคำสั่ง"); } const path = commandTypePath(commandRecive.command.commandType.code); if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ"); await new CallAPI() .PostData(request, path + "/delete", { refIds: [commandRecive.refId], }) .then(async (res) => {}) .catch(() => {}); const commandId = commandRecive.commandId; await this.commandReciveRepository.delete(commandRecive.id); 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); }); return new HttpSuccess(); } /** * API รายละเอียดรายการคำสั่ง tab3 * * @summary API รายละเอียดรายการคำสั่ง tab3 * * @param {string} id Id คำสั่ง */ @Get("tab3/{id}") async GetByIdTab3(@Path() id: string, @Request() request: RequestWithUser) { await new permission().PermissionGet(request, "COMMAND"); const command = await this.commandRepository.findOne({ where: { id }, order: { commandSends: { createdAt: "ASC", }, }, relations: ["commandSends", "commandSends.commandSendCCs"], }); if (!command) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้"); } const _command = command.commandSends.map((item) => ({ id: item.id, citizenId: item.citizenId, prefix: item.prefix, firstName: item.firstName, lastName: item.lastName, position: item.position, org: item.org, sendCC: item.commandSendCCs.sort((a, b) => a.name.localeCompare(b.name)).map((x) => x.name), 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, ) { await new permission().PermissionUpdate(request, "COMMAND"); 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; commandSend.firstName = profile.firstName; 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); if (commandSend && commandSend.id) { let _ccName = new Array("EMAIL", "INBOX"); let _dataSendCC = new Array(); for (let i = 0; i < _ccName.length; i++) { _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); } }), ); 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; sendCC: string[]; }[]; }, @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, "ไม่พบข้อมูลคำสั่งนี้"); } await Promise.all( requestBody.commandSend.map(async (item) => { const commandSendCC = await this.commandSendCCRepository.find({ where: { commandSendId: item.id }, }); await this.commandSendCCRepository.remove(commandSendCC); 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); }), ); }), ); 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, ) { await new permission().PermissionUpdate(request, "COMMAND"); const command = await this.commandSendRepository.findOne({ where: { id: commandSendId } }); if (!command) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลผู้ได้รับสำเนาคำสั่ง"); } await this.commandSendCCRepository.delete({ commandSendId: commandSendId }); await this.commandSendRepository.delete(commandSendId); return new HttpSuccess(); } /** * API คัดลอก * * @summary API คัดลอก * * @param {string} id Id คำสั่ง */ @Put("copy/{id}") async PutCopy( @Path() id: string, @Body() requestBody: { commandNo?: string | null; commandYear?: number | null }, @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 copy = new Command(); Object.assign(copy, { ...command, ...requestBody, id: undefined }); const _null: any = null; copy.status = "NEW"; copy.commandAffectDate = _null; copy.commandExcecuteDate = _null; copy.isSignature = _null; copy.isDraft = false; copy.isSign = false; 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); 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 }, @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, "ไม่พบข้อมูลคำสั่งนี้"); } const path = commandTypePath(command.commandType.code); 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(() => {}); await this.commandReciveRepository.delete({ commandId: command.id }); 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(); } /** * API ทำคำสั่งใหม่ * * @summary API ทำคำสั่งใหม่ * * @param {string} id Id คำสั่ง */ @Put("resume/{id}") async PutDraft( @Path() id: string, @Body() requestBody: { reason?: string | null }, @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, "ไม่พบข้อมูลคำสั่งนี้"); } 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 คำสั่ง */ @Delete("{id}") async Delete(@Path() id: string, @Request() request: RequestWithUser) { await new permission().PermissionDelete(request, "COMMAND"); const command = await this.commandRepository.findOne({ where: { id: id }, relations: ["commandType", "commandRecives"], }); if (!command) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้"); } const commandSend = await this.commandSendRepository.find({ where: { commandId: id }, }); const path = commandTypePath(command.commandType.code); 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(() => {}); await this.commandSendCCRepository.delete({ commandSendId: In(commandSend.map((x) => x.id)) }); await this.commandReciveRepository.delete({ commandId: command.id }); await this.commandSendRepository.delete({ commandId: command.id }); await this.commandRepository.delete(command.id); return new HttpSuccess(); } /** * API รายละเอียดรายการคำสั่ง tab0 * * @summary API รายละเอียดรายการคำสั่ง tab0 * * @param {string} id Id คำสั่ง */ @Get("tab0/{id}") async GetByIdTab0(@Path() id: string, @Request() request: RequestWithUser) { await new permission().PermissionGet(request, "COMMAND"); const command = await this.commandRepository.findOne({ where: { id }, relations: ["commandType"], }); 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, isUploadAttachment: command.isUploadAttachment, isSalary: command.commandType ? command.commandType.isSalary : null, }; 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, ) { await new permission().PermissionUpdate(request, "COMMAND"); 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() requestBody: { sign: 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, "ไม่พบข้อมูลคำสั่งนี้"); } command.isDraft = requestBody.sign; 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, ) { await new permission().PermissionUpdate(request, "COMMAND"); 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(); } /** * API ออกคำสั่ง * * @summary API ออกคำสั่ง * * @param {string} id Id คำสั่ง */ @Put("pending/{id}") async PutSelectPending( @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", "commandSends", "commandSends.commandSendCCs"], }); 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); }); let profilesSend = 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() .PostData(request, "/placement/noti/profiles-send", { subject: `${command.issue}`, body: `${command.issue}`, receiverUserIds: profilesSend, payload: "", //แนบไฟล์ }) .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(); } /** * API ออกคำสั่ง * * @summary API ออกคำสั่ง * * @param {string} id Id คำสั่ง */ @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(); } async cronjobCommand(@Request() request?: RequestWithUser) { const today = new Date(); today.setUTCHours(0, 0, 0, 0); const tomorrow = new Date(today); tomorrow.setDate(tomorrow.getDate() + 1); const command = await this.commandRepository.find({ relations: ["commandType", "commandRecives"], where: { commandExcecuteDate: Between(today, tomorrow), status: "WAITING", }, }); let body = { client_id: "gettoken", client_secret: process.env.AUTH_ACCOUNT_SECRET, grant_type: "client_credentials", }; const postData = querystring.stringify(body); // get admin token const response = await axios.post( `${process.env.KC_URL}/realms/${process.env.KC_REALMS}/protocol/openid-connect/token`, postData, { headers: { "Content-Type": "application/x-www-form-urlencoded", api_key: process.env.API_KEY, }, }, ); const adminToken = response.data.access_token; command.forEach(async (x) => { const path = commandTypePath(x.commandType.code); 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: adminToken, }; sendToQueue(msg); }); return new HttpSuccess(); } async cronjobUpdateRetirementStatus() { let body = { client_id: "gettoken", client_secret: process.env.AUTH_ACCOUNT_SECRET, grant_type: "client_credentials", }; const postData = querystring.stringify(body); const response = await axios.post( `${process.env.KC_URL}/realms/${process.env.KC_REALMS}/protocol/openid-connect/token`, postData, { headers: { "Content-Type": "application/x-www-form-urlencoded", api_key: process.env.API_KEY, }, }, ); const adminToken = response.data.access_token; const today = new Date(); today.setUTCHours(0, 0, 0, 0); let type: string = "OFFICER"; try { const response_ = await axios.get( process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`, { headers: { Authorization: `Bearer ${adminToken}`, "Content-Type": "application/json", api_key: process.env.API_KEY, }, }, ); if (response && response_.data.result.length > 0) { let profiles: Profile[] = []; await Promise.all( response_.data.result.map(async (x: any) => { const _profile = await this.profileRepository.findOneBy({ id: x.profileId }); if (_profile) { _profile.isRetirement = true; _profile.isLeave = true; _profile.leaveType = "RETIRE"; _profile.leaveDate = new Date(); _profile.dateLeave = new Date(); _profile.lastUpdatedAt = new Date(); profiles.push(_profile); } }), ); await this.profileRepository.save(profiles); } } catch {} type = "EMPLOYEE"; try { const response_ = await axios.get( process.env.API_URL + `/retirement/update-status/${type}/${today.getFullYear()}`, { headers: { Authorization: `Bearer ${adminToken}`, "Content-Type": "application/json", api_key: process.env.API_KEY, }, }, ); if (response && response_.data.result.length > 0) { let profiles: ProfileEmployee[] = []; await Promise.all( response_.data.result.map(async (x: any) => { const _profileEmp = await this.profileEmployeeRepository.findOneBy({ id: x.profileId }); if (_profileEmp) { _profileEmp.isRetirement = true; _profileEmp.isLeave = true; _profileEmp.leaveType = "RETIRE"; _profileEmp.leaveDate = new Date(); _profileEmp.dateLeave = new Date(); _profileEmp.lastUpdatedAt = new Date(); profiles.push(_profileEmp); } }), ); await this.profileEmployeeRepository.save(profiles); } } catch {} return new HttpSuccess(); } /** * API รายละเอียดรายการคำสั่ง tab4 คำสั่ง * * @summary API รายละเอียดรายการคำสั่ง tab4 คำสั่ง * * @param {string} id Id คำสั่ง */ @Get("tab4/cover/{id}") async GetByIdTab4Cover(@Path() id: string, @Request() request: RequestWithUser) { await new permission().PermissionGet(request, "COMMAND"); const command = await this.commandRepository.findOne({ where: { id }, relations: ["commandType", "commandRecives"], }); if (!command) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้"); } let issue = command.isBangkok == "OFFICE" ? "สำนักปลัดกรุงเทพมหานคร" : command.isBangkok == "BANGKOK" ? "กรุงเทพมหานคร" : null; let orgRevisionActive: any; if (issue == null) { 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: any) => x.current_holderId == profile.id)[0] ?.orgRoot?.orgRootName || null; } } } if (issue == null) issue = "..................................."; let res: any[] = []; if (command.commandRecives.length > 0) { await new CallAPI() .GetData(request, `/probation/report/command10/appoints/${command.commandRecives[0].refId}`) .then((x) => { res = x; }) .catch((x) => {}); } let _command; let commandCode = command && command.commandType ? command.commandType.code : ""; if (!["C-PM-21", "C-PM-23"].includes(commandCode)) { _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: "...................................", 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}` : "๔. ..........................กรรมการ", }; } else { let _persons: any; _persons = await Promise.all( command.commandRecives.map(async (x, idx) => { const profile = await this.profileEmployeeRepository.findOne({ where: { id: x.profileId, }, relations: [ "posLevel", "posType", "current_holders", "current_holders.orgRevision", "current_holders.orgRoot", "current_holders.orgChild1", "current_holders.orgChild2", "current_holders.orgChild3", "current_holders.orgChild4", ], }); const shortName = profile?.current_holders.length == 0 ? null : profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) != null && profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) ?.orgChild4 != null ? `${profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild4.orgChild4ShortName}${profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.posMasterNo}` : profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) != null && profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) ?.orgChild3 != null ? `${profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild3.orgChild3ShortName}${profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.posMasterNo}` : profile?.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive?.id, ) != null && profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id) ?.orgChild2 != null ? `${profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild2.orgChild2ShortName}${profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.posMasterNo}` : profile?.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive?.id, ) != null && profile?.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive?.id, )?.orgChild1 != null ? `${profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgChild1.orgChild1ShortName}${profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.posMasterNo}` : profile?.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive?.id, ) != null && profile?.current_holders.find( (x) => x.orgRevisionId == orgRevisionActive?.id, )?.orgRoot != null ? `${profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.orgRoot.orgRootShortName}${profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive?.id)?.posMasterNo}` : null; const root = profile?.current_holders == null || profile?.current_holders.length == 0 || profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive.id) == null ? null : profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive.id) ?.orgRoot; const child1 = profile?.current_holders == null || profile?.current_holders.length == 0 || profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive.id) == null ? null : profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive.id) ?.orgChild1; const child2 = profile?.current_holders == null || profile?.current_holders.length == 0 || profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive.id) == null ? null : profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive.id) ?.orgChild2; const child3 = profile?.current_holders == null || profile?.current_holders.length == 0 || profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive.id) == null ? null : profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive.id) ?.orgChild3; const child4 = profile?.current_holders == null || profile?.current_holders.length == 0 || profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive.id) == null ? null : profile?.current_holders.find((x) => x.orgRevisionId == orgRevisionActive.id) ?.orgChild4; let _child1 = child1 == null ? "" : `${child1.orgChild1Name}/`; let _child2 = child2 == null ? "" : `${child2.orgChild2Name}/`; let _child3 = child3 == null ? "" : `${child3.orgChild3Name}/`; let _child4 = child4 == null ? "" : `${child4.orgChild4Name}/`; let _root = root == null ? "" : `${root.orgRootName}`; return { no: Extension.ToThaiNumber((idx + 1).toString()), org: `${_child4}${_child3}${_child2}${_child1}${_root}`, fullName: `${x.prefix}${x.firstName} ${x.lastName}`, citizenId: Extension.ToThaiNumber(x.citizenId), position: profile?.position ? profile?.position : "-", posLevel: profile?.posType && profile?.posLevel ? Extension.ToThaiNumber( `${profile?.posType.posTypeShortName} ${profile?.posLevel.posLevelName}`, ) : "-", posNo: shortName ? Extension.ToThaiNumber(shortName) : "-", amount: x.amount ? Extension.ToThaiNumber(x.amount.toString()) : "-", dateRetire: profile?.dateRetire ? Extension.ToThaiNumber(Extension.ToThaiShortDate_monthYear(profile?.dateRetire)) : "-", dateExecute: command.commandExcecuteDate ? Extension.ToThaiNumber( Extension.ToThaiShortDate_monthYear(command.commandExcecuteDate), ) : "-", remark: x.remarkVertical ? x.remarkVertical : "-", }; }), ); _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: "...................................", 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}` : "๔. ..........................กรรมการ", persons: _persons, }; } return new HttpSuccess({ template: command.commandType.fileCover, reportName: "docx-report", data: _command, }); } /** * API รายละเอียดรายการคำสั่ง tab4 แนบท้าย * * @summary API รายละเอียดรายการคำสั่ง tab4 แนบท้าย * * @param {string} id Id คำสั่ง */ @Get("tab4/attachment/{id}") async GetByIdTab4Attachment(@Path() id: string, @Request() request: RequestWithUser) { await new permission().PermissionGet(request, "COMMAND"); 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, CommandExcecuteDate: command.commandExcecuteDate, })), }) .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)), }, }); } /** * 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"], order: { commandSigns: { createdAt: "ASC", }, }, }); 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, comment: item.comment, isSignatory: item.isSignatory, })); 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); if (commandSign.isSignatory == true) await this.PutSelectPending(commandSign.commandId, { sign: true }, request); return new HttpSuccess(); } /** * API สร้างรายการ body คำสั่ง * * @summary API สร้างรายการ body คำสั่ง * */ @Post("person") async PostPerson( @Body() requestBody: { commandTypeId?: string | null; commandNo?: string | null; commandYear?: number | null; commandId?: string | null; commandAffectDate?: Date | null; commandExcecuteDate?: Date | null; persons: { refId: string; profileId?: string | null; citizenId?: string | null; prefix?: string | null; firstName?: string | null; lastName?: string | null; remarkVertical?: string | null; remarkHorizontal?: string | null; rootId?: string | null; amount?: Double | null; amountSpecial?: Double | null; positionSalaryAmount?: Double | null; mouthSalaryAmount?: Double | null; position?: string | null; posType?: string | null; posLevel?: string | null; }[]; }, @Request() request: RequestWithUser, ) { let command = new Command(); let commandCode: string = ""; let _null: any = null; if ( requestBody.commandId != undefined && requestBody.commandId != null && requestBody.commandId != "" ) { const _command = await this.commandRepository.findOne({ where: { id: requestBody.commandId }, relations: ["commandRecives", "commandType"], order: { commandRecives: { order: "DESC", }, }, }); if (!_command) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบคำสั่งนี้ในระบบ"); } commandCode = _command.commandType.code; command = _command; } else { command = Object.assign(new Command(), requestBody); if (!requestBody.commandTypeId) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่ง"); } const commandType = await this.commandTypeRepository.findOne({ where: { id: requestBody.commandTypeId }, }); if (!commandType) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ"); } commandCode = commandType.code; command.detailHeader = commandType.detailHeader; command.detailBody = commandType.detailBody; command.detailFooter = commandType.detailFooter; command.isAttachment = commandType.isAttachment; command.isUploadAttachment = commandType.isUploadAttachment; command.status = "NEW"; command.issue = commandType.name; (command.commandAffectDate = requestBody.commandAffectDate ? new Date(requestBody.commandAffectDate) : _null), (command.commandExcecuteDate = requestBody.commandExcecuteDate ? new Date(requestBody.commandExcecuteDate) : _null), (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); } const path = commandTypePath(commandCode); if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ"); await new CallAPI() .PostData(request, path, { refIds: requestBody.persons.filter((x) => x.refId != null).map((x) => x.refId), }) .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; let salaryData = _null; // 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({ where: { id: item.profileId, }, }); let _setZero: any = 0; if (!salaryData) { salaryData = await this.profileEmployeeRepository.findOne({ where: { id: item.profileId, }, }); } commandRecive.amount = item.amount ?? (salaryData ? salaryData.amount : _setZero); commandRecive.amountSpecial = item.amountSpecial ?? (salaryData ? salaryData.amountSpecial : _setZero); commandRecive.positionSalaryAmount = item.positionSalaryAmount ?? (salaryData ? salaryData.positionSalaryAmount : _setZero); commandRecive.mouthSalaryAmount = item.mouthSalaryAmount ?? (salaryData ? salaryData.mouthSalaryAmount : _setZero); } else { commandRecive.amount = _null; commandRecive.amountSpecial = _null; commandRecive.positionSalaryAmount = _null; commandRecive.mouthSalaryAmount = _null; } commandRecive.remarkVertical = item.remarkVertical == null ? _null : item.remarkVertical; commandRecive.remarkHorizontal = item.remarkHorizontal == null ? _null : item.remarkHorizontal; commandRecive.order = order; 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(() => {}); if (!["C-PM-10"].includes(commandCode)) { if (requestBody.persons != undefined && requestBody.persons.length > 0) { let posMaster: any; if (["C-PM-36", "C-PM-37"].includes(commandCode)) { posMaster = await this.employeePosMasterRepository.find({ where: { current_holderId: In(requestBody.persons.map((x) => x.profileId)), orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, }, select: ["orgRootId"], }); } else { var posMasterOfficer = await this.posMasterRepository.find({ where: { current_holderId: In(requestBody.persons.map((x) => x.profileId)), orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, }, select: ["orgRootId"], }); var posMasterEmployee = await this.employeePosMasterRepository.find({ where: { current_holderId: In(requestBody.persons.map((x) => x.profileId)), orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, }, select: ["orgRootId"], }); posMaster = [...posMasterOfficer, ...posMasterEmployee]; } let _posMaster: any; if (["C-PM-38", "C-PM-40"].includes(commandCode)) { _posMaster = await this.posMasterRepository.find({ where: { orgRootId: In(posMaster.map((x: any) => 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"], }); } else { _posMaster = await this.posMasterRepository.find({ where: { orgRootId: In(posMaster.map((x: any) => x.orgRootId)), orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, isDirector: true, current_holderId: Not(IsNull()), }, relations: ["current_holder", "orgRoot"], }); } await Promise.all( _posMaster.map(async (item: any) => { const _commandSend = await this.commandSendRepository.findOne({ where: { commandId: command.id, profileId: item.current_holder.id, }, }); 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); if (commandSend && commandSend.id) { let _ccName = new Array("EMAIL", "INBOX"); let _dataSendCC = new Array(); for (let i = 0; i < _ccName.length; i++) { _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); } }), ); const _posMasterNext = await this.posMasterRepository.find({ where: { orgRootId: In( requestBody.persons .filter((x) => x.rootId != undefined && x.rootId != null && x.rootId != "") .map((x) => x.rootId), ), 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, }, }); // if (_commandSend) return; if (!_commandSend) { 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); if (commandSend && commandSend.id) { let _ccName = new Array("EMAIL", "INBOX"); let _dataSendCC = new Array(); for (let i = 0; i < _ccName.length; i++) { _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); } } }), ); } } if (["C-PM-10"].includes(commandCode)) { await Promise.all( requestBody.persons.map(async (item: any) => { const _posMasterDirector = await this.posMasterRepository.findOne({ where: { orgRootId: item.rootId, orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, current_holderId: item.profileId, }, relations: ["current_holder", "orgRoot", "positions"], }); const _commandSend = await this.commandSendRepository.findOne({ where: { commandId: command.id, profileId: item.profileId ?? _null, }, }); if (!_commandSend) { let commandSend = new CommandSend(); commandSend.citizenId = item.citizenId; commandSend.prefix = item.prefix; commandSend.firstName = item.firstName; commandSend.lastName = item.lastName; commandSend.position = _posMasterDirector?.current_holder.position ?? _null; commandSend.org = _posMasterDirector && _posMasterDirector.orgRoot ? _posMasterDirector.orgRoot.orgRootName : _null; commandSend.profileId = item.profileId; 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); if (commandSend && commandSend.id) { let _ccName = new Array("EMAIL", "INBOX"); let _dataSendCC = new Array(); for (let i = 0; i < _ccName.length; i++) { _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); } } }), ); } if ( [ "C-PM-01", "C-PM-02", "C-PM-03", "C-PM-04", "C-PM-05", "C-PM-07", "C-PM-08", "C-PM-09", "C-PM-13", "C-PM-14", "C-PM-15", "C-PM-16", "C-PM-21", "C-PM-22", "C-PM-24", "C-PM-39", ].includes(commandCode) ) { const _posMasterCommission = await this.posMasterRepository.findOne({ where: { orgRoot: { isCommission: true, }, isDirector: true, orgChild1: IsNull(), orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, current_holderId: Not(IsNull()), }, relations: ["current_holder", "orgRoot", "positions"], }); let _commandSend = await this.commandSendRepository.findOne({ where: { commandId: command.id, profileId: _posMasterCommission?.current_holder.id ?? _null, }, }); if (!_commandSend && _posMasterCommission != null) { let commandSend = new CommandSend(); commandSend.citizenId = _posMasterCommission?.current_holder.citizenId ?? _null; commandSend.prefix = _posMasterCommission?.current_holder.prefix ?? _null; commandSend.firstName = _posMasterCommission?.current_holder.firstName ?? _null; commandSend.lastName = _posMasterCommission?.current_holder.lastName ?? _null; commandSend.position = _posMasterCommission?.current_holder.position ?? _null; commandSend.org = _posMasterCommission && _posMasterCommission.orgRoot ? _posMasterCommission.orgRoot.orgRootName : _null; commandSend.profileId = _posMasterCommission?.current_holderId ?? _null; 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); if (commandSend && commandSend.id) { let _ccName = new Array("EMAIL", "INBOX"); let _dataSendCC = new Array(); for (let i = 0; i < _ccName.length; i++) { _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); } } const _posMasterInformation = await this.posMasterRepository.findOne({ where: { orgChild1: { isInformation: true, }, isDirector: true, orgChild2: IsNull(), orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, current_holderId: Not(IsNull()), }, relations: ["current_holder", "orgRoot", "positions"], }); _commandSend = await this.commandSendRepository.findOne({ where: { commandId: command.id, profileId: _posMasterInformation?.current_holder.id ?? _null, }, }); if (!_commandSend && _posMasterInformation != null) { let commandSend = new CommandSend(); commandSend.citizenId = _posMasterInformation?.current_holder.citizenId ?? _null; commandSend.prefix = _posMasterInformation?.current_holder.prefix ?? _null; commandSend.firstName = _posMasterInformation?.current_holder.firstName ?? _null; commandSend.lastName = _posMasterInformation?.current_holder.lastName ?? _null; commandSend.position = _posMasterInformation?.current_holder.position ?? _null; commandSend.org = _posMasterInformation && _posMasterInformation.orgRoot ? _posMasterInformation.orgRoot.orgRootName : _null; commandSend.profileId = _posMasterInformation?.current_holderId ?? _null; 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); if (commandSend && commandSend.id) { let _ccName = new Array("EMAIL", "INBOX"); let _dataSendCC = new Array(); for (let i = 0; i < _ccName.length; i++) { _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); } } const _posMasterOfficer = await this.posMasterRepository.findOne({ where: { orgChild1: { isOfficer: true, }, isDirector: true, orgChild2: IsNull(), orgRevision: { orgRevisionIsDraft: false, orgRevisionIsCurrent: true }, current_holderId: Not(IsNull()), }, relations: ["current_holder", "orgRoot", "positions"], }); _commandSend = await this.commandSendRepository.findOne({ where: { commandId: command.id, profileId: _posMasterOfficer?.current_holder.id ?? _null, }, }); if (!_commandSend && _posMasterOfficer != null) { let commandSend = new CommandSend(); commandSend.citizenId = _posMasterOfficer?.current_holder.citizenId ?? _null; commandSend.prefix = _posMasterOfficer?.current_holder.prefix ?? _null; commandSend.firstName = _posMasterOfficer?.current_holder.firstName ?? _null; commandSend.lastName = _posMasterOfficer?.current_holder.lastName ?? _null; commandSend.position = _posMasterOfficer?.current_holder.position ?? _null; commandSend.org = _posMasterOfficer && _posMasterOfficer.orgRoot ? _posMasterOfficer.orgRoot.orgRootName : _null; commandSend.profileId = _posMasterOfficer?.current_holderId ?? _null; 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); if (commandSend && commandSend.id) { let _ccName = new Array("EMAIL", "INBOX"); let _dataSendCC = new Array(); for (let i = 0; i < _ccName.length; i++) { _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); } } } return new HttpSuccess(command.id); } @Post("excexute/salary-current") public async newSalaryAndUpdateCurrent( @Request() req: RequestWithUser, @Body() body: { data: { profileId: string; amount?: Double | null; amountSpecial?: Double | null; positionSalaryAmount?: Double | null; mouthSalaryAmount?: Double | null; positionExecutive: string | null; positionType: string | null; positionLevel: string | null; posmasterId: string; positionId: string; commandId?: string | null; orgRoot?: string | null; orgChild1?: string | null; orgChild2?: string | null; orgChild3?: string | null; orgChild4?: string | null; commandNo: string | null; commandYear: number | null; posNo: string | null; posNoAbb: string | null; commandDateAffect?: Date | null; commandDateSign?: Date | null; positionName: string | null; commandCode?: string | null; commandName?: string | null; remark: string | null; }[]; }, ) { await Promise.all( body.data.map(async (item) => { const profile: any = await this.profileRepository.findOneBy({ id: item.profileId }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); } let _null: any = null; 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 }); history.commandId = item.commandId ?? _null; history.profileSalaryId = data.id; 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; profile.amount = item.amount ?? null; profile.amountSpecial = item.amountSpecial ?? null; 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; amount?: Double | null; amountSpecial?: Double | null; positionSalaryAmount?: Double | null; mouthSalaryAmount?: Double | null; positionType: string | null; positionLevel: string | null; posmasterId: string; positionId: string; commandId?: string | null; orgRoot?: string | null; orgChild1?: string | null; orgChild2?: string | null; orgChild3?: string | null; orgChild4?: string | null; commandNo: string | null; commandYear: number | null; posNo: string | null; posNoAbb: string | null; commandDateAffect?: Date | null; commandDateSign?: Date | null; positionName: string | null; commandCode?: string | null; commandName?: string | null; remark: string | null; }[]; }, ) { await Promise.all( body.data.map(async (item) => { const profile: any = 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(), }; Object.assign(data, { ...item, ...meta, profileEmployeeId: item.profileId, profileId: undefined, }); 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; // if (posMasterOld != null) posMasterOld.next_holderId = null; 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; posMaster.next_holderId = null; 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; profile.amount = item.amount ?? null; profile.amountSpecial = item.amountSpecial ?? null; 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; amount?: Double | null; amountSpecial?: Double | null; positionSalaryAmount?: Double | null; mouthSalaryAmount?: Double | null; positionExecutive: string | null; positionType: string | null; positionLevel: string | null; isLeave: boolean; leaveReason?: string | null; dateLeave?: Date | null; commandId?: string | null; isGovernment?: boolean | null; orgRoot?: string | null; orgChild1?: string | null; orgChild2?: string | null; orgChild3?: string | null; orgChild4?: string | null; commandNo: string | null; commandYear: number | null; posNo: string | null; posNoAbb: string | null; commandDateAffect?: Date | null; commandDateSign?: Date | null; positionName: string | null; commandCode?: string | null; commandName?: string | null; remark: string | null; }[]; }, ) { const roleKeycloak = await this.roleKeycloakRepo.findOne({ where: { name: Like("USER") }, }); await Promise.all( body.data.map(async (item) => { const profile = await this.profileRepository.findOne({ where: { id: item.profileId }, relations: ["roleKeycloaks"], }); 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 }); data.dateGovernment = meta.createdAt; 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"); } const clearProfile = await checkCommandType(String(item.commandId)); if (clearProfile.status) { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); if (delUserKeycloak) { profile.keycloak = _null; profile.roleKeycloaks = []; profile.isActive = false; } } profile.leaveCommandId = item.commandId ?? _null; profile.leaveCommandNo = item.commandNo ?? _null; profile.leaveRemark = clearProfile.leaveRemark ?? _null; profile.leaveDate = item.commandDateAffect ?? _null; profile.leaveType = clearProfile.LeaveType ?? _null; profile.position = _null; profile.posTypeId = _null; profile.posLevelId = _null; } if (item.isGovernment == true) { const returnWork = await checkReturnCommandType(String(item.commandId)); if (returnWork) { profile.leaveReason = _null; profile.leaveCommandId = _null; profile.leaveCommandNo = _null; profile.leaveRemark = _null; profile.leaveDate = _null; profile.leaveType = _null; } let userKeycloakId; const checkUser = await getUserByUsername(profile.citizenId); //ถ้ายังไม่มี user keycloak ให้สร้างใหม่ if (checkUser.length == 0) { userKeycloakId = await createUser(profile.citizenId, profile.citizenId, { firstName: profile.firstName, lastName: profile.lastName, }); const list = await getRoles(); let result = false; if (Array.isArray(list) && userKeycloakId) { result = await addUserRoles( userKeycloakId, list .filter((v) => v.name === "USER") .map((x) => ({ id: x.id, name: x.name, })), ); } profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : []; profile.keycloak = userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : ""; } //ถ้ามีอยู่แล้วให้ใช้อันเดิม else { const rolesData = await getRoleMappings(checkUser[0].id); if (rolesData) { const _roleKeycloak = await this.roleKeycloakRepo.find({ where: { name: In(rolesData.map((x: any) => x.name)) }, }); profile.roleKeycloaks = _roleKeycloak && _roleKeycloak.length > 0 ? _roleKeycloak : []; } profile.keycloak = checkUser[0].id; } profile.amount = item.amount ?? _null; profile.amountSpecial = item.amountSpecial ?? _null; profile.isActive = true; } await this.profileRepository.save(profile); }), ); return new HttpSuccess(); } @Post("excexute/salary-employee-leave") public async newSalaryEmployeeAndUpdateLeave( @Request() req: RequestWithUser, @Body() body: { data: { profileId: string; amount?: Double | null; amountSpecial?: Double | null; positionSalaryAmount?: Double | null; mouthSalaryAmount?: Double | null; positionType: string | null; positionLevel: string | null; isLeave: boolean; leaveReason?: string | null; dateLeave?: Date | null; isGovernment?: boolean | null; commandId?: string | null; orgRoot?: string | null; orgChild1?: string | null; orgChild2?: string | null; orgChild3?: string | null; orgChild4?: string | null; positionExecutive?: string | null; commandNo: string | null; commandYear: number | null; posNo: string | null; posNoAbb: string | null; commandDateAffect?: Date | null; commandDateSign?: Date | null; positionName: string | null; commandCode?: string | null; commandName?: string | null; remark: string | null; }[]; }, ) { await Promise.all( body.data.map(async (item) => { const profile = await this.profileEmployeeRepository.findOne({ where: { id: item.profileId }, relations: ["roleKeycloaks"], }); 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(), }; Object.assign(data, { ...item, ...meta, profileEmployeeId: item.profileId, profileId: undefined, }); const history = new ProfileSalaryHistory(); Object.assign(history, { ...data, id: undefined }); data.dateGovernment = meta.createdAt; 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"); } const clearProfile = await checkCommandType(String(item.commandId)); if (clearProfile.status) { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); if (delUserKeycloak) { profile.keycloak = _null; profile.roleKeycloaks = []; profile.isActive = false; } } profile.leaveCommandId = item.commandId ?? _null; profile.leaveCommandNo = item.commandNo ?? _null; profile.leaveRemark = clearProfile.leaveRemark ?? _null; profile.leaveDate = item.commandDateAffect ?? _null; profile.leaveType = clearProfile.LeaveType ?? _null; profile.position = _null; profile.posTypeId = _null; profile.posLevelId = _null; } await this.profileEmployeeRepository.save(profile); }), ); return new HttpSuccess(); } @Post("excexute/salary") public async newSalaryAndUpdate( @Request() req: RequestWithUser, @Body() body: { data: { profileId: string; amount?: Double | null; amountSpecial?: Double | null; positionSalaryAmount?: Double | null; mouthSalaryAmount?: Double | null; positionExecutive: string | null; positionType: string | null; positionLevel: string | null; commandId?: string | null; leaveReason?: string | null; dateLeave?: Date | null; isLeave?: boolean; orgRoot?: string | null; orgChild1?: string | null; orgChild2?: string | null; orgChild3?: string | null; orgChild4?: string | null; officerOrg?: string | null; dateStart?: Date | null; dateEnd?: Date | null; commandNo: string | null; commandYear: number | null; posNo: string | null; posNoAbb: string | null; commandDateAffect?: Date | null; commandDateSign?: Date | null; positionName: string | null; commandCode?: string | null; commandName?: string | null; remark: string | null; }[]; }, ) { await Promise.all( body.data.map(async (item) => { const profile: any = await this.profileRepository.findOne({ where: { id: item.profileId }, relations: ["roleKeycloaks"], }); 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(), }; if (item.isLeave != undefined && item.isLeave == true) { await removeProfileInOrganize(profile.id, "OFFICER"); } const clearProfile = await checkCommandType(String(item.commandId)); const _null: any = null; if (clearProfile.status) { if (profile.keycloak != null) { const delUserKeycloak = await deleteUser(profile.keycloak); if (delUserKeycloak) { profile.keycloak = _null; profile.roleKeycloaks = []; profile.isActive = false; } } profile.isLeave = item.isLeave; profile.leaveCommandId = item.commandId ?? _null; profile.leaveCommandNo = item.commandNo ?? _null; profile.leaveRemark = clearProfile.leaveRemark ?? _null; profile.leaveDate = item.commandDateAffect ?? _null; profile.leaveType = clearProfile.LeaveType ?? _null; profile.position = _null; profile.posTypeId = _null; profile.posLevelId = _null; profile.leaveReason = item.leaveReason ?? _null; profile.dateLeave = item.dateLeave ?? _null; profile.amount = item.amount ?? _null; profile.amountSpecial = item.amountSpecial ?? _null; await this.profileRepository.save(profile, { data: req }); } 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 }); if (item.commandId) { const command = await this.commandRepository.findOne({ where: { id: item.commandId }, relations: ["commandType"], }); if (command != null && command.commandType.code == "C-PM-15") { // ประวัติคำสั่งให้ช่วยราชการ const dataAssis = new ProfileAssistance(); const metaAssis = { profileId: item.profileId, agency: item.officerOrg, dateStart: item.dateStart, dateEnd: item.dateEnd, commandNo: item.commandNo, refCommandDate: new Date(), commandId: item.commandId, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, createdAt: new Date(), lastUpdatedAt: new Date(), }; Object.assign(dataAssis, metaAssis); const historyAssis = new ProfileAssistanceHistory(); Object.assign(historyAssis, { ...dataAssis, id: undefined }); await this.assistanceRepository.save(dataAssis); historyAssis.profileAssistanceId = dataAssis.id; await this.assistanceHistoryRepository.save(historyAssis); } } }), ); return new HttpSuccess(); } @Post("excexute/salary-employee") public async newSalaryEmployeeAndUpdate( @Request() req: RequestWithUser, @Body() body: { data: { profileId: string; amount?: Double | null; amountSpecial?: Double | null; positionSalaryAmount?: Double | null; mouthSalaryAmount?: Double | null; positionType: string | null; positionLevel: string | null; commandId?: string | null; orgRoot?: string | null; orgChild1?: string | null; orgChild2?: string | null; orgChild3?: string | null; orgChild4?: string | null; officerOrg?: string | null; dateStart?: Date | null; dateEnd?: Date | null; commandNo: string | null; commandYear: number | null; posNo: string | null; posNoAbb: string | null; commandDateAffect?: Date | null; commandDateSign?: Date | null; positionName: string | null; commandCode?: string | null; commandName?: string | null; remark: 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(), }; Object.assign(data, { ...item, ...meta, profileEmployeeId: item.profileId, profileId: undefined, }); 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 (item.commandId) { const command = await this.commandRepository.findOne({ where: { id: item.commandId }, relations: ["commandType"], }); if (command != null && command.commandType.code == "C-PM-15") { // ประวัติคำสั่งให้ช่วยราชการ const dataAssis = new ProfileAssistance(); const metaAssis = { profileId: item.profileId, agency: item.officerOrg, dateStart: item.dateStart, dateEnd: item.dateEnd, commandNo: item.commandNo, refCommandDate: new Date(), commandId: item.commandId, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, createdAt: new Date(), lastUpdatedAt: new Date(), }; Object.assign(dataAssis, metaAssis); const historyAssis = new ProfileAssistanceHistory(); Object.assign(historyAssis, { ...dataAssis, id: undefined }); await this.assistanceRepository.save(dataAssis); historyAssis.profileAssistanceId = dataAssis.id; await this.assistanceHistoryRepository.save(historyAssis); } } }), ); return new HttpSuccess(); } @Post("excexute/salary-leave-discipline") public async newSalaryAndUpdateLeaveDiscipline( @Request() req: RequestWithUser, @Body() body: { data: { profileId: string; profileType?: string | null; isLeave: boolean | null; leaveReason?: string | null; dateLeave?: Date | null; detail?: string | null; level?: string | null; unStigma?: string | null; commandId?: string | null; amount?: Double | null; amountSpecial?: Double | null; positionSalaryAmount?: Double | null; mouthSalaryAmount?: Double | null; isGovernment?: boolean | null; commandNo: string | null; commandYear: number | null; commandDateAffect?: Date | null; commandDateSign?: Date | null; commandCode?: string | null; commandName?: string | null; remark: string | null; }[]; }, ) { await Promise.all( body.data.map(async (item) => { if (item.profileType && item.profileType.trim().toUpperCase() == "OFFICER") { const profile = await this.profileRepository.findOne({ relations: [ "profileSalary", "posLevel", "posType", "current_holders", "current_holders.orgRoot", "current_holders.orgChild1", "current_holders.orgChild2", "current_holders.orgChild3", "current_holders.orgChild4", "roleKeycloaks", ], where: { id: item.profileId }, order: { profileSalary: { order: "DESC", }, }, }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); } const orgRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false, }, }); const orgRevisionRef = profile?.current_holders?.find((x) => x.orgRevisionId == orgRevision?.id) ?? null; const orgRootRef = orgRevisionRef?.orgRoot ?? null; const orgChild1Ref = orgRevisionRef?.orgChild1 ?? null; const orgChild2Ref = orgRevisionRef?.orgChild2 ?? null; const orgChild3Ref = orgRevisionRef?.orgChild3 ?? null; const orgChild4Ref = orgRevisionRef?.orgChild4 ?? null; const shortName = !profile.current_holders || profile.current_holders.length == 0 ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) ?.orgChild4 != null ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild4.orgChild4ShortName}` : 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) != 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) != null && 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) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) ?.orgRoot != null ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgRoot.orgRootShortName}` : null; const posNo = `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}`; let position = profile.current_holders .filter((x) => x.orgRevisionId == orgRevision?.id)[0] ?.positions?.filter((pos) => pos.positionIsSelected === true)[0] ?? null; // ประวัติตำแหน่ง const data = new ProfileSalary(); const meta = { profileId: profile.id, commandId: item.commandId, position: profile.position, positionType: profile?.posType?.posTypeName ?? null, positionLevel: profile?.posLevel?.posLevelName ?? null, positionExecutive: position?.posExecutive?.posExecutiveName ?? null, amount: item.amount ? item.amount : null, positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null, amountSpecial: item.amountSpecial ? item.amountSpecial : null, mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null, order: profile.profileSalary.length >= 0 ? profile.profileSalary.length > 0 ? profile.profileSalary[0].order + 1 : 1 : null, orgRoot: orgRootRef?.orgRootName ?? null, orgChild1: orgChild1Ref?.orgChild1Name ?? null, orgChild2: orgChild2Ref?.orgChild2Name ?? null, orgChild3: orgChild3Ref?.orgChild3Name ?? null, orgChild4: orgChild4Ref?.orgChild4Name ?? null, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, createdAt: new Date(), lastUpdatedAt: new Date(), dateGovernment: new Date(), isGovernment: item.isGovernment, commandNo: item.commandNo, commandYear: item.commandYear, posNo: shortName, posNoAbb: posNo, commandDateAffect: item.commandDateAffect, commandDateSign: item.commandDateSign, commandCode: item.commandCode, commandName: item.commandName, remark: item.remark, }; 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) { const _profile = await this.profileRepository.findOne({ where: { id: item.profileId }, }); if (!_profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); } 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) { const exceptClear = await checkExceptCommandType(String(item.commandId)); if (exceptClear.status) { _profile.leaveReason = item.leaveReason ?? _null; _profile.leaveCommandId = item.commandId ?? _null; _profile.leaveCommandNo = item.commandNo ?? _null; _profile.leaveRemark = exceptClear.leaveRemark ?? _null; _profile.leaveDate = item.commandDateAffect ?? _null; _profile.leaveType = exceptClear.LeaveType ?? _null; } else { await removeProfileInOrganize(_profile.id, "OFFICER"); } } const clearProfile = await checkCommandType(String(item.commandId)); if (clearProfile.status) { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); if (delUserKeycloak) { _profile.keycloak = _null; _profile.roleKeycloaks = []; _profile.isActive = false; } } _profile.leaveCommandId = item.commandId ?? _null; _profile.leaveCommandNo = item.commandNo ?? _null; _profile.leaveRemark = clearProfile.leaveRemark ?? _null; _profile.leaveDate = item.commandDateAffect ?? _null; _profile.leaveType = clearProfile.LeaveType ?? _null; _profile.position = _null; _profile.posTypeId = _null; _profile.posLevelId = _null; } await this.profileRepository.save(_profile); } } else { const profile = await this.profileEmployeeRepository.findOne({ relations: [ "profileSalary", "posLevel", "posType", "current_holders", "current_holders.orgRoot", "current_holders.orgChild1", "current_holders.orgChild2", "current_holders.orgChild3", "current_holders.orgChild4", "roleKeycloaks", ], where: { id: item.profileId }, order: { profileSalary: { order: "DESC", }, }, }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); } const orgRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false, }, }); const orgRevisionRef = profile?.current_holders?.find((x) => x.orgRevisionId == orgRevision?.id) ?? null; const orgRootRef = orgRevisionRef?.orgRoot ?? null; const orgChild1Ref = orgRevisionRef?.orgChild1 ?? null; const orgChild2Ref = orgRevisionRef?.orgChild2 ?? null; const orgChild3Ref = orgRevisionRef?.orgChild3 ?? null; const orgChild4Ref = orgRevisionRef?.orgChild4 ?? null; const shortName = !profile.current_holders || profile.current_holders.length == 0 ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) ?.orgChild4 != null ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild4.orgChild4ShortName}` : 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) != 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) != null && 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) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) ?.orgRoot != null ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgRoot.orgRootShortName}` : null; const posNo = `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}`; // let position = // profile.current_holders // .filter((x) => x.orgRevisionId == orgRevision?.id)[0] // ?.positions?.filter((pos) => pos.positionIsSelected === true)[0] ?? null; // ประวัติตำแหน่ง const data = new ProfileSalary(); const meta = { profileEmployeeId: profile.id, commandId: item.commandId, position: profile.position, positionType: profile?.posType?.posTypeName ?? null, positionLevel: profile?.posLevel?.posLevelName ?? null, amount: item.amount ? item.amount : null, positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null, mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null, order: profile.profileSalary.length >= 0 ? profile.profileSalary.length > 0 ? profile.profileSalary[0].order + 1 : 1 : null, orgRoot: orgRootRef?.orgRootName ?? null, orgChild1: orgChild1Ref?.orgChild1Name ?? null, orgChild2: orgChild2Ref?.orgChild2Name ?? null, orgChild3: orgChild3Ref?.orgChild3Name ?? null, orgChild4: orgChild4Ref?.orgChild4Name ?? null, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, createdAt: new Date(), lastUpdatedAt: new Date(), dateGovernment: new Date(), isGovernment: item.isGovernment, commandNo: item.commandNo, commandYear: item.commandYear, posNo: shortName, posNoAbb: posNo, commandDateAffect: item.commandDateAffect, commandDateSign: item.commandDateSign, commandCode: item.commandCode, commandName: item.commandName, remark: item.remark, }; 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, profileEmployeeId: item.profileId, profileId: undefined, }); 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) { const _profile = await this.profileEmployeeRepository.findOne({ where: { id: item.profileId }, }); if (!_profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); } 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) { const exceptClear = await checkExceptCommandType(String(item.commandId)); if (exceptClear.status) { _profile.leaveReason = item.leaveReason ?? _null; _profile.leaveCommandId = item.commandId ?? _null; _profile.leaveCommandNo = item.commandNo ?? _null; _profile.leaveRemark = exceptClear.leaveRemark ?? _null; _profile.leaveDate = item.commandDateAffect ?? _null; _profile.leaveType = exceptClear.LeaveType ?? _null; } else { await removeProfileInOrganize(_profile.id, "EMPLOYEE"); } } const clearProfile = await checkCommandType(String(item.commandId)); if (clearProfile.status) { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); if (delUserKeycloak) { _profile.keycloak = _null; _profile.roleKeycloaks = []; _profile.isActive = false; } } _profile.leaveCommandId = item.commandId ?? _null; _profile.leaveCommandNo = item.commandNo ?? _null; _profile.leaveRemark = clearProfile.leaveRemark ?? _null; _profile.leaveDate = item.commandDateAffect ?? _null; _profile.leaveType = clearProfile.LeaveType ?? _null; _profile.position = _null; _profile.posTypeId = _null; _profile.posLevelId = _null; } await this.profileEmployeeRepository.save(_profile); } } }), ); return new HttpSuccess(); } @Post("excexute/salary-employee-leave-discipline") public async newSalaryEmployeeAndUpdateLeaveDiscipline( @Request() req: RequestWithUser, @Body() body: { data: { profileId: string; isLeave: boolean | null; leaveReason?: string | null; dateLeave?: Date | null; detail?: string | null; level?: string | null; unStigma?: string | null; commandId?: string | null; amount?: Double | null; amountSpecial?: Double | null; positionSalaryAmount?: Double | null; mouthSalaryAmount?: Double | null; isGovernment?: boolean | null; commandNo: string | null; commandYear: number | null; commandDateAffect?: Date | null; commandDateSign?: Date | null; commandCode?: string | null; commandName?: string | null; remark: string | null; }[]; }, ) { await Promise.all( body.data.map(async (item) => { const profile = await this.profileEmployeeRepository.findOne({ relations: [ "profileSalary", "posLevel", "posType", "current_holders", "current_holders.orgRoot", "current_holders.orgChild1", "current_holders.orgChild2", "current_holders.orgChild3", "current_holders.orgChild4", "roleKeycloaks", ], where: { id: item.profileId }, order: { profileSalary: { order: "DESC", }, }, }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); } const orgRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false, }, }); const orgRevisionRef = profile?.current_holders?.find((x) => x.orgRevisionId == orgRevision?.id) ?? null; const orgRootRef = orgRevisionRef?.orgRoot ?? null; const orgChild1Ref = orgRevisionRef?.orgChild1 ?? null; const orgChild2Ref = orgRevisionRef?.orgChild2 ?? null; const orgChild3Ref = orgRevisionRef?.orgChild3 ?? null; const orgChild4Ref = orgRevisionRef?.orgChild4 ?? null; const shortName = !profile.current_holders || profile.current_holders.length == 0 ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) ?.orgChild4 != null ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild4.orgChild4ShortName}` : 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) != 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) != null && 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) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) ?.orgRoot != null ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgRoot.orgRootShortName}` : null; const posNo = `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}`; // let position = // profile.current_holders // .filter((x) => x.orgRevisionId == orgRevision?.id)[0] // ?.positions?.filter((pos) => pos.positionIsSelected === true)[0] ?? null; // ประวัติตำแหน่ง const data = new ProfileSalary(); const meta = { profileEmployeeId: profile.id, commandId: item.commandId, position: profile.position, positionType: profile?.posType?.posTypeName ?? null, positionLevel: profile?.posLevel?.posLevelName ?? null, amount: item.amount ? item.amount : null, amountSpecial: item.amountSpecial ? item.amountSpecial : null, positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null, mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null, order: profile.profileSalary.length >= 0 ? profile.profileSalary.length > 0 ? profile.profileSalary[0].order + 1 : 1 : null, orgRoot: orgRootRef?.orgRootName ?? null, orgChild1: orgChild1Ref?.orgChild1Name ?? null, orgChild2: orgChild2Ref?.orgChild2Name ?? null, orgChild3: orgChild3Ref?.orgChild3Name ?? null, orgChild4: orgChild4Ref?.orgChild4Name ?? null, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, createdAt: new Date(), lastUpdatedAt: new Date(), dateGovernment: new Date(), isGovernment: item.isGovernment, commandNo: item.commandNo, commandYear: item.commandYear, posNo: shortName, posNoAbb: posNo, commandDateAffect: item.commandDateAffect, commandDateSign: item.commandDateSign, commandCode: item.commandCode, commandName: item.commandName, remark: item.remark, }; 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, profileEmployeeId: item.profileId, profileId: undefined, }); 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) { const _profile = await this.profileEmployeeRepository.findOne({ where: { id: item.profileId }, }); if (!_profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); } 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) { const exceptClear = await checkExceptCommandType(String(item.commandId)); if (exceptClear.status) { _profile.leaveReason = item.leaveReason ?? _null; _profile.leaveCommandId = item.commandId ?? _null; _profile.leaveCommandNo = item.commandNo ?? _null; _profile.leaveRemark = exceptClear.leaveRemark ?? _null; _profile.leaveDate = item.commandDateAffect ?? _null; _profile.leaveType = exceptClear.LeaveType ?? _null; } else { await removeProfileInOrganize(_profile.id, "EMPLOYEE"); } } const clearProfile = await checkCommandType(String(item.commandId)); if (clearProfile.status) { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); if (delUserKeycloak) { _profile.keycloak = _null; _profile.roleKeycloaks = []; _profile.isActive = false; } } _profile.leaveCommandId = item.commandId ?? _null; _profile.leaveCommandNo = item.commandNo ?? _null; _profile.leaveRemark = clearProfile.leaveRemark ?? _null; _profile.leaveDate = item.commandDateAffect ?? _null; _profile.leaveType = clearProfile.LeaveType ?? _null; _profile.position = _null; _profile.posTypeId = _null; _profile.posLevelId = _null; } await this.profileEmployeeRepository.save(_profile); } }), ); return new HttpSuccess(); } @Post("excexute/salary-probation") public async newSalaryAndUpdateLeaveDisciplinefgh( @Request() req: RequestWithUser, @Body() body: { data: { profileId: string; commandId?: string | null; amount?: Double | null; amountSpecial?: Double | null; positionSalaryAmount?: Double | null; mouthSalaryAmount?: Double | null; commandNo: string | null; commandYear: number | null; posNo: string | null; posNoAbb: string | null; commandDateAffect?: Date | null; commandDateSign?: Date | null; positionName: string | null; commandCode?: string | null; commandName?: string | null; remark: string | null; }[]; }, ) { await Promise.all( body.data.map(async (item) => { const profile = await this.profileRepository.findOne({ relations: [ "profileSalary", "posType", "posLevel", "current_holders", "current_holders.orgRoot", "current_holders.orgChild1", "current_holders.orgChild2", "current_holders.orgChild3", "current_holders.orgChild4", ], where: { id: item.profileId }, order: { profileSalary: { order: "DESC", }, }, }); if (!profile) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลทะเบียนประวัตินี้"); } const orgRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false, }, }); const orgRevisionRef = profile?.current_holders?.find((x) => x.orgRevisionId == orgRevision?.id) ?? null; const orgRootRef = orgRevisionRef?.orgRoot ?? null; const orgChild1Ref = orgRevisionRef?.orgChild1 ?? null; const orgChild2Ref = orgRevisionRef?.orgChild2 ?? null; const orgChild3Ref = orgRevisionRef?.orgChild3 ?? null; const orgChild4Ref = orgRevisionRef?.orgChild4 ?? null; const shortName = !profile.current_holders || profile.current_holders.length == 0 ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) ?.orgChild4 != null ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild4.orgChild4ShortName}` : 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) != 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) != null && 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) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) ?.orgRoot != null ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgRoot.orgRootShortName}` : null; const posNo = `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}`; let position = profile.current_holders .filter((x) => x.orgRevisionId == orgRevision?.id)[0] ?.positions?.filter((pos) => pos.positionIsSelected === true)[0] ?? null; // ประวัติตำแหน่ง const data = new ProfileSalary(); const meta = { profileId: item.profileId, commandId: item.commandId, position: profile.position, positionType: profile?.posType?.posTypeName ?? null, positionLevel: profile?.posLevel?.posLevelName ?? null, positionExecutive: position?.posExecutive?.posExecutiveName ?? null, amount: item.amount ? item.amount : null, amountSpecial: item.amountSpecial ? item.amountSpecial : null, positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null, mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null, order: profile.profileSalary.length >= 0 ? profile.profileSalary.length > 0 ? profile.profileSalary[0].order + 1 : 1 : null, orgRoot: orgRootRef?.orgRootName ?? null, orgChild1: orgChild1Ref?.orgChild1Name ?? null, orgChild2: orgChild2Ref?.orgChild2Name ?? null, orgChild3: orgChild3Ref?.orgChild3Name ?? null, orgChild4: orgChild4Ref?.orgChild4Name ?? null, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, createdAt: new Date(), lastUpdatedAt: new Date(), commandNo: item.commandNo, commandYear: item.commandYear, posNo: shortName, posNoAbb: posNo, commandDateAffect: item.commandDateAffect, commandDateSign: item.commandDateSign, commandCode: item.commandCode, commandName: item.commandName, remark: item.remark, }; 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 checkCommandType = await this.commandRepository.findOne({ where: { id: body.data.length > 0 ? body.data[0].commandId?.toString() : "" }, relations: ["commandType"], }); if (checkCommandType?.commandType.code == "C-PM-11") { const profile = await this.profileRepository.find({ where: { id: In(body.data.map((x) => x.profileId)) }, }); const data = profile.map((x) => ({ ...x, isProbation: false, })); await this.profileRepository.save(data); } return new HttpSuccess(); } @Post("excexute/salary-probation-leave") async ExecuteCommand12Async( @Request() req: RequestWithUser, @Body() body: { data: { profileId: string; commandId?: string | null; amount?: Double | null; amountSpecial?: Double | null; positionSalaryAmount?: Double | null; mouthSalaryAmount?: Double | null; isGovernment?: boolean | null; commandNo: string | null; commandYear: number | null; commandDateAffect?: Date | null; commandDateSign?: Date | null; commandCode?: string | null; commandName?: string | null; remark: string | null; }[]; }, ) { await Promise.all( body.data.map(async (item) => { const profile = await this.profileRepository.findOne({ relations: [ "profileSalary", "posType", "posLevel", "current_holders", "current_holders.orgRoot", "current_holders.orgChild1", "current_holders.orgChild2", "current_holders.orgChild3", "current_holders.orgChild4", "current_holders.positions", "current_holders.positions.posExecutive", ], 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 }, relations: ["roleKeycloaks"], }); if (!_profile) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์"); } let dateLeave_: any = item.commandDateAffect; _profile.isLeave = true; _profile.leaveReason = "คำสั่งให้ข้าราชการออกจากราชการเพราะผลการทดลองปฏิบัติหน้าที่ราชการต่ำกว่ามาตรฐานที่กำหนด"; _profile.dateLeave = dateLeave_; _profile.lastUpdateUserId = req.user.sub; _profile.lastUpdateFullName = req.user.name; _profile.lastUpdatedAt = new Date(); const orgRevision = await this.orgRevisionRepo.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false, }, }); const orgRevisionRef = profile?.current_holders?.find((x) => x.orgRevisionId == orgRevision?.id) ?? null; const orgRootRef = orgRevisionRef?.orgRoot ?? null; const orgChild1Ref = orgRevisionRef?.orgChild1 ?? null; const orgChild2Ref = orgRevisionRef?.orgChild2 ?? null; const orgChild3Ref = orgRevisionRef?.orgChild3 ?? null; const orgChild4Ref = orgRevisionRef?.orgChild4 ?? null; const shortName = !profile.current_holders || profile.current_holders.length == 0 ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) ?.orgChild4 != null ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgChild4.orgChild4ShortName}` : 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) != 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) != null && 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) != null && profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id) ?.orgRoot != null ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.orgRoot.orgRootShortName}` : null; const posNo = `${profile.current_holders.find((x) => x.orgRevisionId == orgRevision?.id)?.posMasterNo}`; let position = profile.current_holders .filter((x) => x.orgRevisionId == orgRevision?.id)[0] ?.positions?.filter((pos) => pos.positionIsSelected === true)[0] ?? null; const profileSalary: ProfileSalary = Object.assign(new ProfileSalary(), { profileId: item.profileId, commandId: item.commandId, position: profile.position, positionType: profile?.posType?.posTypeName ?? null, positionLevel: profile?.posLevel?.posLevelName ?? null, positionExecutive: position?.posExecutive?.posExecutiveName ?? null, amount: item.amount ? item.amount : null, amountSpecial: item.amountSpecial ? item.amountSpecial : null, positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null, mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null, order: profile.profileSalary.length >= 0 ? profile.profileSalary.length > 0 ? profile.profileSalary[0].order + 1 : 1 : null, orgRoot: orgRootRef?.orgRootName ?? null, orgChild1: orgChild1Ref?.orgChild1Name ?? null, orgChild2: orgChild2Ref?.orgChild2Name ?? null, orgChild3: orgChild3Ref?.orgChild3Name ?? null, orgChild4: orgChild4Ref?.orgChild4Name ?? null, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, createdAt: new Date(), lastUpdatedAt: new Date(), dateGovernment: new Date(), isGovernment: item.isGovernment, commandNo: item.commandNo, commandYear: item.commandYear, posNo: shortName, posNoAbb: posNo, commandDateAffect: item.commandDateAffect, commandDateSign: item.commandDateSign, commandCode: item.commandCode, commandName: item.commandName, remark: item.remark, }); await removeProfileInOrganize(profile.id, "OFFICER"); const clearProfile = await checkCommandType(String(item.commandId)); const _null: any = null; if (clearProfile.status) { if (_profile.keycloak != null) { const delUserKeycloak = await deleteUser(_profile.keycloak); if (delUserKeycloak) { _profile.keycloak = _null; _profile.roleKeycloaks = []; _profile.isActive = false; } } _profile.leaveCommandId = item.commandId ?? _null; _profile.leaveCommandNo = item.commandNo ?? _null; _profile.leaveRemark = clearProfile.leaveRemark ?? _null; _profile.leaveDate = item.commandDateAffect ?? _null; _profile.leaveType = clearProfile.LeaveType ?? _null; _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); }), ); return new HttpSuccess(); } @Post("excexute/create-officer-profile") public async CreateOfficeProfileExcecute( @Request() req: RequestWithUser, @Body() body: { data: { bodyProfile: CreateProfileAllFields; bodyEducations?: CreateProfileEducation[]; bodyCertificates?: CreateProfileCertificate[]; bodySalarys?: CreateProfileSalary | null; bodyPosition?: { posmasterId: string; positionId: string; } | null; }[]; }, ) { const roleKeycloak = await this.roleKeycloakRepo.findOne({ where: { name: Like("USER") }, }); const list = await getRoles(); if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); const _null: any = null; 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(), }; const _null: any = null; if (item.bodyProfile.posLevelId === "") item.bodyProfile.posLevelId = null; if (item.bodyProfile.posTypeId === "") item.bodyProfile.posTypeId = null; if ( item.bodyProfile.posLevelId && !(await this.posLevelRepo.findOneBy({ id: item.bodyProfile.posLevelId })) ) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลระดับตำแหน่งนี้"); } if ( item.bodyProfile.posTypeId && !(await this.posTypeRepo.findOneBy({ id: item.bodyProfile.posTypeId })) ) { throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้"); } let registrationProvinceId = await this.provinceRepo.findOneBy({ id: item.bodyProfile.registrationProvinceId ?? "", }); let registrationDistrictId = await this.districtRepo.findOneBy({ id: item.bodyProfile.registrationDistrictId ?? "", }); let registrationSubDistrictId = await this.subDistrictRepo.findOneBy({ id: item.bodyProfile.registrationSubDistrictId ?? "", }); let currentProvinceId = await this.provinceRepo.findOneBy({ id: item.bodyProfile.currentProvinceId ?? "", }); let currentDistrictId = await this.districtRepo.findOneBy({ id: item.bodyProfile.currentDistrictId ?? "", }); let currentSubDistrictId = await this.subDistrictRepo.findOneBy({ id: item.bodyProfile.currentSubDistrictId ?? "", }); let _dateRetire = item.bodyProfile.birthDate == null ? _null : calculateRetireDate(item.bodyProfile.birthDate); let _dateRetireLaw = item.bodyProfile.birthDate == null ? _null : calculateRetireLaw(item.bodyProfile.birthDate); let userKeycloakId: any; let result: any; const checkUser = await getUserByUsername(item.bodyProfile.citizenId); if (checkUser.length == 0) { userKeycloakId = await createUser( item.bodyProfile.citizenId, item.bodyProfile.citizenId, { firstName: item.bodyProfile.firstName, lastName: item.bodyProfile.lastName, }, ); result = await addUserRoles( userKeycloakId, list .filter((v) => v.name === "USER") .map((x) => ({ id: x.id, name: x.name, })), ); } else { userKeycloakId = checkUser[0].id; const rolesData = await getRoleMappings(userKeycloakId); if (rolesData) { const _delRole = rolesData.map((x: any) => ({ id: x.id, name: x.name, })); await removeUserRoles(userKeycloakId, _delRole); } result = await addUserRoles( userKeycloakId, list .filter((v) => v.name === "USER") .map((x) => ({ id: x.id, name: x.name, })), ); } let profile: any = await this.profileRepository.findOne({ where: { citizenId: item.bodyProfile.citizenId /*, isActive: true */ }, relations: ["roleKeycloaks"], }); if (!profile) { profile = Object.assign({ ...item.bodyProfile, ...meta }); profile.dateRetire = _dateRetire; profile.dateRetireLaw = _dateRetireLaw; profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : []; profile.keycloak = userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : ""; profile.registrationProvinceId = registrationProvinceId ? registrationProvinceId.id : _null; profile.registrationDistrictId = registrationDistrictId ? registrationDistrictId.id : _null; profile.registrationSubDistrictId = registrationSubDistrictId ? registrationSubDistrictId.id : _null; profile.currentProvinceId = currentProvinceId ? currentProvinceId.id : _null; profile.currentDistrictId = currentDistrictId ? currentDistrictId.id : _null; profile.currentSubDistrictId = currentSubDistrictId ? currentSubDistrictId.id : _null; profile.email = item.bodyProfile.email; profile.dateStart = item.bodyProfile.dateStart; profile.amount = item.bodyProfile.amount ?? null; profile.amountSpecial = item.bodyProfile.amountSpecial ?? null; profile.isProbation = item.bodyProfile.isProbation; await this.profileRepository.save(profile); setLogDataDiff(req, { before, after: profile }); } //ขรก.ในระบบ หรือ ขรก.ในระบบที่สถานะพ้นจากราชการ else { profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : []; profile.keycloak = userKeycloakId && typeof userKeycloakId === "string" ? userKeycloakId : ""; profile.isProbation = item.bodyProfile.isProbation; profile.isLeave = item.bodyProfile.isLeave; profile.isRetirement = false; profile.isActive = true; profile.dateLeave = _null; profile.dateRetire = _dateRetire; profile.dateRetireLaw = _dateRetireLaw; profile.registrationProvinceId = registrationProvinceId ? registrationProvinceId.id : _null; profile.registrationDistrictId = registrationDistrictId ? registrationDistrictId.id : _null; profile.registrationSubDistrictId = registrationSubDistrictId ? registrationSubDistrictId.id : _null; profile.currentProvinceId = currentProvinceId ? currentProvinceId.id : _null; profile.currentDistrictId = currentDistrictId ? currentDistrictId.id : _null; profile.currentSubDistrictId = currentSubDistrictId ? currentSubDistrictId.id : _null; profile.email = item.bodyProfile.email; profile.telephoneNumber = item.bodyProfile.telephoneNumber; profile.phone = item.bodyProfile.phone; profile.dateStart = item.bodyProfile.dateStart; profile.amount = item.bodyProfile.amount ?? null; profile.amountSpecial = item.bodyProfile.amountSpecial ?? null; profile.leaveCommandId = _null; profile.leaveCommandNo = _null; profile.leaveRemark = _null; profile.leaveDate = _null; profile.leaveType = _null; profile.lastUpdateUserId = req.user.sub; profile.lastUpdateFullName = req.user.name; profile.lastUpdatedAt = new Date(); await this.profileRepository.save(profile); setLogDataDiff(req, { before, after: profile }); } if (profile && profile.id) { //Educations if (item.bodyEducations && item.bodyEducations.length > 0) { 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; const educationLevel = await this.profileEducationRepo.findOne({ select: ["id", "level", "profileId"], where: { profileId: profile.id }, order: { level: "DESC" }, }); profileEdu.level = educationLevel == null ? 1 : educationLevel.level + 1; await this.profileEducationRepo.save(profileEdu, { data: req }); setLogDataDiff(req, { before, after: profileEdu }); eduHistory.profileEducationId = profileEdu.id; await this.profileEducationHistoryRepo.save(eduHistory, { data: req }); }), ); } //Certificates if (item.bodyCertificates && item.bodyCertificates.length > 0) { 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 }); profileCer.profileId = profile.id; await this.certificateRepo.save(profileCer, { data: req }); setLogDataDiff(req, { before, after: profileCer }); cerHistory.profileCertificateId = profileCer.id; await this.certificateHistoryRepo.save(cerHistory, { data: req }); }), ); } //Salary if (item.bodySalarys && item.bodySalarys != null) { const dest_item = await this.salaryRepo.findOne({ where: { profileId: profile.id }, order: { order: "DESC" }, }); const profileSal: any = new ProfileSalary(); Object.assign(profileSal, { ...item.bodySalarys, ...meta }); const salaryHistory = new ProfileSalaryHistory(); Object.assign(salaryHistory, { ...profileSal, id: undefined }); profileSal.order = dest_item == null ? 1 : dest_item.order + 1; profileSal.profileId = profile.id; profileSal.dateGovernment = meta.createdAt; profileSal.amount = item.bodySalarys.amount ?? null; profileSal.amountSpecial = item.bodySalarys.amountSpecial ?? null; profileSal.positionSalaryAmount = item.bodySalarys.positionSalaryAmount ?? null; profileSal.mouthSalaryAmount = item.bodySalarys.mouthSalaryAmount ?? null; await this.salaryRepo.save(profileSal, { data: req }); setLogDataDiff(req, { before, after: profileSal }); salaryHistory.profileSalaryId = profileSal.id; await this.salaryHistoryRepo.save(salaryHistory, { data: req }); } //Position if (item.bodyPosition && item.bodyPosition != null) { const posMaster = await this.posMasterRepository.findOne({ where: { id: item.bodyPosition.posmasterId }, }); if (posMaster == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); const posMasterOld = await this.posMasterRepository.findOne({ where: { current_holderId: profile.id, 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.bodyPosition.posmasterId, positionIsSelected: true, }, }); if (checkPosition.length > 0) { const clearPosition = checkPosition.map((positions) => ({ ...positions, positionIsSelected: false, })); await this.positionRepository.save(clearPosition); } posMaster.current_holderId = profile.id; if (posMasterOld != null) await this.posMasterRepository.save(posMasterOld); await this.posMasterRepository.save(posMaster); 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; profile.dateStart = new Date(); await this.profileRepository.save(profile, { data: req }); setLogDataDiff(req, { before, after: profile }); await this.positionRepository.save(positionNew, { data: req }); } } } }), ); return new HttpSuccess(); } @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); if (posMasters && posMasters.length > 0) { const orgRevision = await this.orgRevisionRepo.findOne({ where: { id: posMasters[0].orgRevisionId }, }); if (orgRevision != null && orgRevision.isLock == false) { await this.orgRevisionRepo.update(orgRevision.id, { isLock: true, }); } } return new HttpSuccess(); } @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(); } @Post("command21/employee/report/excecute") public async command21SalaryEmployeeExcecute( @Request() req: RequestWithUser, @Body() body: { refIds: { refId: string; commandId?: string | null; amount: Double | null; amountSpecial?: Double | null; positionSalaryAmount: Double | null; mouthSalaryAmount: Double | null; commandNo: string | null; commandYear: number; commandDateAffect?: Date | null; commandDateSign?: Date | null; commandCode?: string | null; commandName?: string | null; remark: string | null; }[]; }, ) { let _reqBody: any[] = []; const roleKeycloak = await this.roleKeycloakRepo.findOne({ where: { name: Like("USER") }, }); await Promise.all( body.refIds.map(async (item) => { const profile = await this.profileEmployeeRepository.findOne({ where: { id: item.refId }, relations: ["roleKeycloaks"], }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } const orgRevision = await this.orgRevisionRepository.findOne({ where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false, }, }); const _posMaster = await this.employeePosMasterRepository.findOne({ where: { orgRevisionId: orgRevision?.id, id: profile.posmasterIdTemp, // current_holderId: profile.id }, relations: { orgRoot: true, orgChild1: true, orgChild2: true, orgChild3: true, orgChild4: true, }, }); let orgShortName = ""; if (_posMaster != null) { if (_posMaster.orgChild1Id === null) { orgShortName = _posMaster.orgRoot?.orgRootShortName; } else if (_posMaster.orgChild2Id === null) { orgShortName = _posMaster.orgChild1?.orgChild1ShortName; } else if (_posMaster.orgChild3Id === null) { orgShortName = _posMaster.orgChild2?.orgChild2ShortName; } else if (_posMaster.orgChild4Id === null) { orgShortName = _posMaster.orgChild3?.orgChild3ShortName; } else { orgShortName = _posMaster.orgChild4?.orgChild4ShortName; } } 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, amount: item.amount, amountSpecial: item.amountSpecial, commandId: item.commandId, positionSalaryAmount: item.positionSalaryAmount, mouthSalaryAmount: item.mouthSalaryAmount, position: profile.positionTemp, positionType: profile.posTypeNameTemp, positionLevel: profile.posLevelNameTemp, order: dest_item == null ? 1 : dest_item.order + 1, // orgRoot: orgRootRef?.orgRootName??null, // orgChild1: orgChild1Ref?.orgChild1Name??null, // orgChild2: orgChild2Ref?.orgChild2Name??null, // orgChild3: orgChild3Ref?.orgChild3Name??null, // orgChild4: orgChild4Ref?.orgChild4Name??null, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, createdAt: new Date(), lastUpdatedAt: new Date(), commandNo: item.commandNo, commandYear: item.commandYear, posNo: orgShortName, posNoAbb: profile.posMasterNoTemp ?? "", commandDateAffect: item.commandDateAffect, commandDateSign: item.commandDateSign, commandCode: item.commandCode, commandName: item.commandName, remark: item.remark, }; 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", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], }); 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; // if (posMasterOld != null) posMasterOld.next_holderId = null; 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; posMaster.next_holderId = null; 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, }, }); if (positionNew != null) { // Create Keycloak const checkUser = await getUserByUsername(profile.citizenId); if (checkUser.length == 0) { const userKeycloakId = await createUser(profile.citizenId, profile.citizenId, { firstName: profile.firstName, lastName: profile.lastName, // email: profile.email, }); // if (typeof userKeycloakId !== "string") { // throw new Error(userKeycloakId.errorMessage); // } 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, })), ); // if (!result) throw new Error("Failed. Cannot set user's role."); profile.keycloak = userKeycloakId && typeof userKeycloakId == "string" ? userKeycloakId : ""; profile.roleKeycloaks = result && roleKeycloak ? [roleKeycloak] : []; // End Create Keycloak } else { const rolesData = await getRoleMappings(checkUser[0].id); if (rolesData) { const _roleKeycloak = await this.roleKeycloakRepo.find({ where: { name: In(rolesData.map((x: any) => x.name)) }, }); profile.roleKeycloaks = _roleKeycloak && _roleKeycloak.length > 0 ? _roleKeycloak : []; } profile.keycloak = checkUser[0].id; } 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(); profile.dateStart = new Date(); profile.dateAppoint = new Date(); profile.amount = item.amount == null ? _null : item.amount; profile.amountSpecial = item.amountSpecial == null ? _null : item.amountSpecial; _reqBody.push({ profileId: profile.id, prefix: profile.prefix, firstName: profile.firstName, lastName: profile.lastName, citizenId: profile.citizenId, root: posMaster.orgRoot.orgRootName, rootId: posMaster.orgRootId, rootShortName: posMaster.orgRoot.orgRootShortName, rootDnaId: posMaster.orgRoot?.ancestorDNA ?? _null, child1DnaId: posMaster.orgChild1?.ancestorDNA ?? _null, child2DnaId: posMaster.orgChild2?.ancestorDNA ?? _null, child3DnaId: posMaster.orgChild3?.ancestorDNA ?? _null, child4DnaId: posMaster.orgChild4?.ancestorDNA ?? _null, }); await this.profileEmployeeRepository.save(profile); await this.employeePositionRepository.save(positionNew); } }), ); await new CallAPI() .PostData(req, "/placement/appointment/employee-appoint-21/report/excecute", { profileEmps: _reqBody, }) .catch((error) => { throw new Error("Failed. Cannot update status. ", error); }); return new HttpSuccess(); } @Post("command21/employee/report/delete") 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, statusTemp: "PENDING", })); await this.profileEmployeeRepository.save(data); return new HttpSuccess(); } @Post("command40/officer/report/excecute") public async command40SalaryOfficerExcecute( @Request() req: RequestWithUser, @Body() body: { refIds: { refId: string; commandId?: string | null; amount: Double | null; amountSpecial?: Double | null; positionSalaryAmount: Double | null; mouthSalaryAmount: Double | null; commandNo: string | null; commandYear: number; commandDateAffect?: Date | null; commandDateSign?: Date | null; commandCode?: string | null; commandName?: string | null; remark: string | null; }[]; }, ) { const posMasters = await this.posMasterActRepository.find({ where: { id: In(body.refIds.map((x) => x.refId)) }, relations: [ "posMasterChild", "posMaster", "posMaster.current_holder", "posMaster.orgRoot", "posMaster.orgChild1", "posMaster.orgChild2", "posMaster.orgChild3", "posMaster.orgChild4", ], }); const data = posMasters.map((_data) => ({ ..._data, statusReport: "DONE", })); await this.posMasterActRepository.save(data); await Promise.all( posMasters.map(async (item) => { if (item.posMasterChild != null && item.posMasterChild.current_holderId != null) { // ประวัติคำสั่งให้รักษาการแทน const _actposition = await this.actpositionRepository.find({ where: { profileId: item.posMasterChild.current_holderId }, }); const profileActs = _actposition.map((_data) => ({ ..._data, status: false, dateEnd: _data.status == true ? new Date() : _data.dateEnd, })); await this.actpositionRepository.save(profileActs); const shortName = item.posMaster != null && item.posMaster.orgChild4 != null ? `${item.posMaster.orgChild4.orgChild4ShortName}` : item.posMaster != null && item.posMaster?.orgChild3 != null ? `${item.posMaster.orgChild3.orgChild3ShortName}` : item.posMaster != null && item.posMaster?.orgChild2 != null ? `${item.posMaster.orgChild2.orgChild2ShortName}` : item.posMaster != null && item.posMaster?.orgChild1 != null ? `${item.posMaster.orgChild1.orgChild1ShortName}` : item.posMaster != null && item.posMaster?.orgRoot != null ? `${item.posMaster.orgRoot.orgRootShortName}` : null; const metaAct = { profileId: item.posMasterChild.current_holderId, dateStart: body.refIds[0].commandDateAffect, dateEnd: null, position: item.posMaster.current_holder.position, status: true, commandId: body.refIds[0].commandId, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, createdAt: new Date(), lastUpdatedAt: new Date(), commandNo: body.refIds[0].commandNo, commandYear: body.refIds[0].commandYear, posNo: shortName, posNoAbb: item.posMaster.posMasterNo, commandDateAffect: body.refIds[0].commandDateAffect, commandDateSign: body.refIds[0].commandDateSign, commandCode: body.refIds[0].commandCode, commandName: body.refIds[0].commandName, remark: body.refIds[0].remark, }; const dataAct = new ProfileActposition(); Object.assign(dataAct, metaAct); const historyAct = new ProfileActpositionHistory(); Object.assign(historyAct, { ...dataAct, id: undefined }); await this.actpositionRepository.save(dataAct); historyAct.profileActpositionId = dataAct.id; await this.actpositionHistoryRepository.save(historyAct); } }), ); return new HttpSuccess(); } @Post("command40/officer/report/delete") public async command40SalaryOfficerDelete( @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: "PENDING", })); await this.posMasterActRepository.save(data); return new HttpSuccess(); } @Post("command40/officer/report/attachment") public async command40SalaryOfficerAttachment( @Request() req: RequestWithUser, @Body() body: { refIds: { refId?: string; Sequence?: any | null; CitizenId?: any | null; Prefix?: any | null; FirstName?: any | null; LastName?: any | null; Amount?: any | null; amountSpecial?: Double | null; PositionSalaryAmount?: any | null; MouthSalaryAmount?: any | null; RemarkHorizontal?: any | null; RemarkVertical?: any | null; CommandYear?: any | null; CommandExcecuteDate?: Date | null; }[]; }, ) { 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: { id: item.refId, }, }); 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()), fullName: `${item.Prefix ?? ""}${item.FirstName ?? ""} ${item.LastName ?? ""}`, oc: /*(posMasterAct.posMasterChild?.current_holder?.position ?? "-") + "/" +*/ _organization ?? "-", postype: posMasterAct?.posMasterChild?.current_holder?.posType?.posTypeName ?? "-", poslevel: posMasterAct?.posMasterChild?.current_holder?.posLevel?.posLevelName ?? "-", organizationNew: /*(posMasterAct.posMaster?.current_holder?.position ?? "-") + "/" +*/ _organizationNew ?? "-", // date: Extension.ToThaiShortDate_noPrefix(new Date()), dateStart: item.CommandExcecuteDate ? Extension.ToThaiShortDate(item.CommandExcecuteDate) : "-", dateEnd: "-", RemarkVertical: item.RemarkVertical, RemarkHorizontal: item.RemarkHorizontal, 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; commandId?: string | null; amount: Double | null; amountSpecial?: Double | null; positionSalaryAmount: Double | null; mouthSalaryAmount: Double | null; commandNo: string | null; commandYear: number; commandDateAffect?: Date | null; commandDateSign?: Date | null; commandCode?: string | null; commandName?: string | null; remark: string | null; }[]; }, ) { await Promise.all( body.refIds.map(async (item) => { const posMaster = await this.posMasterRepository.findOne({ where: { id: item.refId }, relations: [ "orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4", "current_holder", "current_holder.posLevel", "current_holder.posType", ], }); if (!posMaster) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบตำแหน่งดังกล่าว"); } if (posMaster.next_holderId != null) { const orgRootRef = posMaster?.orgRoot ?? null; const orgChild1Ref = posMaster?.orgChild1 ?? null; const orgChild2Ref = posMaster?.orgChild2 ?? null; const orgChild3Ref = posMaster?.orgChild3 ?? null; const orgChild4Ref = posMaster?.orgChild4 ?? null; const shortName = posMaster != null && posMaster.orgChild4 != null ? `${posMaster.orgChild4.orgChild4ShortName}` : posMaster != null && posMaster.orgChild3 != null ? `${posMaster.orgChild3.orgChild3ShortName}` : posMaster != null && posMaster.orgChild2 != null ? `${posMaster.orgChild2.orgChild2ShortName}` : posMaster != null && posMaster.orgChild1 != null ? `${posMaster.orgChild1.orgChild1ShortName}` : posMaster != null && posMaster?.orgRoot != null ? `${posMaster.orgRoot.orgRootShortName}` : null; const profile = await this.profileRepository.findOne({ where: { id: posMaster.next_holderId }, }); const position = await this.positionRepository.findOne({ where: { posMasterId: posMaster.id, positionIsSelected: true, }, relations: ["posType", "posLevel"], }); 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, position: position?.positionName ?? null, positionType: position?.posType?.posTypeName ?? null, positionLevel: position?.posLevel?.posLevelName ?? null, order: dest_item == null ? 1 : dest_item.order + 1, orgRoot: orgRootRef?.orgRootName ?? null, orgChild1: orgChild1Ref?.orgChild1Name ?? null, orgChild2: orgChild2Ref?.orgChild2Name ?? null, orgChild3: orgChild3Ref?.orgChild3Name ?? null, orgChild4: orgChild4Ref?.orgChild4Name ?? null, createdUserId: req.user.sub, createdFullName: req.user.name, lastUpdateUserId: req.user.sub, lastUpdateFullName: req.user.name, createdAt: new Date(), lastUpdatedAt: new Date(), commandNo: item.commandNo, commandYear: item.commandYear, posNo: shortName, posNoAbb: posMaster.posMasterNo, commandDateAffect: item.commandDateAffect, commandDateSign: item.commandDateSign, commandCode: item.commandCode, commandName: item.commandName, remark: item.remark, }; 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); // } } }), ); // const posMasters = await this.posMasterRepository.find({ // where: { id: In(body.refIds.map((x) => x.refId)) }, // }); // const data = posMasters.map((_data) => ({ // ..._data, // current_holderId: _data.next_holderId, // next_holderId: null, // statusReport: "PENDING", // })); // await this.posMasterRepository.save(data); return new HttpSuccess(); } @Post("command38/officer/report/delete") public async command38SalaryOfficerDelete( @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: "PENDING", })); await this.posMasterRepository.save(data); if (data && data.length > 0) { const revisionId = data[0].orgRevisionId; const orgRevision = await this.orgRevisionRepo.findOne({ where: { id: revisionId }, relations: ["posMasters"], }); if ( orgRevision != null && !["REPORT", "DONE"].includes( orgRevision.posMasters.find( (x) => x.statusReport === "REPORT" || x.statusReport === "DONE", )?.statusReport || "", ) ) { await this.orgRevisionRepo.update(orgRevision.id, { isLock: false, }); } } return new HttpSuccess(); } /** * 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, isUploadAttachment: command.isUploadAttachment, }; 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)), }, }); } }