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> {
//----> 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 },
where: { id },
relations: ["commandType", "commandRecives"],
});
if (!command) return true;
if (user) {
sendWebSocket(
"send-command-notification",
@ -134,68 +221,76 @@ async function handler(msg: amqp.ConsumeMessage): Promise<boolean> {
{ 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;
});
const recivers = 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,
}));
const batchSize = 50;
const batches = Array.from({ length: Math.ceil(recivers.length / batchSize) }, (_, i) =>
recivers.slice(i * batchSize, i * batchSize + batchSize)
);
const api = new CallAPI();
try {
for (const batch of batches) {
await api.PostData(
{ headers: { authorization: token } },
path + "/excecute",
{ refIds: batch },
false
);
}
console.log("[AMQ] Excecute Command Success");
Object.assign(command, { status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt });
const result = await repo.save(command);
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_command_noti(msg: amqp.ConsumeMessage): Promise<boolean> {
@ -217,7 +312,22 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise<boolean>
isSendNotification: true,
},
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
? command.commandSends
@ -242,7 +352,21 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise<boolean>
payload: "", // แนบไฟล์ (ถ้าจำเป็น)
},
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]);