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()
.PostData( try {
{ const chunks = chunkArray(
headers: { authorization: token }, command.commandRecives.filter((x) => x.refId != null).map((x) => ({
}, refId: x.refId,
path + "/excecute", commandNo: command.commandNo,
{ commandYear: command.commandYear,
refIds: command.commandRecives commandId: command.id,
.filter((x) => x.refId != null) remark: command.positionDetail,
.map((x) => ({ amount: x.amount,
refId: x.refId, amountSpecial: x.amountSpecial,
commandNo: command.commandNo, positionSalaryAmount: x.positionSalaryAmount,
commandYear: command.commandYear, mouthSalaryAmount: x.mouthSalaryAmount,
commandId: command.id, commandCode: command.commandType.commandCode,
remark: command.positionDetail, commandName: command.commandType.name,
amount: x.amount, commandDateAffect: command.commandExcecuteDate,
amountSpecial: x.amountSpecial, commandDateSign: command.commandAffectDate,
positionSalaryAmount: x.positionSalaryAmount, })),
mouthSalaryAmount: x.mouthSalaryAmount, 50
commandCode: command.commandType.commandCode, );
commandName: command.commandType.name,
commandDateAffect: command.commandExcecuteDate, for (const chunk of chunks) {
commandDateSign: command.commandAffectDate, await new CallAPI().PostData(
})), { headers: { authorization: token } },
}, path + "/excecute",
false, { refIds: chunk },
) 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)); Object.assign(command, { status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt });
if (user) { const result = await repo.save(command);
sendWebSocket(
"send-command-notification", if (user) {
{ sendWebSocket(
success: true, "send-command-notification",
message: success_message, { success: true, message: success_message, payload: command },
payload: command, { userId: user?.sub },
}, ).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 },
success: false, { userId: user?.sub },
message: error_message, ).catch(console.error);
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> {