From 5ac856fb18c47b3ff764a4a2c444ee0c22e9074c Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 1 May 2025 10:19:19 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=9B=E0=B8=B4=E0=B8=94=E0=B8=AD=E0=B8=AD?= =?UTF-8?q?=E0=B8=81=E0=B8=84=E0=B8=B3=E0=B8=AA=E0=B8=B1=E0=B9=88=E0=B8=87?= =?UTF-8?q?=20fix=20root=20salary/gen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OrganizationUnauthorizeController.ts | 19 ++ src/services/rabbitmq.ts | 312 +++++++++--------- 2 files changed, 175 insertions(+), 156 deletions(-) diff --git a/src/controllers/OrganizationUnauthorizeController.ts b/src/controllers/OrganizationUnauthorizeController.ts index e65bb06a..9fcaea84 100644 --- a/src/controllers/OrganizationUnauthorizeController.ts +++ b/src/controllers/OrganizationUnauthorizeController.ts @@ -317,10 +317,19 @@ export class OrganizationUnauthorizeController extends Controller { if (!findRevision) { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. OrgRevision"); } + const rootIds = [ + "d7e98989-b5ce-47d6-93c3-ab63ed486348", + "e0545eca-5d0a-4a1c-8bbd-e3e25c2521db", + "7ff1fbf2-cc9e-4f53-b83c-334967c27967", + "26989ffa-d5ab-4bbd-ac97-130646cd1da6", + "6f9b30e1-757a-40d5-b053-61eb1b91c0f0", + "eaf65f33-25e9-4956-9dba-5d909f5eb595", + ]; const [findPosMaster, total] = await AppDataSource.getRepository(viewPosMaster) .createQueryBuilder("viewPosMaster") .where({orgRevisionId: findRevision?.id}) + .andWhere("viewPosMaster.rootId IN (:...rootIds)", { rootIds }) .andWhere( new Brackets((qb) => { qb.where( @@ -744,11 +753,21 @@ export class OrganizationUnauthorizeController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "not found. OrgRevision"); } + const rootIds = [ + "d7e98989-b5ce-47d6-93c3-ab63ed486348", + "e0545eca-5d0a-4a1c-8bbd-e3e25c2521db", + "7ff1fbf2-cc9e-4f53-b83c-334967c27967", + "26989ffa-d5ab-4bbd-ac97-130646cd1da6", + "6f9b30e1-757a-40d5-b053-61eb1b91c0f0", + "eaf65f33-25e9-4956-9dba-5d909f5eb595", + ]; + const [findPosMaster, total] = await AppDataSource.getRepository(viewEmployeePosMaster) .createQueryBuilder("viewEmployeePosMaster") .where({ orgRevisionId: findRevision?.id, }) + .andWhere("viewEmployeePosMaster.rootId IN (:...rootIds)", { rootIds }) .andWhere( new Brackets((qb) => { qb.where( diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 5e62d21e..23adf4ef 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -113,103 +113,16 @@ 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 }, + where: { id: id }, relations: ["commandType", "commandRecives"], }); - if (!command) return true; - if (user) { sendWebSocket( "send-command-notification", @@ -221,78 +134,165 @@ 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() - 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; - } + .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 { +// 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 }, +// 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, "ไม่พบประเภทคำสั่งนี้ในระบบ"); + +// 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 { const { data, token, user } = JSON.parse(msg.content.toString()); const { profiles, command } = data;