Compare commits

..

No commits in common. "2f834d3644dbe5b4441dd69596050da7c574d5bc" and "58dd4cfd603be66a900b569b23412ce6d53097c4" have entirely different histories.

2 changed files with 11 additions and 57 deletions

View file

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

View file

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