From 31c60d2d977a259606ed83d3284d8c42b4b33eb4 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sun, 9 Jul 2023 12:30:32 +0300 Subject: [PATCH] cleaning + fixig issue with env vars --- notification-service/package-lock.json | 20 +++++++++++++++++++ notification-service/package.json | 1 + notification-service/src/main.ts | 19 ++++++++++++------ notification-service/src/mongodb/MongoDb.ts | 1 - notification-service/src/rabbitmq/RabbitMQ.ts | 3 --- .../src/services/DateService.ts | 20 +++++++++++++++++++ 6 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 notification-service/src/services/DateService.ts diff --git a/notification-service/package-lock.json b/notification-service/package-lock.json index ee44322..1f913d5 100644 --- a/notification-service/package-lock.json +++ b/notification-service/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "amqplib": "^0.10.3", "dotenv": "^16.3.1", + "moment-timezone": "^0.5.43", "mongodb": "^5.7.0" }, "devDependencies": { @@ -133,6 +134,25 @@ "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", "optional": true }, + "node_modules/moment": { + "version": "2.29.4", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz", + "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==", + "engines": { + "node": "*" + } + }, + "node_modules/moment-timezone": { + "version": "0.5.43", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.43.tgz", + "integrity": "sha512-72j3aNyuIsDxdF1i7CEgV2FfxM1r6aaqJyLB2vwb33mXYyoyLly+F1zbWqhA3/bVIoJ4szlUoMbUnVdid32NUQ==", + "dependencies": { + "moment": "^2.29.4" + }, + "engines": { + "node": "*" + } + }, "node_modules/mongodb": { "version": "5.7.0", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.7.0.tgz", diff --git a/notification-service/package.json b/notification-service/package.json index 0147c40..64e1a47 100644 --- a/notification-service/package.json +++ b/notification-service/package.json @@ -12,6 +12,7 @@ "dependencies": { "amqplib": "^0.10.3", "dotenv": "^16.3.1", + "moment-timezone": "^0.5.43", "mongodb": "^5.7.0" }, "devDependencies": { diff --git a/notification-service/src/main.ts b/notification-service/src/main.ts index c895981..369ac6a 100644 --- a/notification-service/src/main.ts +++ b/notification-service/src/main.ts @@ -1,12 +1,15 @@ import { ITodo } from "./interfaces/ITodo"; import { RabbitMQ } from "./rabbitmq/RabbitMQ"; import { MongoDbModel } from "./mongodb/MongoDb"; +import { DateService } from "./services/DateService"; export class NotificationService { rabbitmq: RabbitMQ; mongoModel: MongoDbModel; + currentDate: any; constructor() { + this.currentDate = DateService.getInstance(); this.rabbitmq = new RabbitMQ(); this.mongoModel = new MongoDbModel(); this.startListener(); @@ -20,13 +23,17 @@ export class NotificationService { } async newMessageValidator(message: ITodo) { - const todo = await this.mongoModel.getTodoById(message._id.toString()); - if (todo) { - const due_date = new Date(todo.due_date); - if (todo.status === "pending" && due_date > new Date()) { - await this.mongoModel.updateTodoStatus(todo); - await this.sendNotification(todo); // Send notification to user + try { + const todo = await this.mongoModel.getTodoById(message._id.toString()); + if (todo) { + const due_date = new Date(todo.due_date); + if (todo.status === "pending" && due_date > this.currentDate.getDate()) { + await this.mongoModel.updateTodoStatus(todo); + await this.sendNotification(todo); // Send notification to user + } } + } catch { + console.error("todo Not found"); } } diff --git a/notification-service/src/mongodb/MongoDb.ts b/notification-service/src/mongodb/MongoDb.ts index fd1e6e1..994b0ac 100644 --- a/notification-service/src/mongodb/MongoDb.ts +++ b/notification-service/src/mongodb/MongoDb.ts @@ -24,7 +24,6 @@ export class MongoDbModel { { _id: new ObjectId(todo._id) }, { $set: { status: "completed" } } ); - console.log(`Updated status of Todo ${todo._id} to completed`); } catch (error) { console.error("Error updating Todo status:", error); } finally { diff --git a/notification-service/src/rabbitmq/RabbitMQ.ts b/notification-service/src/rabbitmq/RabbitMQ.ts index 603b244..46011f5 100644 --- a/notification-service/src/rabbitmq/RabbitMQ.ts +++ b/notification-service/src/rabbitmq/RabbitMQ.ts @@ -57,7 +57,6 @@ export class RabbitMQ { Buffer.from(message), options ); - console.log("Message sent to the queue"); } catch (error) { console.error("Error sending message to RabbitMQ:", error); throw error; @@ -68,8 +67,6 @@ export class RabbitMQ { this.channel.assertQueue(this.queueName); this.channel.consume(this.queueName, (message: ConsumeMessage | null) => { if (message) { - 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); callback(todo); diff --git a/notification-service/src/services/DateService.ts b/notification-service/src/services/DateService.ts new file mode 100644 index 0000000..5d97d1d --- /dev/null +++ b/notification-service/src/services/DateService.ts @@ -0,0 +1,20 @@ +import moment from "moment-timezone"; + +export class DateService { + private static instance: DateService; + + private constructor() {} + + public static getInstance(): DateService { + if (!DateService.instance) { + DateService.instance = new DateService(); + } + return DateService.instance; + } + + public getCurrentDate(): Date { + const israelTime = moment().tz("Asia/Jerusalem"); + const currentDate = israelTime.toDate(); + return currentDate; + } +}