diff --git a/src/controllers/ApiKeyController.ts b/src/controllers/ApiKeyController.ts index 7de6e415..4c9664d7 100644 --- a/src/controllers/ApiKeyController.ts +++ b/src/controllers/ApiKeyController.ts @@ -20,12 +20,6 @@ import { In } from "typeorm"; import { RequestWithUser } from "../middlewares/user"; import { ApiName } from "../entities/ApiName"; import { ApiHistory } from "../entities/ApiHistory"; -import { OrgRoot } from "../entities/OrgRoot"; -import { OrgChild1 } from "../entities/OrgChild1"; -import { OrgChild2 } from "../entities/OrgChild2"; -import { OrgChild3 } from "../entities/OrgChild3"; -import { OrgChild4 } from "../entities/OrgChild4"; -import { OrgRevision } from "../entities/OrgRevision"; const jwt = require("jsonwebtoken"); @Route("api/v1/org/apiKey") @@ -39,12 +33,6 @@ export class ApiKeyController extends Controller { private apiKeyRepository = AppDataSource.getRepository(ApiKey); private apiNameRepository = AppDataSource.getRepository(ApiName); private apiHistoryRepository = AppDataSource.getRepository(ApiHistory); - private orgRootRepository = AppDataSource.getRepository(OrgRoot); - private orgChild1Repository = AppDataSource.getRepository(OrgChild1); - private orgChild2Repository = AppDataSource.getRepository(OrgChild2); - private orgChild3Repository = AppDataSource.getRepository(OrgChild3); - private orgChild4Repository = AppDataSource.getRepository(OrgChild4); - private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); /** * API ตรวจสอบและถอดรหัส JWT token @@ -163,9 +151,6 @@ export class ApiKeyController extends Controller { relations: ["apiNames", "apiHistorys"], order: { createdAt: "DESC", apiNames: { createdAt: "DESC" } }, }); - - const orgNames = await this.buildOrgNameBatch(apiKey); - const data = apiKey.map((_data) => ({ id: _data.id, createdAt: _data.createdAt, @@ -178,7 +163,6 @@ export class ApiKeyController extends Controller { dnaChild2Id: _data.dnaChild2Id, dnaChild3Id: _data.dnaChild3Id, dnaChild4Id: _data.dnaChild4Id, - orgName: orgNames.get(_data.id), apiNames: _data.apiNames.map((x) => ({ id: x.id, name: x.name, @@ -190,139 +174,10 @@ export class ApiKeyController extends Controller { return new HttpSuccess(data); } - private async buildOrgNameBatch(apiKeys: ApiKey[]): Promise> { - const currentRevision = await this.orgRevisionRepository.findOne({ - where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, - }); - - if (!currentRevision) { - return new Map(apiKeys.map((k) => [k.id, null])); - } - - const currentRevisionId = currentRevision.id; - - const rootIds = [...new Set(apiKeys.map((k) => k.dnaRootId).filter(Boolean))]; - const child1Ids = [...new Set(apiKeys.map((k) => k.dnaChild1Id).filter(Boolean))]; - const child2Ids = [...new Set(apiKeys.map((k) => k.dnaChild2Id).filter(Boolean))]; - const child3Ids = [...new Set(apiKeys.map((k) => k.dnaChild3Id).filter(Boolean))]; - const child4Ids = [...new Set(apiKeys.map((k) => k.dnaChild4Id).filter(Boolean))]; - - const [roots, child1s, child2s, child3s, child4s] = await Promise.all([ - rootIds.length > 0 - ? this.orgRootRepository.find({ - where: [ - { id: In(rootIds), orgRevisionId: currentRevisionId }, - { ancestorDNA: In(rootIds), orgRevisionId: currentRevisionId }, - ], - select: ["id", "ancestorDNA", "orgRootName"], - }) - : [], - child1Ids.length > 0 - ? this.orgChild1Repository.find({ - where: [ - { id: In(child1Ids), orgRevisionId: currentRevisionId }, - { ancestorDNA: In(child1Ids), orgRevisionId: currentRevisionId }, - ], - select: ["id", "ancestorDNA", "orgChild1Name"], - }) - : [], - child2Ids.length > 0 - ? this.orgChild2Repository.find({ - where: [ - { id: In(child2Ids), orgRevisionId: currentRevisionId }, - { ancestorDNA: In(child2Ids), orgRevisionId: currentRevisionId }, - ], - select: ["id", "ancestorDNA", "orgChild2Name"], - }) - : [], - child3Ids.length > 0 - ? this.orgChild3Repository.find({ - where: [ - { id: In(child3Ids), orgRevisionId: currentRevisionId }, - { ancestorDNA: In(child3Ids), orgRevisionId: currentRevisionId }, - ], - select: ["id", "ancestorDNA", "orgChild3Name"], - }) - : [], - child4Ids.length > 0 - ? this.orgChild4Repository.find({ - where: [ - { id: In(child4Ids), orgRevisionId: currentRevisionId }, - { ancestorDNA: In(child4Ids), orgRevisionId: currentRevisionId }, - ], - select: ["id", "ancestorDNA", "orgChild4Name"], - }) - : [], - ]); - - const rootMap = new Map( - roots.map((r) => [r.id, { name: r.orgRootName, ancestorDNA: r.ancestorDNA }]), - ); - const child1Map = new Map( - child1s.map((c) => [c.id, { name: c.orgChild1Name, ancestorDNA: c.ancestorDNA }]), - ); - const child2Map = new Map( - child2s.map((c) => [c.id, { name: c.orgChild2Name, ancestorDNA: c.ancestorDNA }]), - ); - const child3Map = new Map( - child3s.map((c) => [c.id, { name: c.orgChild3Name, ancestorDNA: c.ancestorDNA }]), - ); - const child4Map = new Map( - child4s.map((c) => [c.id, { name: c.orgChild4Name, ancestorDNA: c.ancestorDNA }]), - ); - - const result = new Map(); - for (const apiKey of apiKeys) { - if (apiKey.accessType === "ALL") { - result.set(apiKey.id, null); - continue; - } - - const parts: string[] = []; - - const getOrgName = ( - dnaId: string, - orgMap: Map, - ): string | null => { - const byId = orgMap.get(dnaId); - if (byId) return byId.name; - for (const [, value] of orgMap) { - if (value.ancestorDNA === dnaId) return value.name; - } - return null; - }; - - if (apiKey.dnaChild4Id) { - const name = getOrgName(apiKey.dnaChild4Id, child4Map); - if (name) parts.push(name); - } - if (apiKey.dnaChild3Id) { - const name = getOrgName(apiKey.dnaChild3Id, child3Map); - if (name) parts.push(name); - } - if (apiKey.dnaChild2Id) { - const name = getOrgName(apiKey.dnaChild2Id, child2Map); - if (name) parts.push(name); - } - if (apiKey.dnaChild1Id) { - const name = getOrgName(apiKey.dnaChild1Id, child1Map); - if (name) parts.push(name); - } - if (apiKey.dnaRootId) { - const name = getOrgName(apiKey.dnaRootId, rootMap); - if (name) parts.push(name); - } - - result.set(apiKey.id, parts.length > 0 ? parts.join(" ") : null); - } - - return result; - } - /** - * API รายการ Api Name + * API รายการ Api Key * - * @summary รายการ Api Name (ADMIN) + * @summary รายการ Api Key (ADMIN) * */ @Get("name") diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 474470de..eec261f2 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -41,6 +41,7 @@ import { removeProfileInOrganize, setLogDataDiff, checkReturnCommandType, + checkExceptCommandType, checkCommandType, removePostMasterAct, logPositionIsSelectedChange, @@ -2410,9 +2411,9 @@ export class CommandController extends Controller { ? "" : Extension.ToThaiNumber(Extension.ToThaiYear(command.commandYear).toString()), commandExcecuteDate: - command.commandAffectDate == null + command.commandExcecuteDate == null ? "" - : Extension.ToThaiNumber(Extension.ToThaiFullDate2(command.commandAffectDate)), + : Extension.ToThaiNumber(Extension.ToThaiFullDate2(command.commandExcecuteDate)), operators: operators.length > 0 ? operators.map((x) => ({ @@ -2617,7 +2618,6 @@ export class CommandController extends Controller { const now = new Date(); let command = new Command(); let commandCode: string = ""; - let commandSysId: string = ""; let _null: any = null; let userProfile: any = null; if ( @@ -2637,7 +2637,6 @@ export class CommandController extends Controller { if (!_command) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบคำสั่งนี้ในระบบ"); } - commandSysId = _command.commandType.commandSysId; commandCode = _command.commandType.code; command = _command; } else { @@ -2652,7 +2651,6 @@ export class CommandController extends Controller { if (!commandType) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ"); } - commandSysId = commandType.commandSysId; commandCode = commandType.code; command.detailHeader = commandType.detailHeader; command.detailBody = commandType.detailBody; @@ -2797,7 +2795,7 @@ export class CommandController extends Controller { const path = commandTypePath(commandCode); if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ"); - if (commandSysId && commandSysId.toLocaleUpperCase().trim() !== "DISCIPLINE") { + if (!["C-PM-26", "C-PM-25"].includes(commandCode)) { await new CallAPI() .PostData(request, path, { refIds: requestBody.persons.filter((x) => x.refId != null).map((x) => x.refId), @@ -4312,8 +4310,11 @@ export class CommandController extends Controller { body.data.map(async (item) => { const profile = await this.profileRepository.findOne({ where: { id: item.profileId }, + // relations: ["roleKeycloaks"], relations: { - roleKeycloaks: true + roleKeycloaks: true, + posType: true, + posLevel: true, }, }); if (!profile) { @@ -4609,8 +4610,6 @@ export class CommandController extends Controller { await this.positionRepository.save(positionNew, { data: req }); } await CreatePosMasterHistoryOfficer(posMaster.id, req); - profile.posMasterNo = getPosMasterNo(posMaster); - profile.org = getOrgFullName(posMaster); } const newMapProfileSalary = { profileId: profile.id, @@ -5627,10 +5626,20 @@ export class CommandController extends Controller { _profile.lastUpdateFullName = req.user.name; _profile.lastUpdatedAt = new Date(); if (item.isLeave == true) { - if (orgRevisionRef) { - await CreatePosMasterHistoryOfficer(orgRevisionRef.id, req, "DELETE"); + const exceptClear = await checkExceptCommandType(String(item.commandId)); + if (exceptClear.status) { + _profile.leaveReason = item.leaveReason ?? _null; + _profile.leaveCommandId = item.commandId ?? _null; + _profile.leaveCommandNo = `${item.commandNo}/${_commandYear}`; + _profile.leaveRemark = exceptClear.leaveRemark ?? _null; + _profile.leaveDate = item.commandDateAffect ?? _null; + _profile.leaveType = exceptClear.LeaveType ?? _null; + } else { + if (orgRevisionRef) { + await CreatePosMasterHistoryOfficer(orgRevisionRef.id, req, "DELETE"); + } + await removeProfileInOrganize(_profile.id, "OFFICER"); } - await removeProfileInOrganize(_profile.id, "OFFICER"); } const clearProfile = await checkCommandType(String(item.commandId)); if (clearProfile.status) { @@ -5809,22 +5818,32 @@ export class CommandController extends Controller { _profile.lastUpdateFullName = req.user.name; _profile.lastUpdatedAt = new Date(); if (item.isLeave == true) { - // บันทึกประวัติก่อนลบตำแหน่ง - const curRevision = await this.orgRevisionRepo.findOne({ - where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, - }); - if (curRevision) { - const curPosMaster = await this.employeePosMasterRepository.findOne({ - where: { - current_holderId: _profile.id, - orgRevisionId: curRevision.id, - }, + const exceptClear = await checkExceptCommandType(String(item.commandId)); + if (exceptClear.status) { + _profile.leaveReason = item.leaveReason ?? _null; + _profile.leaveCommandId = item.commandId ?? _null; + _profile.leaveCommandNo = `${item.commandNo}/${_commandYear}`; + _profile.leaveRemark = exceptClear.leaveRemark ?? _null; + _profile.leaveDate = item.commandDateAffect ?? _null; + _profile.leaveType = exceptClear.LeaveType ?? _null; + } else { + // บันทึกประวัติก่อนลบตำแหน่ง + const curRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, }); - if (curPosMaster) { - await CreatePosMasterHistoryEmployee(curPosMaster.id, req, "DELETE"); + if (curRevision) { + const curPosMaster = await this.employeePosMasterRepository.findOne({ + where: { + current_holderId: _profile.id, + orgRevisionId: curRevision.id, + }, + }); + if (curPosMaster) { + await CreatePosMasterHistoryEmployee(curPosMaster.id, req, "DELETE"); + } } + await removeProfileInOrganize(_profile.id, "EMPLOYEE"); } - await removeProfileInOrganize(_profile.id, "EMPLOYEE"); } const clearProfile = await checkCommandType(String(item.commandId)); if (clearProfile.status) { @@ -6144,22 +6163,32 @@ export class CommandController extends Controller { _profile.lastUpdateFullName = req.user.name; _profile.lastUpdatedAt = new Date(); if (item.isLeave == true) { - // บันทึกประวัติก่อนลบตำแหน่ง - const curRevision = await this.orgRevisionRepo.findOne({ - where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, - }); - if (curRevision) { - const curPosMaster = await this.employeePosMasterRepository.findOne({ - where: { - current_holderId: _profile.id, - orgRevisionId: curRevision.id, - }, + const exceptClear = await checkExceptCommandType(String(item.commandId)); + if (exceptClear.status) { + _profile.leaveReason = item.leaveReason ?? _null; + _profile.leaveCommandId = item.commandId ?? _null; + _profile.leaveCommandNo = `${item.commandNo}/${_commandYear}`; + _profile.leaveRemark = exceptClear.leaveRemark ?? _null; + _profile.leaveDate = item.commandDateAffect ?? _null; + _profile.leaveType = exceptClear.LeaveType ?? _null; + } else { + // บันทึกประวัติก่อนลบตำแหน่ง + const curRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, }); - if (curPosMaster) { - await CreatePosMasterHistoryEmployee(curPosMaster.id, req, "DELETE"); + if (curRevision) { + const curPosMaster = await this.employeePosMasterRepository.findOne({ + where: { + current_holderId: _profile.id, + orgRevisionId: curRevision.id, + }, + }); + if (curPosMaster) { + await CreatePosMasterHistoryEmployee(curPosMaster.id, req, "DELETE"); + } } + await removeProfileInOrganize(_profile.id, "EMPLOYEE"); } - await removeProfileInOrganize(_profile.id, "EMPLOYEE"); } const clearProfile = await checkCommandType(String(item.commandId)); if (clearProfile.status) { diff --git a/src/controllers/DevTestController.ts b/src/controllers/DevTestController.ts deleted file mode 100644 index e3edfaa5..00000000 --- a/src/controllers/DevTestController.ts +++ /dev/null @@ -1,576 +0,0 @@ -import { - Controller, - Post, - Put, - Patch, - Delete, - Route, - Security, - Tags, - Body, - Path, - Request, - Response, - Get, - Query, -} from "tsoa"; -import { AppDataSource } from "../database/data-source"; -import HttpStatus from "../interfaces/http-status"; -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, Any } from "typeorm"; -import { CommandType } from "../entities/CommandType"; -import { Profile, CreateProfileAllFields } from "../entities/Profile"; -import { RequestWithUser, RequestWithUserWebService } from "../middlewares/user"; -import { OrgRevision } from "../entities/OrgRevision"; -import { ProfileEmployee } from "../entities/ProfileEmployee"; -import { PosMaster } from "../entities/PosMaster"; -import permission from "../interfaces/permission"; -import { viewCurrentTenureOfficer } from "../entities/view/viewCurrentTenureOfficer"; -import { CommandController } from "./CommandController"; -import Extension from "../interfaces/extension"; -import { viewRegistryOfficer } from "../entities/view/viewRegistryOfficer"; -import { viewRegistryEmployee } from "../entities/view/viewRegistryEmployee"; -import { Registry } from "../entities/Registry"; -import { RegistryEmployee } from "../entities/RegistryEmployee"; -import { TenurePositionOfficer } from "../entities/TenurePositionOfficer"; -import { PosMasterAssign, PosMasterAssignDTO } from "../entities/PosMasterAssign"; -import { PermissionProfile } from "../entities/PermissionProfile"; -import { OrgRoot } from "../entities/OrgRoot"; -import { MetaWorkflow } from "../entities/MetaWorkflow"; -import { MetaState } from "../entities/MetaState"; -import { MetaStateOperator } from "../entities/MetaStateOperator"; -import { Workflow } from "../entities/Workflow"; -import { State } from "../entities/State"; -import { StateOperator } from "../entities/StateOperator"; -import { StateOperatorUser } from "../entities/StateOperatorUser"; -import { - commandTypePath, - calculateGovAge, - calculateAge, - calculateRetireDate, - calculateRetireLaw, - removeProfileInOrganize, - setLogDataDiff, -} from "../interfaces/utils"; -import CallAPI from "../interfaces/call-api"; -import { PostRetireToExprofile } from "./ExRetirementController" -import { Position } from "../entities/Position"; -import { PosLevel } from "../entities/PosLevel"; -import { TenureLevelOfficer } from "../entities/TenureLevelOfficer"; -import { TenurePositionEmployee } from "../entities/TenurePositionEmployee"; -import { TenureLevelEmployee } from "../entities/TenureLevelEmployee"; -import { TenurePositionExecutiveOfficer } from "../entities/TenurePositionExecutiveOfficer"; - -@Route("api/v1/org/DevTest") -@Tags("DevTest") -@Security("bearerAuth") -@Response( - HttpStatusCode.INTERNAL_SERVER_ERROR, - "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง", -) -export class DevTestController extends Controller { - private commandRepository = AppDataSource.getRepository(Command); - private commandTypeRepository = AppDataSource.getRepository(CommandType); - private orgRevisionRepo = AppDataSource.getRepository(OrgRevision); - private orgRootRepo = AppDataSource.getRepository(OrgRoot); - private posMasterRepo = AppDataSource.getRepository(PosMaster); - private profileRepo = AppDataSource.getRepository(Profile); - private profileEmpRepo = AppDataSource.getRepository(ProfileEmployee); - private registryRepo = AppDataSource.getRepository(Registry); - private registryEmployeeRepo = AppDataSource.getRepository(RegistryEmployee); - private posMasterAssignRepository = AppDataSource.getRepository(PosMasterAssign); - private permissionProfilesRepository = AppDataSource.getRepository(PermissionProfile); - private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee); - private metaWorkflowRepo = AppDataSource.getRepository(MetaWorkflow); - private metaStateRepo = AppDataSource.getRepository(MetaState); - private metaStateOperatorRepo = AppDataSource.getRepository(MetaStateOperator); - private workflowRepo = AppDataSource.getRepository(Workflow); - private stateRepo = AppDataSource.getRepository(State); - private stateOperatorRepo = AppDataSource.getRepository(StateOperator); - private stateOperatorUserRepo = AppDataSource.getRepository(StateOperatorUser); - private positionRepository = AppDataSource.getRepository(Position); - private positionOfficerRepo = AppDataSource.getRepository(TenurePositionOfficer); - private positionEmployeeRepo = AppDataSource.getRepository(TenurePositionEmployee); - private levelOfficerRepo = AppDataSource.getRepository(TenureLevelOfficer); - private levelEmployeeRepo = AppDataSource.getRepository(TenureLevelEmployee); - private positionExecutiveOfficerRepo = AppDataSource.getRepository( - TenurePositionExecutiveOfficer, - ); - - @Patch("tick-officer-registry") - public async calculateOfficerPosition( - @Request() req: RequestWithUser, - @Body() - body: { - profileIds: string[]; - }, - ) { - - console.log("1.") - /** - * =============================== - * PREPARE DATA - * =============================== - */ - const profile = await this.profileRepo.find({ - where: { id: In(body.profileIds) }, - relations: { - posLevel: true, - posType: true, - }, - }); - - if (!profile.length) return; - - const [{ today }] = await AppDataSource.query( - "SELECT CURRENT_DATE() as today", - ); - - const orgRevision = await this.orgRevisionRepo.findOne({ - select: ["id"], - where: { - orgRevisionIsDraft: false, - orgRevisionIsCurrent: true, - }, - }); - - /** - * =============================== - * TRANSACTION - * =============================== - */ - const queryRunner = AppDataSource.createQueryRunner(); - await queryRunner.connect(); - await queryRunner.startTransaction(); - console.log("2.") - try { - /** - * =============================== - * RESULT BUFFERS (SAVE ARRAY) - * =============================== - */ - const positionOfficerBulk: any[] = []; - const levelOfficerBulk: any[] = []; - const executiveOfficerBulk: any[] = []; - console.log("3.") - /** - * =============================== - * MAIN LOOP (SINGLE LOOP) - * =============================== - */ - for (const x of profile) { - const currentDate = - x.isLeave && x.leaveDate - ? Extension.toDateOnlyString(x.leaveDate) - : today; - /** - * ==================================== - * PARALLEL STORED PROCEDURES - * ==================================== - */ - const [ - positionResult, - levelResult, - executiveResult, - ] = await Promise.all([ - AppDataSource.query("CALL GetProfileSalaryPosition(?, ?)", [ - x.id, - currentDate, - ]), - AppDataSource.query("CALL GetProfileSalaryLevel(?, ?)", [ - x.id, - currentDate, - ]), - AppDataSource.query("CALL GetProfileSalaryExecutive(?, ?)", [ - x.id, - currentDate, - ]), - ]); - console.log("4.",x.id) - /** - * ==================================== - * POSITION - * ==================================== - */ - const posRows = positionResult?.[0] ?? []; - const posMap = - posRows.length > 1 - ? posRows.slice(1).map((r: any, i: number) => ({ - days_diff: Number(r.days_diff) || 0, - positionName: posRows[i]?.positionName, - })) - : []; - - const posCal = posMap - .filter((p:any) => p.positionName === x.position) - .reduce( - (a:any, c:any) => ({ - days_diff: a.days_diff + c.days_diff, - positionName: c.positionName, - }), - { days_diff: 0, positionName: null }, - ); - - positionOfficerBulk.push({ - profileId: x.id, - positionName: posCal.positionName, - days_diff: posCal.days_diff, - Years: Math.floor(posCal.days_diff / 365.2524), - Months: Math.floor((posCal.days_diff / 30.4375) % 12), - Days: Math.floor(posCal.days_diff % 30.4375), - }); - console.log("5.",x.id) - /** - * ==================================== - * 2️⃣ POSITION LEVEL - * ==================================== - */ - const lvlRows = levelResult?.[0] ?? []; - const lvlMap = - lvlRows.length > 1 - ? lvlRows.slice(1).map((r: any, i: number) => ({ - days_diff: Number(r.days_diff) || 0, - positionType: lvlRows[i]?.positionType, - positionLevel: lvlRows[i]?.positionLevel, - positionCee: lvlRows[i]?.positionCee, - })) - : []; - - const lvlCal = lvlMap - .filter( - (l:any) => - l.positionLevel === x.posLevel?.posLevelName && - l.positionType === x.posType?.posTypeName, - ) - .reduce( - (a:any, c:any) => ({ - days_diff: a.days_diff + c.days_diff, - positionType: c.positionType, - positionLevel: c.positionLevel, - positionCee: c.positionCee, - }), - { - days_diff: 0, - positionType: null, - positionLevel: null, - positionCee: null, - }, - ); - - levelOfficerBulk.push({ - profileId: x.id, - positionType: lvlCal.positionType, - positionLevel: lvlCal.positionLevel, - positionCee: lvlCal.positionCee, - days_diff: lvlCal.days_diff, - Years: x.posLevel ? (lvlCal.days_diff / 365.2524).toFixed(4) : 0, - Months: x.posLevel ? ((lvlCal.days_diff / 30.4375) % 12).toFixed(4) : 0, - Days: x.posLevel ? (lvlCal.days_diff % 30.4375).toFixed(4) : 0, - }); - console.log("6.",x.id) - /** - * ==================================== - * 3️⃣ POSITION EXECUTIVE - * ==================================== - */ - const exeRows = executiveResult?.[0] ?? []; - const exeMap = - exeRows.length > 1 - ? exeRows.slice(1).map((r: any, i: number) => ({ - days_diff: Number(r.days_diff) || 0, - positionExecutive: exeRows[i]?.positionExecutive, - })) - : []; - - const position = await this.positionRepository.findOne({ - where: { - positionIsSelected: true, - posMaster: { - orgRevisionId: orgRevision?.id, - current_holderId: x.id, - }, - }, - order: { createdAt: "DESC" }, - relations: { - posExecutive: true, - }, - }); - - const exeName = position?.posExecutive?.posExecutiveName; - - const exeCal = exeMap - .filter((e:any) => exeName && e.positionExecutive === exeName) - .reduce( - (a:any, c:any) => ({ - days_diff: a.days_diff + c.days_diff, - positionExecutive: c.positionExecutive, - }), - { days_diff: 0, positionExecutive: null }, - ); - - executiveOfficerBulk.push({ - profileId: x.id, - positionExecutiveName: exeCal.positionExecutive, - days_diff: exeCal.days_diff, - Years: (exeCal.days_diff / 365.2524).toFixed(4), - Months: ((exeCal.days_diff / 30.4375) % 12).toFixed(4), - Days: (exeCal.days_diff % 30.4375).toFixed(4), - }); - } - console.log("7.") - /** - * =============================== - * CLEAR ALL DATA AND SAVE ARRAY (BULK) - * =============================== - */ - await queryRunner.manager - .createQueryBuilder() - .delete() - .from(this.positionOfficerRepo.target) - .execute(); - - await queryRunner.manager - .createQueryBuilder() - .delete() - .from(this.levelOfficerRepo.target) - .execute(); - - await queryRunner.manager - .createQueryBuilder() - .delete() - .from(this.positionExecutiveOfficerRepo.target) - .execute(); - console.log("8.") - await queryRunner.manager.save(this.positionOfficerRepo.target, positionOfficerBulk); - await queryRunner.manager.save(this.levelOfficerRepo.target, levelOfficerBulk); - await queryRunner.manager.save(this.positionExecutiveOfficerRepo.target,executiveOfficerBulk); - console.log("9.") - /** - * =============================== - * REGISTRY OFFICER (SYNC VIEW) - * =============================== - */ - const allRegis = await queryRunner.manager - .getRepository(viewRegistryOfficer) - .createQueryBuilder("registryOfficer") - .where("registryOfficer.profileId IN (:...profileIds)", { - profileIds: new Set(profile.map((p) => p.id)) - }) - .getMany(); - - const mapRegistryData = allRegis.map((x) => ({ - ...x, - isProbation: Boolean(x.isProbation), - isLeave: Boolean(x.isLeave), - isRetirement: Boolean(x.isRetirement), - Educations: x.Educations ? JSON.stringify(x.Educations) : "", - })); - console.log("10.") - - await queryRunner.manager - .createQueryBuilder() - .delete() - .from(this.registryRepo.target) - .execute(); - - if (mapRegistryData.length > 0) { - await queryRunner.manager.save(this.registryRepo.target, mapRegistryData); - } - console.log("11.") - /** - * =============================== - * COMMIT - * =============================== - */ - await queryRunner.commitTransaction(); - } catch (error) { - await queryRunner.rollbackTransaction(); - throw error; - } finally { - await queryRunner.release(); - } - } - - @Post("getDNA") - public async GetData( - @Request() req: RequestWithUser - ){ - let _data: any = { - root: null, - child1: null, - child2: null, - child3: null, - child4: null, - }; - - _data = await new permission().PermissionOrgList(req, "COMMAND"); - return new HttpSuccess(_data); - } - - @Post("calculateGovAge") - public async calculateGovAge( - @Request() req: RequestWithUser, - @Body() - body: { - profileId: string; - }, - ){ - return new HttpSuccess(await calculateGovAge(body.profileId, "OFFICER")); - } - - /** - * @summary Test Job กวาดออกคำสั่ง ทำงานทุกๆตี2 - */ - @Post("cronjobCommand") - async CronjobCommand() { - const commandController = new CommandController(); - await commandController.cronjobCommand(); - } - - /** - * @summary payload & Endpoint ออกคำสั่ง - */ - @Put("path-excec/{id}") - async Bright( - @Path() id: string, - @Request() request: RequestWithUser, - ) { - const command = await this.commandRepository.findOne({ - where: { id: id }, - relations: ["commandType", "commandRecives", "commandSends", "commandSends.commandSendCCs"], - }); - if (!command) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้"); - } - const path = commandTypePath(command.commandType.code); - return new HttpSuccess({ - path: path + "/excecute", - refIds: command.commandRecives - .filter((x) => x.refId != null) - .map((x) => ({ - refId: x.refId, - commandNo: command.commandNo, - commandYear: command.commandYear, - commandId: command.id, - remark: command.positionDetail, - amount: x.amount, - amountSpecial: x.amountSpecial, - positionSalaryAmount: x.positionSalaryAmount, - mouthSalaryAmount: x.mouthSalaryAmount, - commandCode: command.commandType.commandCode, - commandName: command.commandType.name, - commandDateAffect: command.commandExcecuteDate, - commandDateSign: command.commandAffectDate, - })), - }); - } - - /** - * API รายละเอียดรายการคำสั่ง tab4 แนบท้าย - * @summary API รายละเอียดรายการคำสั่ง tab4 แนบท้าย - * @param {string} id Id คำสั่ง - * @param {string} profileId profileId - */ - @Get("tab4/attachment/{id}/{profileId}") - async GetByIdTab4Attachment( - @Path() id: string, - @Path() profileId: string, - @Request() request: RequestWithUser - ) { - await new permission().PermissionGet(request, "COMMAND"); - - let profile: Profile | ProfileEmployee | null = null; - profile = await this.profileRepo.findOne({ where: { id: profileId } }); - if (!profile) { - profile = await this.profileEmpRepo.findOne({ where: { id: profileId } }); - if (!profile) - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคลากรนี้"); - } - - 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 && - x.profileId != null && x.profileId == profileId - ) - .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.orgRevisionRepo.findOne({ - where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, - relations: ["posMasters", "posMasters.orgRoot"], - }); - if (orgRevisionActive != null) { - const profile = await this.profileRepo.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)), - }, - }); - } - -} diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index d785a643..8be7aa0f 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -2372,7 +2372,7 @@ export class OrganizationDotnetController extends Controller { @Security("internalAuth") async GetProfileForProcessCheckInAsync(@Path() keycloakId: string) { try { - // console.log(`[check-keycloak] START - keycloakId=${keycloakId}`); + console.log(`[check-keycloak] START - keycloakId=${keycloakId}`); /* ========================= * 1. Load profile (Officer) @@ -2447,14 +2447,14 @@ export class OrganizationDotnetController extends Controller { child4DnaId: currentHolder?.orgChild4?.ancestorDNA ?? null, }; - // console.log( - // `[check-keycloak] SUCCESS_EMPLOYEE - keycloakId=${keycloakId}, profileType=EMPLOYEE`, - // ); + console.log( + `[check-keycloak] SUCCESS_EMPLOYEE - keycloakId=${keycloakId}, profileType=EMPLOYEE`, + ); return new HttpSuccess(mapProfile); } - // console.log(`[check-keycloak] OFFICER_FOUND - keycloakId=${keycloakId}`); + console.log(`[check-keycloak] OFFICER_FOUND - keycloakId=${keycloakId}`); /* ========================================= * 2. current holder (Officer) @@ -2494,9 +2494,9 @@ export class OrganizationDotnetController extends Controller { child4DnaId: currentHolder?.orgChild4?.ancestorDNA ?? null, }; - // console.log( - // `[check-keycloak] SUCCESS_OFFICER - keycloakId=${keycloakId}, profileType=OFFICER`, - // ); + console.log( + `[check-keycloak] SUCCESS_OFFICER - keycloakId=${keycloakId}, profileType=OFFICER`, + ); return new HttpSuccess(mapProfile); } catch (error: any) { @@ -9151,30 +9151,4 @@ export class OrganizationDotnetController extends Controller { }); return new HttpSuccess(filteredPosMasters); } - - /** - * API ตรวจสอบ profileId ที่ลาออกแล้ว - * @summary API ตรวจสอบ profileId ที่ลาออกแล้ว - */ - @Post("check-isLeave") - @Security("internalAuth") - async findProfileIsLeave( - @Body() - req: { profileIds: string[] } - ) { - const profile = await this.profileRepo.find({ - select: { id: true }, - where: { - id: In(req.profileIds), - isLeave: true - } - }); - - if (profile.length === 0) { - return new HttpSuccess([]); - } - - return new HttpSuccess(profile.map(p => p.id)); - } - } diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 42b322d3..92943776 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -93,7 +93,6 @@ import { CreatePosMasterHistoryOfficer, getTopDegrees, getPosMasterPositions } f import { ProfileLeaveService } from "../services/ProfileLeaveService"; // import { PostRetireToExprofile } from "./ExRetirementController"; import { getPosNumCodeSit } from "../services/CommandService"; -import { updateHolderProfileHistory } from "../services/PositionService"; @Route("api/v1/org/profile") @Tags("Profile") @Security("bearerAuth") @@ -5775,26 +5774,29 @@ export class ProfileController extends Controller { } if (body.citizenId) { - Extension.CheckCitizen(body.citizenId); + const citizenIdDigits = body.citizenId.toString().split("").map(Number); + const cal = + citizenIdDigits[0] * 13 + + citizenIdDigits[1] * 12 + + citizenIdDigits[2] * 11 + + citizenIdDigits[3] * 10 + + citizenIdDigits[4] * 9 + + citizenIdDigits[5] * 8 + + citizenIdDigits[6] * 7 + + citizenIdDigits[7] * 6 + + citizenIdDigits[8] * 5 + + citizenIdDigits[9] * 4 + + citizenIdDigits[10] * 3 + + citizenIdDigits[11] * 2; + const calStp2 = cal % 11; + const chkDigit = (11 - calStp2) % 10; + + if (citizenIdDigits[12] !== chkDigit) { + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } } const record = await this.profileRepo.findOneBy({ id }); const before = structuredClone(record); - // เช็คว่ามี profileHistory ของ profile นี้หรือไม่ - const historyCount = await this.profileHistoryRepo.count({ - where: { profileId: id }, - }); - - // ถ้าไม่มีเลย ให้บันทึกข้อมูลเริ่มต้น (ก่อน update) ลงไปก่อน - if (historyCount === 0) { - await this.profileHistoryRepo.save( - Object.assign(new ProfileHistory(), { - ...before, - birthDateOld: before?.birthDate, - profileId: id, - id: undefined, - }), - ); - } if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); @@ -5831,9 +5833,6 @@ export class ProfileController extends Controller { } } - // บันทึกประวัติคนครองตำแหน่ง (ถ้า profile นี้ครองตำแหน่งอยู่) - await updateHolderProfileHistory(record.id, request); - return new HttpSuccess(); } @@ -7951,38 +7950,40 @@ export class ProfileController extends Controller { privacyUser: profile.privacyUser, privacyMgt: profile.privacyMgt, isDeputy: root?.isDeputy ?? false, + // root?.orgRootShortName && posMaster?.posMasterNo + // ? `${root?.orgRootShortName} ${posMaster?.posMasterNo}` + // : "", }; - const _numPart = posMaster ? [posMaster.posMasterNoPrefix, posMaster.posMasterNo, posMaster.posMasterNoSuffix].filter((p) => p !== null && p !== undefined && p !== '').join(' ') : ''; if (_profile.child4Id != null) { _profile.node = 4; _profile.nodeId = _profile.child4Id; _profile.nodeDnaId = _profile.child4DnaId; _profile.nodeShortName = _profile.child4ShortName; - _profile.posNo = `${_profile.child4ShortName} ${_numPart}`; + _profile.posNo = `${_profile.child4ShortName} ${_profile.posMasterNo}`; } else if (_profile.child3Id != null) { _profile.node = 3; _profile.nodeId = _profile.child3Id; _profile.nodeDnaId = _profile.child3DnaId; _profile.nodeShortName = _profile.child3ShortName; - _profile.posNo = `${_profile.child3ShortName} ${_numPart}`; + _profile.posNo = `${_profile.child3ShortName} ${_profile.posMasterNo}`; } else if (_profile.child2Id != null) { _profile.node = 2; _profile.nodeId = _profile.child2Id; _profile.nodeDnaId = _profile.child2DnaId; _profile.nodeShortName = _profile.child2ShortName; - _profile.posNo = `${_profile.child2ShortName} ${_numPart}`; + _profile.posNo = `${_profile.child2ShortName} ${_profile.posMasterNo}`; } else if (_profile.child1Id != null) { _profile.node = 1; _profile.nodeId = _profile.child1Id; _profile.nodeDnaId = _profile.child1DnaId; _profile.nodeShortName = _profile.child1ShortName; - _profile.posNo = `${_profile.child1ShortName} ${_numPart}`; + _profile.posNo = `${_profile.child1ShortName} ${_profile.posMasterNo}`; } else if (_profile.rootId != null) { _profile.node = 0; _profile.nodeId = _profile.rootId; _profile.nodeDnaId = _profile.rootDnaId; _profile.nodeShortName = _profile.rootShortName; - _profile.posNo = `${_profile.rootShortName} ${_numPart}`; + _profile.posNo = `${_profile.rootShortName} ${_profile.posMasterNo}`; } return new HttpSuccess(_profile); } @@ -8122,39 +8123,41 @@ export class ProfileController extends Controller { privacyUser: profile.privacyUser, privacyMgt: profile.privacyMgt, isDeputy: root?.isDeputy ?? false, + // root?.orgRootShortName && posMaster?.posMasterNo + // ? `${root?.orgRootShortName} ${posMaster?.posMasterNo}` + // : "", }; - const _numPart = posMaster ? [posMaster.posMasterNoPrefix, posMaster.posMasterNo, posMaster.posMasterNoSuffix].filter((p) => p !== null && p !== undefined && p !== '').join(' ') : ''; if (_profile.child4Id != null) { _profile.node = 4; _profile.nodeId = _profile.child4Id; _profile.nodeDnaId = _profile.child4DnaId; _profile.nodeShortName = _profile.child4ShortName; - _profile.posNo = `${_profile.child4ShortName} ${_numPart}`; + _profile.posNo = `${_profile.child4ShortName} ${posMaster?.posMasterNo}`; } else if (_profile.child3Id != null) { _profile.node = 3; _profile.nodeId = _profile.child3Id; _profile.nodeDnaId = _profile.child3DnaId; _profile.nodeShortName = _profile.child3ShortName; - _profile.posNo = `${_profile.child3ShortName} ${_numPart}`; + _profile.posNo = `${_profile.child3ShortName} ${posMaster?.posMasterNo}`; } else if (_profile.child2Id != null) { _profile.node = 2; _profile.nodeId = _profile.child2Id; _profile.nodeDnaId = _profile.child2DnaId; _profile.nodeShortName = _profile.child2ShortName; - _profile.posNo = `${_profile.child2ShortName} ${_numPart}`; + _profile.posNo = `${_profile.child2ShortName} ${posMaster?.posMasterNo}`; } else if (_profile.child1Id != null) { _profile.node = 1; _profile.nodeId = _profile.child1Id; _profile.nodeDnaId = _profile.child1DnaId; _profile.nodeShortName = _profile.child1ShortName; - _profile.posNo = `${_profile.child1ShortName} ${_numPart}`; + _profile.posNo = `${_profile.child1ShortName} ${posMaster?.posMasterNo}`; } else if (_profile.rootId != null) { _profile.node = 0; _profile.nodeId = _profile.rootId; _profile.nodeDnaId = _profile.rootDnaId; _profile.nodeShortName = _profile.rootShortName; - _profile.posNo = `${_profile.rootShortName} ${_numPart}`; + _profile.posNo = `${_profile.rootShortName} ${posMaster?.posMasterNo}`; } return new HttpSuccess(_profile); } @@ -9325,32 +9328,26 @@ export class ProfileController extends Controller { : "-", }; - const _numPart = posMaster ? [posMaster.posMasterNoPrefix, posMaster.posMasterNo, posMaster.posMasterNoSuffix].filter((p) => p !== null && p !== undefined && p !== '').join(' ') : ''; if (_profile.child4Id != null) { _profile.node = 4; _profile.nodeId = _profile.child4Id; _profile.nodeShortName = _profile.child4ShortName; - _profile.posNo = `${_profile.child4ShortName} ${_numPart}`; } else if (_profile.child3Id != null) { _profile.node = 3; _profile.nodeId = _profile.child3Id; _profile.nodeShortName = _profile.child3ShortName; - _profile.posNo = `${_profile.child3ShortName} ${_numPart}`; } else if (_profile.child2Id != null) { _profile.node = 2; _profile.nodeId = _profile.child2Id; _profile.nodeShortName = _profile.child2ShortName; - _profile.posNo = `${_profile.child2ShortName} ${_numPart}`; } else if (_profile.child1Id != null) { _profile.node = 1; _profile.nodeId = _profile.child1Id; _profile.nodeShortName = _profile.child1ShortName; - _profile.posNo = `${_profile.child1ShortName} ${_numPart}`; } else if (_profile.rootId != null) { _profile.node = 0; _profile.nodeId = _profile.rootId; _profile.nodeShortName = _profile.rootShortName; - _profile.posNo = `${_profile.rootShortName} ${_numPart}`; } return new HttpSuccess(_profile); } @@ -9531,28 +9528,38 @@ export class ProfileController extends Controller { const mapDataProfile = await Promise.all( findProfile.map(async (item: Profile) => { const fullName = `${item.prefix}${item.firstName} ${item.lastName}`; - const holder = item.current_holders?.find((x) => x.orgRevisionId == findRevision.id); - const _numPart = holder ? [holder.posMasterNoPrefix, holder.posMasterNo, holder.posMasterNoSuffix].filter((p) => p !== null && p !== undefined && p !== '').join(' ') : ''; - const shortName = !holder - ? null - : holder.orgChild4 != null - ? `${holder.orgChild4.orgChild4ShortName} ${_numPart}` - : holder.orgChild3 != null - ? `${holder.orgChild3.orgChild3ShortName} ${_numPart}` - : holder.orgChild2 != null - ? `${holder.orgChild2.orgChild2ShortName} ${_numPart}` - : holder.orgChild1 != null - ? `${holder.orgChild1.orgChild1ShortName} ${_numPart}` - : holder.orgRoot != null - ? `${holder.orgRoot.orgRootShortName} ${_numPart}` - : null; + const shortName = + item.current_holders.length == 0 + ? null + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild4.orgChild4ShortName} ${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3 != + null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild3.orgChild3ShortName} ${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild2 != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild2.orgChild2ShortName} ${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgChild1 != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgChild1.orgChild1ShortName} ${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != + null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id) + ?.orgRoot != null + ? `${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot.orgRootShortName} ${item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.posMasterNo}` + : null; const root = item.current_holders.length == 0 || - (holder != null && - holder?.orgRoot == null) + (item.current_holders.find((x) => x.orgRevisionId == findRevision.id) != null && + item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot == null) ? null - : holder?.orgRoot; + : item.current_holders.find((x) => x.orgRevisionId == findRevision.id)?.orgRoot; const rootHolder = item.current_holders?.find( (x) => x.orgRevisionId == findRevision.id, diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 59f9e91d..f79e27cd 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -84,7 +84,6 @@ import { ProfileDuty } from "../entities/ProfileDuty"; import { CreatePosMasterHistoryEmployee, getTopDegrees } from "../services/PositionService"; import { ProfileLeaveService } from "../services/ProfileLeaveService"; import { CommandCode } from "../entities/CommandCode"; -import { updateHolderProfileHistory } from "../services/PositionService"; @Route("api/v1/org/profile-employee") @Tags("ProfileEmployee") @Security("bearerAuth") @@ -2382,27 +2381,28 @@ export class ProfileEmployeeController extends Controller { } if (body.citizenId) { - Extension.CheckCitizen(body.citizenId); + const citizenIdDigits = body.citizenId.toString().split("").map(Number); + const cal = + citizenIdDigits[0] * 13 + + citizenIdDigits[1] * 12 + + citizenIdDigits[2] * 11 + + citizenIdDigits[3] * 10 + + citizenIdDigits[4] * 9 + + citizenIdDigits[5] * 8 + + citizenIdDigits[6] * 7 + + citizenIdDigits[7] * 6 + + citizenIdDigits[8] * 5 + + citizenIdDigits[9] * 4 + + citizenIdDigits[10] * 3 + + citizenIdDigits[11] * 2; + const calStp2 = cal % 11; + const chkDigit = (11 - calStp2) % 10; + + if (citizenIdDigits[12] !== chkDigit) { + throw new HttpError(HttpStatus.NOT_FOUND, "ข้อมูลรหัสบัตรประจำตัวประชาชนไม่ถูกต้อง"); + } } const record = await this.profileRepo.findOneBy({ id }); - const before = structuredClone(record); - // เช็คว่ามี profileHistory ของ profile นี้หรือไม่ - const historyCount = await this.profileHistoryRepo.count({ - where: { profileEmployeeId: id }, - }); - - // ถ้าไม่มีเลย ให้บันทึกข้อมูลเริ่มต้น (ก่อน update) ลงไปก่อน - if (historyCount === 0) { - await this.profileHistoryRepo.save( - Object.assign(new ProfileEmployeeHistory(), { - ...before, - birthDateOld: before?.birthDate, - profileEmployeeId: id, - id: undefined, - }), - ); - } - if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); if (body.employeeClass == null || body.employeeClass == undefined || body.employeeClass == "") { @@ -2434,8 +2434,6 @@ export class ProfileEmployeeController extends Controller { }), ); await this.profileRepo.save(record); - // บันทึกประวัติคนครองตำแหน่ง (ถ้า profile นี้ครองตำแหน่งอยู่) - await updateHolderProfileHistory(record.id, request, "EMPLOYEE"); return new HttpSuccess(); } @@ -4021,38 +4019,40 @@ export class ProfileEmployeeController extends Controller { salary: profile ? profile.amount : null, amountSpecial: profile ? profile.amountSpecial : null, posNo: null, + // root?.orgRootShortName && posMaster?.posMasterNo + // ? `${root?.orgRootShortName} ${posMaster?.posMasterNo}` + // : "", }; - const _numPart = posMaster ? [posMaster.posMasterNoPrefix, posMaster.posMasterNo, posMaster.posMasterNoSuffix].filter((p) => p !== null && p !== undefined && p !== '').join(' ') : ''; if (_profile.child4Id != null) { _profile.node = 4; _profile.nodeId = _profile.child4Id; _profile.nodeDnaId = _profile.child4DnaId; _profile.nodeShortName = _profile.child4ShortName; - _profile.posNo = `${_profile.child4ShortName} ${_numPart}`; + _profile.posNo = `${_profile.child4ShortName} ${_profile.posMasterNo}`; } else if (_profile.child3Id != null) { _profile.node = 3; _profile.nodeId = _profile.child3Id; _profile.nodeDnaId = _profile.child3DnaId; _profile.nodeShortName = _profile.child3ShortName; - _profile.posNo = `${_profile.child3ShortName} ${_numPart}`; + _profile.posNo = `${_profile.child3ShortName} ${_profile.posMasterNo}`; } else if (_profile.child2Id != null) { _profile.node = 2; _profile.nodeId = _profile.child2Id; _profile.nodeDnaId = _profile.child2DnaId; _profile.nodeShortName = _profile.child2ShortName; - _profile.posNo = `${_profile.child2ShortName} ${_numPart}`; + _profile.posNo = `${_profile.child2ShortName} ${_profile.posMasterNo}`; } else if (_profile.child1Id != null) { _profile.node = 1; _profile.nodeId = _profile.child1Id; _profile.nodeDnaId = _profile.child1DnaId; _profile.nodeShortName = _profile.child1ShortName; - _profile.posNo = `${_profile.child1ShortName} ${_numPart}`; + _profile.posNo = `${_profile.child1ShortName} ${_profile.posMasterNo}`; } else if (_profile.rootId != null) { _profile.node = 0; _profile.nodeId = _profile.rootId; _profile.nodeDnaId = _profile.rootDnaId; _profile.nodeShortName = _profile.rootShortName; - _profile.posNo = `${_profile.rootShortName} ${_numPart}`; + _profile.posNo = `${_profile.rootShortName} ${_profile.posMasterNo}`; } return new HttpSuccess(_profile); } @@ -6460,7 +6460,33 @@ export class ProfileEmployeeController extends Controller { null ? null : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild4; - const _numPart = posMaster ? [posMaster.posMasterNoPrefix, posMaster.posMasterNo, posMaster.posMasterNoSuffix].filter((p) => p !== null && p !== undefined && p !== '').join(' ') : ''; + const shortName = + profile.current_holders.length == 0 + ? null + : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) != null && + profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) + ?.orgChild4 != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild4.orgChild4ShortName} ${profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.posMasterNo}` + : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) != null && + profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) + ?.orgChild3 != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild3.orgChild3ShortName} ${profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.posMasterNo}` + : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) != + null && + profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) + ?.orgChild2 != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild2.orgChild2ShortName} ${profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.posMasterNo}` + : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) != + null && + profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) + ?.orgChild1 != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgChild1.orgChild1ShortName} ${profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.posMasterNo}` + : profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) != + null && + profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id) + ?.orgRoot != null + ? `${profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.orgRoot.orgRootShortName} ${profile.current_holders.find((x) => x.orgRevisionId == orgRevisionPublish.id)?.posMasterNo}` + : null; const _profile: any = { profileId: profile.id, prefix: profile.prefix, @@ -6502,7 +6528,7 @@ export class ProfileEmployeeController extends Controller { child4ShortName: child4 == null ? null : child4.orgChild4ShortName, node: null, nodeId: null, - posNo: null, + posNo: shortName, salary: profile.amount, education: profile && profile.profileEducations.length > 0 @@ -6517,27 +6543,22 @@ export class ProfileEmployeeController extends Controller { _profile.node = 4; _profile.nodeId = _profile.child4Id; _profile.nodeShortName = _profile.child4ShortName; - _profile.posNo = `${_profile.child4ShortName} ${_numPart}`; } else if (_profile.child3Id != null) { _profile.node = 3; _profile.nodeId = _profile.child3Id; _profile.nodeShortName = _profile.child3ShortName; - _profile.posNo = `${_profile.child3ShortName} ${_numPart}`; } else if (_profile.child2Id != null) { _profile.node = 2; _profile.nodeId = _profile.child2Id; _profile.nodeShortName = _profile.child2ShortName; - _profile.posNo = `${_profile.child2ShortName} ${_numPart}`; } else if (_profile.child1Id != null) { _profile.node = 1; _profile.nodeId = _profile.child1Id; _profile.nodeShortName = _profile.child1ShortName; - _profile.posNo = `${_profile.child1ShortName} ${_numPart}`; } else if (_profile.rootId != null) { _profile.node = 0; _profile.nodeId = _profile.rootId; _profile.nodeShortName = _profile.rootShortName; - _profile.posNo = `${_profile.rootShortName} ${_numPart}`; } return new HttpSuccess(_profile); } diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index 406dce69..a8c017ae 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -1001,24 +1001,6 @@ export class ProfileEmployeeTempController extends Controller { } const record = await this.profileRepo.findOneBy({ id }); - const before = structuredClone(record); - // เช็คว่ามี profileHistory ของ profile นี้หรือไม่ - const historyCount = await this.profileHistoryRepo.count({ - where: { profileEmployeeId: id }, - }); - - // ถ้าไม่มีเลย ให้บันทึกข้อมูลเริ่มต้น (ก่อน update) ลงไปก่อน - if (historyCount === 0) { - await this.profileHistoryRepo.save( - Object.assign(new ProfileEmployeeHistory(), { - ...before, - birthDateOld: before?.birthDate, - profileEmployeeId: id, - id: undefined, - }), - ); - } - if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); if (body.employeeClass == null || body.employeeClass == undefined || body.employeeClass == "") { diff --git a/src/interfaces/utils.ts b/src/interfaces/utils.ts index 3a48ab2b..be2f3bf9 100644 --- a/src/interfaces/utils.ts +++ b/src/interfaces/utils.ts @@ -280,7 +280,7 @@ export async function removeProfileInOrganize(profileId: string, type: string) { await AppDataSource.getRepository(PosMaster) .createQueryBuilder() .update(PosMaster) - .set({ current_holderId: null, isSit: false }) + .set({ current_holderId: null }) .where("id = :id", { id: findProfileInposMaster?.id }) .execute(); @@ -293,7 +293,7 @@ export async function removeProfileInOrganize(profileId: string, type: string) { await AppDataSource.getRepository(PosMaster) .createQueryBuilder() .update(PosMaster) - .set({ next_holderId: null, isSit: false }) + .set({ next_holderId: null }) .where("id = :id", { id: findProfileInposMasterDraft?.id }) .execute(); @@ -326,7 +326,7 @@ export async function removeProfileInOrganize(profileId: string, type: string) { await AppDataSource.getRepository(EmployeePosMaster) .createQueryBuilder() .update(EmployeePosMaster) - .set({ current_holderId: null, isSit: false }) + .set({ current_holderId: null }) .where("id = :id", { id: findProfileInEmpPosMaster?.id }) .execute(); @@ -395,6 +395,43 @@ export async function checkReturnCommandType(commandId: string) { return true; } +export async function checkExceptCommandType(commandId: string) { + const commandRepository = AppDataSource.getRepository(Command); + const commandReciveRepository = AppDataSource.getRepository(CommandRecive); + const _type = await commandRepository.findOne({ + where: { + id: commandId, + }, + relations: ["commandType"], + }); + if (!["C-PM-25", "C-PM-26"].includes(String(_type?.commandType.code))) { + return { status: false, LeaveType: null, leaveRemark: null }; + } + const _commandRecive = await commandReciveRepository.findOne({ + where: { commandId: commandId }, + }); + + let _leaveType: string = ""; + switch (String(_type?.commandType.code)) { + case "C-PM-25": { + _leaveType = "DISCIPLINE_SUSPEND"; //คำสั่งพักจากราชการ + break; + } + case "C-PM-26": { + _leaveType = "DISCIPLINE_TEMP_SUSPEND"; //คำสั่งให้ออกจากราชการไว้ก่อน + break; + } + default: { + _leaveType = ""; + } + } + return { + status: true, + LeaveType: _leaveType, + leaveRemark: _commandRecive ? _commandRecive.remarkVertical : null, + }; +} + export async function checkCommandType(commandId: string) { const commandRepository = AppDataSource.getRepository(Command); const commandReciveRepository = AppDataSource.getRepository(CommandRecive); @@ -414,8 +451,6 @@ export async function checkCommandType(commandId: string) { "C-PM-23", "C-PM-19", "C-PM-20", - "C-PM-25", - "C-PM-26", "C-PM-43", ].includes(String(_type?.commandType.code)) ) { @@ -465,16 +500,6 @@ export async function checkCommandType(commandId: string) { _retireTypeName = "ลาออกจากราชการ"; break; } - case "C-PM-25": { - _leaveType = "DISCIPLINE_SUSPEND"; - _retireTypeName = "พักจากราชการ"; - break; - } - case "C-PM-26": { - _leaveType = "DISCIPLINE_TEMP_SUSPEND"; - _retireTypeName = "ให้ออกจากราชการไว้ก่อน"; - break; - } case "C-PM-43": { _leaveType = "RETIRE_OUT_EMP"; _retireTypeName = "ให้ออกจากราชการ"; diff --git a/src/middlewares/authInternal.ts b/src/middlewares/authInternal.ts index 77da531c..d3d9a5b6 100644 --- a/src/middlewares/authInternal.ts +++ b/src/middlewares/authInternal.ts @@ -19,7 +19,7 @@ export async function handleInternalAuth(request: express.Request) { throw new HttpError(HttpStatus.UNAUTHORIZED, "Invalid API Key"); } - // console.log(`[InternalAuth] Authentication successful`); + console.log(`[InternalAuth] Authentication successful`); return { sub: "internal_service",