From a3672348c80e73b1fbdeefd7741bc7b9071c888e Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 30 Apr 2025 15:25:03 +0700 Subject: [PATCH] test excecute 50 --- src/services/rabbitmq.ts | 250 +++++++++++++++++++++++++++++---------- 1 file changed, 187 insertions(+), 63 deletions(-) diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 7785b03a..5e62d21e 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -113,16 +113,103 @@ function createConsumer( //----> consumer ); } +// async function handler(msg: amqp.ConsumeMessage): Promise { +// //----> 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 { - //----> 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 { { 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 { @@ -217,7 +312,22 @@ async function handler_command_noti(msg: amqp.ConsumeMessage): Promise 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 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]);