Adding Prettier and fixing up eslint version
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / featureGroups / FeatureGroupsActionHelper.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
13  * or implied. See the License for the specific language governing
14  * permissions and 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
23 function baseUrl(licenseModelId, version) {
24     const restPrefix = Configuration.get('restPrefix');
25     const { id: versionId } = version;
26     return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/feature-groups`;
27 }
28
29 function fetchFeatureGroup(licenseModelId, featureGroupId, version) {
30     return RestAPIUtil.fetch(
31         `${baseUrl(licenseModelId, version)}/${featureGroupId}`
32     );
33 }
34
35 function fetchFeatureGroupsList(licenseModelId, version) {
36     return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
37 }
38
39 function deleteFeatureGroup(licenseModelId, featureGroupId, version) {
40     return RestAPIUtil.destroy(
41         `${baseUrl(licenseModelId, version)}/${featureGroupId}`
42     );
43 }
44
45 function addFeatureGroup(licenseModelId, featureGroup, version) {
46     return RestAPIUtil.post(baseUrl(licenseModelId, version), {
47         name: featureGroup.name,
48         description: featureGroup.description,
49         partNumber: featureGroup.partNumber,
50         manufacturerReferenceNumber: featureGroup.manufacturerReferenceNumber,
51         addedLicenseKeyGroupsIds: featureGroup.licenseKeyGroupsIds,
52         addedEntitlementPoolsIds: featureGroup.entitlementPoolsIds
53     });
54 }
55
56 function updateFeatureGroup(
57     licenseModelId,
58     previousFeatureGroup,
59     featureGroup,
60     version
61 ) {
62     const { licenseKeyGroupsIds = [] } = featureGroup;
63     const {
64         licenseKeyGroupsIds: prevLicenseKeyGroupsIds = []
65     } = previousFeatureGroup;
66     const { entitlementPoolsIds = [] } = featureGroup;
67     const {
68         entitlementPoolsIds: prevEntitlementPoolsIds = []
69     } = previousFeatureGroup;
70     return RestAPIUtil.put(
71         `${baseUrl(licenseModelId, version)}/${featureGroup.id}`,
72         {
73             name: featureGroup.name,
74             description: featureGroup.description,
75             partNumber: featureGroup.partNumber,
76             manufacturerReferenceNumber:
77                 featureGroup.manufacturerReferenceNumber,
78             addedLicenseKeyGroupsIds: licenseKeyGroupsIds.filter(
79                 licenseKeyGroupId =>
80                     prevLicenseKeyGroupsIds.indexOf(licenseKeyGroupId) === -1
81             ),
82             removedLicenseKeyGroupsIds: prevLicenseKeyGroupsIds.filter(
83                 prevLicenseKeyGroupId =>
84                     licenseKeyGroupsIds.indexOf(prevLicenseKeyGroupId) === -1
85             ),
86             addedEntitlementPoolsIds: entitlementPoolsIds.filter(
87                 entitlementPoolId =>
88                     prevEntitlementPoolsIds.indexOf(entitlementPoolId) === -1
89             ),
90             removedEntitlementPoolsIds: prevEntitlementPoolsIds.filter(
91                 prevEntitlementPoolId =>
92                     entitlementPoolsIds.indexOf(prevEntitlementPoolId) === -1
93             )
94         }
95     );
96 }
97
98 export default {
99     fetchFeatureGroup(dispatch, { licenseModelId, featureGroupId, version }) {
100         return fetchFeatureGroup(licenseModelId, featureGroupId, version);
101     },
102
103     fetchFeatureGroupsList(dispatch, { licenseModelId, version }) {
104         return fetchFeatureGroupsList(licenseModelId, version).then(response =>
105             dispatch({
106                 type: featureGroupsActionConstants.FEATURE_GROUPS_LIST_LOADED,
107                 response
108             })
109         );
110     },
111
112     deleteFeatureGroup(dispatch, { licenseModelId, featureGroupId, version }) {
113         return deleteFeatureGroup(licenseModelId, featureGroupId, version).then(
114             () => {
115                 dispatch({
116                     type: featureGroupsActionConstants.DELETE_FEATURE_GROUPS,
117                     featureGroupId
118                 });
119                 return ItemsHelper.checkItemStatus(dispatch, {
120                     itemId: licenseModelId,
121                     versionId: version.id
122                 });
123             }
124         );
125     },
126
127     saveFeatureGroup(
128         dispatch,
129         { licenseModelId, previousFeatureGroup, featureGroup, version }
130     ) {
131         if (previousFeatureGroup) {
132             return updateFeatureGroup(
133                 licenseModelId,
134                 previousFeatureGroup,
135                 featureGroup,
136                 version
137             ).then(() => {
138                 dispatch({
139                     type: featureGroupsActionConstants.EDIT_FEATURE_GROUPS,
140                     featureGroup
141                 });
142                 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(
143                     dispatch,
144                     { licenseModelId, version }
145                 );
146                 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(
147                     dispatch,
148                     { licenseModelId, version }
149                 );
150                 return ItemsHelper.checkItemStatus(dispatch, {
151                     itemId: licenseModelId,
152                     versionId: version.id
153                 });
154             });
155         } else {
156             return addFeatureGroup(licenseModelId, featureGroup, version).then(
157                 response => {
158                     dispatch({
159                         type: featureGroupsActionConstants.ADD_FEATURE_GROUPS,
160                         featureGroup: {
161                             ...featureGroup,
162                             id: response.value,
163                             referencingLicenseAgreements: []
164                         }
165                     });
166                     EntitlementPoolsActionHelper.fetchEntitlementPoolsList(
167                         dispatch,
168                         { licenseModelId, version }
169                     );
170                     LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(
171                         dispatch,
172                         { licenseModelId, version }
173                     );
174                     return ItemsHelper.checkItemStatus(dispatch, {
175                         itemId: licenseModelId,
176                         versionId: version.id
177                     });
178                 }
179             );
180         }
181     },
182
183     selectEntitlementPoolsEditorTab(dispatch, { tab }) {
184         dispatch({
185             type: featureGroupsActionConstants.featureGroupsEditor.SELECT_TAB,
186             tab
187         });
188     },
189
190     openFeatureGroupsEditor(
191         dispatch,
192         { featureGroup, licenseModelId, version }
193     ) {
194         return Promise.all([
195             EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
196                 licenseModelId,
197                 version
198             }),
199             LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {
200                 licenseModelId,
201                 version
202             })
203         ]).then(() => {
204             dispatch({
205                 type: featureGroupsActionConstants.featureGroupsEditor.OPEN,
206                 featureGroup
207             });
208         });
209     },
210
211     closeFeatureGroupsEditor(dispatch) {
212         dispatch({
213             type: featureGroupsActionConstants.featureGroupsEditor.CLOSE
214         });
215     }
216 };