diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 8640472c..7785b03a 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -123,15 +123,17 @@ async function handler(msg: amqp.ConsumeMessage): Promise { relations: ["commandType", "commandRecives"], }); if (!command) return true; - sendWebSocket( - "send-command-notification", - { - success: true, - message: `ระบบทำการออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543}`, - payload: command, - }, - { userId: user.sub ?? "" }, - ).catch(console.error); + 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() @@ -166,30 +168,32 @@ async function handler(msg: amqp.ConsumeMessage): Promise { console.log("[AMQ] Excecute Command Success"); Object.assign(command, { status, lastUpdateUserId, lastUpdateFullName, lastUpdatedAt }); const result = await repo.save(command).catch((e) => console.log(e)); - - sendWebSocket( - "send-command-notification", - { - success: true, - message: `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} เสร็จสิ้น`, - payload: command, - }, - { userId: user.sub ?? "" }, - ).catch(console.error); - + 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); - sendWebSocket( - "send-command-notification", - { - success: false, - message: `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} ผิดพลาด`, - payload: command, - }, - { userId: user.sub ?? "" }, - ).catch(console.error); + if(user){ + sendWebSocket( + "send-command-notification", + { + success: false, + message: `ระบบออกคำสั่งเลขที่ ${command.commandNo}/${command.commandYear + 543} ผิดพลาด`, + payload: command, + }, + { userId: user?.sub}, + ).catch(console.error); + } return false; }); }