Commit fbe3c417 authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Minor refactor vsa api requests

Updates the remaining vsa routes to include
value stream id
parent 2f9231ac
......@@ -154,22 +154,7 @@ export const receiveGroupStagesSuccess = ({ commit, dispatch }, stages) => {
return dispatch('setDefaultSelectedStage');
};
export const fetchValueStreamStages = ({
hasCreateMultipleValueStreams,
valueStreamId,
groupId,
params,
}) => {
return hasCreateMultipleValueStreams
? Api.cycleAnalyticsValueStreamGroupStagesAndEvents(groupId, valueStreamId, params)
: Api.cycleAnalyticsGroupStagesAndEvents(groupId, params);
};
export const fetchGroupStagesAndEvents = ({ state, dispatch, getters }) => {
const {
featureFlags: { hasCreateMultipleValueStreams = false },
} = state;
export const fetchGroupStagesAndEvents = ({ dispatch, getters }) => {
const {
currentValueStreamId: valueStreamId,
currentGroupPath: groupId,
......@@ -178,8 +163,7 @@ export const fetchGroupStagesAndEvents = ({ state, dispatch, getters }) => {
dispatch('requestGroupStages');
dispatch('customStages/setStageEvents', []);
return fetchValueStreamStages({
hasCreateMultipleValueStreams,
return Api.cycleAnalyticsGroupStagesAndEvents({
groupId,
valueStreamId,
params: { start_date: created_after, project_ids },
......@@ -224,18 +208,16 @@ export const receiveUpdateStageError = (
return dispatch('customStages/setStageFormErrors', errors);
};
export const updateStage = ({ dispatch, state }, { id, ...rest }) => {
const {
selectedGroup: { fullPath },
} = state;
export const updateStage = ({ dispatch, getters }, { id, ...params }) => {
const { currentGroupPath, currentValueStreamId } = getters;
dispatch('requestUpdateStage');
dispatch('customStages/setSavingCustomStage');
return Api.cycleAnalyticsUpdateStage(id, fullPath, { ...rest })
return Api.cycleAnalyticsUpdateStage(currentGroupPath, currentValueStreamId, id, params)
.then(({ data }) => dispatch('receiveUpdateStageSuccess', data))
.catch(({ response: { status = 400, data: responseData } = {} }) =>
dispatch('receiveUpdateStageError', { status, responseData, data: { id, ...rest } }),
dispatch('receiveUpdateStageError', { status, responseData, data: { id, ...params } }),
);
};
......@@ -300,17 +282,14 @@ export const receiveReorderStageError = ({ commit }) => {
createFlash(__('There was an error updating the stage order. Please try reloading the page.'));
};
export const reorderStage = ({ dispatch, state }, initialData) => {
export const reorderStage = ({ dispatch, getters }, initialData) => {
dispatch('requestReorderStage');
const {
selectedGroup: { fullPath },
} = state;
const { currentGroupPath, currentValueStreamId } = getters;
const { id, moveAfterId, moveBeforeId } = initialData;
const params = moveAfterId ? { move_after_id: moveAfterId } : { move_before_id: moveBeforeId };
return Api.cycleAnalyticsUpdateStage(id, fullPath, params)
return Api.cycleAnalyticsUpdateStage(currentGroupPath, currentValueStreamId, id, params)
.then(({ data }) => dispatch('receiveReorderStageSuccess', data))
.catch(({ response: { status = 400, data: responseData } = {} }) =>
dispatch('receiveReorderStageError', { status, responseData }),
......@@ -322,14 +301,11 @@ export const receiveCreateValueStreamSuccess = ({ commit, dispatch }) => {
return dispatch('fetchValueStreams');
};
export const createValueStream = ({ commit, dispatch, rootState }, data) => {
const {
selectedGroup: { fullPath },
} = rootState;
commit(types.REQUEST_CREATE_VALUE_STREAM);
export const createValueStream = ({ commit, dispatch, getters }, data) => {
const { currentGroupPath } = getters;
commit(types.REQUEST_CREATE_VALUE_STREAM);cycleAnalyticsSummaryData
return Api.cycleAnalyticsCreateValueStream(fullPath, data)
return Api.cycleAnalyticsCreateValueStream(currentGroupPath, data)
.then(() => dispatch('receiveCreateValueStreamSuccess'))
.catch(({ response } = {}) => {
const { data: { message, payload: { errors } } = null } = response;
......
......@@ -65,17 +65,13 @@ export const receiveCreateStageError = (
return dispatch('setStageFormErrors', errors);
};
export const createStage = ({ dispatch, rootState, rootGetters }, data) => {
const {
selectedGroup: { fullPath },
} = rootState;
const { currentValueStreamId } = rootGetters;
export const createStage = ({ dispatch, rootGetters }, data) => {
const { currentGroupPath, currentValueStreamId } = rootGetters;
dispatch('clearFormErrors');
dispatch('setSavingCustomStage');
return Api.cycleAnalyticsCreateStage(fullPath, currentValueStreamId, data)
return Api.cycleAnalyticsCreateStage(currentGroupPath, currentValueStreamId, data)
.then(response => {
const { status, data: responseData } = response;
return dispatch('receiveCreateStageSuccess', { status, data: responseData });
......
......@@ -14,19 +14,17 @@ export default {
cycleAnalyticsTopLabelsPath: '/groups/:id/-/analytics/type_of_work/tasks_by_type/top_labels',
cycleAnalyticsSummaryDataPath: '/groups/:id/-/analytics/value_stream_analytics/summary',
cycleAnalyticsTimeSummaryDataPath: '/groups/:id/-/analytics/value_stream_analytics/time_summary',
cycleAnalyticsGroupStagesAndEventsPath: '/groups/:id/-/analytics/value_stream_analytics/stages',
cycleAnalyticsValueStreamGroupStagesAndEventsPath:
'/groups/:group_id/-/analytics/value_stream_analytics/value_streams/:value_stream_id/stages',
cycleAnalyticsValueStreamsPath:
'/groups/:group_id/-/analytics/value_stream_analytics/value_streams',
cycleAnalyticsGroupStagesAndEventsPath:
'/groups/:id/-/analytics/value_stream_analytics/value_streams/:value_stream_id/stages',
cycleAnalyticsValueStreamsPath: '/groups/:id/-/analytics/value_stream_analytics/value_streams',
cycleAnalyticsStageEventsPath:
'/groups/:group_id/-/analytics/value_stream_analytics/value_streams/:value_stream_id/stages/:stage_id/records',
'/groups/:id/-/analytics/value_stream_analytics/value_streams/:value_stream_id/stages/:stage_id/records',
cycleAnalyticsStageMedianPath:
'/groups/:group_id/-/analytics/value_stream_analytics/value_streams/:value_stream_id/stages/:stage_id/median',
'/groups/:id/-/analytics/value_stream_analytics/value_streams/:value_stream_id/stages/:stage_id/median',
cycleAnalyticsStagePath:
'/groups/:group_id/-/analytics/value_stream_analytics/value_streams/:value_stream_id/stages/:stage_id',
'/groups/:id/-/analytics/value_stream_analytics/value_streams/:value_stream_id/stages/:stage_id',
cycleAnalyticsDurationChartPath:
'/groups/:group_id/-/analytics/value_stream_analytics/value_streams/:value_stream_id/stages/:stage_id/duration_chart',
'/groups/:id/-/analytics/value_stream_analytics/value_streams/:value_stream_id/stages/:stage_id/duration_chart',
cycleAnalyticsGroupLabelsPath: '/groups/:namespace_path/-/labels.json',
codeReviewAnalyticsPath: '/api/:version/analytics/code_review',
groupActivityIssuesPath: '/api/:version/analytics/group_activity/issues_count',
......@@ -140,15 +138,9 @@ export default {
return axios.get(url, { params });
},
cycleAnalyticsGroupStagesAndEvents(groupId, params = {}) {
const url = Api.buildUrl(this.cycleAnalyticsGroupStagesAndEventsPath).replace(':id', groupId);
return axios.get(url, { params });
},
cycleAnalyticsValueStreamGroupStagesAndEvents(groupId, valueStreamId, params = {}) {
const url = Api.buildUrl(this.cycleAnalyticsValueStreamGroupStagesAndEventsPath)
.replace(':group_id', groupId)
cycleAnalyticsGroupStagesAndEvents(groupId, valueStreamId, params = {}) {
const url = Api.buildUrl(this.cycleAnalyticsGroupStagesAndEventsPath)
.replace(':id', groupId)
.replace(':value_stream_id', valueStreamId);
return axios.get(url, { params });
......@@ -156,8 +148,8 @@ export default {
cycleAnalyticsStageEvents(groupId, valueStreamId, stageId, params = {}) {
const url = Api.buildUrl(this.cycleAnalyticsStageEventsPath)
.replace(':id', groupId)
.replace(':value_stream_id', valueStreamId)
.replace(':group_id', groupId)
.replace(':stage_id', stageId);
return axios.get(url, { params });
......@@ -165,7 +157,7 @@ export default {
cycleAnalyticsStageMedian(groupId, valueStreamId, stageId, params = {}) {
const url = Api.buildUrl(this.cycleAnalyticsStageMedianPath)
.replace(':group_id', groupId)
.replace(':id', groupId)
.replace(':value_stream_id', valueStreamId)
.replace(':stage_id', stageId);
......@@ -173,32 +165,32 @@ export default {
},
cycleAnalyticsCreateStage(groupId, valueStreamId, data) {
const url = Api.buildUrl(this.cycleAnalyticsValueStreamGroupStagesAndEventsPath)
.replace(':group_id', groupId)
const url = Api.buildUrl(this.cycleAnalyticsGroupStagesAndEventsPath)
.replace(':id', groupId)
.replace(':value_stream_id', valueStreamId);
return axios.post(url, data);
},
cycleAnalyticsCreateValueStream(groupId, data) {
const url = Api.buildUrl(this.cycleAnalyticsValueStreamsPath).replace(':group_id', groupId);
const url = Api.buildUrl(this.cycleAnalyticsValueStreamsPath).replace(':id', groupId);
return axios.post(url, data);
},
cycleAnalyticsValueStreams(groupId, data) {
const url = Api.buildUrl(this.cycleAnalyticsValueStreamsPath).replace(':group_id', groupId);
const url = Api.buildUrl(this.cycleAnalyticsValueStreamsPath).replace(':id', groupId);
return axios.get(url, data);
},
cycleAnalyticsStageUrl(groupId, valueStreamId, stageId) {
return Api.buildUrl(this.cycleAnalyticsStagePath)
.replace(':group_id', groupId)
.replace(':id', groupId)
.replace(':value_stream_id', valueStreamId)
.replace(':stage_id', stageId);
},
cycleAnalyticsUpdateStage(groupId, valueStreamId, stageId, data) {
const url = this.cycleAnalyticsStageUrl(valueStreamId, stageId, groupId);
const url = this.cycleAnalyticsStageUrl(groupId, valueStreamId, stageId);
return axios.put(url, data);
},
......@@ -211,7 +203,7 @@ export default {
cycleAnalyticsDurationChart(groupId, valueStreamId, stageSlug, params = {}) {
const url = Api.buildUrl(this.cycleAnalyticsDurationChartPath)
.replace(':group_id', groupId)
.replace(':id', groupId)
.replace(':value_stream_id', valueStreamId)
.replace(':stage_id', stageSlug);
......
......@@ -201,15 +201,18 @@ describe('Api', () => {
});
describe('Cycle analytics', () => {
const groupId = 'counting-54321';
const createdBefore = '2019-11-18';
const createdAfter = '2019-08-18';
const groupId = 'counting-54321';
const stageId = 'thursday';
const dummyCycleAnalyticsUrlRoot = `${dummyUrlRoot}/groups/${groupId}`;
const valueStreamId = 'a-city-by-the-light-divided';
const dummyCycleAnalyticsUrlRoot = `${dummyUrlRoot}/groups/${groupId}/-/analytics/value_stream_analytics`;
const defaultParams = {
created_after: createdAfter,
created_before: createdBefore,
};
const valueStreamBaseUrl = ({ resource = '', id = null }) =>
[dummyCycleAnalyticsUrlRoot, id ? `value_streams/${id}/${resource}` : resource].join('/');
const expectRequestWithCorrectParameters = (responseObj, { params, expectedUrl, response }) => {
const {
......@@ -288,11 +291,8 @@ describe('Api', () => {
describe('cycleAnalyticsSummaryData', () => {
it('fetches value stream analytics summary data', done => {
const response = [{ value: 0, title: 'New Issues' }, { value: 0, title: 'Deploys' }];
const params = {
...defaultParams,
};
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/summary`;
const params = { ...defaultParams };
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/summary`;
mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsSummaryData(groupId, params)
......@@ -314,11 +314,9 @@ describe('Api', () => {
{ value: '10.0', title: 'Lead time', unit: 'per day' },
{ value: '2.0', title: 'Cycle Time', unit: 'per day' },
];
const params = {
...defaultParams,
};
const params = { ...defaultParams };
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/time_summary`;
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/time_summary`;
mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsTimeSummaryData(groupId, params)
......@@ -337,8 +335,7 @@ describe('Api', () => {
describe('cycleAnalyticsValueStreams', () => {
it('fetches custom value streams', done => {
const response = [{ name: 'value stream 1', id: 1 }];
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/value_streams`;
const expectedUrl = valueStreamBaseUrl({ resource: 'value_streams' });
mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsValueStreams(groupId)
......@@ -356,10 +353,8 @@ describe('Api', () => {
describe('cycleAnalyticsCreateValueStream', () => {
it('submit the custom value stream data', done => {
const response = {};
const customValueStream = {
name: 'cool-value-stream-stage',
};
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/value_streams`;
const customValueStream = { name: 'cool-value-stream-stage' };
const expectedUrl = valueStreamBaseUrl({ resource: 'value_streams' });
mock.onPost(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsCreateValueStream(groupId, customValueStream)
......@@ -381,10 +376,10 @@ describe('Api', () => {
'cycle_analytics[created_after]': createdAfter,
'cycle_analytics[created_before]': createdBefore,
};
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages`;
const expectedUrl = valueStreamBaseUrl({ id: valueStreamId, resource: 'stages' });
mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsGroupStagesAndEvents(groupId, params)
Api.cycleAnalyticsGroupStagesAndEvents(groupId, valueStreamId, params)
.then(responseObj =>
expectRequestWithCorrectParameters(responseObj, {
response,
......@@ -400,13 +395,14 @@ describe('Api', () => {
describe('cycleAnalyticsStageEvents', () => {
it('fetches stage events', done => {
const response = { events: [] };
const params = {
...defaultParams,
};
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/${stageId}/records`;
const params = { ...defaultParams };
const expectedUrl = valueStreamBaseUrl({
id: valueStreamId,
resource: `stages/${stageId}/records`,
});
mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsStageEvents(groupId, stageId, params)
Api.cycleAnalyticsStageEvents(groupId, valueStreamId, stageId, params)
.then(responseObj =>
expectRequestWithCorrectParameters(responseObj, {
response,
......@@ -422,13 +418,14 @@ describe('Api', () => {
describe('cycleAnalyticsStageMedian', () => {
it('fetches stage events', done => {
const response = { value: '5 days ago' };
const params = {
...defaultParams,
};
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/${stageId}/median`;
const params = { ...defaultParams };
const expectedUrl = valueStreamBaseUrl({
id: valueStreamId,
resource: `stages/${stageId}/median`,
});
mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsStageMedian(groupId, stageId, params)
Api.cycleAnalyticsStageMedian(groupId, valueStreamId, stageId, params)
.then(responseObj =>
expectRequestWithCorrectParameters(responseObj, {
response,
......@@ -442,8 +439,6 @@ describe('Api', () => {
});
describe('cycleAnalyticsCreateStage', () => {
const valueStreamId = 'fake-value-stream';
it('submit the custom stage data', done => {
const response = {};
const customStage = {
......@@ -453,7 +448,10 @@ describe('Api', () => {
end_event_identifier: 'issue_closed',
end_event_label_id: null,
};
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/value_streams/${valueStreamId}/stages`;
const expectedUrl = valueStreamBaseUrl({
id: valueStreamId,
resource: 'stages',
});
mock.onPost(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsCreateStage(groupId, valueStreamId, customStage)
......@@ -462,6 +460,7 @@ describe('Api', () => {
expect(JSON.parse(reqData)).toMatchObject(customStage);
expect(url).toEqual(expectedUrl);
})
.then(done)
.catch(done.fail);
});
......@@ -470,14 +469,14 @@ describe('Api', () => {
describe('cycleAnalyticsUpdateStage', () => {
it('updates the stage data', done => {
const response = { id: stageId, custom: false, hidden: true, name: 'nice-stage' };
const stageData = {
name: 'nice-stage',
hidden: true,
};
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/${stageId}`;
const stageData = { name: 'nice-stage', hidden: true };
const expectedUrl = valueStreamBaseUrl({
id: valueStreamId,
resource: `stages/${stageId}`,
});
mock.onPut(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsUpdateStage(stageId, groupId, stageData)
Api.cycleAnalyticsUpdateStage(groupId, valueStreamId, stageId, stageData)
.then(({ data, config: { data: reqData, url } }) => {
expect(data).toEqual(response);
expect(JSON.parse(reqData)).toMatchObject(stageData);
......@@ -491,10 +490,13 @@ describe('Api', () => {
describe('cycleAnalyticsRemoveStage', () => {
it('deletes the specified data', done => {
const response = { id: stageId, hidden: true, custom: true };
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/${stageId}`;
const expectedUrl = valueStreamBaseUrl({
id: valueStreamId,
resource: `stages/${stageId}`,
});
mock.onDelete(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsRemoveStage(stageId, groupId)
Api.cycleAnalyticsRemoveStage(groupId, valueStreamId, stageId)
.then(({ data, config: { url } }) => {
expect(data).toEqual(response);
......@@ -508,13 +510,14 @@ describe('Api', () => {
describe('cycleAnalyticsDurationChart', () => {
it('fetches stage duration data', done => {
const response = [];
const params = {
...defaultParams,
};
const expectedUrl = `${dummyCycleAnalyticsUrlRoot}/-/analytics/value_stream_analytics/stages/thursday/duration_chart`;
const params = { ...defaultParams };
const expectedUrl = valueStreamBaseUrl({
id: valueStreamId,
resource: `stages/${stageId}/duration_chart`,
});
mock.onGet(expectedUrl).reply(httpStatus.OK, response);
Api.cycleAnalyticsDurationChart(groupId, stageId, params)
Api.cycleAnalyticsDurationChart(groupId, valueStreamId, stageId, params)
.then(responseObj =>
expectRequestWithCorrectParameters(responseObj, {
response,
......
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