All files / ee/app/assets/javascripts/analytics/cycle_analytics/store/actions value_streams.js

100% Statements 54/54
47.05% Branches 8/17
100% Functions 24/24
100% Lines 54/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                  5x 1x 1x     5x 2x 2x   2x 1x   1x 1x       5x       2x 2x   2x   1x 1x     1x 1x       5x 2x 2x   2x 1x 1x   1x 1x       5x 87x 87x 87x 87x   5x 2x 2x     5x       89x   89x 1x     88x 2x 2x 2x 2x     86x 86x 86x     5x 98x   98x   98x 87x       11x 11x 11x      
import {
  createValueStream as apiCreateValueStream,
  updateValueStream as apiUpdateValueStream,
  deleteValueStream as apiDeleteValueStream,
  getValueStreams,
} from 'ee/api/analytics_api';
import { FETCH_VALUE_STREAM_DATA } from '../../constants';
import * as types from '../mutation_types';
 
export const receiveCreateValueStreamSuccess = ({ commit }, valueStream = {}) => {
  commit(types.RECEIVE_CREATE_VALUE_STREAM_SUCCESS, valueStream);
  commit(types.SET_CREATING_AGGREGATION, true);
};
 
export const createValueStream = ({ commit, dispatch, getters }, data) => {
  const { namespacePath } = getters;
  commit(types.REQUEST_CREATE_VALUE_STREAM);
 
  return apiCreateValueStream(namespacePath, data)
    .then(({ data: newValueStream }) => dispatch('receiveCreateValueStreamSuccess', newValueStream))
    .catch(({ response } = {}) => {
      const { data: { message, payload: { errors } } = null } = response;
      commit(types.RECEIVE_CREATE_VALUE_STREAM_ERROR, { message, errors, data });
    });
};
 
export const updateValueStream = (
  { commit, dispatch, getters },
  { id: valueStreamId, ...data },
) => {
  const { namespacePath } = getters;
  commit(types.REQUEST_UPDATE_VALUE_STREAM);
 
  return apiUpdateValueStream({ namespacePath, valueStreamId, data })
    .then(({ data: newValueStream }) => {
      commit(types.RECEIVE_UPDATE_VALUE_STREAM_SUCCESS, newValueStream);
      return dispatch('fetchCycleAnalyticsData');
    })
    .catch(({ response } = {}) => {
      const { data: { message, payload: { errors } } = null } = response;
      commit(types.RECEIVE_UPDATE_VALUE_STREAM_ERROR, { message, errors, data });
    });
};
 
export const deleteValueStream = ({ commit, dispatch, getters }, valueStreamId) => {
  const { namespacePath } = getters;
  commit(types.REQUEST_DELETE_VALUE_STREAM);
 
  return apiDeleteValueStream(namespacePath, valueStreamId)
    .then(() => commit(types.RECEIVE_DELETE_VALUE_STREAM_SUCCESS))
    .then(() => dispatch('fetchCycleAnalyticsData'))
    .catch(({ response } = {}) => {
      const { data: { message } = null } = response;
      commit(types.RECEIVE_DELETE_VALUE_STREAM_ERROR, message);
    });
};
 
export const fetchValueStreamData = ({ dispatch }) =>
  Promise.resolve()
    .then(() => dispatch('fetchGroupStagesAndEvents'))
    .then(() => dispatch('fetchStageMedianValues'))
    .then(() => dispatch('durationChart/fetchDurationData'));
 
export const setSelectedValueStream = ({ commit, dispatch }, valueStream) => {
  commit(types.SET_SELECTED_VALUE_STREAM, valueStream);
  return dispatch(FETCH_VALUE_STREAM_DATA);
};
 
export const receiveValueStreamsSuccess = (
  { state: { selectedValueStream = null }, commit, dispatch },
  data = [],
) => {
  commit(types.RECEIVE_VALUE_STREAMS_SUCCESS, data);
 
  if (!selectedValueStream && !data.length) {
    return dispatch('fetchGroupStagesAndEvents');
  }
 
  if (!selectedValueStream && data.length) {
    const [firstStream] = data;
    return Promise.resolve()
      .then(() => dispatch('setSelectedValueStream', firstStream))
      .then(() => dispatch('fetchStageCountValues'));
  }
 
  return Promise.resolve()
    .then(() => dispatch(FETCH_VALUE_STREAM_DATA))
    .then(() => dispatch('fetchStageCountValues'));
};
 
export const fetchValueStreams = ({ commit, dispatch, getters }) => {
  const { namespacePath } = getters;
 
  commit(types.REQUEST_VALUE_STREAMS);
 
  return getValueStreams(namespacePath)
    .then(({ data }) => dispatch('receiveValueStreamsSuccess', data))
    .catch((error) => {
      const {
        response: { status },
      } = error;
      commit(types.RECEIVE_VALUE_STREAMS_ERROR, status);
      throw error;
    });
};