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