diff --git a/README.md b/README.md index 9235bd3..6736ade 100644 --- a/README.md +++ b/README.md @@ -12,24 +12,6 @@ This README.md file provides instructions for setting up and running various com docker-compose up -d ``` -## SQS/Terraform - -Before proceeding, ensure that you have Terraform installed and have configured AWS CLI with the keys of an authorized user. - -1. Navigate to the /infra/terraform directory. - -2. Run the following command to initialize Terraform: - -```terraform -terraform init -``` - -3. Run the following command to apply the Terraform configuration: - -```terraform -terraform apply -``` - ## NotificationService Before running the NotificationService, ensure that you have Node.js, TypeScript (tsc), and npm installed. diff --git a/infra/docker/docker-compose.yaml b/infra/docker/docker-compose.yaml index be78535..c99e742 100644 --- a/infra/docker/docker-compose.yaml +++ b/infra/docker/docker-compose.yaml @@ -9,11 +9,15 @@ services: - mongodb_vol:/data/db - ./mongo-init-scripts/init.js:/docker-entrypoint-initdb.d/mongo-init.js platform: linux/arm64/v8 + environment: + - MONGO_INITDB_DATABASE=${MONGO_INITDB_DATABASE} + - MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME} + - MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD} expose: - 27017 rabbitmq: - image: kfda89/todorabbit:latest + image: todorabbit:latest restart: always ports: - 5672:5672 @@ -21,6 +25,9 @@ services: volumes: - rabbitmq_volume:/var/lib/rabbitmq - ./rabbitmq-init-scripts/init.sh:/docker-entrypoint-initdb.d/init.sh + environment: + - RABBITMQ_DEFAULT_USER=${RABBITMQ_DEFAULT_USER} + - RABBITMQ_DEFAULT_PASS=${RABBITMQ_DEFAULT_PASS} expose: - 5672 - 15672 diff --git a/infra/terraform/main.tf b/infra/terraform/main.tf deleted file mode 100644 index a652c60..0000000 --- a/infra/terraform/main.tf +++ /dev/null @@ -1,12 +0,0 @@ -provider "aws" { - region = "eu-west-1" -} - -resource "aws_sqs_queue" "queue" { - name = "todo_queue" - delay_seconds = 0 - max_message_size = 262144 - message_retention_seconds = 345600 - visibility_timeout_seconds = 30 - receive_wait_time_seconds = 0 -} diff --git a/request.http b/request.http index 6c12383..0127d3b 100644 --- a/request.http +++ b/request.http @@ -13,7 +13,7 @@ Content-Type: application/json } ### update request -PUT http://localhost:3000/todo/64a9e3987a2dba152b1e44b0 +PUT http://localhost:3000/todo/64aa7b55f73a81f5e374663b Content-Type: application/json { diff --git a/todo-service/package.json b/todo-service/package.json index f3d8678..40e9b4d 100644 --- a/todo-service/package.json +++ b/todo-service/package.json @@ -4,7 +4,7 @@ "description": "", "main": "main.js", "scripts": { - "dev": "node dist/main.js" + "dev": "nodemon dist/main.js" }, "keywords": [], "author": "", diff --git a/todo-service/src/middleware/createTodoMiddleWare.ts b/todo-service/src/middleware/createTodoMiddleWare.ts index eb8aeed..6e6a852 100644 --- a/todo-service/src/middleware/createTodoMiddleWare.ts +++ b/todo-service/src/middleware/createTodoMiddleWare.ts @@ -2,6 +2,9 @@ import { Request, Response, NextFunction } from "express"; import { ApiError } from "../utils/ApiError"; import { DateTime } from "luxon"; import mongoose from "mongoose"; +import { DateService } from "../services/DateService"; + +const currentDate = DateService.getInstance().getCurrentDate(); const createTodoMiddleWare = async ( req: Request, @@ -27,7 +30,7 @@ const createTodoMiddleWare = async ( ); return next(error); } - if (new Date(due_date) < new Date()) { + if (new Date(due_date) < currentDate) { const error = new ApiError( `due_date must be greater than current date`, 400, @@ -48,7 +51,6 @@ const paramIdMiddleware = async ( res: Response, next: NextFunction ) => { - // check if it's a valid mongo id const { id } = req.params; if (!mongoose.Types.ObjectId.isValid(id)) { const error = new ApiError( diff --git a/todo-service/src/rabbitmq/RabbitMQ.ts b/todo-service/src/rabbitmq/RabbitMQ.ts index c28a3a9..59e16d1 100644 --- a/todo-service/src/rabbitmq/RabbitMQ.ts +++ b/todo-service/src/rabbitmq/RabbitMQ.ts @@ -1,6 +1,7 @@ import amqp, { Options } from "amqplib"; import { ITodo } from "../schemas/todoSchema"; import { EnvService } from "../services/EnvService"; +import { DateService } from "../services/DateService"; export class RabbitMQ { connection: amqp.Connection; @@ -8,8 +9,10 @@ export class RabbitMQ { queue: string; exchange: string; envService: EnvService; + currentDate: DateService; constructor() { + this.currentDate = DateService.getInstance(); this.envService = EnvService.getInstance(); this.queue = this.envService.getEnvVariable("RABBITMQ_QUEUE_NAME"); this.exchange = "delayed_exchange"; @@ -44,15 +47,12 @@ export class RabbitMQ { await this.channel.bindQueue(this.queue, this.exchange, this.queue); } catch (error) { - console.error("Error connecting to RabbitMQ:", error); throw error; } } async create(payload: ITodo) { const delayTimeForQueue = this.calculateDelayTimeForQueue(payload); - console.log("The Queue will be delayed for: ", delayTimeForQueue, " ms"); - const message = JSON.stringify({ payload }); const options: Options.Publish = { headers: { "x-delay": delayTimeForQueue }, @@ -64,15 +64,13 @@ export class RabbitMQ { Buffer.from(message), options ); - console.log(`Queue name is: ${this.queue}`); } catch (error) { - console.error("Error sending message to RabbitMQ:", error); throw error; } } calculateDelayTimeForQueue(payload: ITodo) { - const delayTime = payload.due_date.getTime() - Date.now(); + const delayTime = payload.due_date.getTime() - this.currentDate.getCurrentDate().getTime(); return delayTime; } } diff --git a/todo-service/src/routes/todoRouter.ts b/todo-service/src/routes/todoRouter.ts index 4ea15e5..602ec41 100644 --- a/todo-service/src/routes/todoRouter.ts +++ b/todo-service/src/routes/todoRouter.ts @@ -4,12 +4,15 @@ import { createTodoMiddleWare, paramIdMiddleware, } from "../middleware/createTodoMiddleWare"; +import { DateService } from "../services/DateService"; class TodoRouter { router: Router; todoController: TodoController; + currentDate: DateService; constructor() { + this.currentDate = DateService.getInstance(); this.router = Router(); this.todoController = new TodoController(); this.setRoutes(); diff --git a/todo-service/src/services/DateService.ts b/todo-service/src/services/DateService.ts new file mode 100644 index 0000000..5d97d1d --- /dev/null +++ b/todo-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; + } +}