adding business logic

This commit is contained in:
Kfir Dayan 2023-07-09 01:13:33 +03:00
parent 23a3a83deb
commit ac12291605
4 changed files with 83 additions and 13 deletions

View file

@ -1,4 +1,4 @@
import { ObjectId } from "mongodb"; import { ObjectId } from 'mongodb';
export interface ITodo { export interface ITodo {
_id: ObjectId; _id: ObjectId;

View file

@ -1,18 +1,51 @@
// import { Sqs } from './aws/Sqs'; import { ITodo } from "./interfaces/ITodo";
import { RabbitMQ } from './rabbitmq/RabbitMQ'; import { RabbitMQ } from "./rabbitmq/RabbitMQ";
import { EnvService } from "./services/EnvService";
import { MongoDbModel } from "./mongodb/MongoDb";
export class NotificationService { export class NotificationService {
rabbitmq: RabbitMQ; rabbitmq: RabbitMQ;
mongoModle: MongoDbModel;
constructor() { constructor() {
this.rabbitmq = new RabbitMQ(); this.rabbitmq = new RabbitMQ();
this.mongoModle = new MongoDbModel();
this.startListener(); this.startListener();
} }
async startListener() { async startListener() {
if (this.IsUserConnected()) {
await this.rabbitmq.connect(); await this.rabbitmq.connect();
this.rabbitmq.startConsumer(); 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(); const notificationService = new NotificationService();

View file

@ -8,13 +8,17 @@ export class MongoDbModel {
constructor() { constructor() {
this.envSerivce = EnvService.getInstance(); 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) => { public updateTodoStatus = async (todo: ITodo) => {
try { try {
await this.client.connect(); 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 todosCollection = db.collection("todos");
const result = await todosCollection.updateOne( const result = await todosCollection.updateOne(
{ _id: new ObjectId(todo._id) }, { _id: new ObjectId(todo._id) },
@ -27,4 +31,38 @@ export class MongoDbModel {
await this.client.close(); await this.client.close();
} }
}; };
public getTodoById = async (id: string): Promise<ITodo | void> => {
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();
}
};
} }

View file

@ -65,14 +65,13 @@ export class RabbitMQ {
} }
} }
async startConsumer() { async startConsumer(): Promise<ITodo | void> {
this.channel.assertQueue(this.queueName); this.channel.assertQueue(this.queueName);
console.log("Consuming");
this.channel.consume(this.queueName, (message: ConsumeMessage | null) => { this.channel.consume(this.queueName, (message: ConsumeMessage | null) => {
if (message) { if (message) {
const todo = JSON.parse(message.content.toString()); const todo: ITodo = JSON.parse(message.content.toString());
this.channel.ack(message); this.channel.ack(message);
console.log("Received notification:", todo); return todo;
} }
}); });
} }