Operations on 2 separate collections

mail@pastecode.io avatar
unknown
typescript
a year ago
1.4 kB
5
Indexable
Never
getTasksByCategory(category:string):Observable<any> {
    const daysFromThisWeek = this.getDaysFromThisWeek();
    return forkJoin({
      tasks: this.tasksS.tasksCollection(),
      goals: this.goalsS.goalsCollection(),
    })
    // !!! OPERATIONS ON GOALS !!! 
    .pipe(
      // filter goals with category from parameter
      map(({ tasks, goals }) => {
        return goals.filter((item:any) => item.category === category);
      }),
      // get ID's of found goals in the previous step
      map((goals:any) => {
        const goalsIDs = goals.map((item:any) => item.id);
        return goalsIDs;
      })
    )
    // !!! OPERATIONS ON TASKS !!!
    .pipe(
      // get IDs-matching-tasks
      map(({ tasks, goalsIDs }) => {
        let modArr = [] as any;
        goalsIDs.forEach((goalId:any) => {
          const forModArr = tasks.filter((task:any) => task.goal_id === goalId);
          modArr = modArr.concat(forModArr);
      })
      return modArr;
    }),
    map(tasksArr => {
      // get number of IDs-matching-tasks on each week day
      let finalTasks = [] as any;
      daysFromThisWeek.forEach((day:any) => {
          const forFinalTasks = tasksArr.filter((task:any) => task.taskDate === day);
          finalTasks = finalTasks.concat(forFinalTasks.length);
      })
      return finalTasks;
    })
    )
}