diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index cc237a5b..e0b3240b 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -517,6 +517,7 @@ export class CommandController extends Controller { position: x.position, posType: x.posType, posLevel: x.posLevel, + posNo: x.posNo, })), }; return new HttpSuccess(_command); @@ -2250,6 +2251,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); }), ); @@ -2289,7 +2327,7 @@ export class CommandController extends Controller { let _commandSend = await this.commandSendRepository.findOne({ where: { commandId: command.id, - profileId: _posMasterCommission?.current_holder.id ?? _null, + profileId: _posMasterCommission?.current_holderId ?? _null, }, }); if (!_commandSend && _posMasterCommission != null) { @@ -2346,7 +2384,7 @@ export class CommandController extends Controller { _commandSend = await this.commandSendRepository.findOne({ where: { commandId: command.id, - profileId: _posMasterInformation?.current_holder.id ?? _null, + profileId: _posMasterInformation?.current_holderId ?? _null, }, }); if (!_commandSend && _posMasterInformation != null) { @@ -2403,7 +2441,7 @@ export class CommandController extends Controller { _commandSend = await this.commandSendRepository.findOne({ where: { commandId: command.id, - profileId: _posMasterOfficer?.current_holder.id ?? _null, + profileId: _posMasterOfficer?.current_holderId ?? _null, }, }); if (!_commandSend && _posMasterOfficer != null) { @@ -2588,42 +2626,43 @@ export class CommandController extends Controller { const _commandSend = await this.commandSendRepository.findOne({ where: { commandId: command.id, - profileId: item.current_holder.id, + profileId: item.current_holderId, }, }); - if (_commandSend) return; - let commandSend = new CommandSend(); - commandSend.citizenId = item.current_holder.citizenId; - commandSend.prefix = item.current_holder.prefix; - commandSend.firstName = item.current_holder.firstName; - commandSend.lastName = item.current_holder.lastName; - commandSend.position = item.current_holder.position; - commandSend.org = item.orgRoot.orgRootName; - commandSend.profileId = item.current_holder.id; - commandSend.commandId = command.id; - commandSend.createdUserId = request.user.sub; - commandSend.createdFullName = request.user.name; - commandSend.createdAt = new Date(); - commandSend.lastUpdateUserId = request.user.sub; - commandSend.lastUpdateFullName = request.user.name; - commandSend.lastUpdatedAt = new Date(); - await this.commandSendRepository.save(commandSend); - if (commandSend && commandSend.id) { - let _ccName = new Array("EMAIL", "INBOX"); - let _dataSendCC = new Array(); - for (let i = 0; i < _ccName.length; i++) { - _dataSendCC.push({ - commandSendId: commandSend.id, - name: _ccName[i], - createdUserId: request.user.sub, - createdFullName: request.user.name, - createdAt: new Date(), - lastUpdateUserId: request.user.sub, - lastUpdateFullName: request.user.name, - lastUpdatedAt: new Date(), - }); + if (!_commandSend) { + let commandSend = new CommandSend(); + commandSend.citizenId = item.current_holder.citizenId; + commandSend.prefix = item.current_holder.prefix; + commandSend.firstName = item.current_holder.firstName; + commandSend.lastName = item.current_holder.lastName; + commandSend.position = item.current_holder.position; + commandSend.org = item.orgRoot.orgRootName; + commandSend.profileId = item.current_holderId; + commandSend.commandId = command.id; + commandSend.createdUserId = request.user.sub; + commandSend.createdFullName = request.user.name; + commandSend.createdAt = new Date(); + commandSend.lastUpdateUserId = request.user.sub; + commandSend.lastUpdateFullName = request.user.name; + commandSend.lastUpdatedAt = new Date(); + await this.commandSendRepository.save(commandSend); + if (commandSend && commandSend.id) { + let _ccName = new Array("EMAIL", "INBOX"); + let _dataSendCC = new Array(); + for (let i = 0; i < _ccName.length; i++) { + _dataSendCC.push({ + commandSendId: commandSend.id, + name: _ccName[i], + createdUserId: request.user.sub, + createdFullName: request.user.name, + createdAt: new Date(), + lastUpdateUserId: request.user.sub, + lastUpdateFullName: request.user.name, + lastUpdatedAt: new Date(), + }); + } + await this.commandSendCCRepository.save(_dataSendCC); } - await this.commandSendCCRepository.save(_dataSendCC); } }), ); @@ -2643,11 +2682,11 @@ export class CommandController extends Controller { relations: ["current_holder", "orgRoot"], }); await Promise.all( - _posMasterNext.map(async (item) => { + _posMasterNext.map(async (item: any) => { const _commandSend = await this.commandSendRepository.findOne({ where: { commandId: command.id, - profileId: item.current_holder.id, + profileId: item.current_holderId, }, }); // if (_commandSend) return; @@ -2659,7 +2698,7 @@ export class CommandController extends Controller { commandSend.lastName = item.current_holder.lastName; commandSend.position = item.current_holder.position; commandSend.org = item.orgRoot.orgRootName; - commandSend.profileId = item.current_holder.id; + commandSend.profileId = item.current_holderId; commandSend.commandId = command.id; commandSend.createdUserId = request.user.sub; commandSend.createdFullName = request.user.name; @@ -4505,7 +4544,10 @@ export class CommandController extends Controller { position: profile.position, positionName: profile.position, positionType: profile?.posType?.posTypeName ?? null, - positionLevel: 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, @@ -4793,7 +4835,10 @@ export class CommandController extends Controller { commandId: item.commandId, position: profile.position, positionType: profile?.posType?.posTypeName ?? null, - positionLevel: profile?.posLevel?.posLevelName ?? null, + positionLevel: + profile?.posType && profile?.posLevel + ? `${profile?.posType?.posTypeShortName} ${profile?.posLevel?.posLevelName}` + : null, amount: item.amount ? item.amount : null, amountSpecial: item.amountSpecial ? item.amountSpecial : null, positionSalaryAmount: item.positionSalaryAmount ? item.positionSalaryAmount : null, @@ -6080,12 +6125,13 @@ export class CommandController extends Controller { @Body() body: { refIds: string[]; + status: string; }, ) { const profile = await this.profileEmployeeRepository.find({ where: { id: In(body.refIds) } }); const data = profile.map((_data) => ({ ..._data, - statusTemp: "REPORT", + statusTemp: body.status, })); await this.profileEmployeeRepository.save(data); return new HttpSuccess(); @@ -6097,12 +6143,13 @@ export class CommandController extends Controller { @Body() body: { refIds: string[]; + status: string; }, ) { const posMasters = await this.posMasterRepository.find({ where: { id: In(body.refIds) } }); const data = posMasters.map((_data) => ({ ..._data, - statusReport: "REPORT", + statusReport: body.status, })); await this.posMasterRepository.save(data); if (posMasters && posMasters.length > 0) { @@ -6124,12 +6171,13 @@ export class CommandController extends Controller { @Body() body: { refIds: string[]; + status: string; }, ) { const posMasters = await this.posMasterActRepository.find({ where: { id: In(body.refIds) } }); const data = posMasters.map((_data) => ({ ..._data, - statusReport: "REPORT", + statusReport: body.status, })); await this.posMasterActRepository.save(data); return new HttpSuccess(); @@ -6540,17 +6588,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", @@ -6559,48 +6611,72 @@ 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; + const positionName = + selectedPosition + ?.map((pos) => pos.positionName) + .filter(Boolean) + .join(", ") ?? "-"; + + // 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 }); @@ -6608,19 +6684,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(); } @@ -6733,8 +6824,8 @@ export class CommandController extends Controller { ? Extension.ToThaiShortDate(item.CommandExcecuteDate) : "-", dateEnd: "-", - RemarkVertical: item.RemarkVertical ? Extension.ToThaiNumber(item.RemarkVertical) : "-", - RemarkHorizontal: item.RemarkHorizontal + remarkVertical: item.RemarkVertical ? Extension.ToThaiNumber(item.RemarkVertical) : "-", + remarkHorizontal: item.RemarkHorizontal ? Extension.ToThaiNumber(item.RemarkHorizontal) : "-", order: 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: "ประเภท",