adding business logic

This commit is contained in:
Kfir Dayan 2023-07-09 01:26:40 +03:00
parent ac12291605
commit b26eef9b2c
3 changed files with 11 additions and 7 deletions

View file

@ -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

View file

@ -65,13 +65,15 @@ export class RabbitMQ {
}
}
async startConsumer(): Promise<ITodo | void> {
async startConsumer(callback: Function): Promise<ITodo | void> {
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);
}
});
}

View file

@ -74,6 +74,6 @@ export class RabbitMQ {
calculateDelayTimeForQueue(payload: ITodo) {
const delayTime = payload.due_date.getTime() - Date.now();
return delayTime;
return 0;
}
}