diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index c1e98a6c..b5e40dd4 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -93,6 +93,7 @@ import { Gender } from "../entities/Gender"; import { ProfileAvatar } from "../entities/ProfileAvatar"; import { CreatePosMasterHistoryEmployee, + CreatePosMasterHistoryEmployeeTemp, CreatePosMasterHistoryOfficer, } from "../services/PositionService"; @Route("api/v1/org/command") @@ -2175,8 +2176,7 @@ export class CommandController extends Controller { command.isDraft = true; command.isSign = true; command.status = "PENDING"; - } - else { + } else { command.status = "NEW"; } command.issue = commandType.name; @@ -3171,7 +3171,6 @@ export class CommandController extends Controller { await CreatePosMasterHistoryOfficer(posMasterOld.id, req); } await this.posMasterRepository.save(posMaster); - await CreatePosMasterHistoryOfficer(posMaster.id, req); const positionNew = await this.positionRepository.findOne({ where: { @@ -3189,6 +3188,7 @@ export class CommandController extends Controller { await this.profileRepository.save(profile); await this.positionRepository.save(positionNew); } + await CreatePosMasterHistoryOfficer(posMaster.id, req); }), ); @@ -3363,8 +3363,6 @@ export class CommandController extends Controller { await CreatePosMasterHistoryEmployee(posMasterOld.id, req); } await this.employeePosMasterRepository.save(posMaster); - await CreatePosMasterHistoryEmployee(posMaster.id, req); - const positionNew = await this.employeePositionRepository.findOne({ where: { id: item.positionId, @@ -3383,6 +3381,7 @@ export class CommandController extends Controller { await this.profileEmployeeRepository.save(profile); await this.employeePositionRepository.save(positionNew); } + await CreatePosMasterHistoryEmployee(posMaster.id, req); }), ); @@ -3541,10 +3540,29 @@ export class CommandController extends Controller { profile.lastUpdateUserId = req.user.sub; profile.lastUpdateFullName = req.user.name; profile.lastUpdatedAt = new Date(); + const clearProfile = await checkCommandType(String(item.commandId)); + + //ปั๊มประวัติก่อนลบตำแหน่ง + const curRevision = await this.orgRevisionRepo.findOne({ + where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false } + }) + + if(curRevision){ + const curPosMaster = await this.posMasterRepository.findOne({ + where:{ + current_holderId: profile.id, + orgRevisionId: curRevision.id + } + }) + if (curPosMaster && clearProfile.LeaveType != "RETIRE_OUT_EMP") { + await CreatePosMasterHistoryOfficer(curPosMaster.id, req); + } + } + + //ลบตำแหน่ง 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); @@ -3593,7 +3611,6 @@ export class CommandController extends Controller { posMaster.conditionReason = _null; posMaster.isCondition = false; await this.posMasterRepository.save(posMaster); - await CreatePosMasterHistoryOfficer(posMaster.id, req); const positionNew = await this.positionRepository.findOne({ where: { posMasterId: posMaster.id, @@ -3603,6 +3620,7 @@ export class CommandController extends Controller { positionNew.positionIsSelected = true; await this.positionRepository.save(positionNew, { data: req }); } + await CreatePosMasterHistoryOfficer(posMaster.id, req); } const newMapProfileSalary = { profileId: profile.id, @@ -6056,7 +6074,6 @@ export class CommandController extends Controller { await CreatePosMasterHistoryOfficer(posMasterOld.id, req); } await this.posMasterRepository.save(posMaster); - await CreatePosMasterHistoryOfficer(posMaster.id, req); const positionNew = await this.positionRepository.findOne({ where: { @@ -6074,6 +6091,7 @@ export class CommandController extends Controller { setLogDataDiff(req, { before, after: profile }); await this.positionRepository.save(positionNew, { data: req }); } + await CreatePosMasterHistoryOfficer(posMaster.id, req); } // Insignia if (_oldInsigniaIds.length > 0) { @@ -6471,6 +6489,12 @@ export class CommandController extends Controller { })); await this.employeePositionRepository.save(clearTempPosition); } + await Promise.all( + clsTempPosmaster.map( + async (posMasterTemp) => + await CreatePosMasterHistoryEmployeeTemp(posMasterTemp.id, req), + ), + ); } const positionNew = await this.employeePositionRepository.findOne({ @@ -6570,6 +6594,7 @@ export class CommandController extends Controller { }); await this.profileEmployeeRepository.save(profile); await this.employeePositionRepository.save(positionNew); + await CreatePosMasterHistoryEmployee(posMaster.id, req); //ลบออกคนออกจากโครงสร้างลูกจ้างชั่วคราว const posMasterTemp = await this.employeeTempPosMasterRepository.findOne({ where: { @@ -6581,6 +6606,7 @@ export class CommandController extends Controller { await this.employeeTempPosMasterRepository.update(posMasterTemp.id, { current_holderId: _null, }); + await CreatePosMasterHistoryEmployeeTemp(posMasterTemp.id, req); } } }), diff --git a/src/controllers/DevelopmentRequestController.ts b/src/controllers/DevelopmentRequestController.ts index ba4e8458..752ae293 100644 --- a/src/controllers/DevelopmentRequestController.ts +++ b/src/controllers/DevelopmentRequestController.ts @@ -45,13 +45,15 @@ export class DevelopmentRequestController extends Controller { @Query("page") page: number = 1, @Query("pageSize") pageSize: number = 10, @Query("status") status?: string, + @Query("sortBy") sortBy?: string, + @Query("descending") descending?: boolean, ) { const profile = await this.profileRepository.findOneBy({ keycloak: req.user.sub }); if (!profile) { throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว"); } - - const [lists, total] = await AppDataSource.getRepository(DevelopmentRequest) + + let query = await AppDataSource.getRepository(DevelopmentRequest) .createQueryBuilder("developmentRequest") .andWhere( status == undefined || status.trim().toUpperCase() == "ALL" || status == "" @@ -103,6 +105,15 @@ export class DevelopmentRequestController extends Controller { }), ) .orderBy("developmentRequest.createdAt", "DESC") + + if (sortBy) { + query = query.orderBy( + `developmentRequest.${sortBy}`, + descending ? "DESC" : "ASC" + ); + } + + const [lists, total] = await query .skip((page - 1) * pageSize) .take(pageSize) .getManyAndCount(); diff --git a/src/controllers/EmployeePositionController.ts b/src/controllers/EmployeePositionController.ts index 632261e0..b0091494 100644 --- a/src/controllers/EmployeePositionController.ts +++ b/src/controllers/EmployeePositionController.ts @@ -39,8 +39,8 @@ import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; import { setLogDataDiff } from "../interfaces/utils"; import { - CreatePosMasterHistoryOfficer, CreatePosMasterHistoryEmployee, + CreatePosMasterHistoryOfficer, } from "../services/PositionService"; import { PosMasterEmployeeHistory } from "../entities/PosMasterEmployeeHistory"; @Route("api/v1/org/employee/pos") @@ -2288,7 +2288,7 @@ export class EmployeePositionController extends Controller { await new permission().PermissionDelete(request, "SYS_ORG_EMP"); const dataMaster = await this.employeePosMasterRepository.findOne({ where: { id: id }, - relations: ["positions"], + relations: ["positions", "orgRevision"], }); if (!dataMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); @@ -2321,7 +2321,9 @@ export class EmployeePositionController extends Controller { positionIsSelected: false, }); }); - + if (dataMaster.orgRevision.orgRevisionIsCurrent) { + await CreatePosMasterHistoryEmployee(dataMaster.id, request); + } return new HttpSuccess(); } @@ -2451,7 +2453,6 @@ export class EmployeePositionController extends Controller { await CreatePosMasterHistoryEmployee(posMasterOld.id, request); } await this.employeePosMasterRepository.save(posMaster); - await CreatePosMasterHistoryEmployee(posMaster.id, request); const positionNew = await this.employeePositionRepository.findOne({ where: { @@ -2472,6 +2473,7 @@ export class EmployeePositionController extends Controller { await this.profileRepository.save(profile); await this.employeePositionRepository.save(positionNew); } + await CreatePosMasterHistoryEmployee(posMaster.id, request); return new HttpSuccess(); } diff --git a/src/controllers/EmployeeTempPositionController.ts b/src/controllers/EmployeeTempPositionController.ts index 5d304de0..7f526c8d 100644 --- a/src/controllers/EmployeeTempPositionController.ts +++ b/src/controllers/EmployeeTempPositionController.ts @@ -2024,7 +2024,7 @@ export class EmployeeTempPositionController extends Controller { await new permission().PermissionDelete(request, "SYS_ORG_TEMP"); const dataMaster = await this.employeeTempPosMasterRepository.findOne({ where: { id: id }, - relations: ["positions"], + relations: ["positions", "orgRevision"], }); if (!dataMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); @@ -2057,6 +2057,9 @@ export class EmployeeTempPositionController extends Controller { positionIsSelected: false, }); }); + if (dataMaster.orgRevision.orgRevisionIsCurrent) { + await CreatePosMasterHistoryEmployeeTemp(dataMaster.id, request); + } return new HttpSuccess(); } @@ -2187,7 +2190,6 @@ export class EmployeeTempPositionController extends Controller { await CreatePosMasterHistoryEmployeeTemp(posMasterOld.id, request); } await this.employeeTempPosMasterRepository.save(posMaster); - await CreatePosMasterHistoryEmployeeTemp(posMaster.id, request); const positionNew = await this.employeePositionRepository.findOne({ where: { @@ -2208,6 +2210,7 @@ export class EmployeeTempPositionController extends Controller { await this.profileRepository.save(profile); await this.employeePositionRepository.save(positionNew); } + await CreatePosMasterHistoryEmployeeTemp(posMaster.id, request); return new HttpSuccess(); } diff --git a/src/controllers/OrganizationDotnetController.ts b/src/controllers/OrganizationDotnetController.ts index 01cec5dd..415759af 100644 --- a/src/controllers/OrganizationDotnetController.ts +++ b/src/controllers/OrganizationDotnetController.ts @@ -4724,6 +4724,8 @@ export class OrganizationDotnetController extends Controller { firstName: item.firstName, lastName: item.lastName, citizenId: item.citizenId, + dateStart: item.dateStart, + dateAppoint: item.dateAppoint, keycloak: item.keycloak, posNo: shortName, position: item.position, @@ -5284,6 +5286,8 @@ export class OrganizationDotnetController extends Controller { firstName: item.firstName, lastName: item.lastName, citizenId: item.citizenId, + dateStart: item.dateStart, + dateAppoint: item.dateAppoint, keycloak: item.keycloak, posNo: shortName, position: item.position, diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 6ffa9708..5279876a 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -961,37 +961,42 @@ export class PositionController extends Controller { if (!posMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - let orgRoot: any = null; let SName: any = null; + let revisionId:any = null; if (requestBody.orgRootId != null) orgRoot = await this.orgRootRepository.findOne({ where: { id: requestBody.orgRootId }, }); + revisionId = orgRoot?.orgRevisionId; if (!orgRoot) { let orgChild1: any = null; if (requestBody.orgChild1Id != null) orgChild1 = await this.child1Repository.findOne({ where: { id: requestBody.orgChild1Id }, }); + revisionId = orgChild1?.orgRevisionId; if (!orgChild1) { let orgChild2: any = null; if (requestBody.orgChild2Id != null) orgChild2 = await this.child2Repository.findOne({ where: { id: requestBody.orgChild2Id }, }); + revisionId = orgChild2?.orgRevisionId; if (!orgChild2) { let orgChild3: any = null; if (requestBody.orgChild3Id != null) orgChild3 = await this.child3Repository.findOne({ where: { id: requestBody.orgChild3Id }, }); + revisionId = orgChild3?.orgRevisionId; if (!orgChild3) { let orgChild4: any = null; if (requestBody.orgChild4Id != null) orgChild4 = await this.child4Repository.findOne({ where: { id: requestBody.orgChild4Id }, }); + revisionId = orgChild4?.orgRevisionId; if (!orgChild4) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโครงสร้าง"); } else { @@ -1089,7 +1094,6 @@ export class PositionController extends Controller { posMaster.orgRevisionId = orgRoot.orgRevisionId; SName = orgRoot.orgRootShortName; } - const chk_SName0 = await this.posMasterRepository.findOne({ where: { orgRevisionId: posMaster.orgRevisionId, @@ -1105,7 +1109,6 @@ export class PositionController extends Controller { "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", ); } - const chk_SName1 = await this.posMasterRepository.findOne({ where: { orgRevisionId: posMaster.orgRevisionId, @@ -1121,7 +1124,6 @@ export class PositionController extends Controller { "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", ); } - const chk_SName2 = await this.posMasterRepository.findOne({ where: { orgRevisionId: posMaster.orgRevisionId, @@ -1137,7 +1139,6 @@ export class PositionController extends Controller { "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", ); } - const chk_SName3 = await this.posMasterRepository.findOne({ where: { orgRevisionId: posMaster.orgRevisionId, @@ -1153,7 +1154,6 @@ export class PositionController extends Controller { "ไม่สามารถใส่เลขที่ตำแหน่งซ้ำกันได้", ); } - const chk_SName4 = await this.posMasterRepository.findOne({ where: { orgRevisionId: posMaster.orgRevisionId, @@ -1169,13 +1169,26 @@ export class PositionController extends Controller { ); } const before = null; + let chkRevision:any = null; + if(revisionId){ + chkRevision = await this.orgRevisionRepository.findOne({ + where: { id: revisionId }, + }); + } posMaster.createdUserId = request.user.sub; posMaster.createdFullName = request.user.name; posMaster.lastUpdateUserId = request.user.sub; posMaster.lastUpdateFullName = request.user.name; posMaster.createdAt = new Date(); posMaster.lastUpdatedAt = new Date(); - await this.posMasterRepository.save(posMaster, { data: request }); + const saved = await this.posMasterRepository.save(posMaster, { data: request }); + + // รอบสอง set ancestorDNA = id ที่เพิ่งได้มา + if (chkRevision?.orgRevisionIsCurrent) { + saved.ancestorDNA = saved.id; //โครงสร้างปัจจุบันเอาตัวเองเป็น dna + await this.posMasterRepository.save(saved, { data: request }); + } + setLogDataDiff(request, { before, after: posMaster }); await Promise.all( requestBody.positions.map(async (x: any) => { @@ -3676,11 +3689,16 @@ export class PositionController extends Controller { await new permission().PermissionDelete(request, "SYS_ORG"); const dataMaster = await this.posMasterRepository.findOne({ where: { id: id }, - relations: ["positions"], + relations: ["positions", "orgRevision"], }); if (!dataMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); } + + if (dataMaster.orgRevision.orgRevisionIsCurrent) { + await CreatePosMasterHistoryOfficer(dataMaster.id, request); + } + let _profileId: string = ""; if (dataMaster?.current_holderId) { _profileId = dataMaster?.current_holderId; @@ -4087,17 +4105,17 @@ export class PositionController extends Controller { }); posLevel = await this.posLevelRepository.find({ where: { - posTypeId: In(posType.map((x: any) => x.id)), - posLevelName: In(["ทรงคุณวุฒิ", "สูง"]) - }, + posTypeId: In(posType.map((x: any) => x.id)), + posLevelName: In(["ทรงคุณวุฒิ", "สูง"]), + }, }); - conditionA = "positions.posTypeId IN (:...posTypeIds) AND positions.posLevelId IN (:...posLevelIds)"; + conditionA = + "positions.posTypeId IN (:...posTypeIds) AND positions.posLevelId IN (:...posLevelIds)"; params = { posTypeIds: posType.map((x: any) => x.id), posLevelIds: posLevel.map((x: any) => x.id), }; - } - else { + } else { posType = await this.posTypeRepository.findOne({ where: { id: String(body.posType) }, }); @@ -4195,13 +4213,13 @@ export class PositionController extends Controller { .andWhere( new Brackets((qb) => { qb.andWhere(typeCondition) - // .andWhere(conditionA == null ? "1=1" : conditionA, { - // posType: posType == null ? `%%` : `${posType.id}`, - // posLevel: posLevel == null ? `%%` : `${posLevel.id}`, - // posTypeRank: posType == null ? 0 : posType.posTypeRank, - // posLevelRank: posLevel == null ? 0 : posLevel.posLevelRank, - // }); - .andWhere(conditionA, params); + // .andWhere(conditionA == null ? "1=1" : conditionA, { + // posType: posType == null ? `%%` : `${posType.id}`, + // posLevel: posLevel == null ? `%%` : `${posLevel.id}`, + // posTypeRank: posType == null ? 0 : posType.posTypeRank, + // posLevelRank: posLevel == null ? 0 : posLevel.posLevelRank, + // }); + .andWhere(conditionA, params); }), ) .orderBy("orgRoot.orgRootOrder", "ASC") @@ -4924,7 +4942,6 @@ export class PositionController extends Controller { await CreatePosMasterHistoryOfficer(posMasterOld.id, request); } await this.posMasterRepository.save(posMaster); - await CreatePosMasterHistoryOfficer(posMaster.id, request); const positionNew = await this.positionRepository.findOne({ where: { @@ -4940,6 +4957,8 @@ export class PositionController extends Controller { await this.profileRepository.save(profile); await this.positionRepository.save(positionNew); } + await CreatePosMasterHistoryOfficer(posMaster.id, request); + return new HttpSuccess(); } diff --git a/src/controllers/SocketController.ts b/src/controllers/SocketController.ts new file mode 100644 index 00000000..eb0b72c2 --- /dev/null +++ b/src/controllers/SocketController.ts @@ -0,0 +1,25 @@ +import { Body, Controller, Post, Route } from "tsoa"; +import { sendWebSocket } from "../services/webSocket"; + +@Route("/api/v1/org/through-socket") +export class SocketController extends Controller { + @Post("notify") + async notify( + @Body() + payload: { + message: string; + userId?: string | string[]; + roles?: string | string[]; + error?: boolean; + }, + ) { + sendWebSocket( + "socket-notification", + { success: !payload.error, message: payload.message }, + { + roles: payload.roles || [], + userId: payload.userId || [], + }, + ); + } +} diff --git a/src/services/PositionService.ts b/src/services/PositionService.ts index cbf842bb..87672292 100644 --- a/src/services/PositionService.ts +++ b/src/services/PositionService.ts @@ -38,9 +38,9 @@ export async function CreatePosMasterHistoryOfficer( const h = new PosMasterHistory(); const selectedPosition = pm.positions.length > 0 - ? pm.positions.find((p) => p.positionIsSelected === true) ?? null + ? pm.positions.find((p) => p.positionIsSelected === true) ?? null : null; - h.ancestorDNA = pm.ancestorDNA; + h.ancestorDNA = pm.ancestorDNA? pm.ancestorDNA : _null; h.prefix = pm.current_holder?.prefix || _null; h.firstName = pm.current_holder?.firstName || _null; h.lastName = pm.current_holder?.lastName || _null;