adding business logic
This commit is contained in:
parent
ac12291605
commit
b26eef9b2c
3 changed files with 11 additions and 7 deletions
|
@ -16,17 +16,19 @@ export class NotificationService {
|
||||||
async startListener() {
|
async startListener() {
|
||||||
if (this.IsUserConnected()) {
|
if (this.IsUserConnected()) {
|
||||||
await this.rabbitmq.connect();
|
await this.rabbitmq.connect();
|
||||||
this.rabbitmq.startConsumer();
|
this.rabbitmq.startConsumer(this.newMessageValidator.bind(this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async newMessageValidator(message: ITodo) {
|
async newMessageValidator(message: ITodo) {
|
||||||
|
console.log("Validating message:", message)
|
||||||
const todo = await this.mongoModle.getTodoById(message._id.toString());
|
const todo = await this.mongoModle.getTodoById(message._id.toString());
|
||||||
|
console.log("Todo:", todo)
|
||||||
if (todo) {
|
if (todo) {
|
||||||
|
const due_date = new Date(todo.due_date);
|
||||||
if (
|
if (
|
||||||
todo.status === "pending" &&
|
todo.status === "pending" &&
|
||||||
todo.due_date < new Date() &&
|
due_date > new Date()
|
||||||
todo.due_date === message.due_date
|
|
||||||
) {
|
) {
|
||||||
await this.mongoModle.updateTodoStatus(todo);
|
await this.mongoModle.updateTodoStatus(todo);
|
||||||
await this.sendNotification(todo); // Send notification to user
|
await this.sendNotification(todo); // Send notification to user
|
||||||
|
|
|
@ -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.assertQueue(this.queueName);
|
||||||
this.channel.consume(this.queueName, (message: ConsumeMessage | null) => {
|
this.channel.consume(this.queueName, (message: ConsumeMessage | null) => {
|
||||||
if (message) {
|
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);
|
this.channel.ack(message);
|
||||||
return todo;
|
callback(todo);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -74,6 +74,6 @@ export class RabbitMQ {
|
||||||
|
|
||||||
calculateDelayTimeForQueue(payload: ITodo) {
|
calculateDelayTimeForQueue(payload: ITodo) {
|
||||||
const delayTime = payload.due_date.getTime() - Date.now();
|
const delayTime = payload.due_date.getTime() - Date.now();
|
||||||
return delayTime;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue