[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / licenseKeyGroups / LicenseKeyGroupsActionHelper.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 licenseKeyGroupsConstants} from './LicenseKeyGroupsConstants.js';
19 import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js';
20
21 function baseUrl(licenseModelId, version) {
22         const restPrefix = Configuration.get('restPrefix');
23         const {id: versionId} = version;
24         return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/license-key-groups`;
25 }
26
27 function fetchLicenseKeyGroupsList(licenseModelId, version) {
28         return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
29 }
30
31 function deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version) {
32         return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}`);
33 }
34
35 function postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
36         return RestAPIUtil.post(baseUrl(licenseModelId, version), {
37                 name: licenseKeyGroup.name,
38                 description: licenseKeyGroup.description,
39                 operationalScope: licenseKeyGroup.operationalScope,
40                 type: licenseKeyGroup.type
41         });
42 }
43
44 function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
45         return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`, {
46                 name: licenseKeyGroup.name,
47                 description: licenseKeyGroup.description,
48                 operationalScope: licenseKeyGroup.operationalScope,
49                 type: licenseKeyGroup.type
50         });
51 }
52
53
54 export default {
55         fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version}) {
56                 return fetchLicenseKeyGroupsList(licenseModelId, version).then(response => dispatch({
57                         type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED,
58                         response
59                 }));
60         },
61
62         openLicenseKeyGroupsEditor(dispatch, {licenseKeyGroup} = {}) {
63                 dispatch({
64                         type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN,
65                         licenseKeyGroup
66                 });
67         },
68
69         closeLicenseKeyGroupEditor(dispatch){
70                 dispatch({
71                         type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE
72                 });
73         },
74
75         saveLicenseKeyGroup(dispatch, {licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version}) {
76                 if (previousLicenseKeyGroup) {
77                         return putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version).then(() => {
78                                 dispatch({
79                                         type: licenseKeyGroupsConstants.EDIT_LICENSE_KEY_GROUP,
80                                         licenseKeyGroup
81                                 });
82                         });
83                 }
84                 else {
85                         return postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version).then(response => {
86                                 dispatch({
87                                         type: licenseKeyGroupsConstants.ADD_LICENSE_KEY_GROUP,
88                                         licenseKeyGroup: {
89                                                 ...licenseKeyGroup,
90                                                 referencingFeatureGroups: [],
91                                                 id: response.value
92                                         }
93                                 });
94                         });
95                 }
96
97
98         },
99
100         deleteLicenseKeyGroup(dispatch, {licenseModelId, licenseKeyGroupId, version}){
101                 return deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version).then(()=> {
102                         dispatch({
103                                 type: licenseKeyGroupsConstants.DELETE_LICENSE_KEY_GROUP,
104                                 licenseKeyGroupId
105                         });
106                 });
107         },
108
109         hideDeleteConfirm(dispatch) {
110                 dispatch({
111                         type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
112                         licenseKeyGroupToDelete: false
113                 });
114         },
115
116         openDeleteLicenseAgreementConfirm(dispatch, {licenseKeyGroup}) {
117                 dispatch({
118                         type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
119                         licenseKeyGroupToDelete: licenseKeyGroup
120                 });
121         },
122
123         switchVersion(dispatch, {licenseModelId, version}) {
124                 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version}).then(() => {
125                         this.fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version});
126                 });
127         }
128 };