Merge branch 'task-151-fix/try_catch_blocks_backend' into 'development'

Task 151 fix/try catch blocks backend

See merge request !130
parents 4903e1d5 729a8bad
......@@ -12,6 +12,7 @@ const dataSource = myDataSource;
/** Check if user with given token exists , return user */
export const auth = async(req: Request,res: Response, next:NextFunction):Promise<void | express.Response<Response>>=>{
try{
const token = req.get('Authorization');
if(!token) return res.status(401).send({Message:'token not exists'})
const user = await dataSource
......@@ -23,10 +24,14 @@ export const auth = async(req: Request,res: Response, next:NextFunction):Promise
if (!user) return res.status(404).send({Message:'user not found'})
req.body={...req.body,user:user}
next()
}catch(e) {
return res.status(502).send({message:(e as Error).message})
}
};
/**Check if user with the given token is executor or author of task with the given Id(taskId) */
export const authAuthorOrExecutorOfTask = async(req: Request,res: Response, next:NextFunction):Promise<void | express.Response<Response>>=>{
try{
const token = req.get('Authorization');
const {user} = req.body
let taskId = null
......@@ -65,11 +70,15 @@ export const authAuthorOrExecutorOfTask = async(req: Request,res: Response, next
}
req.body={...req.body, task:task}
next()
}catch(e) {
return res.status(502).send({message:(e as Error).message})
}
};
/**Check if user with the given token is executor or author of task with the given dateTimeTaskId */
export const authAuthorOrExecutorOfDateTimeTask = async(req: Request,res: Response, next:NextFunction):Promise<void | express.Response<Response>>=>{
try{
const token = req.get('Authorization');
let dateTimeTaskId = null
req.body={...req.body,executorStatus:false}
......@@ -105,11 +114,15 @@ export const authAuthorOrExecutorOfDateTimeTask = async(req: Request,res: Respon
}
if (req.body.authorStatus ===false && req.body.executorStatus===false) return res.status(403).send({Message:'user is not uathorized'})
next()
}catch(e) {
return res.status(502).send({message:(e as Error).message})
}
}
/**check if user is admin of the project, receives userId and projectId*/
export const authAdminProject = async(req: Request,res: Response, next:NextFunction):Promise<void | express.Response<Response>>=>{
try{
const token = req.get('Authorization');
const {projectId} = req.body;
const adminOfProject = await dataSource
......@@ -127,6 +140,9 @@ export const authAdminProject = async(req: Request,res: Response, next:NextFunct
}
req.body ={...req.body,adminStatus:true}
next()
}catch(e) {
return res.status(502).send({message:(e as Error).message})
}
}
......
......@@ -9,6 +9,7 @@ const dataSource = myDataSource;
/** make copy of task in calendar view */
router.post("/make-copy",auth,authAuthorOrExecutorOfTask, async(req:Request, res:Response):Promise<Response>=>{
try{
const {taskId, dateTimeDue, dateTimeStart, authorStatus, executorStatus, task} = req.body;
if(!task) return res.status(401).send({message:'task with possible user involved is not found'})
const newDateTimeTask = new DateTimeTask();
......@@ -17,10 +18,14 @@ router.post("/make-copy",auth,authAuthorOrExecutorOfTask, async(req:Request, res
newDateTimeTask.task = taskId;
await newDateTimeTask.save();
return res.send({task, message:"copyTask created!"})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
} )
/** change date time of copy of task in calendar view */
router.put("/change-copy/:dateTimeTaskId",auth, authAuthorOrExecutorOfTask, async(req:Request, res: Response):Promise<Response>=>{
try{
const {dateTimeTaskId} = req.params
const {executorStatus,authorStatus, task, dateTimeStart, dateTimeDue, description, title, priority, project} = req.body
if(authorStatus){
......@@ -76,10 +81,14 @@ router.put("/change-copy/:dateTimeTaskId",auth, authAuthorOrExecutorOfTask, asyn
task.project = project
await task.save()
return res.send({task})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/**delete copyTask by dateTimeTaskId */
router.delete('/:dateTimeTaskId',authAuthorOrExecutorOfDateTimeTask, async(req:Request, res:Response):Promise<Response|void>=>{
try{
const {authorStatus, executorStatus, task} = req.body
const {dateTimeTaskId} = req.params
......@@ -117,6 +126,9 @@ router.delete('/:dateTimeTaskId',authAuthorOrExecutorOfDateTimeTask, async(req:R
return res.send({message:"not uathorized to delete task"})
}
}
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
}
)
......
......@@ -13,6 +13,7 @@ const dataSource = myDataSource;
/**Make requiest to init recovery process */
router.post ('/', async (req:Request, res:Response):Promise<void |Response>=>{
try{
const {email} = req.body
console.log("emeil", email)
const user = await dataSource
......@@ -25,7 +26,6 @@ router.post ('/', async (req:Request, res:Response):Promise<void |Response>=>{
console.log('user ', user)
if (!user) return res.status(404).send({message:'user not found'})
const token = nanoid();
try{
const passwordRecovery = new PasswordRecovery()
passwordRecovery.user= user;
passwordRecovery.token=token;
......@@ -50,6 +50,7 @@ router.post ('/', async (req:Request, res:Response):Promise<void |Response>=>{
/**reset token in password recovery */
router.get('/', async(req: Request, res: Response):Promise<Response|void>=>{
try{
const token = req.query.token;
if(!token) return res.status(401).send({Message:'token not exists'})
const passwordRecovery = await dataSource
......@@ -62,16 +63,16 @@ router.get('/', async(req: Request, res: Response):Promise<Response|void>=>{
if(!passwordRecovery || !passwordRecovery.enabled) return res.status(404).send({message:"Token is not valid"})
res.send(passwordRecovery)
passwordRecovery.enabled=false;
try{
await passwordRecovery.save();
} catch(e){
console.log(e)
}
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/**change password */
router.patch('/:id/change-password', async (req: Request, res: Response):Promise<Response|void>=>{
try{
const user = await dataSource
.getRepository(User)
.findOneBy({id:req.params.id})
......@@ -79,7 +80,6 @@ router.patch('/:id/change-password', async (req: Request, res: Response):Promise
const salt = await bcrypt.genSalt(SALT_WORK_FACTOR);
const newPassword:string = await bcrypt.hash(req.body.password, salt);
user.password = newPassword
try{
await user.save()
res.send({message:"Password saved"})
} catch (e){
......
......@@ -9,12 +9,17 @@ const dataSource = myDataSource;
router.get('/',async (req:Request, res:Response): Promise<Response>=> {
try{
const projects:Project[] = await dataSource.manager.find(Project)
return res.send({projects})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/**get projects were user is member, by user token */
router.get('/my',auth, async (req:Request, res:Response): Promise<Response>=> {
try{
const user = req.body.user
const rawProjects = await dataSource
.createQueryBuilder()
......@@ -42,6 +47,9 @@ router.get('/my',auth, async (req:Request, res:Response): Promise<Response>=> {
.where('project.id IN(:...projectIds)', {projectIds})
.getMany()
return res.send({projects})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
......@@ -49,10 +57,9 @@ router.get('/my',auth, async (req:Request, res:Response): Promise<Response>=> {
/**create new project */
router.post('/',auth, async (req:Request, res:Response): Promise<Response> => {
try{
if (!req.body) return res.status(400).send({Message:'problem in incoming req.body'})
const {user, title,color}= req.body;
try{
const projectCheck = await dataSource
.getRepository(Project)
.find({
......@@ -61,13 +68,6 @@ router.post('/',auth, async (req:Request, res:Response): Promise<Response> => {
}
})
if(projectCheck.length>0) {return res.status(400).send({message:"suchc Project already exists"})}
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
try{
const member:Member = new Member;
member.user= user;
member.roleProject= MemberRole.ADMIN;
......@@ -85,7 +85,7 @@ router.post('/',auth, async (req:Request, res:Response): Promise<Response> => {
})
/**get project with all FK & tasks with all FK, by project ID */
router.get("/:id",async (req:Request, res:Response): Promise<Response> => {
try{
const project = await dataSource
.createQueryBuilder()
.select("project")
......@@ -110,10 +110,14 @@ router.get("/:id",async (req:Request, res:Response): Promise<Response> => {
}
)
return res.send({project, tasks})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/** Delete project by project ID*/
router.delete('/',authAdminProject,async (req: Request, res: Response):Promise<Response>=>{
try{
const {projectId} = req.body;
await myDataSource
.createQueryBuilder()
......@@ -122,12 +126,16 @@ router.delete('/',authAdminProject,async (req: Request, res: Response):Promise<R
.where("id = :id", { id: projectId })
.execute()
return res.send({message: 'Project deleted successfully'})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/** Get projects were user is admin, by user ID*/
router.get('/user/:userId', async (req : Request, res : Response): Promise<Response>=>{
try{
const userId:string = req.params.userId
const userProjects = await dataSource
.createQueryBuilder()
......@@ -136,14 +144,17 @@ router.get('/user/:userId', async (req : Request, res : Response): Promise<Respo
.where("project.admin = :id", { id: userId })
.getMany();
return res.send({userProjects})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/** Add user to specific project */
router.post('/add-user/', authAdminProject, async (req: Request, res: Response):Promise<Response>=>{
try{
const {userId, projectId, roleProject} = req.body;
const newMember:Member = new Member();
try{
newMember.user= userId;
newMember.project=projectId
newMember.roleProject=roleProject
......@@ -159,10 +170,10 @@ router.post('/add-user/', authAdminProject, async (req: Request, res: Response):
/** Remove user from specific project by userId */
router.delete('/remove-user/:userId', authAdminProject,async (req: Request, res: Response):Promise<Response>=> {
try{
const {projectId} = req.body;
const {userId }=req.params;
try{
const projectMember = await dataSource
.getRepository(Member)
.findOne({
......@@ -192,9 +203,9 @@ router.delete('/remove-user/:userId', authAdminProject,async (req: Request, res:
/**change rights of user inside of project by admin, recieve userId, new roleProject */
router.put('/change-project-role/:userId',authAdminProject, async (req: Request, res: Response):Promise<Response|void> =>{
try{
const {userId}= req.params
const {projectId, newRoleProject} =req.body
const member = await dataSource
.createQueryBuilder()
.select("member")
......@@ -205,14 +216,12 @@ router.put('/change-project-role/:userId',authAdminProject, async (req: Request
.andWhere("project.id=:projectId",{projectId})
.getOne()
if(!member) return res.status(404).send({Message:'user and project are not relevant'})
try{
member.roleProject = newRoleProject
await member.save()
return res.send({message:"User's new role ", newRoleProject})
} catch(e){
return res.send({message:"failed to change role"})
}
return res.send({message:"User's new role ", newRoleProject})
})
......
......@@ -14,6 +14,7 @@ const dataSource = myDataSource;
/**get all tasks */
router.get('/', async(req:Request, res:Response):Promise<Response> => {
try{
const tasks = await dataSource
.getRepository(Task)
.find({
......@@ -22,14 +23,16 @@ router.get('/', async(req:Request, res:Response):Promise<Response> => {
author:true,
}})
return res.send({tasks})
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
})
/**create new task for table MyTask*/
router.post('/', auth, async(req:Request, res:Response):Promise<Response>=>{
try{
const {user,title,description,project,executor,dateTimeStart,dateTimeDue, dateTimeDeadLine,priority, calendar} = req.body;
console.log('dateTimeStart \n', dateTimeStart, "dateTimeDue\n", dateTimeDue, 'dateTimeDeadLine\n ', dateTimeDeadLine)
console.log(req.body)
let dateTimeDueFinal = dateTimeDue
let dateTimeStartFinal = dateTimeStart
let dateTimeDeadlineFinal= dateTimeDeadLine
......@@ -105,11 +108,16 @@ router.post('/', auth, async(req:Request, res:Response):Promise<Response>=>{
await newDateTimeTask.save()
}
return res.send({newTask});
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
})
//** create new task for table UsersTasks*/
router.post('/users', auth, async(req:Request, res:Response):Promise<Response>=>{
try{
const {user,title,description,project,executor,dateTimeStart,dateTimeDue, dateTimeDeadLine,priority} = req.body;
const newTask = new Task();
newTask.title = title;
......@@ -174,11 +182,16 @@ router.post('/users', auth, async(req:Request, res:Response):Promise<Response>=>
await newDateTimeTask.save()
}
return res.send({newTask});
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
})
/**check tasks of specific user by userID */
router.get('/user/:userId', async (req: Request, res: Response):Promise<Response>=>{
try{
const userId = req.params.userId;
const tasks = await dataSource
.getRepository(Task)
......@@ -194,6 +207,9 @@ router.get('/user/:userId', async (req: Request, res: Response):Promise<Response
},
})
return res.send({tasks})
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
})
......@@ -258,6 +274,7 @@ router.get('/my',auth, async (req: Request, res: Response):Promise<Response>=>{
/**return tasks & users of projects in which current user is involved, search by token*/
router.get('/related', auth,async (req: Request, res: Response):Promise<Response>=>{
try{
const user = req.body.user
const tasks = await dataSource
.createQueryBuilder()
......@@ -296,11 +313,15 @@ router.get('/related', auth,async (req: Request, res: Response):Promise<Response
})
}
return res.send({tasks, users})
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
})
/**delete of task by task id */
router.delete('/:taskId',async (req: Request, res: Response):Promise<Response>=>{
try{
const taskId = req.params.taskId;
await myDataSource
.createQueryBuilder()
......@@ -309,21 +330,22 @@ router.delete('/:taskId',async (req: Request, res: Response):Promise<Response>=>
.where("id = :id", { id: taskId })
.execute()
return res.send({message: 'Task deleted successfully'})
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
})
/**change of task by task id */
router.put('/:taskId',auth,authAuthorOrExecutorOfTask,async(req:Request, res:Response)=> {
router.put('/:taskId',auth,authAuthorOrExecutorOfTask,async(req:Request, res:Response):Promise<Response>=> {
try{
const {taskId} = req.params
const {authorStatus,executorStatus,title,description,note, archive,project,dateTimeTaskId,dateTimeStart,
dateTimeDue,executor,accomplish,dateTimeDeadLine, dateTimeFactDeadLine,priority} = req.body;
const task = await taskFinderById(taskId)
if (!task) return res.status(404).send({Message:'task not found'})
console.log(req.body)
console.log ('dateTimeDeadLine ', dateTimeDeadLine,'authorStatus '
,authorStatus,'executorStatus ',executorStatus)
let dateTimeTask = null;
if (dateTimeTaskId) {
const dateTimeTaskData = await dataSource
......@@ -357,13 +379,18 @@ router.put('/:taskId',auth,authAuthorOrExecutorOfTask,async(req:Request, res:Res
}
task.accomplish= accomplish
await task.save()
res.send({message:'update task successfully'})
return res.send({message:'update task successfully'})
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
})
/** search all tasks on given projects: project Id array[] */
router.post('/project',async (req: Request, res: Response):Promise<Response>=>{
let projectArray :string[]= req.body;
try{
if (projectArray.length===0) {
const rawTasks = await
dataSource
......@@ -392,6 +419,9 @@ router.post('/project',async (req: Request, res: Response):Promise<Response>=>{
return res.send({tasks})
} catch(e){
return res.status(502).send({message:(e as Error).message})
}
})
......
......@@ -29,15 +29,20 @@ const upload = multer({ storage })
//** return all users of DB */
router.get('/', async (req : Request, res : Response):Promise<object> => {
try{
const users = await dataSource
.getRepository(User)
.createQueryBuilder("user")
.getMany()
return res.send({users})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
//** return displayName of user, requested by userId */
router.get('/display-name/:userId', auth, async (req : Request, res : Response):Promise<object> => {
try{
const {userId} = req.params
const displayName = await dataSource
.createQueryBuilder()
......@@ -47,11 +52,15 @@ return res.send({users})
.getOne()
if (!displayName) return res.status(404).send({message:'displayName not found'})
return res.send(displayName)
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
//** return all users of DB */
router.get('/all-fields/', async (req : Request, res : Response):Promise<object> => {
try{
const users = await dataSource
.getRepository(User)
.find({
......@@ -63,11 +72,15 @@ router.get('/all-fields/', async (req : Request, res : Response):Promise<object>
}
})
return res.send({users})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
//** return displayName of user, requested by userId */
router.get('/display-name/:userId',auth, async (req : Request, res : Response):Promise<object> => {
try{
const {userId} =req.params
const displayName = await dataSource
.createQueryBuilder()
......@@ -77,11 +90,15 @@ router.get('/all-fields/', async (req : Request, res : Response):Promise<object>
.getOne()
if (!displayName) return res.status(404).send({message:'displayName not found'})
return res.send({displayName})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/**create new user*/
router.post('/', upload.single("avatar"), async (req : Request, res : Response):Promise<object> => {
try{
const {name,surname,password,email, role} = req.body;
const displayName = surname+' '+name[0]+'.'
const user = new User();
......@@ -118,10 +135,14 @@ router.post('/', upload.single("avatar"), async (req : Request, res : Response):
email: user.email
})
return res.send({userToFront})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/** log in*/
router.post('/sessions/', async (req : Request, res : Response):Promise<object> => {
try{
const {email, password} = req.body;
const user = await dataSource
.createQueryBuilder()
......@@ -143,10 +164,14 @@ router.post('/sessions/', async (req : Request, res : Response):Promise<object>
message: "message: 'Correct user & password",
user: userToFront
})
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/** log out*/
router.delete('/sessions', async(req: Request, res: Response):Promise<void | object> => {
try{
const token = req.get('Authorization');
const successMsg = {message:'success'};
if(!token) return res.send(successMsg)
......@@ -157,7 +182,9 @@ router.delete('/sessions', async(req: Request, res: Response):Promise<void | obj
user.token = nanoid();
await user.save();
return res.send(successMsg)
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment