fixing small bug

This commit is contained in:
Kfir Dayan 2023-07-13 11:19:22 +03:00
parent 6e34384129
commit 739f3b9913
3 changed files with 16 additions and 6 deletions

View file

@ -6,7 +6,7 @@ import { DateService } from "./services/DateService";
export class NotificationService { export class NotificationService {
rabbitmq: RabbitMQ; rabbitmq: RabbitMQ;
mongoModel: MongoDbModel; mongoModel: MongoDbModel;
currentDate: any; currentDate: DateService;
constructor() { constructor() {
this.currentDate = DateService.getInstance(); this.currentDate = DateService.getInstance();
@ -20,6 +20,12 @@ export class NotificationService {
if (this.IsUserConnected()) { if (this.IsUserConnected()) {
await this.rabbitmq.connect(); await this.rabbitmq.connect();
this.rabbitmq.startConsumer(this.newMessageValidator.bind(this)); this.rabbitmq.startConsumer(this.newMessageValidator.bind(this));
} else {
// Retry in 5 seconds
setTimeout(() => {
console.log("Waiting for user to connect...")
this.startListener();
}, 5000);
} }
} }
@ -28,14 +34,16 @@ export class NotificationService {
const todo: ITodo | void = await this.mongoModel.getTodoById(message._id.toString()); const todo: ITodo | void = await this.mongoModel.getTodoById(message._id.toString());
if (todo) { if (todo) {
const due_date = new Date(todo.due_date); const due_date = new Date(todo.due_date);
if (todo.status === "pending" && due_date > this.currentDate.getDate()) { if (todo.status === "pending" && due_date > this.currentDate.getCurrentDate()) {
await this.mongoModel.updateTodoStatus(todo); await this.mongoModel.updateTodoStatus(todo);
await this.sendNotification(todo); // Send notification to user await this.sendNotification(todo); // Send notification to user
} }
} } else {
} catch {
console.error("todo Not found"); console.error("todo Not found");
} }
} catch (error) {
console.error(error.message);
}
} }
private IsUserConnected() { private IsUserConnected() {
@ -46,3 +54,5 @@ export class NotificationService {
console.log("Sending notification for Todo:", todo._id); console.log("Sending notification for Todo:", todo._id);
} }
} }
new NotificationService();

View file

@ -9,7 +9,7 @@ Content-Type: application/json
{ {
"title": "-2!!!!NEW!!!!", "title": "-2!!!!NEW!!!!",
"description": "Go to the gym at 8pm", "description": "Go to the gym at 8pm",
"due_date": "2023-07-09T19:00:00.891Z" "due_date": "2023-07-13T19:00:00.891Z"
} }
### update request ### update request

View file

@ -71,6 +71,6 @@ export class RabbitMQ {
calculateDelayTimeForQueue(payload: ITodo) { calculateDelayTimeForQueue(payload: ITodo) {
const delayTime = payload.due_date.getTime() - this.currentDate.getCurrentDate().getTime(); const delayTime = payload.due_date.getTime() - this.currentDate.getCurrentDate().getTime();
return delayTime; return 0;
} }
} }