adding business logic

This commit is contained in:
Kfir Dayan 2023-07-09 01:39:17 +03:00
parent ad9a796c7a
commit e507df5556

View file

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