import logger from 'kolibri.lib.logging';
import filter from 'lodash/filter';
import { TaskResource } from 'kolibri.resources';
import client from 'kolibri.client';
import urls from 'kolibri.urls';
import { TaskStatuses, TaskTypes } from '../../constants';

const logging = logger.getLogger(__filename);

function startCSVExport(store, logtype, creating, commitStart) {
  const params = {
    logtype: logtype,
  };
  if (!creating) {
    let promise = TaskResource.startexportlogcsv(params);
    return promise.then(task => {
      store.commit(commitStart, task.entity.id);
      return task.entity.id;
    });
  }
}

function startSummaryCSVExport(store) {
  return startCSVExport(
    store,
    'summary',
    store.getters.inSummaryCSVCreation,
    'START_SUMMARY_CSV_EXPORT'
  );
}

function startSessionCSVExport(store) {
  return startCSVExport(
    store,
    'session',
    store.getters.inSessionCSVCreation,
    'START_SESSION_CSV_EXPORT'
  );
}

function getExportedLogsInfo(store) {
  return client({
    path: urls['kolibri:core:exportedlogsinfo'](),
  }).then(response => {
    const data = response.entity;
    let sessionTimeStamp = null;
    if (data.session != null) {
      sessionTimeStamp = new Date(data.session * 1000);
      store.commit('SET_FINISHED_SESSION_CSV_CREATION', sessionTimeStamp);
    }
    let summaryTimeStamp = null;
    if (data.summary != null) {
      summaryTimeStamp = new Date(data.summary * 1000);
      store.commit('SET_FINISHED_SUMMARY_CSV_CREATION', summaryTimeStamp);
    }
  });
}

function checkTaskStatus(store, newTasks, taskType, taskId, commitStart, commitFinish) {
  // if task job has already been fetched, just continually check if its completed
  if (taskId) {
    const completed = filter(newTasks, {
      id: taskId,
    });
    if (completed.length > 0) {
      const task = completed[0];
      if (task.status === TaskStatuses.COMPLETED) {
        store.commit(commitFinish, new Date());
        TaskResource.deleteFinishedTasks();
      }
    }
  } else {
    const running = filter(newTasks, function(task) {
      return (
        task.type === taskType &&
        task.status !== TaskStatuses.COMPLETED &&
        task.status !== TaskStatuses.FAILED
      );
    });
    if (running.length > 0) store.commit(commitStart, running[0]);
  }
}

function refreshTaskList(store) {
  return TaskResource.fetchCollection({
    force: true,
  })
    .then(newTasks => {
      checkTaskStatus(
        store,
        newTasks,
        TaskTypes.EXPORTSESSIONLOGCSV,
        store.getters.sessionTaskId,
        'START_SESSION_CSV_EXPORT',
        'SET_FINISHED_SESSION_CSV_CREATION'
      );
      checkTaskStatus(
        store,
        newTasks,
        TaskTypes.EXPORTSUMMARYLOGCSV,
        store.getters.summaryTaskId,
        'START_SUMMARY_CSV_EXPORT',
        'SET_FINISHED_SUMMARY_CSV_CREATION'
      );
      checkTaskStatus(
        store,
        newTasks,
        TaskTypes.SYNCDATAPORTAL,
        store.state.facilityTaskId,
        'START_FACILITY_SYNC',
        'SET_FINISH_FACILITY_SYNC'
      );
    })
    .catch(error => {
      logging.error('There was an error while fetching the task list: ', error);
    });
}

export default {
  refreshTaskList,
  startSummaryCSVExport,
  startSessionCSVExport,
  getExportedLogsInfo,
};

@@ -39,44 +39,8 @@
 g';%0A
-import filter from 'lodash/filter';%0A
 impo
@@ -527,19 +527,16 @@
 k.entity
-.id
 );%0A     
@@ -1694,73 +1694,67 @@
 nst 
-completed = filter(newTasks, %7B%0A      id: taskId,%0A    %7D
+task = newTasks.find(task =%3E task.id === taskId
 );%0A
+%0A
     if (
 comp
@@ -1753,75 +1753,16 @@
 if (
-completed.length %3E 0) %7B%0A      const task = completed%5B0%5D;%0A      if (
+task && 
 task
@@ -1801,26 +1801,24 @@
 ED) %7B%0A      
-  
 store.commit
@@ -1849,60 +1849,8 @@
 ));%0A
-        TaskResource.deleteFinishedTasks();%0A      %7D%0A
     
@@ -1886,23 +1886,16 @@
 g = 
-filter(
 newTasks
 , fu
@@ -1894,24 +1894,23 @@
 asks
-, function
+.filter
 (task
-)
+ =%3E
  %7B%0A 
@@ -2900,24 +2900,201 @@
 NC'%0A      );
+%0A%0A      const completed = newTasks.filter(task =%3E task.status === TaskStatuses.COMPLETED);%0A%0A      if (completed.length %3E 0) %7B%0A        TaskResource.deleteFinishedTasks();%0A      %7D
 %0A    %7D)%0A    

Fix passing id instead of task, and other cleanup