react 16 upgrade
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / licenseKeyGroups / LicenseKeyGroupsActionHelper.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 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import { actionTypes as licenseKeyGroupsConstants } from './LicenseKeyGroupsConstants.js';
19 import { actionTypes as limitEditorActions } from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js';
20 import { default as getValue, getStrValue } from 'nfvo-utils/getValue.js';
21 import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
22 import i18n from 'nfvo-utils/i18n/i18n.js';
23 import {
24     actionTypes as modalActionTypes,
25     modalSizes
26 } from 'nfvo-components/modal/GlobalModalConstants.js';
27 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.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}/license-key-groups`;
33 }
34
35 function fetchLicenseKeyGroupsList(licenseModelId, version) {
36     return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
37 }
38
39 function deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version) {
40     return RestAPIUtil.destroy(
41         `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}`
42     );
43 }
44
45 function postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
46     return RestAPIUtil.post(baseUrl(licenseModelId, version), {
47         name: licenseKeyGroup.name,
48         description: licenseKeyGroup.description,
49         type: licenseKeyGroup.type,
50         increments: licenseKeyGroup.increments,
51         thresholdValue: licenseKeyGroup.thresholdValue,
52         thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
53         startDate: licenseKeyGroup.startDate,
54         expiryDate: licenseKeyGroup.expiryDate,
55         manufacturerReferenceNumber: licenseKeyGroup.manufacturerReferenceNumber
56     });
57 }
58
59 function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
60     return RestAPIUtil.put(
61         `${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`,
62         {
63             name: licenseKeyGroup.name,
64             description: licenseKeyGroup.description,
65             type: licenseKeyGroup.type,
66             increments: licenseKeyGroup.increments,
67             thresholdValue: licenseKeyGroup.thresholdValue,
68             thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
69             startDate: licenseKeyGroup.startDate,
70             expiryDate: licenseKeyGroup.expiryDate,
71             manufacturerReferenceNumber:
72                 licenseKeyGroup.manufacturerReferenceNumber
73         }
74     );
75 }
76
77 function fetchLimitsList(licenseModelId, licenseKeyGroupId, version) {
78     return RestAPIUtil.fetch(
79         `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`
80     );
81 }
82
83 function deleteLimit(licenseModelId, licenseKeyGroupId, version, limitId) {
84     return RestAPIUtil.destroy(
85         `${baseUrl(
86             licenseModelId,
87             version
88         )}/${licenseKeyGroupId}/limits/${limitId}`
89     );
90 }
91
92 function postLimit(licenseModelId, licenseKeyGroupId, version, limit) {
93     return RestAPIUtil.post(
94         `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`,
95         {
96             name: limit.name,
97             type: limit.type,
98             description: limit.description,
99             metric: getStrValue(limit.metric),
100             value: limit.value,
101             unit: getStrValue(limit.unit),
102             aggregationFunction: getValue(limit.aggregationFunction),
103             time: getValue(limit.time)
104         }
105     );
106 }
107
108 function putLimit(licenseModelId, licenseKeyGroupId, version, limit) {
109     return RestAPIUtil.put(
110         `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${
111             limit.id
112         }`,
113         {
114             name: limit.name,
115             type: limit.type,
116             description: limit.description,
117             metric: getStrValue(limit.metric),
118             value: limit.value,
119             unit: getStrValue(limit.unit),
120             aggregationFunction: getValue(limit.aggregationFunction),
121             time: getValue(limit.time)
122         }
123     );
124 }
125
126 export default {
127     fetchLicenseKeyGroupsList(dispatch, { licenseModelId, version }) {
128         return fetchLicenseKeyGroupsList(licenseModelId, version).then(
129             response =>
130                 dispatch({
131                     type:
132                         licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED,
133                     response
134                 })
135         );
136     },
137
138     openLicenseKeyGroupsEditor(
139         dispatch,
140         { licenseKeyGroup, licenseModelId, version, isReadOnlyMode } = {}
141     ) {
142         if (licenseModelId && version && licenseKeyGroup) {
143             this.fetchLimits(dispatch, {
144                 licenseModelId,
145                 version,
146                 licenseKeyGroup
147             });
148         }
149         dispatch({
150             type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN,
151             licenseKeyGroup
152         });
153         dispatch({
154             type: modalActionTypes.GLOBAL_MODAL_SHOW,
155             data: {
156                 modalComponentName: modalContentMapper.LKG_EDITOR,
157                 modalComponentProps: {
158                     version,
159                     licenseModelId,
160                     isReadOnlyMode,
161                     size: modalSizes.LARGE
162                 },
163                 title:
164                     licenseModelId && version && licenseKeyGroup
165                         ? i18n('Edit License Key Group')
166                         : i18n('Create New License Key Group')
167             }
168         });
169     },
170
171     closeLicenseKeyGroupEditor(dispatch) {
172         dispatch({
173             type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE
174         });
175         dispatch({
176             type: modalActionTypes.GLOBAL_MODAL_CLOSE
177         });
178     },
179
180     async saveLicenseKeyGroup(
181         dispatch,
182         { licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version }
183     ) {
184         if (previousLicenseKeyGroup) {
185             await putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version);
186         } else {
187             await postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version);
188         }
189         await ItemsHelper.checkItemStatus(dispatch, {
190             itemId: licenseModelId,
191             versionId: version.id
192         });
193         await this.fetchLicenseKeyGroupsList(dispatch, {
194             licenseModelId,
195             version
196         });
197     },
198
199     async deleteLicenseKeyGroup(
200         dispatch,
201         { licenseModelId, licenseKeyGroupId, version }
202     ) {
203         await deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version);
204         await ItemsHelper.checkItemStatus(dispatch, {
205             itemId: licenseModelId,
206             versionId: version.id
207         });
208         await this.fetchLicenseKeyGroupsList(dispatch, {
209             licenseModelId,
210             version
211         });
212     },
213
214     hideDeleteConfirm(dispatch) {
215         dispatch({
216             type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
217             licenseKeyGroupToDelete: false
218         });
219     },
220
221     openDeleteLicenseAgreementConfirm(dispatch, { licenseKeyGroup }) {
222         dispatch({
223             type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
224             licenseKeyGroupToDelete: licenseKeyGroup
225         });
226     },
227
228     fetchLimits(dispatch, { licenseModelId, version, licenseKeyGroup }) {
229         return fetchLimitsList(
230             licenseModelId,
231             licenseKeyGroup.id,
232             version
233         ).then(response => {
234             dispatch({
235                 type:
236                     licenseKeyGroupsConstants.licenseKeyGroupsEditor
237                         .LIMITS_LIST_LOADED,
238                 response
239             });
240         });
241     },
242
243     submitLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) {
244         const promise = limit.id
245             ? putLimit(licenseModelId, licenseKeyGroup.id, version, limit)
246             : postLimit(licenseModelId, licenseKeyGroup.id, version, limit);
247         return promise.then(() => {
248             dispatch({
249                 type: limitEditorActions.CLOSE
250             });
251             this.fetchLimits(dispatch, {
252                 licenseModelId,
253                 version,
254                 licenseKeyGroup
255             });
256             return ItemsHelper.checkItemStatus(dispatch, {
257                 itemId: licenseModelId,
258                 versionId: version.id
259             });
260         });
261     },
262
263     deleteLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) {
264         return deleteLimit(
265             licenseModelId,
266             licenseKeyGroup.id,
267             version,
268             limit.id
269         ).then(() => {
270             this.fetchLimits(dispatch, {
271                 licenseModelId,
272                 version,
273                 licenseKeyGroup
274             });
275             return ItemsHelper.checkItemStatus(dispatch, {
276                 itemId: licenseModelId,
277                 versionId: version.id
278             });
279         });
280     }
281 };