Adding Prettier and fixing up eslint version
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / LicenseModelActionHelper.js
1 /*
2  * Copyright © 2016-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 RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import { actionTypes } from './LicenseModelConstants.js';
19 import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
20 import { actionsEnum as vcActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
21 import i18n from 'nfvo-utils/i18n/i18n.js';
22 import LicenseAgreementActionHelper from './licenseAgreement/LicenseAgreementActionHelper.js';
23 import FeatureGroupsActionHelper from './featureGroups/FeatureGroupsActionHelper.js';
24 import EntitlementPoolsActionHelper from './entitlementPools/EntitlementPoolsActionHelper.js';
25 import LicenseKeyGroupsActionHelper from './licenseKeyGroups/LicenseKeyGroupsActionHelper.js';
26 import { default as ItemsHelper } from 'sdc-app/common/helpers/ItemsHelper.js';
27 import MergeEditorActionHelper from 'sdc-app/common/merge/MergeEditorActionHelper.js';
28 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
29 import { CommitModalType } from 'nfvo-components/panel/versionController/components/CommitCommentModal.jsx';
30 import versionPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js';
31 import { itemTypes } from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js';
32 import { catalogItemStatuses } from 'sdc-app/onboarding/onboard/onboardingCatalog/OnboardingCatalogConstants.js';
33 import { actionsEnum as VersionControllerActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
34
35 function baseUrl() {
36     const restPrefix = Configuration.get('restPrefix');
37     return `${restPrefix}/v1.0/vendor-license-models/`;
38 }
39
40 function fetchLicenseModels() {
41     return RestAPIUtil.fetch(
42         `${baseUrl()}?versionFilter=${catalogItemStatuses.DRAFT}`
43     );
44 }
45
46 function fetchFinalizedLicenseModels() {
47     return RestAPIUtil.fetch(
48         `${baseUrl()}?versionFilter=${catalogItemStatuses.CERTIFIED}`
49     );
50 }
51 function fetchArchivedLicenseModels() {
52     return RestAPIUtil.fetch(
53         `${baseUrl()}?Status=${catalogItemStatuses.ARCHIVED}`
54     );
55 }
56 function fetchLicenseModelById(licenseModelId, version) {
57     const { id: versionId } = version;
58     return RestAPIUtil.fetch(
59         `${baseUrl()}${licenseModelId}/versions/${versionId}`
60     );
61 }
62
63 function putLicenseModel(licenseModel) {
64     let {
65         id,
66         vendorName,
67         description,
68         iconRef,
69         version: { id: versionId }
70     } = licenseModel;
71     return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}`, {
72         vendorName,
73         description,
74         iconRef
75     });
76 }
77
78 function putLicenseModelAction({ itemId, action, version }) {
79     const { id: versionId } = version;
80     return RestAPIUtil.put(
81         `${baseUrl()}${itemId}/versions/${versionId}/actions`,
82         { action: action }
83     );
84 }
85
86 const LicenseModelActionHelper = {
87     fetchLicenseModels(dispatch) {
88         return fetchLicenseModels().then(response => {
89             dispatch({
90                 type: actionTypes.LICENSE_MODELS_LIST_LOADED,
91                 response
92             });
93         });
94     },
95
96     fetchFinalizedLicenseModels(dispatch) {
97         return fetchFinalizedLicenseModels().then(response =>
98             dispatch({
99                 type: actionTypes.FINALIZED_LICENSE_MODELS_LIST_LOADED,
100                 response
101             })
102         );
103     },
104
105     fetchArchivedLicenseModels(dispatch) {
106         return fetchArchivedLicenseModels().then(response =>
107             dispatch({
108                 type: actionTypes.ARCHIVED_LICENSE_MODELS_LIST_LOADED,
109                 response
110             })
111         );
112     },
113
114     fetchLicenseModelById(dispatch, { licenseModelId, version }) {
115         return fetchLicenseModelById(licenseModelId, version).then(response => {
116             dispatch({
117                 type: actionTypes.LICENSE_MODEL_LOADED,
118                 response: { ...response, version }
119             });
120         });
121     },
122
123     fetchLicenseModelItems(dispatch, { licenseModelId, version }) {
124         return Promise.all([
125             LicenseAgreementActionHelper.fetchLicenseAgreementList(dispatch, {
126                 licenseModelId,
127                 version
128             }),
129             FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
130                 licenseModelId,
131                 version
132             }),
133             EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
134                 licenseModelId,
135                 version
136             }),
137             LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {
138                 licenseModelId,
139                 version
140             })
141         ]);
142     },
143
144     manageSubmitAction(dispatch, { licenseModelId, version, isDirty }) {
145         if (isDirty) {
146             const onCommit = comment => {
147                 return this.performVCAction(dispatch, {
148                     licenseModelId,
149                     action: vcActionsEnum.COMMIT,
150                     version,
151                     comment
152                 }).then(() => {
153                     return this.performSubmitAction(dispatch, {
154                         licenseModelId,
155                         version
156                     });
157                 });
158             };
159             dispatch({
160                 type: modalActionTypes.GLOBAL_MODAL_SHOW,
161                 data: {
162                     modalComponentName: modalContentMapper.COMMIT_COMMENT,
163                     modalComponentProps: {
164                         onCommit,
165                         type: CommitModalType.COMMIT_SUBMIT
166                     },
167                     title: i18n('Commit & Submit')
168                 }
169             });
170             return Promise.reject();
171         }
172         return this.performSubmitAction(dispatch, { licenseModelId, version });
173     },
174
175     performSubmitAction(dispatch, { licenseModelId, version }) {
176         return putLicenseModelAction({
177             itemId: licenseModelId,
178             action: vcActionsEnum.SUBMIT,
179             version
180         }).then(() => {
181             return ItemsHelper.checkItemStatus(dispatch, {
182                 itemId: licenseModelId,
183                 versionId: version.id
184             }).then(updatedVersion => {
185                 dispatch({
186                     type: modalActionTypes.GLOBAL_MODAL_SUCCESS,
187                     data: {
188                         title: i18n('Submit Succeeded'),
189                         msg: i18n('This license model successfully submitted'),
190                         cancelButtonText: i18n('OK'),
191                         timeout: 2000
192                     }
193                 });
194                 versionPageActionHelper.fetchVersions(dispatch, {
195                     itemType: itemTypes.LICENSE_MODEL,
196                     itemId: licenseModelId
197                 });
198                 return Promise.resolve(updatedVersion);
199             });
200         });
201     },
202
203     performVCAction(dispatch, { licenseModelId, action, version, comment }) {
204         return MergeEditorActionHelper.analyzeSyncResult(dispatch, {
205             itemId: licenseModelId,
206             version
207         }).then(({ inMerge, isDirty, updatedVersion }) => {
208             if (
209                 (updatedVersion.status === catalogItemStatuses.CERTIFIED ||
210                     updatedVersion.archivedStatus ===
211                         catalogItemStatuses.ARCHIVED) &&
212                 (action === VersionControllerActionsEnum.COMMIT ||
213                     action === VersionControllerActionsEnum.SYNC)
214             ) {
215                 versionPageActionHelper.fetchVersions(dispatch, {
216                     itemType: itemTypes.LICENSE_MODEL,
217                     itemId: licenseModelId
218                 });
219                 const msg =
220                     updatedVersion.archivedStatus ===
221                     catalogItemStatuses.ARCHIVED
222                         ? i18n('Item was Archived')
223                         : i18n('Item version already Certified');
224                 dispatch({
225                     type: modalActionTypes.GLOBAL_MODAL_WARNING,
226                     data: {
227                         title: i18n('Commit error'),
228                         msg,
229                         cancelButtonText: i18n('Cancel')
230                     }
231                 });
232                 return Promise.resolve(updatedVersion);
233             }
234             if (!inMerge) {
235                 if (action === vcActionsEnum.SUBMIT) {
236                     return this.manageSubmitAction(dispatch, {
237                         licenseModelId,
238                         version,
239                         isDirty
240                     });
241                 } else {
242                     return ItemsHelper.performVCAction({
243                         itemId: licenseModelId,
244                         action,
245                         version,
246                         comment
247                     }).then(() => {
248                         versionPageActionHelper.fetchVersions(dispatch, {
249                             itemType: itemTypes.LICENSE_MODEL,
250                             itemId: licenseModelId
251                         });
252                         if (action === vcActionsEnum.SYNC) {
253                             return MergeEditorActionHelper.analyzeSyncResult(
254                                 dispatch,
255                                 { itemId: licenseModelId, version }
256                             ).then(({ updatedVersion }) => {
257                                 return Promise.resolve(updatedVersion);
258                             });
259                         } else {
260                             return ItemsHelper.checkItemStatus(dispatch, {
261                                 itemId: licenseModelId,
262                                 versionId: version.id
263                             });
264                         }
265                     });
266                 }
267             }
268         });
269     },
270
271     saveLicenseModel(dispatch, { licenseModel }) {
272         return putLicenseModel(licenseModel).then(() => {
273             dispatch({
274                 type: actionTypes.LICENSE_MODEL_LOADED,
275                 response: licenseModel
276             });
277             const { id, version: { id: versionId } } = licenseModel;
278             return ItemsHelper.checkItemStatus(dispatch, {
279                 itemId: id,
280                 versionId
281             }).then(updatedVersion => {
282                 if (updatedVersion.status !== licenseModel.version.status) {
283                     versionPageActionHelper.fetchVersions(dispatch, {
284                         itemType: itemTypes.LICENSE_MODEL,
285                         itemId: licenseModel.id
286                     });
287                 }
288             });
289         });
290     }
291 };
292
293 export default LicenseModelActionHelper;