Merge branch 'develop' into adiDev

This commit is contained in:
AdisakKanthawilang 2025-07-30 10:47:40 +07:00
commit 90a0c8dd0b
2 changed files with 190 additions and 91 deletions

View file

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

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: "ประเภท",