From c2695c7eb82921ad1d42690f6d50f4398c1ffc51 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sat, 8 Jul 2023 12:16:49 +0300 Subject: [PATCH] back to consume --- todo-service/package-lock.json | 39 ++++++++++++++++++- todo-service/package.json | 1 + .../src/controllers/todoController.ts | 1 - .../src/middleware/createTodoMiddleWare.ts | 24 +++++++----- todo-service/src/rabbitmq/RabbitMQ.ts | 2 + todo-service/src/routes/todoRouter.ts | 6 +-- 6 files changed, 59 insertions(+), 14 deletions(-) diff --git a/todo-service/package-lock.json b/todo-service/package-lock.json index 85ced60..59c4ccb 100644 --- a/todo-service/package-lock.json +++ b/todo-service/package-lock.json @@ -14,11 +14,15 @@ "aws-sdk": "^2.1413.0", "dotenv": "^16.3.1", "express": "^4.18.2", + "luxon": "^3.3.0", + "moment": "^2.29.4", + "moment-timezone": "^0.5.43", "mongoose": "^7.3.1" }, "devDependencies": { "@types/amqplib": "^0.10.1", - "@types/express": "^4.17.17" + "@types/express": "^4.17.17", + "@types/luxon": "^3.3.0" } }, "node_modules/@acuminous/bitsyntax": { @@ -129,6 +133,12 @@ "integrity": "sha512-/K3ds8TRAfBvi5vfjuz8y6+GiAYBZ0x4tXv1Av6CWBWn0IlADc+ZX9pMq7oU0fNQPnBwIZl3rmeLp6SBApbxSQ==", "dev": true }, + "node_modules/@types/luxon": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.3.0.tgz", + "integrity": "sha512-uKRI5QORDnrGFYgcdAVnHvEIvEZ8noTpP/Bg+HeUzZghwinDlIS87DEenV5r1YoOF9G4x600YsUXLWZ19rmTmg==", + "dev": true + }, "node_modules/@types/mime": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.2.tgz", @@ -733,6 +743,14 @@ "node": ">=12.0.0" } }, + "node_modules/luxon": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.3.0.tgz", + "integrity": "sha512-An0UCfG/rSiqtAIiBPO0Y9/zAnHUZxAMiCpTd5h2smgsj7GGmcenvrvww2cqNA8/4A5ZrD1gJpHN2mIHZQF+Mg==", + "engines": { + "node": ">=12" + } + }, "node_modules/media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -790,6 +808,25 @@ "node": ">= 0.6" } }, + "node_modules/moment": { + "version": "2.29.4", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz", + "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==", + "engines": { + "node": "*" + } + }, + "node_modules/moment-timezone": { + "version": "0.5.43", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.43.tgz", + "integrity": "sha512-72j3aNyuIsDxdF1i7CEgV2FfxM1r6aaqJyLB2vwb33mXYyoyLly+F1zbWqhA3/bVIoJ4szlUoMbUnVdid32NUQ==", + "dependencies": { + "moment": "^2.29.4" + }, + "engines": { + "node": "*" + } + }, "node_modules/mongodb": { "version": "5.6.0", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.6.0.tgz", diff --git a/todo-service/package.json b/todo-service/package.json index d7e954d..22d2802 100644 --- a/todo-service/package.json +++ b/todo-service/package.json @@ -15,6 +15,7 @@ "aws-sdk": "^2.1413.0", "dotenv": "^16.3.1", "express": "^4.18.2", + "moment": "^2.29.4", "mongoose": "^7.3.1" }, "devDependencies": { diff --git a/todo-service/src/controllers/todoController.ts b/todo-service/src/controllers/todoController.ts index 667934d..bec1e0e 100644 --- a/todo-service/src/controllers/todoController.ts +++ b/todo-service/src/controllers/todoController.ts @@ -39,7 +39,6 @@ export class TodoController { if (todo instanceof ApiError) { return next(todo); } - const id = todo._id; const delayTimeForQueue = Math.floor((new Date(todo.due_date).getTime() - new Date().getTime()) / 1000); this.queue.create(todo, delayTimeForQueue); diff --git a/todo-service/src/middleware/createTodoMiddleWare.ts b/todo-service/src/middleware/createTodoMiddleWare.ts index 2fddd87..30a4347 100644 --- a/todo-service/src/middleware/createTodoMiddleWare.ts +++ b/todo-service/src/middleware/createTodoMiddleWare.ts @@ -1,23 +1,29 @@ -import { Request, Response, NextFunction } from 'express'; import { ApiError } from '../utils/ApiError'; +import { Request, Response, NextFunction } from 'express'; +import moment from 'moment'; -const createTodoMiddleWare = async (req: Request, res: Response, next: NextFunction) => { +const createTodoMiddleware = async (req: Request, res: Response, next: NextFunction) => { const { title, description, due_date } = req.body; + if (!title || !due_date) { const error = new ApiError(`${!title ? 'title' : 'due_date'} is required`, 400, 'Bad Request'); return next(error); } + try { - if(new Date(due_date) < new Date()) { - const error = new ApiError(`due_date must be greater than current date`, 400, 'Bad Request'); + const inputDate = new Date(due_date); + if (isNaN(inputDate.getTime()) || inputDate < new Date()) { + const error = new ApiError('due_date must be greater than current date', 400, 'Bad Request'); return next(error); } } catch { - const error = new ApiError(`due_date must be a valid date`, 400, 'Bad Request'); - return next(error); + const parsedDate = moment(due_date, 'YYYY-MM-DD HH:mm:ss'); + if (!parsedDate.isValid() || parsedDate < moment()) { + const error = new ApiError('due_date must be greater than current date', 400, 'Bad Request'); + return next(error); + } } - if (!description) { req.body.description = ''; } @@ -26,5 +32,5 @@ const createTodoMiddleWare = async (req: Request, res: Response, next: NextFunct } export { - createTodoMiddleWare -} \ No newline at end of file + createTodoMiddleware +} diff --git a/todo-service/src/rabbitmq/RabbitMQ.ts b/todo-service/src/rabbitmq/RabbitMQ.ts index 9d31cb9..01dba89 100644 --- a/todo-service/src/rabbitmq/RabbitMQ.ts +++ b/todo-service/src/rabbitmq/RabbitMQ.ts @@ -36,6 +36,8 @@ export class RabbitMQ { } async create(payload: ITodo, delayTimeForQueue: number) { + console.log('Creating message to send to the queue') + console.log(`with delayTimeForQueue ${delayTimeForQueue}`) const message = JSON.stringify({ payload, delayTimeForQueue: delayTimeForQueue }); const options = { persistent: true, delayTimeForQueue: delayTimeForQueue.toString() }; diff --git a/todo-service/src/routes/todoRouter.ts b/todo-service/src/routes/todoRouter.ts index d671710..2b5567e 100644 --- a/todo-service/src/routes/todoRouter.ts +++ b/todo-service/src/routes/todoRouter.ts @@ -1,6 +1,6 @@ import { Router } from 'express'; import { TodoController } from '../controllers/todoController'; -import { createTodoMiddleWare } from '../middleware/createTodoMiddleWare'; +import { createTodoMiddleware } from '../middleware/createTodoMiddleWare'; class TodoRouter { router: Router; @@ -15,8 +15,8 @@ class TodoRouter { private setRoutes() { this.router.get('/', this.todoController.getAll); this.router.get('/:id', this.todoController.getOne); - this.router.post('/', createTodoMiddleWare, this.todoController.createOne); - this.router.put('/:id', createTodoMiddleWare, this.todoController.updateOne); + this.router.post('/', createTodoMiddleware, this.todoController.createOne); + this.router.put('/:id', createTodoMiddleware, this.todoController.updateOne); this.router.delete('/:id', this.todoController.deleteOne); this.router.delete('/', this.todoController.removeAll)