#69 optimize tasks/related & fix fixtures

parent 9224cf77
...@@ -113,7 +113,7 @@ const loadFixtures = async () => { ...@@ -113,7 +113,7 @@ const loadFixtures = async () => {
} }
const members:Member[]=[] const members:Member[]=[]
for (let i = 0; i <3; i++) { for (let i = 0; i <5; i++) {
const newMember = new Member(); const newMember = new Member();
newMember.user = faker.helpers.arrayElement(users); newMember.user = faker.helpers.arrayElement(users);
newMember.roleProject=cycleThroughObject(countRolesProject, MemberRole); newMember.roleProject=cycleThroughObject(countRolesProject, MemberRole);
...@@ -126,7 +126,7 @@ const loadFixtures = async () => { ...@@ -126,7 +126,7 @@ const loadFixtures = async () => {
const newProject = new Project(); const newProject = new Project();
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 = [cycleArrayOfMembers(countMembers,members), cycleArrayOfMembers(countMembers,members),cycleArrayOfMembers(countMembers,members)] newProject.members = faker.helpers.arrayElements(members, 3);
newProject.tasks= faker.helpers.arrayElements(tasks, randomIntFromInterval(2, 19)); newProject.tasks= faker.helpers.arrayElements(tasks, randomIntFromInterval(2, 19));
await newProject.save(); await newProject.save();
projects.push(newProject) projects.push(newProject)
......
...@@ -95,47 +95,45 @@ router.get('/my',auth, async (req: Request, res: Response):Promise<Response>=>{ ...@@ -95,47 +95,45 @@ router.get('/my',auth, async (req: Request, res: Response):Promise<Response>=>{
/**check tasks of projects in which current user is involved, search by token*/ /**check tasks 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>=>{
console.log('related')
const user = req.body.user const user = req.body.user
const rawMembership = await dataSource const tasks = await dataSource
.createQueryBuilder() .createQueryBuilder()
.select("member") .select(["task"])
.from(Member, "member") .from(Task,"task")
.leftJoinAndSelect('member.user', 'user' ) .leftJoinAndSelect("task.executor","user")
.leftJoinAndSelect('member.project', 'project' ) .leftJoinAndSelect("task.dateTimeTasks","dateTimeTask")
.leftJoinAndSelect('project.tasks', 'task' ) .leftJoinAndSelect("task.author","users")
.where("member.userId = :id", { id: user.id }) .leftJoinAndSelect("task.project","project")
.leftJoinAndSelect("project.members", "member")
.where("member.userId = :userId", {userId:user.id})
.getMany() .getMany()
let projectIds:any[] = [] let projectIds:any[] = []
if (rawMembership.length>0){ if (tasks.length>0){
for (let member of rawMembership){ for (let task of tasks){
const projectId = member?.project?.id const projectId = task?.project?.id
projectIds.push(projectId) projectIds.push(projectId)
} }
} }
let tasks :Task[]= []
const projectsIdArray = [...new Set(projectIds)]; const projectsIdArray = [...new Set(projectIds)];
const searchByProjectsIdArray:object[] = [] let users:User[]=[]
for (let projectId of projectsIdArray){
searchByProjectsIdArray.push({id:projectId})
}
if (projectIds.length>0){ if (projectIds.length>0){
tasks = await dataSource users = await dataSource
.getRepository(Task) .getRepository(User)
.find({ .find({
relations:{ relations:{
author:true, members:true,
executor:true,
project:true
}, },
where:{ where:{
project: members:
{id:In( {project:In(
projectsIdArray projectsIdArray
)} )}
} }
}) })
} }
return res.send({tasks}) return res.send({tasks, users})
}) })
......
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