diff --git a/notification-service/src/interfaces/ITodo.ts b/notification-service/src/interfaces/ITodo.ts index b7efceb..03cb001 100644 --- a/notification-service/src/interfaces/ITodo.ts +++ b/notification-service/src/interfaces/ITodo.ts @@ -1,4 +1,4 @@ -import { ObjectId } from "mongodb"; +import { ObjectId } from 'mongodb'; export interface ITodo { _id: ObjectId; @@ -8,4 +8,4 @@ export interface ITodo { createAt: Date; updateAt: Date; status: string; -} +} \ No newline at end of file diff --git a/notification-service/src/main.ts b/notification-service/src/main.ts index a8463a6..0681546 100644 --- a/notification-service/src/main.ts +++ b/notification-service/src/main.ts @@ -1,18 +1,51 @@ -// import { Sqs } from './aws/Sqs'; -import { RabbitMQ } from './rabbitmq/RabbitMQ'; +import { ITodo } from "./interfaces/ITodo"; +import { RabbitMQ } from "./rabbitmq/RabbitMQ"; +import { EnvService } from "./services/EnvService"; +import { MongoDbModel } from "./mongodb/MongoDb"; export class NotificationService { rabbitmq: RabbitMQ; + mongoModle: MongoDbModel; constructor() { this.rabbitmq = new RabbitMQ(); + this.mongoModle = new MongoDbModel(); this.startListener(); } async startListener() { - await this.rabbitmq.connect(); - this.rabbitmq.startConsumer(); + if (this.IsUserConnected()) { + await this.rabbitmq.connect(); + this.rabbitmq.startConsumer(); + } + } + + async newMessageValidator(message: ITodo) { + const todo = await this.mongoModle.getTodoById(message._id.toString()); + if (todo) { + if ( + todo.status === "pending" && + todo.due_date < new Date() && + todo.due_date === message.due_date + ) { + await this.mongoModle.updateTodoStatus(todo); + await this.sendNotification(todo); // Send notification to user + } else { + console.log("Todo is not valid for notification"); + console.log(`todo.status === "pending" - ${todo.status === "pending"}`); + console.log(`todo.due_date < new Date() - ${todo.due_date < new Date()}`); + console.log(`todo.due_date === message.due_date - ${todo.due_date === message.due_date}`); + } + } + } + + private IsUserConnected() { + return true; + } + + private async sendNotification(todo: ITodo) { + console.log("Sending notification for Todo:", todo._id); } } -const notificationService = new NotificationService(); \ No newline at end of file +const notificationService = new NotificationService(); diff --git a/notification-service/src/mongodb/MongoDb.ts b/notification-service/src/mongodb/MongoDb.ts index fa731ab..fd1e6e1 100644 --- a/notification-service/src/mongodb/MongoDb.ts +++ b/notification-service/src/mongodb/MongoDb.ts @@ -8,13 +8,17 @@ export class MongoDbModel { constructor() { this.envSerivce = EnvService.getInstance(); - this.client = new MongoClient(this.envSerivce.getEnvVariable('DATABASE_URL')); + this.client = new MongoClient( + this.envSerivce.getEnvVariable("DATABASE_URL") + ); } public updateTodoStatus = async (todo: ITodo) => { try { await this.client.connect(); - const db = this.client.db(this.envSerivce.getEnvVariable('MONGO_DB_NAME')); + const db = this.client.db( + this.envSerivce.getEnvVariable("MONGO_DB_NAME") + ); const todosCollection = db.collection("todos"); const result = await todosCollection.updateOne( { _id: new ObjectId(todo._id) }, @@ -27,4 +31,38 @@ export class MongoDbModel { await this.client.close(); } }; + + public getTodoById = async (id: string): Promise => { + try { + await this.client.connect(); + const db = this.client.db( + this.envSerivce.getEnvVariable("MONGO_DB_NAME") + ); + const todosCollection = db.collection("todos"); + const todo = await todosCollection.findOne({ + _id: new ObjectId(id), + }); + + // Convert the document to ITodo type + if (todo) { + const convertedTodo: ITodo = { + _id: new ObjectId(todo._id), + title: todo.title, + description: todo.description, + due_date: todo.due_date, + createAt: todo.createAt, + updateAt: todo.updateAt, + status: todo.status, + }; + + return convertedTodo; + } + + return undefined; + } catch (error) { + console.error("Error getting Todo by id:", error); + } finally { + await this.client.close(); + } + }; } diff --git a/notification-service/src/rabbitmq/RabbitMQ.ts b/notification-service/src/rabbitmq/RabbitMQ.ts index ebb9167..9f34b47 100644 --- a/notification-service/src/rabbitmq/RabbitMQ.ts +++ b/notification-service/src/rabbitmq/RabbitMQ.ts @@ -65,14 +65,13 @@ export class RabbitMQ { } } - async startConsumer() { + async startConsumer(): Promise { this.channel.assertQueue(this.queueName); - console.log("Consuming"); this.channel.consume(this.queueName, (message: ConsumeMessage | null) => { if (message) { - const todo = JSON.parse(message.content.toString()); + const todo: ITodo = JSON.parse(message.content.toString()); this.channel.ack(message); - console.log("Received notification:", todo); + return todo; } }); }