This commit is contained in:
AdisakKanthawilang 2025-08-18 09:48:41 +07:00
parent 8992698651
commit ce2114131d
2 changed files with 161 additions and 68 deletions

View file

@ -553,6 +553,14 @@ export async function checkQueueInProgress(queueName: string) {
// return false; // return false;
} }
export function chunkArray(array: any, size: number) {
const result = [];
for (let i = 0; i < array.length; i += size) {
result.push(array.slice(i, i + size));
}
return result;
}
export function commandTypePath(commandCode: string): string | null { export function commandTypePath(commandCode: string): string | null {
switch (commandCode) { switch (commandCode) {
case "C-PM-01": case "C-PM-01":

View file

@ -1,7 +1,7 @@
import amqp from "amqplib"; import amqp from "amqplib";
import { AppDataSource } from "../database/data-source"; import { AppDataSource } from "../database/data-source";
import { Command } from "../entities/Command"; import { Command } from "../entities/Command";
import { commandTypePath } from "../interfaces/utils"; import { chunkArray, commandTypePath } from "../interfaces/utils";
import CallAPI from "../interfaces/call-api"; import CallAPI from "../interfaces/call-api";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
@ -115,95 +115,180 @@ function createConsumer( //----> consumer
async function handler(msg: amqp.ConsumeMessage): Promise<boolean> { async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {
//----> condition before process consumer //----> condition before process consumer
// const repo = AppDataSource.getRepository(Command);
// const { data, token, user } = JSON.parse(msg.content.toString());
// const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data;
// const command = await repo.findOne({
// where: { id: id },
// relations: ["commandType", "commandRecives"],
// });
// if (!command) return true;
// let waiting_message = `ระบบทำการออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543}`;
// let success_message = `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} เสร็จสิ้น`;
// let error_message = `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} ผิดพลาด`
// if(command.commandType?.code == "C-PM-47"){
// waiting_message = `ระบบทำการออกคำสั่งเลขที่ ${command.commandNo}`;
// success_message = `ระบบออกคำสั่งเลขที่ ${command.commandNo} เสร็จสิ้น`;
// error_message = `ระบบออกคำสั่งเลขที่ ${command.commandNo} ผิดพลาด`;
// }
// if (user) {
// sendWebSocket(
// "send-command-notification",
// {
// success: true,
// message: waiting_message,
// payload: command,
// },
// { userId: user?.sub },
// ).catch(console.error);
// }
// const path = commandTypePath(command.commandType.code);
// if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
// return await new CallAPI()
// //chunk 50
// .PostData(
// {
// headers: { authorization: token },
// },
// path + "/excecute",
// {
// refIds: command.commandRecives //chunk
// .filter((x) => x.refId != null)
// .map((x) => ({
// refId: x.refId,
// commandNo: command.commandNo,
// commandYear: command.commandYear,
// commandId: command.id,
// remark: command.positionDetail,
// amount: x.amount,
// amountSpecial: x.amountSpecial,
// positionSalaryAmount: x.positionSalaryAmount,
// mouthSalaryAmount: x.mouthSalaryAmount,
// commandCode: command.commandType.commandCode,
// commandName: command.commandType.name,
// commandDateAffect: command.commandExcecuteDate,
// commandDateSign: command.commandAffectDate,
// })),
// },
// false,
// )
// .then(async (res) => {
// console.log("[AMQ] Excecute Command Success");
// Object.assign(command, { status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt });
// const result = await repo.save(command).catch((e) => console.log(e));
// if (user) {
// sendWebSocket(
// "send-command-notification",
// {
// success: true,
// message: success_message,
// payload: command,
// },
// { userId: user?.sub },
// ).catch(console.error);
// }
// return !!result;
// })
// .catch((e) => {
// console.error(e);
// if (user) {
// sendWebSocket(
// "send-command-notification",
// {
// success: false,
// message: error_message,
// payload: command,
// },
// { userId: user?.sub },
// ).catch(console.error);
// }
// return false;
// });
const repo = AppDataSource.getRepository(Command); const repo = AppDataSource.getRepository(Command);
const { data, token, user } = JSON.parse(msg.content.toString()); const { data, token, user } = JSON.parse(msg.content.toString());
const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data; const { id, status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt } = data;
const command = await repo.findOne({ const command = await repo.findOne({
where: { id: id }, where: { id: id },
relations: ["commandType", "commandRecives"], relations: ["commandType", "commandRecives"],
}); });
if (!command) return true; if (!command) return true;
let waiting_message = `ระบบทำการออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543}`; let waiting_message = `ระบบทำการออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543}`;
let success_message = `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} เสร็จสิ้น`; let success_message = `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} เสร็จสิ้น`;
let error_message = `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} ผิดพลาด` let error_message = `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} ผิดพลาด`;
if(command.commandType?.code == "C-PM-47"){
if (command.commandType?.code == "C-PM-47") {
waiting_message = `ระบบทำการออกคำสั่งเลขที่ ${command.commandNo}`; waiting_message = `ระบบทำการออกคำสั่งเลขที่ ${command.commandNo}`;
success_message = `ระบบออกคำสั่งเลขที่ ${command.commandNo} เสร็จสิ้น`; success_message = `ระบบออกคำสั่งเลขที่ ${command.commandNo} เสร็จสิ้น`;
error_message = `ระบบออกคำสั่งเลขที่ ${command.commandNo} ผิดพลาด`; error_message = `ระบบออกคำสั่งเลขที่ ${command.commandNo} ผิดพลาด`;
} }
if (user) { if (user) {
sendWebSocket( sendWebSocket(
"send-command-notification", "send-command-notification",
{ { success: true, message: waiting_message, payload: command },
success: true,
message: waiting_message,
payload: command,
},
{ userId: user?.sub }, { userId: user?.sub },
).catch(console.error); ).catch(console.error);
} }
const path = commandTypePath(command.commandType.code); const path = commandTypePath(command.commandType.code);
if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ"); if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ");
return await new CallAPI()
try {
.PostData( const chunks = chunkArray(
{ command.commandRecives.filter((x) => x.refId != null).map((x) => ({
headers: { authorization: token }, refId: x.refId,
}, commandNo: command.commandNo,
path + "/excecute", commandYear: command.commandYear,
{ commandId: command.id,
refIds: command.commandRecives remark: command.positionDetail,
.filter((x) => x.refId != null) amount: x.amount,
.map((x) => ({ amountSpecial: x.amountSpecial,
refId: x.refId, positionSalaryAmount: x.positionSalaryAmount,
commandNo: command.commandNo, mouthSalaryAmount: x.mouthSalaryAmount,
commandYear: command.commandYear, commandCode: command.commandType.commandCode,
commandId: command.id, commandName: command.commandType.name,
remark: command.positionDetail, commandDateAffect: command.commandExcecuteDate,
amount: x.amount, commandDateSign: command.commandAffectDate,
amountSpecial: x.amountSpecial, })),
positionSalaryAmount: x.positionSalaryAmount, 50
mouthSalaryAmount: x.mouthSalaryAmount, );
commandCode: command.commandType.commandCode,
commandName: command.commandType.name, for (const chunk of chunks) {
commandDateAffect: command.commandExcecuteDate, await new CallAPI().PostData(
commandDateSign: command.commandAffectDate, { headers: { authorization: token } },
})), path + "/excecute",
}, { refIds: chunk },
false, false
) );
.then(async (res) => { }
console.log("[AMQ] Excecute Command Success");
Object.assign(command, { status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt }); Object.assign(command, { status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt });
const result = await repo.save(command).catch((e) => console.log(e)); const result = await repo.save(command);
if (user) {
sendWebSocket( if (user) {
"send-command-notification", sendWebSocket(
{ "send-command-notification",
success: true, { success: true, message: success_message, payload: command },
message: success_message, { userId: user?.sub },
payload: command, ).catch(console.error);
}, }
{ userId: user?.sub },
).catch(console.error); console.log("[AMQ] Excecute Command Success");
} return !!result;
return !!result;
}) } catch (e) {
.catch((e) => { console.error(e);
console.error(e); if (user) {
if (user) { sendWebSocket(
sendWebSocket( "send-command-notification",
"send-command-notification", { success: false, message: error_message, payload: command },
{ { userId: user?.sub },
success: false, ).catch(console.error);
message: error_message, }
payload: command, return false;
}, }
{ userId: user?.sub },
).catch(console.error);
}
return false;
});
} }
// async function handler(msg: amqp.ConsumeMessage): Promise<boolean> { // async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {