back to consume

This commit is contained in:
Kfir Dayan 2023-07-08 12:16:49 +03:00
parent 8bc7e1d8a0
commit c2695c7eb8
6 changed files with 59 additions and 14 deletions

View file

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

View file

@ -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": {

View file

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

View file

@ -1,22 +1,28 @@
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');
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
createTodoMiddleware
}

View file

@ -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() };

View file

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