All files / add-report middleware.js

100% Statements 15/15
100% Branches 13/13
100% Functions 5/5
100% Lines 14/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67          2x 8x       7x 7x   3x               3x                     3x   2x                       2x           2x   1x           1x   1x   7x    
import { actions, actionTypes as fetchActions } from '@bufferapp/async-data-fetch';
import { actions as analyticsActions } from '@bufferapp/analytics-middleware';
import { getNumOfDaysForDateRange } from '@bufferapp/analyze-shared-components/utils';
import { actionTypes } from './actions';
 
const isChartWithNoProfilesAttached = chartId =>
  chartId === 'comparison' ||
  chartId.match(/aggregate/) ||
  chartId === 'recent-posts';
 
export default store => next => (action) => { // eslint-disable-line no-unused-vars
  switch (action.type) {
    case actionTypes.ADD_TO_REPORT:
      store.dispatch(analyticsActions.trackEvent('Report Module Added', {
        module: action.chart_id.replace('-', ' '),
        reportId: action.reportId,
        channel: isChartWithNoProfilesAttached(action.chart_id) ?
          null :
          store.getState().profiles.selectedProfile.service,
      }));
 
      store.dispatch(actions.fetch({
        name: 'add_chart_to_report',
        args: {
          reportId: action.reportId,
          profileId: isChartWithNoProfilesAttached(action.chart_id) ?
            null :
            store.getState().profiles.selectedProfileId,
          chartId: action.chart_id,
          state: action.state,
        },
      }));
      break;
    case actionTypes.CREATE_REPORT:
      store.dispatch(actions.fetch({
        name: 'create_report',
        args: {
          userId: store.getState().appSidebar.user.id,
          organizationId: store.getState().profiles.organizationId,
          profileId: isChartWithNoProfilesAttached(action.chart_id) ?
            null :
            store.getState().profiles.selectedProfileId,
          chartId: action.chart_id,
          name: action.name,
          state: action.state,
          dateRange: {
            range: store.getState().date.presets.find(preset => preset.selected).range,
            start: store.getState().date.startDate,
            end: store.getState().date.endDate,
          },
        },
      }));
      break;
    case `create_report_${fetchActions.FETCH_SUCCESS}`:
      store.dispatch(analyticsActions.trackEvent('Report Created', {
        title: action.result.name,
        days: getNumOfDaysForDateRange(store.getState().date),
        reportId: action.result._id,
      }));
 
      break;
    default:
      break;
  }
  return next(action);
};