#151 added try blocks to projects.ts

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