fix ประวัติคนครอง #2405

This commit is contained in:
harid 2026-04-02 17:02:20 +07:00
parent 212360a764
commit 2fd99aaa94
2 changed files with 57 additions and 11 deletions

View file

@ -6928,12 +6928,40 @@ export class CommandController extends Controller {
await this.posMasterRepository.save(posMaster); await this.posMasterRepository.save(posMaster);
// STEP 5: กำหนด position ใหม่ // STEP 5: กำหนด position ใหม่
const positionNew = await this.positionRepository.findOne({ // เช็คว่า posMaster เปลี่ยนจากเก่าเป็นใหม่หรือไม่
where: { const originalPosMasterId = item.bodyPosition.posmasterId;
id: item.bodyPosition.positionId, const isPosMasterChanged = originalPosMasterId !== posMaster.id;
posMasterId: posMaster.id, // ใช้ id ของ posMaster ตัวใหม่
}, let positionNew = null;
});
if (isPosMasterChanged) {
// posMaster เปลี่ยน ต้องหา position ใหม่จากคุณสมบัติของ position เก่า
// 1. หา position เก่าจาก id ที่ส่งมา
const positionOld = await this.positionRepository.findOne({
where: { id: item.bodyPosition.positionId },
});
if (positionOld) {
// 2. ใช้ posTypeId + posLevelId + positionName หา position ใหม่ใน posMaster ตัวใหม่
positionNew = await this.positionRepository.findOne({
where: {
posMasterId: posMaster.id, // ใช้ posMaster ตัวใหม่
posTypeId: positionOld.posTypeId,
posLevelId: positionOld.posLevelId,
positionName: positionOld.positionName,
},
});
}
} else {
// posMaster ไม่เปลี่ยน - ใช้วิธีเดิม
positionNew = await this.positionRepository.findOne({
where: {
id: item.bodyPosition.positionId,
posMasterId: posMaster.id,
},
});
}
// ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ // ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ
if (positionNew != null) { if (positionNew != null) {
positionNew.positionIsSelected = true; positionNew.positionIsSelected = true;
@ -6947,7 +6975,10 @@ export class CommandController extends Controller {
} }
await this.positionRepository.save(positionNew, { data: req }); await this.positionRepository.save(positionNew, { data: req });
} }
await CreatePosMasterHistoryOfficer(posMaster.id, req); // await CreatePosMasterHistoryOfficer(posMaster.id, req);
await CreatePosMasterHistoryOfficer(posMaster.id, req, null, {
positionId: positionNew?.id
});
} }
// Insignia // Insignia
if (_oldInsigniaIds.length > 0) { if (_oldInsigniaIds.length > 0) {

View file

@ -8,6 +8,7 @@ import { PosMaster } from "../entities/PosMaster";
import { PosMasterEmployeeHistory } from "../entities/PosMasterEmployeeHistory"; import { PosMasterEmployeeHistory } from "../entities/PosMasterEmployeeHistory";
import { PosMasterEmployeeTempHistory } from "../entities/PosMasterEmployeeTempHistory"; import { PosMasterEmployeeTempHistory } from "../entities/PosMasterEmployeeTempHistory";
import { PosMasterHistory } from "../entities/PosMasterHistory"; import { PosMasterHistory } from "../entities/PosMasterHistory";
import { Position } from "../entities/Position";
import { ProfileEducation } from "../entities/ProfileEducation"; import { ProfileEducation } from "../entities/ProfileEducation";
import { RequestWithUser } from "../middlewares/user"; import { RequestWithUser } from "../middlewares/user";
@ -15,12 +16,14 @@ export async function CreatePosMasterHistoryOfficer(
posMasterId: string, posMasterId: string,
request: RequestWithUser | null, request: RequestWithUser | null,
type?: string | null, type?: string | null,
positionData?: { positionId?: string } | null,
): Promise<boolean> { ): Promise<boolean> {
try { try {
await AppDataSource.transaction(async (manager) => { await AppDataSource.transaction(async (manager) => {
const repoPosmaster = manager.getRepository(PosMaster); const repoPosmaster = manager.getRepository(PosMaster);
const repoHistory = manager.getRepository(PosMasterHistory); const repoHistory = manager.getRepository(PosMasterHistory);
const repoOrgRevision = manager.getRepository(OrgRevision); const repoOrgRevision = manager.getRepository(OrgRevision);
const repoPosition = manager.getRepository(Position);
const pm = await repoPosmaster.findOne({ const pm = await repoPosmaster.findOne({
where: { id: posMasterId }, where: { id: posMasterId },
@ -51,10 +54,22 @@ export async function CreatePosMasterHistoryOfficer(
}); });
const _null: any = null; const _null: any = null;
const h = new PosMasterHistory(); const h = new PosMasterHistory();
const selectedPosition =
pm.positions.length > 0 // query position โดยตรงจาก positionRepository
? pm.positions.find((p) => p.positionIsSelected === true) ?? null let selectedPosition: Position | null = null;
: null; if (positionData?.positionId) {
selectedPosition = await repoPosition.findOne({
where: { id: positionData.positionId },
relations: { posLevel: true, posType: true, posExecutive: true },
});
} else {
// ใช้ logic เดิม หาจาก pm.positions ที่ positionIsSelected = true
selectedPosition =
pm.positions.length > 0
? pm.positions.find((p) => p.positionIsSelected === true) ?? null
: null;
}
h.ancestorDNA = pm.ancestorDNA ? pm.ancestorDNA : _null; h.ancestorDNA = pm.ancestorDNA ? pm.ancestorDNA : _null;
if (!type || type != "DELETE") { if (!type || type != "DELETE") {
if (checkCurrentRevision) { if (checkCurrentRevision) {