react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / featureGroups / FeatureGroupsActionHelper.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 as featureGroupsActionConstants } from './FeatureGroupsConstants.js';
19 import EntitlementPoolsActionHelper from 'sdc-app/onboarding/licenseModel/entitlementPools/EntitlementPoolsActionHelper.js';
20 import LicenseKeyGroupsActionHelper from 'sdc-app/onboarding/licenseModel/licenseKeyGroups/LicenseKeyGroupsActionHelper.js';
21 import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
22 import {
23     actionTypes as modalActionTypes,
24     modalSizes
25 } from 'nfvo-components/modal/GlobalModalConstants.js';
26 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
27 import i18n from 'nfvo-utils/i18n/i18n.js';
28
29 function baseUrl(licenseModelId, version) {
30     const restPrefix = Configuration.get('restPrefix');
31     const { id: versionId } = version;
32     return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/feature-groups`;
33 }
34
35 function fetchFeatureGroup(licenseModelId, featureGroupId, version) {
36     return RestAPIUtil.fetch(
37         `${baseUrl(licenseModelId, version)}/${featureGroupId}`
38     );
39 }
40
41 function fetchFeatureGroupsList(licenseModelId, version) {
42     return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
43 }
44
45 function deleteFeatureGroup(licenseModelId, featureGroupId, version) {
46     return RestAPIUtil.destroy(
47         `${baseUrl(licenseModelId, version)}/${featureGroupId}`
48     );
49 }
50
51 function addFeatureGroup(licenseModelId, featureGroup, version) {
52     return RestAPIUtil.post(baseUrl(licenseModelId, version), {
53         name: featureGroup.name,
54         description: featureGroup.description,
55         partNumber: featureGroup.partNumber,
56         manufacturerReferenceNumber: featureGroup.manufacturerReferenceNumber,
57         addedLicenseKeyGroupsIds: featureGroup.licenseKeyGroupsIds,
58         addedEntitlementPoolsIds: featureGroup.entitlementPoolsIds
59     });
60 }
61
62 function updateFeatureGroup(
63     licenseModelId,
64     previousFeatureGroup,
65     featureGroup,
66     version
67 ) {
68     const { licenseKeyGroupsIds = [] } = featureGroup;
69     const {
70         licenseKeyGroupsIds: prevLicenseKeyGroupsIds = []
71     } = previousFeatureGroup;
72     const { entitlementPoolsIds = [] } = featureGroup;
73     const {
74         entitlementPoolsIds: prevEntitlementPoolsIds = []
75     } = previousFeatureGroup;
76     return RestAPIUtil.put(
77         `${baseUrl(licenseModelId, version)}/${featureGroup.id}`,
78         {
79             name: featureGroup.name,
80             description: featureGroup.description,
81             partNumber: featureGroup.partNumber,
82             manufacturerReferenceNumber:
83                 featureGroup.manufacturerReferenceNumber,
84             addedLicenseKeyGroupsIds: licenseKeyGroupsIds.filter(
85                 licenseKeyGroupId =>
86                     prevLicenseKeyGroupsIds.indexOf(licenseKeyGroupId) === -1
87             ),
88             removedLicenseKeyGroupsIds: prevLicenseKeyGroupsIds.filter(
89                 prevLicenseKeyGroupId =>
90                     licenseKeyGroupsIds.indexOf(prevLicenseKeyGroupId) === -1
91             ),
92             addedEntitlementPoolsIds: entitlementPoolsIds.filter(
93                 entitlementPoolId =>
94                     prevEntitlementPoolsIds.indexOf(entitlementPoolId) === -1
95             ),
96             removedEntitlementPoolsIds: prevEntitlementPoolsIds.filter(
97                 prevEntitlementPoolId =>
98                     entitlementPoolsIds.indexOf(prevEntitlementPoolId) === -1
99             )
100         }
101     );
102 }
103
104 export default {
105     fetchFeatureGroup(dispatch, { licenseModelId, featureGroupId, version }) {
106         return fetchFeatureGroup(licenseModelId, featureGroupId, version);
107     },
108
109     fetchFeatureGroupsList(dispatch, { licenseModelId, version }) {
110         return fetchFeatureGroupsList(licenseModelId, version).then(response =>
111             dispatch({
112                 type: featureGroupsActionConstants.FEATURE_GROUPS_LIST_LOADED,
113                 response
114             })
115         );
116     },
117
118     deleteFeatureGroup(dispatch, { licenseModelId, featureGroupId, version }) {
119         return deleteFeatureGroup(licenseModelId, featureGroupId, version).then(
120             () => {
121                 dispatch({
122                     type: featureGroupsActionConstants.DELETE_FEATURE_GROUPS,
123                     featureGroupId
124                 });
125                 return ItemsHelper.checkItemStatus(dispatch, {
126                     itemId: licenseModelId,
127                     versionId: version.id
128                 });
129             }
130         );
131     },
132
133     saveFeatureGroup(
134         dispatch,
135         { licenseModelId, previousFeatureGroup, featureGroup, version }
136     ) {
137         if (previousFeatureGroup) {
138             return updateFeatureGroup(
139                 licenseModelId,
140                 previousFeatureGroup,
141                 featureGroup,
142                 version
143             ).then(() => {
144                 dispatch({
145                     type: featureGroupsActionConstants.EDIT_FEATURE_GROUPS,
146                     featureGroup
147                 });
148                 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(
149                     dispatch,
150                     { licenseModelId, version }
151                 );
152                 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(
153                     dispatch,
154                     { licenseModelId, version }
155                 );
156                 return ItemsHelper.checkItemStatus(dispatch, {
157                     itemId: licenseModelId,
158                     versionId: version.id
159                 });
160             });
161         } else {
162             return addFeatureGroup(licenseModelId, featureGroup, version).then(
163                 response => {
164                     dispatch({
165                         type: featureGroupsActionConstants.ADD_FEATURE_GROUPS,
166                         featureGroup: {
167                             ...featureGroup,
168                             id: response.value,
169                             referencingLicenseAgreements: []
170                         }
171                     });
172                     EntitlementPoolsActionHelper.fetchEntitlementPoolsList(
173                         dispatch,
174                         { licenseModelId, version }
175                     );
176                     LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(
177                         dispatch,
178                         { licenseModelId, version }
179                     );
180                     return ItemsHelper.checkItemStatus(dispatch, {
181                         itemId: licenseModelId,
182                         versionId: version.id
183                     });
184                 }
185             );
186         }
187     },
188
189     selectEntitlementPoolsEditorTab(dispatch, { tab }) {
190         dispatch({
191             type: featureGroupsActionConstants.featureGroupsEditor.SELECT_TAB,
192             tab
193         });
194     },
195
196     openFeatureGroupsEditor(
197         dispatch,
198         { featureGroup, licenseModelId, version, isReadOnlyMode }
199     ) {
200         return Promise.all([
201             EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
202                 licenseModelId,
203                 version
204             }),
205             LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {
206                 licenseModelId,
207                 version
208             })
209         ]).then(() => {
210             dispatch({
211                 type: featureGroupsActionConstants.featureGroupsEditor.OPEN,
212                 featureGroup
213             });
214             dispatch({
215                 type: featureGroupsActionConstants.featureGroupsEditor.OPEN,
216                 featureGroup
217             });
218             dispatch({
219                 type: modalActionTypes.GLOBAL_MODAL_SHOW,
220                 data: {
221                     modalComponentName: modalContentMapper.FG_EDITOR,
222                     modalComponentProps: {
223                         version,
224                         licenseModelId,
225                         isReadOnlyMode,
226                         size: modalSizes.LARGE
227                     },
228                     title:
229                         licenseModelId && version && featureGroup
230                             ? i18n('Edit Feature Group')
231                             : i18n('Create New Feature Group')
232                 }
233             });
234         });
235     },
236
237     closeFeatureGroupsEditor(dispatch) {
238         dispatch({
239             type: featureGroupsActionConstants.featureGroupsEditor.CLOSE
240         });
241         dispatch({
242             type: modalActionTypes.GLOBAL_MODAL_CLOSE
243         });
244     }
245 };