fix ประวัติคนครอง #2405
This commit is contained in:
parent
212360a764
commit
2fd99aaa94
2 changed files with 57 additions and 11 deletions
|
|
@ -6928,12 +6928,40 @@ export class CommandController extends Controller {
|
|||
await this.posMasterRepository.save(posMaster);
|
||||
|
||||
// STEP 5: กำหนด position ใหม่
|
||||
const positionNew = await this.positionRepository.findOne({
|
||||
where: {
|
||||
id: item.bodyPosition.positionId,
|
||||
posMasterId: posMaster.id, // ใช้ id ของ posMaster ตัวใหม่
|
||||
},
|
||||
});
|
||||
// เช็คว่า 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,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// ถ้าไม่ใช่ตำแหน่งนั่งทับ (isSit = false) ถึงจะอัพเดทตำแหน่งในทะเบียนประวัติ
|
||||
if (positionNew != null) {
|
||||
positionNew.positionIsSelected = true;
|
||||
|
|
@ -6947,7 +6975,10 @@ export class CommandController extends Controller {
|
|||
}
|
||||
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
|
||||
if (_oldInsigniaIds.length > 0) {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ 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";
|
||||
|
||||
|
|
@ -15,12 +16,14 @@ 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 },
|
||||
|
|
@ -51,10 +54,22 @@ export async function CreatePosMasterHistoryOfficer(
|
|||
});
|
||||
const _null: any = null;
|
||||
const h = new PosMasterHistory();
|
||||
const selectedPosition =
|
||||
pm.positions.length > 0
|
||||
? pm.positions.find((p) => p.positionIsSelected === true) ?? null
|
||||
: null;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
h.ancestorDNA = pm.ancestorDNA ? pm.ancestorDNA : _null;
|
||||
if (!type || type != "DELETE") {
|
||||
if (checkCurrentRevision) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue