diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index e97abce5..da0ed3fe 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -48,7 +48,6 @@ import { import { Position } from "../entities/Position"; import { PosMaster } from "../entities/PosMaster"; import { EmployeePosition } from "../entities/EmployeePosition"; -import { getPosMasterNo, getOrgFullName } from "../utils/org-formatting"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { ProfileDiscipline } from "../entities/ProfileDiscipline"; import { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory"; @@ -3661,7 +3660,6 @@ export class CommandController extends Controller { const posMaster = await this.posMasterRepository.findOne({ where: { id: item.posmasterId }, - relations: ["orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], }); if (posMaster == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); @@ -3717,22 +3715,14 @@ export class CommandController extends Controller { id: item.positionId, posMasterId: item.posmasterId, }, - relations: ["posExecutive"], }); // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ if (positionNew != null) { positionNew.positionIsSelected = true; - // อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit - profile.posMasterNo = getPosMasterNo(posMaster); - profile.org = getOrgFullName(posMaster); if(!posMaster.isSit){ profile.posLevelId = positionNew.posLevelId; profile.posTypeId = positionNew.posTypeId; profile.position = positionNew.positionName; - profile.positionField = positionNew.positionField ?? null; - profile.posExecutive = positionNew.posExecutive?.posExecutiveName ?? null; - profile.positionArea = positionNew.positionArea ?? null; - profile.positionExecutiveField = positionNew.positionExecutiveField ?? null; } profile.amount = item.amount ?? null; profile.amountSpecial = item.amountSpecial ?? null; @@ -6886,7 +6876,7 @@ export class CommandController extends Controller { where: { id: item.bodyPosition.posmasterId, }, - relations: { orgRevision: true, orgRoot: true, orgChild1: true, orgChild2: true, orgChild3: true, orgChild4: true } + relations: { orgRevision: true } }); // เช็คว่า posMaster ที่หามาอยู่ในโครงสร้างปัจจุบันหรือไม่ @@ -6903,8 +6893,9 @@ export class CommandController extends Controller { orgRevisionIsDraft: false } }, - relations: { orgRevision: true, orgRoot: true, orgChild1: true, orgChild2: true, orgChild3: true, orgChild4: true } - }); } + relations: { orgRevision: true } + }); + } if (posMaster == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); @@ -6992,28 +6983,20 @@ export class CommandController extends Controller { id: item.bodyPosition.positionId, posMasterId: posMaster.id, }, - relations: ["posExecutive"], }); } // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ if (positionNew != null) { positionNew.positionIsSelected = true; - // อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit - profile.posMasterNo = getPosMasterNo(posMaster); - profile.org = getOrgFullName(posMaster); if(!posMaster.isSit){ profile.posLevelId = positionNew.posLevelId; profile.posTypeId = positionNew.posTypeId; profile.position = positionNew.positionName; - profile.positionField = positionNew.positionField ?? null; - profile.posExecutive = positionNew.posExecutive?.posExecutiveName ?? null; - profile.positionArea = positionNew.positionArea ?? null; - profile.positionExecutiveField = positionNew.positionExecutiveField ?? null; // profile.dateStart = new Date(); + await this.profileRepository.save(profile, { data: req }); + setLogDataDiff(req, { before, after: profile }); } - await this.profileRepository.save(profile, { data: req }); - setLogDataDiff(req, { before, after: profile }); await this.positionRepository.save(positionNew, { data: req }); } // await CreatePosMasterHistoryOfficer(posMaster.id, req); diff --git a/src/controllers/OrganizationController.ts b/src/controllers/OrganizationController.ts index ee8f3413..39752b7e 100644 --- a/src/controllers/OrganizationController.ts +++ b/src/controllers/OrganizationController.ts @@ -66,7 +66,7 @@ import { import { orgStructureCache } from "../utils/OrgStructureCache"; import { OrgIdMapping, AllOrgMappings, SavePosMasterHistory } from "../interfaces/OrgMapping"; import { OrgPermissionData, NodeLevel } from "../interfaces/OrgTypes"; -import { formatPosMaster, generateLabelName, filterPosMasters, getPosMasterNo, getOrgFullName } from "../utils/org-formatting"; +import { formatPosMaster, generateLabelName, filterPosMasters } from "../utils/org-formatting"; @Route("api/v1/org") @Tags("Organization") @@ -8933,25 +8933,13 @@ export class OrganizationController extends Controller { const draftPosMaster = draftPosMasterMap.get(draftPosMasterId) as any; // Collect profile update for the selected position - // อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit - if (nextHolderId != null && draftPos.positionIsSelected) { - const _null: any = null; - profileUpdates.set(nextHolderId, { - posMasterNo: draftPosMaster ? (getPosMasterNo(draftPosMaster as PosMaster) ?? _null) : _null, - org: draftPosMaster ? (getOrgFullName(draftPosMaster as PosMaster) ?? _null) : _null, - }); - } // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ if (nextHolderId != null && draftPos.positionIsSelected && !draftPosMaster?.isSit) { - const existing = profileUpdates.get(nextHolderId) || {}; - existing.position = draftPos.positionName; - existing.posTypeId = draftPos.posTypeId; - existing.posLevelId = draftPos.posLevelId; - existing.positionField = draftPos.positionField ?? null; - existing.posExecutive = (draftPos as any).posExecutive?.posExecutiveName ?? null; - existing.positionArea = draftPos.positionArea ?? null; - existing.positionExecutiveField = draftPos.positionExecutiveField ?? null; - profileUpdates.set(nextHolderId, existing); + profileUpdates.set(nextHolderId, { + position: draftPos.positionName, + posTypeId: draftPos.posTypeId, + posLevelId: draftPos.posLevelId, + }); if (draftPosMaster && draftPosMaster.ancestorDNA) { // Find the selected position from draft positions const selectedPos = diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index 7b1e1594..75d6c2a0 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -39,7 +39,6 @@ import { AuthRole } from "../entities/AuthRole"; import { RequestWithUser } from "../middlewares/user"; import permission from "../interfaces/permission"; import { resolveNodeLevel, setLogDataDiff } from "../interfaces/utils"; -import { getPosMasterNo, getOrgFullName } from "../utils/org-formatting"; import { PosMasterAssign } from "../entities/PosMasterAssign"; import { Assign } from "../entities/Assign"; import { ProfileEmployee } from "../entities/ProfileEmployee"; @@ -1257,7 +1256,7 @@ export class PositionController extends Controller { ) { await new permission().PermissionUpdate(request, "SYS_ORG"); const posMaster = await this.posMasterRepository.findOne({ - relations: ["positions", "orgRevision", "orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], + relations: ["positions", "orgRevision"], where: { id: id }, }); if (!posMaster) { @@ -1452,17 +1451,6 @@ export class PositionController extends Controller { }), ); - // อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit - if (posMaster.orgRevision?.orgRevisionIsCurrent == true && posMaster.current_holderId) { - const _profile = await this.profileRepository.findOne({ - where: { id: posMaster.current_holderId }, - }); - if (_profile) { - _profile.posMasterNo = getPosMasterNo(posMaster); - _profile.org = getOrgFullName(posMaster); - await this.profileRepository.save(_profile); - } - } // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ if (posMaster.orgRevision?.orgRevisionIsCurrent == true && !posMaster.isSit) { const _position = requestBody.positions.find((p) => p.positionIsSelected == true); @@ -1475,10 +1463,6 @@ export class PositionController extends Controller { _profile.position = _position.posDictName ?? _null; _profile.posTypeId = _position.posTypeId; _profile.posLevelId = _position.posLevelId; - _profile.positionField = _position.posDictField ?? _null; - _profile.posExecutive = _position.posExecutiveId ?? _null; - _profile.positionArea = _position.posDictArea ?? _null; - _profile.positionExecutiveField = _position.posDictExecutiveField ?? _null; await this.profileRepository.save(_profile); } } @@ -3809,7 +3793,7 @@ export class PositionController extends Controller { await new permission().PermissionUpdate(request, "SYS_ORG"); const dataMaster = await this.posMasterRepository.findOne({ where: { id: requestBody.posMaster }, - relations: ["positions", "orgRoot", "orgChild1", "orgChild2", "orgChild3", "orgChild4"], + relations: ["positions"], }); if (!dataMaster) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้"); @@ -3841,24 +3825,16 @@ export class PositionController extends Controller { if (_profile) { let _position = await this.positionRepository.findOne({ where: { id: requestBody.position, posMasterId: requestBody.posMaster }, - relations: ["posExecutive"], }); if (_position) { - // อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit - _profile.posMasterNo = getPosMasterNo(dataMaster); - _profile.org = getOrgFullName(dataMaster); // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ if(!dataMaster.isSit){ _profile.position = _position.positionName; _profile.posTypeId = _position.posTypeId; _profile.posLevelId = _position.posLevelId; - _profile.positionField = _position.positionField ?? undefined; - _profile.posExecutive = _position.posExecutive?.posExecutiveName ?? undefined; - _profile.positionArea = _position.positionArea ?? undefined; - _profile.positionExecutiveField = _position.positionExecutiveField ?? undefined; + await this.profileRepository.save(_profile); + setLogDataDiff(request, { before, after: _profile }); } - await this.profileRepository.save(_profile); - setLogDataDiff(request, { before, after: _profile }); } } dataMaster.current_holderId = requestBody.profileId; diff --git a/src/controllers/ProfileSalaryController.ts b/src/controllers/ProfileSalaryController.ts index 8cc9d376..c8193750 100644 --- a/src/controllers/ProfileSalaryController.ts +++ b/src/controllers/ProfileSalaryController.ts @@ -23,7 +23,6 @@ import { ProfileEmployee } from "../entities/ProfileEmployee"; import { In, IsNull, LessThan, MoreThan, Not } from "typeorm"; import permission from "../interfaces/permission"; import { setLogDataDiff } from "../interfaces/utils"; -import { calculateTenure } from "../utils/tenure"; import { TenurePositionOfficer } from "../entities/TenurePositionOfficer"; import { TenureLevelOfficer } from "../entities/TenureLevelOfficer"; import { TenurePositionEmployee } from "../entities/TenurePositionEmployee"; @@ -93,14 +92,16 @@ export class ProfileSalaryController extends Controller { }, { days_diff: 0, positionName: null }, ); - const { year, month, day } = calculateTenure(calDayDiff.days_diff); const mapData: any = { profileId: x.id, positionName: calDayDiff.positionName, days_diff: calDayDiff.days_diff, - Years: year, - Months: month, - Days: day, + // Years: (calDayDiff.days_diff / 365.2524).toFixed(4), + // Months: ((calDayDiff.days_diff / 30.4375) % 12).toFixed(4), + // Days: (calDayDiff.days_diff % 30.4375).toFixed(4), + Years: Math.floor(calDayDiff.days_diff / 365.2524), + Months: Math.floor((calDayDiff.days_diff / 30.4375) % 12), + Days: Math.floor(calDayDiff.days_diff % 30.4375), }; // data.push(_mapData); await this.positionOfficerRepo.save(mapData); @@ -142,14 +143,16 @@ export class ProfileSalaryController extends Controller { }, { days_diff: 0, positionName: null }, ); - const { year, month, day } = calculateTenure(calDayDiff.days_diff); const mapData: any = { profileEmployeeId: x.id, positionName: calDayDiff.positionName, days_diff: calDayDiff.days_diff, - Years: year, - Months: month, - Days: day, + // Years: (calDayDiff.days_diff / 365.2524).toFixed(4), + // Months: ((calDayDiff.days_diff / 30.4375) % 12).toFixed(4), + // Days: (calDayDiff.days_diff % 30.4375).toFixed(4), + Years: Math.floor(calDayDiff.days_diff / 365.2524), + Months: Math.floor((calDayDiff.days_diff / 30.4375) % 12), + Days: Math.floor(calDayDiff.days_diff % 30.4375), }; // data.push(_mapData); await this.positionEmployeeRepo.save(mapData); @@ -199,16 +202,15 @@ export class ProfileSalaryController extends Controller { }, { days_diff: 0, positionType: null, positionLevel: null, positionCee: null }, ); - const { year, month, day } = calculateTenure(calDayDiff.days_diff); const mapData: any = { profileId: x.id, positionType: calDayDiff.positionType, positionLevel: calDayDiff.positionLevel, positionCee: calDayDiff.positionCee, days_diff: calDayDiff.days_diff, - Years: x.posLevel == null ? 0 : year.toFixed(4), - Months: x.posLevel == null ? 0 : month.toFixed(4), - Days: x.posLevel == null ? 0 : day.toFixed(4), + Years: x.posLevel == null ? 0 : (calDayDiff.days_diff / 365.2524).toFixed(4), + Months: x.posLevel == null ? 0 : ((calDayDiff.days_diff / 30.4375) % 12).toFixed(4), + Days: x.posLevel == null ? 0 : (calDayDiff.days_diff % 30.4375).toFixed(4), }; // data.push(_mapData); await this.levelOfficerRepo.save(mapData); @@ -258,16 +260,15 @@ export class ProfileSalaryController extends Controller { }, { days_diff: 0, positionType: null, positionLevel: null, positionCee: null }, ); - const { year, month, day } = calculateTenure(calDayDiff.days_diff); const mapData: any = { profileEmployeeId: x.id, positionType: calDayDiff.positionType, positionLevel: calDayDiff.positionLevel, positionCee: calDayDiff.positionCee, days_diff: calDayDiff.days_diff, - Years: x.posLevel == null ? 0 : year.toFixed(4), - Months: x.posLevel == null ? 0 : month.toFixed(4), - Days: x.posLevel == null ? 0 : day.toFixed(4), + Years: x.posLevel == null ? 0 : (calDayDiff.days_diff / 365.2524).toFixed(4), + Months: x.posLevel == null ? 0 : ((calDayDiff.days_diff / 30.4375) % 12).toFixed(4), + Days: x.posLevel == null ? 0 : (calDayDiff.days_diff % 30.4375).toFixed(4), }; // data.push(_mapData); await this.levelEmployeeRepo.save(mapData); @@ -330,14 +331,13 @@ export class ProfileSalaryController extends Controller { }, { days_diff: 0, positionExecutive: null }, ); - const { year, month, day } = calculateTenure(calDayDiff.days_diff); const mapData: any = { profileId: x.id, positionExecutiveName: calDayDiff.positionExecutive, days_diff: calDayDiff.days_diff, - Years: year.toFixed(4), - Months: month.toFixed(4), - Days: day.toFixed(4), + Years: (calDayDiff.days_diff / 365.2524).toFixed(4), + Months: ((calDayDiff.days_diff / 30.4375) % 12).toFixed(4), + Days: (calDayDiff.days_diff % 30.4375).toFixed(4), }; await this.positionExecutiveOfficerRepo.save(mapData); } @@ -602,10 +602,10 @@ export class ProfileSalaryController extends Controller { acc.push(existing); } - const { year, month, day } = calculateTenure(existing.days); - existing.year = year; - existing.month = month; - existing.day = day; + // Recalculate year, month, and day + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.ceil(existing.days % 30.4375); return acc; }, @@ -641,10 +641,10 @@ export class ProfileSalaryController extends Controller { acc.push(existing); } - const { year, month, day } = calculateTenure(existing.days); - existing.year = year; - existing.month = month; - existing.day = day; + // Recalculate year, month, and day + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.ceil(existing.days % 30.4375); return acc; }, @@ -675,10 +675,10 @@ export class ProfileSalaryController extends Controller { acc.push(existing); } - const { year, month, day } = calculateTenure(existing.days); - existing.year = year; - existing.month = month; - existing.day = day; + // Recalculate year, month, and day + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.ceil(existing.days % 30.4375); return acc; }, @@ -739,10 +739,10 @@ export class ProfileSalaryController extends Controller { acc.push(existing); } - const { year, month, day } = calculateTenure(existing.days); - existing.year = year; - existing.month = month; - existing.day = day; + // Recalculate year, month, and day + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.ceil(existing.days % 30.4375); return acc; }, @@ -782,10 +782,10 @@ export class ProfileSalaryController extends Controller { acc.push(existing); } - const { year, month, day } = calculateTenure(existing.days); - existing.year = year; - existing.month = month; - existing.day = day; + // Recalculate year, month, and day + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.ceil(existing.days % 30.4375); return acc; }, @@ -819,10 +819,10 @@ export class ProfileSalaryController extends Controller { acc.push(existing); } - const { year, month, day } = calculateTenure(existing.days); - existing.year = year; - existing.month = month; - existing.day = day; + // Recalculate year, month, and day + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.ceil(existing.days % 30.4375); return acc; }, diff --git a/src/controllers/ProfileSalaryEmployeeController.ts b/src/controllers/ProfileSalaryEmployeeController.ts index 7428e913..5b87003c 100644 --- a/src/controllers/ProfileSalaryEmployeeController.ts +++ b/src/controllers/ProfileSalaryEmployeeController.ts @@ -27,7 +27,6 @@ import { Profile } from "../entities/Profile"; import { In, LessThan, IsNull, MoreThan } from "typeorm"; import permission from "../interfaces/permission"; import { setLogDataDiff } from "../interfaces/utils"; -import { calculateTenure } from "../utils/tenure"; import { Command } from "../entities/Command"; import { OrgRoot } from "../entities/OrgRoot"; import Extension from "../interfaces/extension"; @@ -176,10 +175,9 @@ export class ProfileSalaryEmployeeController extends Controller { acc.push(existing); } - const { year, month, day } = calculateTenure(existing.days); - existing.year = year; - existing.month = month; - existing.day = day; + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.ceil(existing.days % 30.4375); return acc; }, @@ -213,10 +211,9 @@ export class ProfileSalaryEmployeeController extends Controller { acc.push(existing); } - const { year, month, day } = calculateTenure(existing.days); - existing.year = year; - existing.month = month; - existing.day = day; + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.ceil(existing.days % 30.4375); return acc; }, @@ -269,10 +266,9 @@ export class ProfileSalaryEmployeeController extends Controller { acc.push(existing); } - const { year, month, day } = calculateTenure(existing.days); - existing.year = year; - existing.month = month; - existing.day = day; + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.ceil(existing.days % 30.4375); return acc; }, @@ -306,10 +302,9 @@ export class ProfileSalaryEmployeeController extends Controller { acc.push(existing); } - const { year, month, day } = calculateTenure(existing.days); - existing.year = year; - existing.month = month; - existing.day = day; + existing.year = Math.floor(existing.days / 365.2524); + existing.month = Math.floor((existing.days / 30.4375) % 12); + existing.day = Math.ceil(existing.days % 30.4375); return acc; }, diff --git a/src/entities/Profile.ts b/src/entities/Profile.ts index a875a969..72a1d505 100644 --- a/src/entities/Profile.ts +++ b/src/entities/Profile.ts @@ -140,54 +140,6 @@ export class Profile extends EntityBase { }) posTypeId: string | null; - @Column({ - nullable: true, - comment: "สายงาน", - length: 45, - default: null, - }) - positionField: string; - - @Column({ - nullable: true, - comment: "ตำแหน่งทางการบริหาร", - length: 255, - default: null, - }) - posExecutive?: string; - - @Column({ - nullable: true, - comment: "ด้าน/สาขา", - length: 255, - default: null, - }) - positionArea?: string; - - @Column({ - nullable: true, - comment: "ด้านทางการบริหาร", - length: 255, - default: null, - }) - positionExecutiveField?: string; - - @Column({ - nullable: true, - comment: "เลขที่ตำแหน่ง", - length: 255, - default: null, - }) - posMasterNo?: string; - - @Column({ - nullable: true, - comment: "สังกัด", - type: "text", - default: null, - }) - org?: string; - @Column({ nullable: true, length: 255, diff --git a/src/migration/1776308026834-add_position_fields_to_profile.ts b/src/migration/1776308026834-add_position_fields_to_profile.ts deleted file mode 100644 index 9b214460..00000000 --- a/src/migration/1776308026834-add_position_fields_to_profile.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { MigrationInterface, QueryRunner } from "typeorm"; - -export class AddPositionFieldsToProfile1776308026834 implements MigrationInterface { - name = 'AddPositionFieldsToProfile1776308026834' - - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE \`profile\` ADD \`positionField\` varchar(45) NULL COMMENT 'สายงาน'`); - await queryRunner.query(`ALTER TABLE \`profile\` ADD \`posExecutive\` varchar(255) NULL COMMENT 'ตำแหน่งทางการบริหาร'`); - await queryRunner.query(`ALTER TABLE \`profile\` ADD \`positionArea\` varchar(255) NULL COMMENT 'ด้าน/สาขา'`); - await queryRunner.query(`ALTER TABLE \`profile\` ADD \`positionExecutiveField\` varchar(255) NULL COMMENT 'ด้านทางการบริหาร'`); - await queryRunner.query(`ALTER TABLE \`profile\` ADD \`posMasterNo\` varchar(255) NULL COMMENT 'เลขที่ตำแหน่ง'`); - await queryRunner.query(`ALTER TABLE \`profile\` ADD \`org\` text NULL COMMENT 'สังกัด'`); - - await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`positionField\` varchar(45) NULL COMMENT 'สายงาน'`); - await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`posExecutive\` varchar(255) NULL COMMENT 'ตำแหน่งทางการบริหาร'`); - await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`positionArea\` varchar(255) NULL COMMENT 'ด้าน/สาขา'`); - await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`positionExecutiveField\` varchar(255) NULL COMMENT 'ด้านทางการบริหาร'`); - await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`posMasterNo\` varchar(255) NULL COMMENT 'เลขที่ตำแหน่ง'`); - await queryRunner.query(`ALTER TABLE \`profileHistory\` ADD \`org\` text NULL COMMENT 'สังกัด'`); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`org\``); - await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`posMasterNo\``); - await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`positionExecutiveField\``); - await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`positionArea\``); - await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`posExecutive\``); - await queryRunner.query(`ALTER TABLE \`profileHistory\` DROP COLUMN \`positionField\``); - - await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`org\``); - await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`posMasterNo\``); - await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`positionExecutiveField\``); - await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`positionArea\``); - await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`posExecutive\``); - await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`positionField\``); - } -} diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 6ba40258..a8011900 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -3,7 +3,6 @@ import { AppDataSource } from "../database/data-source"; import { Command } from "../entities/Command"; import { chunkArray, commandTypePath } from "../interfaces/utils"; import CallAPI from "../interfaces/call-api"; -import { getPosMasterNo, getOrgFullName } from "../utils/org-formatting"; import HttpError from "../interfaces/http-error"; import HttpStatusCode from "../interfaces/http-status"; import { PosMaster } from "../entities/PosMaster"; @@ -652,33 +651,23 @@ async function handler_org(msg: amqp.ConsumeMessage): Promise { await posMasterAssignRepository.save(newAssigns); } - // อัพเดท org และ posMasterNo ตลอดไม่ต้องดัก isSit - if (item.next_holderId != null) { + // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ + if (item.next_holderId != null && !item.isSit) { const profile = await repoProfile.findOne({ where: { id: item.next_holderId == null ? "" : item.next_holderId }, }); - if (profile != null) { - profile.posMasterNo = getPosMasterNo(item) ?? _null; - profile.org = getOrgFullName(item) ?? _null; - - // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ - if (!item.isSit && item.positions.length > 0) { - let position = await item.positions.find((x) => x.positionIsSelected == true); + if (profile != null && item.positions.length > 0) { + let position = await item.positions.find((x) => x.positionIsSelected == true); + if (position == null) { + position = await item.positions.find((x) => x.posLevelId == profile?.posLevelId); if (position == null) { - position = await item.positions.find((x) => x.posLevelId == profile?.posLevelId); - if (position == null) { - position = await item.positions.sort((a, b) => a.orderNo - b.orderNo)[0]; - } + position = await item.positions.sort((a, b) => a.orderNo - b.orderNo)[0]; } - - profile.posLevelId = position?.posLevelId ?? _null; - profile.posTypeId = position?.posTypeId ?? _null; - profile.position = position?.positionName ?? _null; - profile.positionField = position?.positionField ?? _null; - profile.posExecutive = position?.posExecutive?.posExecutiveName ?? _null; - profile.positionArea = position?.positionArea ?? _null; - profile.positionExecutiveField = position?.positionExecutiveField ?? _null; } + + profile.posLevelId = position?.posLevelId ?? _null; + profile.posTypeId = position?.posTypeId ?? _null; + profile.position = position?.positionName ?? _null; await repoProfile.save(profile); } } diff --git a/src/utils/org-formatting.ts b/src/utils/org-formatting.ts index 8b460a2f..701fb478 100644 --- a/src/utils/org-formatting.ts +++ b/src/utils/org-formatting.ts @@ -68,42 +68,3 @@ export function filterPosMasters( ): PosMaster[] { return posMasters.filter((x) => x[childLevelIdKey] == null && x.isDirector === true); } - -/** - * สร้าง orgShortName จาก posMaster (ต้อง load org relations มาก่อน) - */ -export function getOrgShortName(posMaster: PosMaster): string { - if (posMaster.orgChild1Id === null) { - return posMaster.orgRoot?.orgRootShortName ?? ""; - } else if (posMaster.orgChild2Id === null) { - return posMaster.orgChild1?.orgChild1ShortName ?? ""; - } else if (posMaster.orgChild3Id === null) { - return posMaster.orgChild2?.orgChild2ShortName ?? ""; - } else if (posMaster.orgChild4Id === null) { - return posMaster.orgChild3?.orgChild3ShortName ?? ""; - } else { - return posMaster.orgChild4?.orgChild4ShortName ?? ""; - } -} - -/** - * สร้างชื่อสังกัดเต็ม จาก posMaster (join ด้วย \n) - */ -export function getOrgFullName(posMaster: PosMaster): string { - const parts = [ - posMaster.orgChild4?.orgChild4Name, - posMaster.orgChild3?.orgChild3Name, - posMaster.orgChild2?.orgChild2Name, - posMaster.orgChild1?.orgChild1Name, - posMaster.orgRoot?.orgRootName, - ]; - return parts.filter((part) => part !== undefined && part !== null).join("\n"); -} - -/** - * สร้างเลขที่ตำแหน่ง เช่น "กทม. 1234" - */ -export function getPosMasterNo(posMaster: PosMaster): string { - const orgShortName = getOrgShortName(posMaster); - return `${orgShortName} ${posMaster.posMasterNo}`; -} diff --git a/src/utils/tenure.ts b/src/utils/tenure.ts deleted file mode 100644 index 577d314b..00000000 --- a/src/utils/tenure.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** - * คำนวณอายุงานจากจำนวนวันรวม - * @param totalDays จำนวนวันรวม - * @returns { year, month, day } ปี เดือน วัน - */ -export function calculateTenure(totalDays: number) { - // 1. แปลงเป็น year เต็ม - const year = Math.floor(totalDays / 365.2524); - - // 2. วันที่เหลือหลังหัก year ออก - const remainAfterYear = totalDays - year * 365.2524; - - // 3. แปลงเป็น month เต็ม - const month = Math.floor(remainAfterYear / 30.4375); - - // 4. วันที่เหลือหลังหัก month ออก - const remainAfterMonth = remainAfterYear - month * 30.4375; - - // 5. ปัดลง เฉพาะวัน - const day = Math.floor(remainAfterMonth); - - return { year, month, day }; -}