#151 added try blocks to helpers.ts

parent 8e20da5c
...@@ -12,6 +12,7 @@ const dataSource = myDataSource; ...@@ -12,6 +12,7 @@ const dataSource = myDataSource;
/** Check if user with given token exists , return user */ /** Check if user with given token exists , return user */
export const auth = async(req: Request,res: Response, next:NextFunction):Promise<void | express.Response<Response>>=>{ export const auth = async(req: Request,res: Response, next:NextFunction):Promise<void | express.Response<Response>>=>{
try{
const token = req.get('Authorization'); const token = req.get('Authorization');
if(!token) return res.status(401).send({Message:'token not exists'}) if(!token) return res.status(401).send({Message:'token not exists'})
const user = await dataSource const user = await dataSource
...@@ -23,10 +24,14 @@ export const auth = async(req: Request,res: Response, next:NextFunction):Promise ...@@ -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'}) if (!user) return res.status(404).send({Message:'user not found'})
req.body={...req.body,user:user} req.body={...req.body,user:user}
next() 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) */ /**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>>=>{ export const authAuthorOrExecutorOfTask = async(req: Request,res: Response, next:NextFunction):Promise<void | express.Response<Response>>=>{
try{
const token = req.get('Authorization'); const token = req.get('Authorization');
const {user} = req.body const {user} = req.body
let taskId = null let taskId = null
...@@ -65,11 +70,15 @@ export const authAuthorOrExecutorOfTask = async(req: Request,res: Response, next ...@@ -65,11 +70,15 @@ export const authAuthorOrExecutorOfTask = async(req: Request,res: Response, next
} }
req.body={...req.body, task:task} req.body={...req.body, task:task}
next() 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 */ /**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>>=>{ export const authAuthorOrExecutorOfDateTimeTask = async(req: Request,res: Response, next:NextFunction):Promise<void | express.Response<Response>>=>{
try{
const token = req.get('Authorization'); const token = req.get('Authorization');
let dateTimeTaskId = null let dateTimeTaskId = null
req.body={...req.body,executorStatus:false} req.body={...req.body,executorStatus:false}
...@@ -105,11 +114,15 @@ export const authAuthorOrExecutorOfDateTimeTask = async(req: Request,res: Respon ...@@ -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'}) if (req.body.authorStatus ===false && req.body.executorStatus===false) return res.status(403).send({Message:'user is not uathorized'})
next() 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*/ /**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>>=>{ export const authAdminProject = async(req: Request,res: Response, next:NextFunction):Promise<void | express.Response<Response>>=>{
try{
const token = req.get('Authorization'); const token = req.get('Authorization');
const {projectId} = req.body; const {projectId} = req.body;
const adminOfProject = await dataSource const adminOfProject = await dataSource
...@@ -127,6 +140,9 @@ export const authAdminProject = async(req: Request,res: Response, next:NextFunct ...@@ -127,6 +140,9 @@ export const authAdminProject = async(req: Request,res: Response, next:NextFunct
} }
req.body ={...req.body,adminStatus:true} req.body ={...req.body,adminStatus:true}
next() 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