diff --git a/todo-service/src/middleware/createTodoMiddleWare.ts b/todo-service/src/middleware/createTodoMiddleWare.ts index 460fad4..eb8aeed 100644 --- a/todo-service/src/middleware/createTodoMiddleWare.ts +++ b/todo-service/src/middleware/createTodoMiddleWare.ts @@ -1,7 +1,7 @@ import { Request, Response, NextFunction } from "express"; import { ApiError } from "../utils/ApiError"; import { DateTime } from "luxon"; -import mongoose from 'mongoose'; +import mongoose from "mongoose"; const createTodoMiddleWare = async ( req: Request, @@ -17,17 +17,22 @@ const createTodoMiddleWare = async ( ); 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" - ); - return next(error); - } - } catch { - const error = new ApiError(`due_date must be ISO `, 400, "Bad Request"); + //check if date is valid, this is valid: 2023-07-08T14:00:00.000Z + const date = DateTime.fromISO(due_date); + if (!date.isValid) { + const error = new ApiError( + `due_date must be ISO Format`, + 400, + "Bad Request" + ); + return next(error); + } + if (new Date(due_date) < new Date()) { + const error = new ApiError( + `due_date must be greater than current date`, + 400, + "Bad Request" + ); return next(error); } @@ -35,17 +40,6 @@ const createTodoMiddleWare = async ( req.body.description = ""; } - //check if date is valid, this is valid: 2023-07-08T14:00:00.000Z - const date = DateTime.fromISO(due_date); - if (!date.isValid) { - const error = new ApiError( - `due_date must be a valid date Format`, - 400, - "Bad Request" - ); - return next(error); - } - next(); }; @@ -65,8 +59,6 @@ const paramIdMiddleware = async ( return next(error); } next(); - }; - export { createTodoMiddleWare, paramIdMiddleware };