react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / entitlementPools / EntitlementPoolsActionHelper.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
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16
17 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
18 import Configuration from 'sdc-app/config/Configuration.js';
19 import { actionTypes as entitlementPoolsActionTypes } from './EntitlementPoolsConstants.js';
20 import { actionTypes as limitEditorActions } from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js';
21 import { default as getValue, getStrValue } from 'nfvo-utils/getValue.js';
22 import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
23 import i18n from 'nfvo-utils/i18n/i18n.js';
24 import {
25     actionTypes as modalActionTypes,
26     modalSizes
27 } from 'nfvo-components/modal/GlobalModalConstants.js';
28 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
29
30 function baseUrl(licenseModelId, version) {
31     const restPrefix = Configuration.get('restPrefix');
32     const { id: versionId } = version;
33     return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/entitlement-pools`;
34 }
35
36 function fetchEntitlementPoolsList(licenseModelId, version) {
37     return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
38 }
39
40 function postEntitlementPool(licenseModelId, entitlementPool, version) {
41     return RestAPIUtil.post(baseUrl(licenseModelId, version), {
42         name: entitlementPool.name,
43         description: entitlementPool.description,
44         thresholdValue: entitlementPool.thresholdValue,
45         thresholdUnits: getValue(entitlementPool.thresholdUnits),
46         increments: entitlementPool.increments,
47         time: entitlementPool.time,
48         startDate: entitlementPool.startDate,
49         expiryDate: entitlementPool.expiryDate,
50         manufacturerReferenceNumber: entitlementPool.manufacturerReferenceNumber
51     });
52 }
53
54 function putEntitlementPool(
55     licenseModelId,
56     previousEntitlementPool,
57     entitlementPool,
58     version
59 ) {
60     return RestAPIUtil.put(
61         `${baseUrl(licenseModelId, version)}/${entitlementPool.id}`,
62         {
63             name: entitlementPool.name,
64             description: entitlementPool.description,
65             thresholdValue: entitlementPool.thresholdValue,
66             thresholdUnits: getValue(entitlementPool.thresholdUnits),
67             increments: entitlementPool.increments,
68             time: entitlementPool.time,
69             startDate: entitlementPool.startDate,
70             expiryDate: entitlementPool.expiryDate,
71             manufacturerReferenceNumber:
72                 entitlementPool.manufacturerReferenceNumber
73         }
74     );
75 }
76
77 function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
78     return RestAPIUtil.destroy(
79         `${baseUrl(licenseModelId, version)}/${entitlementPoolId}`
80     );
81 }
82
83 function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
84     return RestAPIUtil.fetch(
85         `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`
86     );
87 }
88
89 function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
90     return RestAPIUtil.destroy(
91         `${baseUrl(
92             licenseModelId,
93             version
94         )}/${entitlementPoolId}/limits/${limitId}`
95     );
96 }
97
98 function postLimit(licenseModelId, entitlementPoolId, version, limit) {
99     return RestAPIUtil.post(
100         `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`,
101         {
102             name: limit.name,
103             type: limit.type,
104             description: limit.description,
105             metric: getStrValue(limit.metric),
106             value: limit.value,
107             unit: getStrValue(limit.unit),
108             aggregationFunction: getValue(limit.aggregationFunction),
109             time: getValue(limit.time)
110         }
111     );
112 }
113
114 function putLimit(licenseModelId, entitlementPoolId, version, limit) {
115     return RestAPIUtil.put(
116         `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${
117             limit.id
118         }`,
119         {
120             name: limit.name,
121             type: limit.type,
122             description: limit.description,
123             metric: getStrValue(limit.metric),
124             value: limit.value,
125             unit: getStrValue(limit.unit),
126             aggregationFunction: getValue(limit.aggregationFunction),
127             time: getValue(limit.time)
128         }
129     );
130 }
131
132 const EntitlementPoolsActionHelper = {
133     fetchEntitlementPoolsList(dispatch, { licenseModelId, version }) {
134         return fetchEntitlementPoolsList(licenseModelId, version).then(
135             response =>
136                 dispatch({
137                     type:
138                         entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED,
139                     response
140                 })
141         );
142     },
143
144     openEntitlementPoolsEditor(
145         dispatch,
146         { entitlementPool, licenseModelId, version, isReadOnlyMode } = {}
147     ) {
148         if (licenseModelId && version && entitlementPool) {
149             this.fetchLimits(dispatch, {
150                 licenseModelId,
151                 version,
152                 entitlementPool
153             });
154         }
155         dispatch({
156             type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
157             entitlementPool
158         });
159         dispatch({
160             type: modalActionTypes.GLOBAL_MODAL_SHOW,
161             data: {
162                 modalComponentName: modalContentMapper.EP_EDITOR,
163                 modalComponentProps: {
164                     version,
165                     licenseModelId,
166                     isReadOnlyMode,
167                     size: modalSizes.LARGE
168                 },
169                 title:
170                     licenseModelId && version && entitlementPool
171                         ? i18n('Edit Entitlement Pool')
172                         : i18n('Create New Entitlement Pool')
173             }
174         });
175     },
176
177     async deleteEntitlementPool(
178         dispatch,
179         { licenseModelId, entitlementPoolId, version }
180     ) {
181         await deleteEntitlementPool(licenseModelId, entitlementPoolId, version);
182
183         await ItemsHelper.checkItemStatus(dispatch, {
184             itemId: licenseModelId,
185             versionId: version.id
186         });
187
188         await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
189             licenseModelId,
190             version
191         });
192     },
193
194     entitlementPoolsEditorDataChanged(dispatch, { deltaData }) {
195         dispatch({
196             type:
197                 entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
198             deltaData
199         });
200     },
201
202     closeEntitlementPoolsEditor(dispatch) {
203         dispatch({
204             type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
205         });
206         dispatch({
207             type: modalActionTypes.GLOBAL_MODAL_CLOSE
208         });
209     },
210
211     async saveEntitlementPool(
212         dispatch,
213         { licenseModelId, previousEntitlementPool, entitlementPool, version }
214     ) {
215         if (previousEntitlementPool) {
216             await putEntitlementPool(
217                 licenseModelId,
218                 previousEntitlementPool,
219                 entitlementPool,
220                 version
221             );
222         } else {
223             await postEntitlementPool(licenseModelId, entitlementPool, version);
224         }
225         await ItemsHelper.checkItemStatus(dispatch, {
226             itemId: licenseModelId,
227             versionId: version.id
228         });
229         await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
230             licenseModelId,
231             version
232         });
233     },
234
235     hideDeleteConfirm(dispatch) {
236         dispatch({
237             type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
238             entitlementPoolToDelete: false
239         });
240     },
241     openDeleteEntitlementPoolConfirm(dispatch, { entitlementPool }) {
242         dispatch({
243             type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
244             entitlementPoolToDelete: entitlementPool
245         });
246     },
247
248     fetchLimits(dispatch, { licenseModelId, version, entitlementPool }) {
249         return fetchLimitsList(
250             licenseModelId,
251             entitlementPool.id,
252             version
253         ).then(response => {
254             dispatch({
255                 type:
256                     entitlementPoolsActionTypes.entitlementPoolsEditor
257                         .LIMITS_LIST_LOADED,
258                 response
259             });
260         });
261     },
262
263     submitLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) {
264         const propmise = limit.id
265             ? putLimit(licenseModelId, entitlementPool.id, version, limit)
266             : postLimit(licenseModelId, entitlementPool.id, version, limit);
267         return propmise.then(() => {
268             dispatch({
269                 type: limitEditorActions.CLOSE
270             });
271             this.fetchLimits(dispatch, {
272                 licenseModelId,
273                 version,
274                 entitlementPool
275             });
276             return ItemsHelper.checkItemStatus(dispatch, {
277                 itemId: licenseModelId,
278                 versionId: version.id
279             });
280         });
281     },
282
283     deleteLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) {
284         return deleteLimit(
285             licenseModelId,
286             entitlementPool.id,
287             version,
288             limit.id
289         ).then(() => {
290             this.fetchLimits(dispatch, {
291                 licenseModelId,
292                 version,
293                 entitlementPool
294             });
295             return ItemsHelper.checkItemStatus(dispatch, {
296                 itemId: licenseModelId,
297                 versionId: version.id
298             });
299         });
300     }
301 };
302
303 export default EntitlementPoolsActionHelper;