diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 19205d41..4b5ceba8 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -1127,6 +1127,88 @@ export class CommandController extends Controller { return new HttpSuccess(); } + /** + * API ออกคำสั่ง + * + * @summary API ออกคำสั่ง + * + * @param {string} id Id คำสั่ง + */ + @Put("testRabbit/{id}") + async testRabbit( + @Path() id: string, + @Body() + requestBody: { sign?: boolean }, + @Request() request: RequestWithUser, + ) { + await new permission().PermissionUpdate(request, "COMMAND"); + const command = await this.commandRepository.findOne({ + where: { id: id }, + relations: ["commandType", "commandRecives",], + }); + if (!command) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลคำสั่งนี้"); + } + command.isSign = true; + if (command.commandExcecuteDate == null) + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบวันที่คำสั่งมีผล"); + + let profiles = + command && command.commandRecives.length > 0 + ? command.commandRecives + .filter((x) => x.profileId != null) + .map((x) => ({ + receiverUserId: x.profileId, + notiLink: "", + })) + : []; + + await new CallAPI() + .PostData(request, "/placement/noti/profiles", { + subject: `${command.issue}`, + body: `${command.issue}`, + receiverUserIds: profiles, + payload: "", //แนบไฟล์ + isSendMail: true, + isSendInbox: true, + isSendNotification: true, + }) + .catch((error) => { + console.error("Error calling API:", error); + }); + + if ( + new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()) < + new Date( + command.commandExcecuteDate.getFullYear(), + command.commandExcecuteDate.getMonth(), + command.commandExcecuteDate.getDate(), + ) + ) { + command.status = "WAITING"; + command.lastUpdateUserId = request.user.sub; + command.lastUpdateFullName = request.user.name; + command.lastUpdatedAt = new Date(); + await this.commandRepository.save(command); + } else { + const path = commandTypePath(command.commandType.code); + if (path == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบประเภทคำสั่งนี้ในระบบ"); + const msg = { + data: { + id: command.id, + status: "REPORTED", + lastUpdateUserId: request.user.sub, + lastUpdateFullName: request.user.name, + lastUpdatedAt: new Date(), + }, + user: request.user, + token: request.headers["authorization"], + }; + sendToQueue(msg); + } + return new HttpSuccess(); + } + async cronjobCommand(@Request() request?: RequestWithUser) { const today = new Date(); today.setUTCHours(0, 0, 0, 0); diff --git a/src/services/rabbitmq.ts b/src/services/rabbitmq.ts index 8bd75016..ecd30f66 100644 --- a/src/services/rabbitmq.ts +++ b/src/services/rabbitmq.ts @@ -19,8 +19,12 @@ export async function init() { const connection = await amqp.connect(url); //----> (1.3) set up url with amqp protocol + console.log(connection ? "[AMQ] connection success" : "[AMQ] connection failed"); + const channel = await connection.createChannel(); //----> (1.4) create Channel + console.log(channel ? "[AMQ] create channel success" : "[AMQ] create channel failed"); + channel.assertQueue(queue, { durable: true }); //----> (1.5) assert queue and set durable (if "true" save to disk on RabbitMQ) channel.assertQueue(queue_org, { durable: true }); channel.prefetch(1);