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 { 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 };