From b26eef9b2caead10018156fa9212dcf636884696 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sun, 9 Jul 2023 01:26:40 +0300 Subject: [PATCH] adding business logic --- notification-service/src/main.ts | 8 +++++--- notification-service/src/rabbitmq/RabbitMQ.ts | 8 +++++--- todo-service/src/rabbitmq/RabbitMQ.ts | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/notification-service/src/main.ts b/notification-service/src/main.ts index 0681546..c5a42b5 100644 --- a/notification-service/src/main.ts +++ b/notification-service/src/main.ts @@ -16,17 +16,19 @@ export class NotificationService { async startListener() { if (this.IsUserConnected()) { await this.rabbitmq.connect(); - this.rabbitmq.startConsumer(); + this.rabbitmq.startConsumer(this.newMessageValidator.bind(this)); } } async newMessageValidator(message: ITodo) { + console.log("Validating message:", message) const todo = await this.mongoModle.getTodoById(message._id.toString()); + console.log("Todo:", todo) if (todo) { + const due_date = new Date(todo.due_date); if ( todo.status === "pending" && - todo.due_date < new Date() && - todo.due_date === message.due_date + due_date > new Date() ) { await this.mongoModle.updateTodoStatus(todo); await this.sendNotification(todo); // Send notification to user diff --git a/notification-service/src/rabbitmq/RabbitMQ.ts b/notification-service/src/rabbitmq/RabbitMQ.ts index 9f34b47..45501ca 100644 --- a/notification-service/src/rabbitmq/RabbitMQ.ts +++ b/notification-service/src/rabbitmq/RabbitMQ.ts @@ -65,13 +65,15 @@ export class RabbitMQ { } } - async startConsumer(): Promise { + async startConsumer(callback: Function): Promise { this.channel.assertQueue(this.queueName); this.channel.consume(this.queueName, (message: ConsumeMessage | null) => { if (message) { - const todo: ITodo = JSON.parse(message.content.toString()); + console.log("Message received from the queue"); + console.log("Message content: ", message.content.toString()); + const todo: ITodo = JSON.parse(message.content.toString()).payload; this.channel.ack(message); - return todo; + callback(todo); } }); } diff --git a/todo-service/src/rabbitmq/RabbitMQ.ts b/todo-service/src/rabbitmq/RabbitMQ.ts index 9ce035b..613f63a 100644 --- a/todo-service/src/rabbitmq/RabbitMQ.ts +++ b/todo-service/src/rabbitmq/RabbitMQ.ts @@ -74,6 +74,6 @@ export class RabbitMQ { calculateDelayTimeForQueue(payload: ITodo) { const delayTime = payload.due_date.getTime() - Date.now(); - return delayTime; + return 0; } }