adding business logic
This commit is contained in:
parent
ad9a796c7a
commit
e507df5556
1 changed files with 17 additions and 25 deletions
|
@ -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,7 +17,16 @@ const createTodoMiddleWare = async (
|
||||||
);
|
);
|
||||||
return next(error);
|
return next(error);
|
||||||
}
|
}
|
||||||
try {
|
//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()) {
|
if (new Date(due_date) < new Date()) {
|
||||||
const error = new ApiError(
|
const error = new ApiError(
|
||||||
`due_date must be greater than current date`,
|
`due_date must be greater than current date`,
|
||||||
|
@ -26,26 +35,11 @@ const createTodoMiddleWare = async (
|
||||||
);
|
);
|
||||||
return next(error);
|
return next(error);
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
const error = new ApiError(`due_date must be ISO `, 400, "Bad Request");
|
|
||||||
return next(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!description) {
|
if (!description) {
|
||||||
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 };
|
||||||
|
|
Loading…
Reference in a new issue