Merge branch 'task-63-enhance/routers_tasks_projects' into 'development'

Task 63 enhance/routers tasks projects

See merge request !42
parents d19dd8ff a7fd397b
...@@ -68,8 +68,6 @@ const loadFixtures = async () => { ...@@ -68,8 +68,6 @@ const loadFixtures = async () => {
newTask.title = `Buy ${faker.commerce.productName()}`; newTask.title = `Buy ${faker.commerce.productName()}`;
newTask.description = faker.random.words(4); newTask.description = faker.random.words(4);
newTask.executor = faker.helpers.arrayElement(users); newTask.executor = faker.helpers.arrayElement(users);
// newTask.dateTimeDue = null;
// newTask.dateTimeStart = null;
newTask.author = faker.helpers.arrayElement(users); newTask.author = faker.helpers.arrayElement(users);
newTask.accomplish = accomplish[0]; newTask.accomplish = accomplish[0];
newTask.priority = faker.helpers.arrayElement(priorities); newTask.priority = faker.helpers.arrayElement(priorities);
...@@ -93,9 +91,7 @@ const loadFixtures = async () => { ...@@ -93,9 +91,7 @@ const loadFixtures = async () => {
newProject.title = `Project ${faker.random.words(1)}`; newProject.title = `Project ${faker.random.words(1)}`;
newProject.color = faker.random.words(1); newProject.color = faker.random.words(1);
newProject.members = faker.helpers.arrayElements(members,randomIntFromInterval(1, 3)) newProject.members = faker.helpers.arrayElements(members,randomIntFromInterval(1, 3))
// newProject.admin = faker.helpers.arrayElement(users);
newProject.tasks= faker.helpers.arrayElements(tasks, randomIntFromInterval(1, 3)); newProject.tasks= faker.helpers.arrayElements(tasks, randomIntFromInterval(1, 3));
// newProject.workers = faker.helpers.arrayElements(users, randomIntFromInterval(1, 3));
await newProject.save(); await newProject.save();
projects.push(newProject) projects.push(newProject)
} }
......
import express,{Router, Request, Response} from 'express';
import {myDataSource} from '../app-data-source';
import { Member } from "../models/Member";
const router:Router = express.Router();
const dataSource = myDataSource;
...@@ -5,7 +5,6 @@ import { User } from '../models/User'; ...@@ -5,7 +5,6 @@ import { User } from '../models/User';
import { Member, MemberRole } from '../models/Member'; import { Member, MemberRole } from '../models/Member';
import { userInfo } from 'os'; import { userInfo } from 'os';
import { Task } from '../models/Task'; import { Task } from '../models/Task';
import { getRepository } from 'typeorm';
const router:Router = express.Router(); const router:Router = express.Router();
const dataSource = myDataSource; const dataSource = myDataSource;
...@@ -26,7 +25,37 @@ router.get('/my',async (req:Request, res:Response): Promise<Response>=> { ...@@ -26,7 +25,37 @@ router.get('/my',async (req:Request, res:Response): Promise<Response>=> {
.getOne(); .getOne();
if(!user) return res.status(404).send({Message:'user not found'}) if(!user) return res.status(404).send({Message:'user not found'})
const projects:Project[] = await dataSource.manager.find(Project)
const rawProjects = await dataSource
.createQueryBuilder()
.from(Project, "project")
.select(["project.id"])
.leftJoinAndSelect('project.members', 'member')
.loadRelationCountAndMap('project.tasks', 'project.tasks')
.leftJoinAndSelect('member.user', 'user' )
.where('member.userId = :userId',{userId:user.id})
// .where("project.id = :id", { id: req.params.id })
// .select(["*","members"])
// .select("*")
.getMany()
const projectIds = []
if (rawProjects.length>0){
for(let project of rawProjects){
projectIds.push(project.id
)
}
}
console.log('projectIds', projectIds)
const projects = await dataSource
.createQueryBuilder()
.from(Project, "project")
.select(["project"])
.leftJoinAndSelect('project.members', 'member')
.loadRelationCountAndMap('project.tasks', 'project.tasks')
.leftJoinAndSelect('member.user', 'user')
.where('project.id IN(:...projectIds)', {projectIds})
.getMany()
// const projects:Project[] = await dataSource.manager.find(Project)
return res.send({projects}) return res.send({projects})
}) })
...@@ -82,6 +111,19 @@ router.get("/:id",async (req:Request, res:Response): Promise<Response> => { ...@@ -82,6 +111,19 @@ router.get("/:id",async (req:Request, res:Response): Promise<Response> => {
}) })
router.delete('/:projectId',async (req: Request, res: Response):Promise<Response>=>{
const projectId = req.params.projectId;
await myDataSource
.createQueryBuilder()
.delete()
.from(Project)
.where("id = :id", { id: projectId })
.execute()
return res.send({message: 'Project deleted successfully'})
})
router.get('/user/:userId', async (req : Request, res : Response): Promise<Response>=>{ router.get('/user/:userId', async (req : Request, res : Response): Promise<Response>=>{
const userId:string = req.params.userId const userId:string = req.params.userId
const userProjects = await dataSource const userProjects = await dataSource
...@@ -93,4 +135,62 @@ router.get('/user/:userId', async (req : Request, res : Response): Promise<Respo ...@@ -93,4 +135,62 @@ router.get('/user/:userId', async (req : Request, res : Response): Promise<Respo
return res.send({userProjects}) return res.send({userProjects})
}) })
/** Add user to specific project */
router.post('/add-user/', async (req: Request, res: Response):Promise<Response>=>{
const {userId, projectId, roleProject} = req.body;
const newMember:Member = new Member();
try{
newMember.user= userId;
newMember.project=projectId
newMember.roleProject=roleProject
await newMember.save()
return res.send({newMember})
} catch(e){
return res.send({message:"add user to project failed" })
}
})
/** Remove user from specific project */
router.post('/remove-user', async (req: Request, res: Response):Promise<Response>=> {
console.log('in delete user')
const token = req.get('Authorization');
console.log('token ', token)
const {userId, projectId} = req.body;
console.log('req.body', req.body )
const adminOfProject = await dataSource
.createQueryBuilder()
.select("user")
.from(User, "user")
.leftJoinAndSelect("user.membership","member")
.leftJoinAndSelect('member.project', 'project' )
.where("user.token = :token", { token })
.andWhere('project.id=:projectId',{projectId})
.andWhere('member.roleProject=:roleProject',{roleProject:'admin'})
.getOne()
if (!adminOfProject){
return res.send({message:'User is not authorized'})
}
// return res.send({adminOfProject})
console.log('adminOfProject', adminOfProject)
try{
await dataSource
.createQueryBuilder()
.delete()
.from(Member)
.where("user = :userId", { userId })
.andWhere("project=:projectId",{projectId})
.execute()
return res.send({message:"User removed from project successfully" })
} catch(e){
return res.send({message:'Failed to remove user from project'})
}
})
export default router; export default router;
...@@ -2,6 +2,8 @@ import express,{Router, Request, Response} from 'express'; ...@@ -2,6 +2,8 @@ import express,{Router, Request, Response} from 'express';
import {Task} from '../models/Task'; import {Task} from '../models/Task';
import {myDataSource} from '../app-data-source'; import {myDataSource} from '../app-data-source';
import { User } from '../models/User'; import { User } from '../models/User';
import { Member } from '../models/Member';
import { In } from 'typeorm';
const router:Router = express.Router(); const router:Router = express.Router();
const dataSource = myDataSource; const dataSource = myDataSource;
...@@ -97,6 +99,61 @@ router.get('/my', async (req: Request, res: Response):Promise<Response>=>{ ...@@ -97,6 +99,61 @@ router.get('/my', async (req: Request, res: Response):Promise<Response>=>{
return res.send({tasks}) return res.send({tasks})
}) })
router.get('/related', async (req: Request, res: Response):Promise<Response>=>{
const token = req.get('Authorization');
const user = await dataSource
.createQueryBuilder()
.select("user")
.from(User, "user")
.where("user.token = :token", { token: token })
.getOne()
if(!user) return res.status(404).send({Message:'user not found'})
const rawMembership = await dataSource
.createQueryBuilder()
.select("member")
.from(Member, "member")
.leftJoinAndSelect('member.user', 'user' )
.leftJoinAndSelect('member.project', 'project' )
.leftJoinAndSelect('project.tasks', 'task' )
.where("member.userId = :id", { id: user.id })
.getMany()
let projectIds:any[] = []
if (rawMembership.length>0){
for (let member of rawMembership){
const projectId = member?.project?.id
projectIds.push(projectId)
}
}
let tasks :Task[]= []
const projectsIdArray = [...new Set(projectIds)];
const searchByProjectsIdArray:object[] = []
for (let projectId of projectsIdArray){
searchByProjectsIdArray.push({id:projectId})
}
if (projectIds.length>0){
tasks = await dataSource
.getRepository(Task)
.find({
relations:{
author:true,
executor:true,
project:true
},
where:{
project:
{id:In(
projectsIdArray
)}
}
})
}
return res.send({tasks})
})
router.delete('/:taskId',async (req: Request, res: Response):Promise<Response>=>{ router.delete('/:taskId',async (req: Request, res: Response):Promise<Response>=>{
const taskId = req.params.taskId; const taskId = req.params.taskId;
await myDataSource await myDataSource
...@@ -144,7 +201,6 @@ router.put('/',async(req:Request, res:Response)=> { ...@@ -144,7 +201,6 @@ router.put('/',async(req:Request, res:Response)=> {
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;
console.log('projectArray ', projectArray)
if (projectArray.length===0) { if (projectArray.length===0) {
const rawTasks = await const rawTasks = await
dataSource dataSource
......
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