#151 added try blocks to projects.ts

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