test excecute 50

This commit is contained in:
AdisakKanthawilang 2025-04-30 15:25:03 +07:00
parent e3ae4babfe
commit a3672348c8

View file

@ -113,16 +113,103 @@ function createConsumer( //----> consumer
); );
} }
// async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {
// //----> 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;
// if (user) {
// sendWebSocket(
// "send-command-notification",
// {
// success: true,
// message: `ระบบทำการออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543}`,
// 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()
// .PostData(
// {
// headers: { authorization: token },
// },
// path + "/excecute",
// {
// refIds: command.commandRecives
// .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: `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} เสร็จสิ้น`,
// payload: command,
// },
// { userId: user?.sub},
// ).catch(console.error);
// }
// return !!result;
// })
// .catch((e) => {
// console.error(e);
// if(user){
// sendWebSocket(
// "send-command-notification",
// {
// success: false,
// message: `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} ผิดพลาด`,
// payload: command,
// },
// { userId: user?.sub},
// ).catch(console.error);
// }
// return false;
// });
// }
async function handler(msg: amqp.ConsumeMessage): Promise<boolean> { async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {
//----> condition before process consumer
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 },
relations: ["commandType", "commandRecives"], relations: ["commandType", "commandRecives"],
}); });
if (!command) return true; if (!command) return true;
if (user) { if (user) {
sendWebSocket( sendWebSocket(
"send-command-notification", "send-command-notification",
@ -134,68 +221,76 @@ async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {
{ 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( const recivers = command.commandRecives
{ .filter((x) => x.refId != null)
headers: { authorization: token }, .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,
commandCode: command.commandType.commandCode, const batchSize = 50;
commandName: command.commandType.name, const batches = Array.from({ length: Math.ceil(recivers.length / batchSize) }, (_, i) =>
commandDateAffect: command.commandExcecuteDate, recivers.slice(i * batchSize, i * batchSize + batchSize)
commandDateSign: command.commandAffectDate, );
})),
}, const api = new CallAPI();
false, try {
) for (const batch of batches) {
.then(async (res) => { await api.PostData(
console.log("[AMQ] Excecute Command Success"); { headers: { authorization: token } },
Object.assign(command, { status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt }); path + "/excecute",
const result = await repo.save(command).catch((e) => console.log(e)); { refIds: batch },
if(user){ false
sendWebSocket( );
"send-command-notification", }
{
success: true, console.log("[AMQ] Excecute Command Success");
message: `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} เสร็จสิ้น`, Object.assign(command, { status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt });
payload: command, const result = await repo.save(command);
},
{ userId: user?.sub}, if (user) {
).catch(console.error); sendWebSocket(
} "send-command-notification",
return !!result; {
}) success: true,
.catch((e) => { message: `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} เสร็จสิ้น`,
console.error(e); payload: command,
if(user){ },
sendWebSocket( { userId: user?.sub },
"send-command-notification", ).catch(console.error);
{ }
success: false,
message: `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} ผิดพลาด`, return !!result;
payload: command, } catch (e) {
}, console.error(e);
{ userId: user?.sub}, if (user) {
).catch(console.error); sendWebSocket(
} "send-command-notification",
return false; {
}); success: false,
message: `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} ผิดพลาด`,
payload: command,
},
{ userId: user?.sub },
).catch(console.error);
}
return false;
}
} }
async function handler_command_noti(msg: amqp.ConsumeMessage): Promise<boolean> { async function handler_command_noti(msg: amqp.ConsumeMessage): Promise<boolean> {
@ -217,7 +312,22 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise<boolean>
isSendNotification: true, isSendNotification: true,
}, },
false false
); )
.catch((error) => {
if (error.response) {
// Server ตอบกลับ (มี status code 4xx หรือ 5xx)
console.error("Error status:", error.response.status);
console.error("Error data:", error.response.data);
console.error("Error headers:", error.response.headers);
} else if (error.request) {
// ไม่มีการตอบกลับจาก server
console.error("No response received:", error.request);
} else {
// เกิดข้อผิดพลาดอื่น เช่น โค้ด Axios ผิด
console.error("Axios error:", error.message);
}
console.error("Full error object:", error);
});
let profilesSend = command && command.commandSends.length > 0 let profilesSend = command && command.commandSends.length > 0
? command.commandSends ? command.commandSends
@ -242,7 +352,21 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise<boolean>
payload: "", // แนบไฟล์ (ถ้าจำเป็น) payload: "", // แนบไฟล์ (ถ้าจำเป็น)
}, },
false false
); ).catch((error) => {
if (error.response) {
// Server ตอบกลับ (มี status code 4xx หรือ 5xx)
console.error("Error status:", error.response.status);
console.error("Error data:", error.response.data);
console.error("Error headers:", error.response.headers);
} else if (error.request) {
// ไม่มีการตอบกลับจาก server
console.error("No response received:", error.request);
} else {
// เกิดข้อผิดพลาดอื่น เช่น โค้ด Axios ผิด
console.error("Axios error:", error.message);
}
console.error("Full error object:", error);
});
await Promise.all([profilesNotiRequest, profilesSendRequest]); await Promise.all([profilesNotiRequest, profilesSendRequest]);