Migrate sdc-sdc-workflow-designer docs
[sdc/sdc-workflow-designer.git] / workflow-designer-ui / src / main / frontend / src / features / version / versionSaga.js
1 /*
2 * Copyright © 2018 European Support Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 import { all, call, put, takeEvery, takeLatest } from 'redux-saga/effects';
17 import { I18n } from 'react-redux-i18n';
18
19 import { genericNetworkErrorAction } from 'src/appConstants';
20 import {
21     setWorkflowVersionAction,
22     versionStateChangedAction,
23     FETCH_REQUESTED
24 } from 'features/version/versionConstants';
25 import { setInputsOutputs } from 'features/version/inputOutput/inputOutputActions';
26 import { SUBMIT_VERSION } from 'features/version/create/createVersionConstants';
27 import {
28     SAVE_ACTION,
29     CERTIFY_ACTION
30 } from 'features/version/versionController/versionControllerConstants';
31 import versionApi from 'features/version/versionApi';
32 import { notificationActions } from 'shared/notifications/notificationsActions';
33 import { versionState } from 'features/version/versionConstants';
34 import overviewApi from '../workflow/overview/overviewApi';
35 import { versionListFetchAction } from '../workflow/overview/overviewConstansts';
36 import {
37     updateComposition,
38     deleteCompositionArtifact
39 } from 'features/version/composition/compositionActions';
40 import { getActivitiesList } from 'features/activities/activitiesActions';
41
42 /**
43  * Composition validation - converting artifact string to xml
44  * and checking if bpmn diagram has only one child
45  * @param composition
46  * @returns {boolean}
47  */
48 function validateCurrentArtifact(composition) {
49     const parser = new DOMParser();
50     const xml = parser.parseFromString(composition, 'text/xml');
51     return Boolean(
52         xml.getElementsByTagName('bpmndi:BPMNPlane').BPMNPlane_1.children.length
53     );
54 }
55
56 function* fetchVersion(action) {
57     try {
58         yield put(getActivitiesList());
59         const data = yield call(versionApi.fetchVersion, action.payload);
60         const { inputs, outputs, ...rest } = data;
61         let composition;
62
63         if (rest.hasArtifact) {
64             composition = yield call(
65                 versionApi.fetchVersionArtifact,
66                 action.payload
67             );
68         } else {
69             //Clearing the store from old artifact using init the default
70             yield put(deleteCompositionArtifact());
71         }
72         yield all([
73             put(setWorkflowVersionAction(rest)),
74             put(setInputsOutputs({ inputs, outputs })),
75             composition && put(updateComposition(composition))
76         ]);
77     } catch (error) {
78         yield put(genericNetworkErrorAction(error));
79     }
80 }
81
82 function* watchSubmitVersion(action) {
83     try {
84         const { workflowId, history } = action.payload;
85         const data = yield call(versionApi.createNewVersion, action.payload);
86         const versions = yield call(overviewApi.getVersions, workflowId);
87         yield put(versionListFetchAction(versions));
88         yield call(
89             history.push(`/workflows/workflow/${workflowId}/version/${data.id}`)
90         );
91     } catch (error) {
92         yield put(genericNetworkErrorAction(error));
93     }
94 }
95
96 function* watchUpdateVersion(action) {
97     try {
98         const {
99             workflowId,
100             workflowName,
101             params: { composition, ...versionData }
102         } = action.payload;
103         const isArtifactValid = validateCurrentArtifact(composition);
104         yield call(versionApi.updateVersion, {
105             workflowId,
106             params: versionData
107         });
108         yield put(
109             notificationActions.showSuccess({
110                 title: I18n.t('workflow.confirmationMessages.updateTitle'),
111                 message: I18n.t('workflow.confirmationMessages.updateMessage')
112             })
113         );
114         if (isArtifactValid) {
115             yield call(versionApi.updateVersionArtifact, {
116                 workflowId,
117                 workflowName,
118                 versionName: versionData.name.split('.').join('_'),
119                 versionId: versionData.id,
120                 payload: composition
121             });
122         } else {
123             yield call(versionApi.deleteVersionArtifact, {
124                 workflowId,
125                 versionId: versionData.id
126             });
127         }
128         return isArtifactValid;
129     } catch (error) {
130         yield put(genericNetworkErrorAction(error));
131     }
132 }
133
134 function* watchCertifyVersion(action) {
135     try {
136         const isArtifactValid = yield call(watchUpdateVersion, action);
137         if (!isArtifactValid)
138             throw new Error('Could not update empty artifact');
139         yield call(versionApi.certifyVersion, {
140             ...action.payload
141         });
142         yield put(versionStateChangedAction({ state: versionState.CERTIFIED }));
143         yield put(
144             notificationActions.showSuccess({
145                 title: I18n.t('workflow.confirmationMessages.certifyTitle'),
146                 message: I18n.t('workflow.confirmationMessages.certifyMessage')
147             })
148         );
149     } catch (error) {
150         yield put(
151             notificationActions.showError({
152                 title: I18n.t('workflow.confirmationMessages.certifyTitle'),
153                 message: I18n.t('workflow.composition.certifyArtifact')
154             })
155         );
156         yield put(genericNetworkErrorAction(error));
157     }
158 }
159
160 function* versionSaga() {
161     yield takeLatest(FETCH_REQUESTED, fetchVersion);
162     yield takeEvery(SUBMIT_VERSION, watchSubmitVersion);
163     yield takeEvery(SAVE_ACTION, watchUpdateVersion);
164     yield takeEvery(CERTIFY_ACTION, watchCertifyVersion);
165 }
166
167 export default versionSaga;