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