Add new code new version
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / licenseAgreement / LicenseAgreementActionHelper.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
22 import Configuration from 'sdc-app/config/Configuration.js';
23 import {actionTypes as licenseAgreementActionTypes} from './LicenseAgreementConstants.js';
24 import FeatureGroupsActionHelper from 'sdc-app/onboarding/licenseModel/featureGroups/FeatureGroupsActionHelper.js';
25 import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js';
26
27 function baseUrl(licenseModelId) {
28         const restPrefix = Configuration.get('restPrefix');
29         return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/license-agreements`;
30 }
31
32 function fetchLicenseAgreementList(licenseModelId, version) {
33         let versionQuery = version ? `?version=${version}` : '';
34         return RestAPIUtil.fetch(`${baseUrl(licenseModelId)}${versionQuery}`);
35 }
36
37 function postLicenseAgreement(licenseModelId, licenseAgreement) {
38         return RestAPIUtil.create(baseUrl(licenseModelId), {
39                 name: licenseAgreement.name,
40                 description: licenseAgreement.description,
41                 licenseTerm: licenseAgreement.licenseTerm,
42                 requirementsAndConstrains: licenseAgreement.requirementsAndConstrains,
43                 addedFeatureGroupsIds: licenseAgreement.featureGroupsIds
44         });
45 }
46
47 function putLicenseAgreement(licenseModelId, previousLicenseAgreement, licenseAgreement) {
48         const {featureGroupsIds = []} = licenseAgreement;
49         const {featureGroupsIds: prevFeatureGroupsIds = []} = previousLicenseAgreement;
50         return RestAPIUtil.save(`${baseUrl(licenseModelId)}/${licenseAgreement.id}`, {
51                 name: licenseAgreement.name,
52                 description: licenseAgreement.description,
53                 licenseTerm: licenseAgreement.licenseTerm,
54                 requirementsAndConstrains: licenseAgreement.requirementsAndConstrains,
55                 addedFeatureGroupsIds: featureGroupsIds.filter(featureGroupId => prevFeatureGroupsIds.indexOf(featureGroupId) === -1),
56                 removedFeatureGroupsIds: prevFeatureGroupsIds.filter(prevFeatureGroupsId => featureGroupsIds.indexOf(prevFeatureGroupsId) === -1)
57         });
58 }
59
60 function deleteLicenseAgreement(licenseModelId, licenseAgreementId) {
61         return RestAPIUtil.destroy(`${baseUrl(licenseModelId)}/${licenseAgreementId}`);
62 }
63
64 export default {
65
66         fetchLicenseAgreementList(dispatch, {licenseModelId, version}) {
67                 return fetchLicenseAgreementList(licenseModelId, version).then(response => dispatch({
68                         type: licenseAgreementActionTypes.LICENSE_AGREEMENT_LIST_LOADED,
69                         response
70                 }));
71         },
72
73         openLicenseAgreementEditor(dispatch, {licenseModelId, licenseAgreement}) {
74                 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {licenseModelId});
75                 dispatch({
76                         type: licenseAgreementActionTypes.licenseAgreementEditor.OPEN,
77                         licenseAgreement
78                 });
79         },
80
81         licenseAgreementEditorDataChanged(dispatch, {deltaData}) {
82                 dispatch({
83                         type: licenseAgreementActionTypes.licenseAgreementEditor.DATA_CHANGED,
84                         deltaData
85                 });
86         },
87
88         closeLicenseAgreementEditor(dispatch) {
89                 dispatch({
90                         type: licenseAgreementActionTypes.licenseAgreementEditor.CLOSE
91                 });
92         },
93
94
95         saveLicenseAgreement(dispatch, {licenseModelId, previousLicenseAgreement, licenseAgreement}) {
96                 if (previousLicenseAgreement) {
97                         return putLicenseAgreement(licenseModelId, previousLicenseAgreement, licenseAgreement).then(() => {
98                                 dispatch({
99                                         type: licenseAgreementActionTypes.EDIT_LICENSE_AGREEMENT,
100                                         licenseAgreement
101                                 });
102                         });
103                 }
104                 else {
105                         return postLicenseAgreement(licenseModelId, licenseAgreement).then(response => {
106                                 dispatch({
107                                         type: licenseAgreementActionTypes.ADD_LICENSE_AGREEMENT,
108                                         licenseAgreement: {
109                                                 ...licenseAgreement,
110                                                 id: response.value
111                                         }
112                                 });
113                         });
114                 }
115         },
116
117         deleteLicenseAgreement(dispatch, {licenseModelId, licenseAgreementId}) {
118                 return deleteLicenseAgreement(licenseModelId, licenseAgreementId).then(() => {
119                         dispatch({
120                                 type: licenseAgreementActionTypes.DELETE_LICENSE_AGREEMENT,
121                                 licenseAgreementId
122                         });
123                 });
124         },
125
126         selectLicenseAgreementEditorTab(dispatch, {tab}) {
127                 dispatch({
128                         type: licenseAgreementActionTypes.licenseAgreementEditor.SELECT_TAB,
129                         tab
130                 });
131         },
132
133         selectLicenseAgreementEditorFeatureGroupsButtonTab(dispatch, {buttonTab}) {
134                 dispatch({
135                         type: licenseAgreementActionTypes.licenseAgreementEditor.SELECT_FEATURE_GROUPS_BUTTONTAB,
136                         buttonTab
137                 });
138         },
139
140         hideDeleteConfirm(dispatch) {
141                 dispatch({
142                         type: licenseAgreementActionTypes.LICENSE_AGREEMENT_DELETE_CONFIRM,
143                         licenseAgreementToDelete: false
144                 });
145         },
146
147         openDeleteLicenseAgreementConfirm(dispatch, {licenseAgreement} ) {
148                 dispatch({
149                         type: licenseAgreementActionTypes.LICENSE_AGREEMENT_DELETE_CONFIRM,
150                         licenseAgreementToDelete: licenseAgreement
151                 });
152         },
153
154         switchVersion(dispatch, {licenseModelId, version}) {
155                 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version}).then(() => {
156                         this.fetchLicenseAgreementList(dispatch, {licenseModelId, version});
157                 });
158         }
159 };
160