diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 57844dae..bae5a1a5 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -2250,6 +2250,43 @@ export class CommandController extends Controller { commandRecive.lastUpdateUserId = request.user.sub; commandRecive.lastUpdateFullName = request.user.name; 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); }), ); @@ -4505,9 +4542,10 @@ export class CommandController extends Controller { position: profile.position, positionName: profile.position, positionType: profile?.posType?.posTypeName ?? null, - positionLevel: profile?.posType && profile?.posLevel - ? `${profile?.posType?.posTypeShortName} ${profile?.posLevel?.posLevelName }` - : null, + positionLevel: + profile?.posType && profile?.posLevel + ? `${profile?.posType?.posTypeShortName} ${profile?.posLevel?.posLevelName}` + : null, amount: item.amount ? item.amount : null, positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null, mouthSalaryAmount: item.mouthSalaryAmount ? item.mouthSalaryAmount : null, @@ -4795,8 +4833,9 @@ export class CommandController extends Controller { commandId: item.commandId, position: profile.position, positionType: profile?.posType?.posTypeName ?? null, - positionLevel: profile?.posType && profile?.posLevel - ? `${profile?.posType?.posTypeShortName} ${profile?.posLevel?.posLevelName }` + positionLevel: + profile?.posType && profile?.posLevel + ? `${profile?.posType?.posTypeShortName} ${profile?.posLevel?.posLevelName}` : null, amount: item.amount ? item.amount : null, amountSpecial: item.amountSpecial ? item.amountSpecial : null, @@ -6544,17 +6583,21 @@ export class CommandController extends Controller { }[]; }, ) { + // 1. Bulk update status await this.posMasterActRepository.update( { id: In(body.refIds.map((x) => x.refId)) }, { statusReport: "DONE" }, ); + // 2. ดึงข้อมูลครบทุก relation ที่จำเป็น const posMasters = await this.posMasterActRepository.find({ where: { id: In(body.refIds.map((x) => x.refId)) }, relations: [ "posMasterChild", + "posMasterChild.current_holder", "posMaster", "posMaster.current_holder", + "posMaster.positions", "posMaster.orgRoot", "posMaster.orgChild1", "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( posMasters.map(async (item) => { - if (item.posMasterChild?.current_holderId) { - const shortName = - [ - item.posMaster?.orgChild4?.orgChild4ShortName, - item.posMaster?.orgChild3?.orgChild3ShortName, - item.posMaster?.orgChild2?.orgChild2ShortName, - item.posMaster?.orgChild1?.orgChild1ShortName, - item.posMaster?.orgRoot?.orgRootShortName, - ].find(Boolean) ?? ""; + // 4. ตรวจสอบข้อมูลที่จำเป็นทั้งหมด + if (!item.posMasterChild?.current_holderId || !item.posMaster) { + console.warn(`ข้ามรายการ ${item.id}: ข้อมูลไม่ครบ`); + return; + } - const metaAct = { - profileId: item.posMasterChild.current_holderId ?? null, - dateStart: body.refIds[0]?.commandDateAffect ?? null, - dateEnd: null, - position: item.posMaster?.current_holder?.position ?? "-", - status: true, - commandId: body.refIds[0]?.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: 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, - }; + // 5. สร้าง orgShortName แบบปลอดภัย + const orgShortName = + [ + item.posMaster?.orgChild4?.orgChild4ShortName, + item.posMaster?.orgChild3?.orgChild3ShortName, + item.posMaster?.orgChild2?.orgChild2ShortName, + item.posMaster?.orgChild1?.orgChild1ShortName, + item.posMaster?.orgRoot?.orgRootShortName, + ].find(Boolean) ?? ""; + // 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(); Object.assign(dataAct, metaAct); + const historyAct = new ProfileActpositionHistory(); Object.assign(historyAct, { ...dataAct, id: undefined }); @@ -6612,19 +6678,34 @@ export class CommandController extends Controller { historyAct.profileActpositionId = dataAct.id; await this.actpositionHistoryRepository.save(historyAct); - // ปิดสถานะรักษาการเดิม - const _actposition = await this.actpositionRepository.find({ - where: { profileId: item.posMasterChild.current_holderId }, + // 9. ปิดสถานะรักษาการ + const existingActPositions = await this.actpositionRepository.find({ + where: { + profileId: item.posMasterChild.current_holderId, + status: true, + id: Not(dataAct.id), + }, }); - const profileActs = _actposition.map((_data) => ({ - ..._data, - status: false, - dateEnd: _data.status === true ? new Date() : _data.dateEnd, - })); - await this.actpositionRepository.save(profileActs); + + if (existingActPositions.length > 0) { + const updatedActPositions = existingActPositions.map((_data) => ({ + ..._data, + status: false, + 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(); } diff --git a/src/entities/CommandRecive.ts b/src/entities/CommandRecive.ts index ef638fe5..c12a4efc 100644 --- a/src/entities/CommandRecive.ts +++ b/src/entities/CommandRecive.ts @@ -44,6 +44,14 @@ export class CommandRecive extends EntityBase { }) position: string; + @Column({ + nullable: true, + comment: "เลขที่ตำแหน่ง", + length: 255, + default: null, + }) + posNo: string; + @Column({ nullable: true, comment: "ประเภท",