test posmasteract

This commit is contained in:
mamoss 2025-07-29 21:30:38 +07:00
parent 52597bfa5b
commit 51fc374e9a
2 changed files with 139 additions and 50 deletions

View file

@ -2250,6 +2250,43 @@ export class CommandController extends Controller {
commandRecive.lastUpdateUserId = request.user.sub; commandRecive.lastUpdateUserId = request.user.sub;
commandRecive.lastUpdateFullName = request.user.name; commandRecive.lastUpdateFullName = request.user.name;
commandRecive.lastUpdatedAt = new Date(); commandRecive.lastUpdatedAt = new Date();
if (commandCode == "C-PM-40") {
const posMasterAct = await this.posMasterActRepository.findOne({
where: { id: item.refId },
relations: [
"posMaster",
"posMaster.positions",
"posMaster.orgRoot",
"posMaster.orgChild1",
"posMaster.orgChild2",
"posMaster.orgChild3",
"posMaster.orgChild4",
],
});
if (posMasterAct) {
commandRecive.remarkVertical =
"ลำดับที่ " + Extension.ToThaiNumber(posMasterAct?.posMasterOrder?.toString() ?? "");
const selectedPosition = posMasterAct?.posMaster?.positions?.find(
(x) => x.positionIsSelected === true,
);
commandRecive.position = selectedPosition?.positionName ?? _null;
const orgShortName =
[
posMasterAct.posMaster?.orgChild4?.orgChild4ShortName,
posMasterAct.posMaster?.orgChild3?.orgChild3ShortName,
posMasterAct.posMaster?.orgChild2?.orgChild2ShortName,
posMasterAct.posMaster?.orgChild1?.orgChild1ShortName,
posMasterAct.posMaster?.orgRoot?.orgRootShortName,
].find(Boolean) ?? "";
commandRecive.posNo =
orgShortName && posMasterAct.posMaster?.posMasterNo
? `${orgShortName} ${posMasterAct.posMaster?.posMasterNo}`
: posMasterAct.posMaster?.posMasterNo?.toString() ?? "-";
}
}
await this.commandReciveRepository.save(commandRecive); await this.commandReciveRepository.save(commandRecive);
}), }),
); );
@ -4505,9 +4542,10 @@ export class CommandController extends Controller {
position: profile.position, position: profile.position,
positionName: profile.position, positionName: profile.position,
positionType: profile?.posType?.posTypeName ?? null, positionType: profile?.posType?.posTypeName ?? null,
positionLevel: profile?.posType && profile?.posLevel positionLevel:
? `${profile?.posType?.posTypeShortName} ${profile?.posLevel?.posLevelName }` profile?.posType && profile?.posLevel
: null, ? `${profile?.posType?.posTypeShortName} ${profile?.posLevel?.posLevelName}`
: null,
amount: item.amount ? item.amount : null, amount: item.amount ? item.amount : null,
positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null, positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null,
mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null, mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null,
@ -4795,8 +4833,9 @@ export class CommandController extends Controller {
commandId: item.commandId, commandId: item.commandId,
position: profile.position, position: profile.position,
positionType: profile?.posType?.posTypeName ?? null, positionType: profile?.posType?.posTypeName ?? null,
positionLevel: profile?.posType && profile?.posLevel positionLevel:
? `${profile?.posType?.posTypeShortName} ${profile?.posLevel?.posLevelName }` profile?.posType && profile?.posLevel
? `${profile?.posType?.posTypeShortName} ${profile?.posLevel?.posLevelName}`
: null, : null,
amount: item.amount ? item.amount : null, amount: item.amount ? item.amount : null,
amountSpecial: item.amountSpecial ? item.amountSpecial : null, amountSpecial: item.amountSpecial ? item.amountSpecial : null,
@ -6544,17 +6583,21 @@ export class CommandController extends Controller {
}[]; }[];
}, },
) { ) {
// 1. Bulk update status
await this.posMasterActRepository.update( await this.posMasterActRepository.update(
{ id: In(body.refIds.map((x) => x.refId)) }, { id: In(body.refIds.map((x) => x.refId)) },
{ statusReport: "DONE" }, { statusReport: "DONE" },
); );
// 2. ดึงข้อมูลครบทุก relation ที่จำเป็น
const posMasters = await this.posMasterActRepository.find({ const posMasters = await this.posMasterActRepository.find({
where: { id: In(body.refIds.map((x) => x.refId)) }, where: { id: In(body.refIds.map((x) => x.refId)) },
relations: [ relations: [
"posMasterChild", "posMasterChild",
"posMasterChild.current_holder",
"posMaster", "posMaster",
"posMaster.current_holder", "posMaster.current_holder",
"posMaster.positions",
"posMaster.orgRoot", "posMaster.orgRoot",
"posMaster.orgChild1", "posMaster.orgChild1",
"posMaster.orgChild2", "posMaster.orgChild2",
@ -6563,48 +6606,71 @@ export class CommandController extends Controller {
], ],
}); });
// 3. ตรวจสอบว่ามี body.refIds[0] หรือไม่
const firstRef = body.refIds[0];
if (!firstRef) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบข้อมูล refIds");
}
await Promise.all( await Promise.all(
posMasters.map(async (item) => { posMasters.map(async (item) => {
if (item.posMasterChild?.current_holderId) { // 4. ตรวจสอบข้อมูลที่จำเป็นทั้งหมด
const shortName = if (!item.posMasterChild?.current_holderId || !item.posMaster) {
[ console.warn(`ข้ามรายการ ${item.id}: ข้อมูลไม่ครบ`);
item.posMaster?.orgChild4?.orgChild4ShortName, return;
item.posMaster?.orgChild3?.orgChild3ShortName, }
item.posMaster?.orgChild2?.orgChild2ShortName,
item.posMaster?.orgChild1?.orgChild1ShortName,
item.posMaster?.orgRoot?.orgRootShortName,
].find(Boolean) ?? "";
const metaAct = { // 5. สร้าง orgShortName แบบปลอดภัย
profileId: item.posMasterChild.current_holderId ?? null, const orgShortName =
dateStart: body.refIds[0]?.commandDateAffect ?? null, [
dateEnd: null, item.posMaster?.orgChild4?.orgChild4ShortName,
position: item.posMaster?.current_holder?.position ?? "-", item.posMaster?.orgChild3?.orgChild3ShortName,
status: true, item.posMaster?.orgChild2?.orgChild2ShortName,
commandId: body.refIds[0]?.commandId ?? null, item.posMaster?.orgChild1?.orgChild1ShortName,
createdUserId: req.user?.sub ?? null, item.posMaster?.orgRoot?.orgRootShortName,
createdFullName: req.user?.name ?? null, ].find(Boolean) ?? "";
lastUpdateUserId: req.user?.sub ?? null,
lastUpdateFullName: req.user?.name ?? null,
createdAt: new Date(),
lastUpdatedAt: new Date(),
commandNo: body.refIds[0]?.commandNo ?? null,
refCommandNo: `${body.refIds[0]?.commandNo ?? ""}/${body.refIds[0]?.commandYear ?? ""}`,
commandYear: body.refIds[0]?.commandYear ?? null,
posNo:
shortName && item.posMaster?.posMasterNo
? `${shortName} ${item.posMaster.posMasterNo}`
: item.posMaster?.posMasterNo ?? "-",
posNoAbb: shortName,
commandDateAffect: body.refIds[0]?.commandDateAffect ?? null,
commandDateSign: body.refIds[0]?.commandDateSign ?? null,
commandCode: body.refIds[0]?.commandCode ?? null,
commandName: body.refIds[0]?.commandName ?? null,
remark: body.refIds[0]?.remark ?? null,
};
// 6. หา position ที่ถูกเลือกแบบปลอดภัย
const selectedPosition = item.posMaster?.positions?.find(
(x) => x.positionIsSelected === true,
);
const positionName =
selectedPosition?.positionName ?? item.posMaster?.current_holder?.position ?? "-";
// 7. สร้าง metaAct แบบปลอดภัย
const metaAct = {
profileId: item.posMasterChild.current_holderId,
dateStart: firstRef.commandDateAffect ?? null,
dateEnd: null,
position: positionName,
status: true,
commandId: firstRef.commandId ?? null,
createdUserId: req.user?.sub ?? null,
createdFullName: req.user?.name ?? null,
lastUpdateUserId: req.user?.sub ?? null,
lastUpdateFullName: req.user?.name ?? null,
createdAt: new Date(),
lastUpdatedAt: new Date(),
commandNo: firstRef.commandNo ?? null,
refCommandNo: `${firstRef.commandNo ?? ""}/${firstRef.commandYear ?? ""}`,
commandYear: firstRef.commandYear ?? null,
posNo:
orgShortName && item.posMaster?.posMasterNo
? `${orgShortName} ${item.posMaster.posMasterNo}`
: item.posMaster?.posMasterNo ?? "-",
posNoAbb: orgShortName,
commandDateAffect: firstRef.commandDateAffect ?? null,
commandDateSign: firstRef.commandDateSign ?? null,
commandCode: firstRef.commandCode ?? null,
commandName: firstRef.commandName ?? null,
remark: firstRef.remark ?? null,
};
try {
// 8. บันทึกข้อมูลใหม่
const dataAct = new ProfileActposition(); const dataAct = new ProfileActposition();
Object.assign(dataAct, metaAct); Object.assign(dataAct, metaAct);
const historyAct = new ProfileActpositionHistory(); const historyAct = new ProfileActpositionHistory();
Object.assign(historyAct, { ...dataAct, id: undefined }); Object.assign(historyAct, { ...dataAct, id: undefined });
@ -6612,19 +6678,34 @@ export class CommandController extends Controller {
historyAct.profileActpositionId = dataAct.id; historyAct.profileActpositionId = dataAct.id;
await this.actpositionHistoryRepository.save(historyAct); await this.actpositionHistoryRepository.save(historyAct);
// ปิดสถานะรักษาการเดิม // 9. ปิดสถานะรักษาการ
const _actposition = await this.actpositionRepository.find({ const existingActPositions = await this.actpositionRepository.find({
where: { profileId: item.posMasterChild.current_holderId }, where: {
profileId: item.posMasterChild.current_holderId,
status: true,
id: Not(dataAct.id),
},
}); });
const profileActs = _actposition.map((_data) => ({
..._data, if (existingActPositions.length > 0) {
status: false, const updatedActPositions = existingActPositions.map((_data) => ({
dateEnd: _data.status === true ? new Date() : _data.dateEnd, ..._data,
})); status: false,
await this.actpositionRepository.save(profileActs); dateEnd: new Date(),
}));
await this.actpositionRepository.save(updatedActPositions);
}
} catch (error) {
console.error(`Error processing item ${item.id}:`, error);
throw new HttpError(
HttpStatus.INTERNAL_SERVER_ERROR,
`เกิดข้อผิดพลาดในการประมวลผลรายการ ${item.id}`,
);
} }
}), }),
); );
return new HttpSuccess(); return new HttpSuccess();
} }

View file

@ -44,6 +44,14 @@ export class CommandRecive extends EntityBase {
}) })
position: string; position: string;
@Column({
nullable: true,
comment: "เลขที่ตำแหน่ง",
length: 255,
default: null,
})
posNo: string;
@Column({ @Column({
nullable: true, nullable: true,
comment: "ประเภท", comment: "ประเภท",