#151 added try blocks to helpers.ts

parent 8e20da5c
......@@ -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})
}
}
......
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