Adding Prettier and fixing up eslint version
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / licenseAgreement / LicenseAgreementActionHelper.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 licenseAgreementActionTypes } from './LicenseAgreementConstants.js';
19 import FeatureGroupsActionHelper from 'sdc-app/onboarding/licenseModel/featureGroups/FeatureGroupsActionHelper.js';
20 import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
21
22 function baseUrl(licenseModelId, version) {
23     const restPrefix = Configuration.get('restPrefix');
24     const { id: versionId } = version;
25     return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/license-agreements`;
26 }
27
28 function fetchLicenseAgreementList(licenseModelId, version) {
29     return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
30 }
31
32 function fetchLicenseAgreement(licenseModelId, licenseAgreementId, version) {
33     return RestAPIUtil.fetch(
34         `${baseUrl(licenseModelId, version)}/${licenseAgreementId}`
35     );
36 }
37
38 function postLicenseAgreement(licenseModelId, licenseAgreement, version) {
39     return RestAPIUtil.post(baseUrl(licenseModelId, version), {
40         name: licenseAgreement.name,
41         description: licenseAgreement.description,
42         licenseTerm: licenseAgreement.licenseTerm,
43         requirementsAndConstrains: licenseAgreement.requirementsAndConstrains,
44         addedFeatureGroupsIds: licenseAgreement.featureGroupsIds
45     });
46 }
47
48 function putLicenseAgreement(
49     licenseModelId,
50     previousLicenseAgreement,
51     licenseAgreement,
52     version
53 ) {
54     const { featureGroupsIds = [] } = licenseAgreement;
55     const {
56         featureGroupsIds: prevFeatureGroupsIds = []
57     } = previousLicenseAgreement;
58     return RestAPIUtil.put(
59         `${baseUrl(licenseModelId, version)}/${licenseAgreement.id}`,
60         {
61             name: licenseAgreement.name,
62             description: licenseAgreement.description,
63             licenseTerm: licenseAgreement.licenseTerm,
64             requirementsAndConstrains:
65                 licenseAgreement.requirementsAndConstrains,
66             addedFeatureGroupsIds: featureGroupsIds.filter(
67                 featureGroupId =>
68                     prevFeatureGroupsIds.indexOf(featureGroupId) === -1
69             ),
70             removedFeatureGroupsIds: prevFeatureGroupsIds.filter(
71                 prevFeatureGroupsId =>
72                     featureGroupsIds.indexOf(prevFeatureGroupsId) === -1
73             )
74         }
75     );
76 }
77
78 function deleteLicenseAgreement(licenseModelId, licenseAgreementId, version) {
79     return RestAPIUtil.destroy(
80         `${baseUrl(licenseModelId, version)}/${licenseAgreementId}`
81     );
82 }
83
84 export default {
85     fetchLicenseAgreementList(dispatch, { licenseModelId, version }) {
86         return fetchLicenseAgreementList(licenseModelId, version).then(
87             response =>
88                 dispatch({
89                     type:
90                         licenseAgreementActionTypes.LICENSE_AGREEMENT_LIST_LOADED,
91                     response
92                 })
93         );
94     },
95
96     fetchLicenseAgreement(
97         dispatch,
98         { licenseModelId, licenseAgreementId, version }
99     ) {
100         return fetchLicenseAgreement(
101             licenseModelId,
102             licenseAgreementId,
103             version
104         );
105     },
106
107     openLicenseAgreementEditor(
108         dispatch,
109         { licenseModelId, licenseAgreement, version }
110     ) {
111         FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
112             licenseModelId,
113             version
114         });
115         dispatch({
116             type: licenseAgreementActionTypes.licenseAgreementEditor.OPEN,
117             licenseAgreement
118         });
119     },
120
121     closeLicenseAgreementEditor(dispatch) {
122         dispatch({
123             type: licenseAgreementActionTypes.licenseAgreementEditor.CLOSE
124         });
125     },
126
127     saveLicenseAgreement(
128         dispatch,
129         { licenseModelId, previousLicenseAgreement, licenseAgreement, version }
130     ) {
131         if (previousLicenseAgreement) {
132             return putLicenseAgreement(
133                 licenseModelId,
134                 previousLicenseAgreement,
135                 licenseAgreement,
136                 version
137             ).then(() => {
138                 this.fetchLicenseAgreementList(dispatch, {
139                     licenseModelId,
140                     version
141                 });
142                 return ItemsHelper.checkItemStatus(dispatch, {
143                     itemId: licenseModelId,
144                     versionId: version.id
145                 });
146             });
147         } else {
148             return postLicenseAgreement(
149                 licenseModelId,
150                 licenseAgreement,
151                 version
152             ).then(() => {
153                 this.fetchLicenseAgreementList(dispatch, {
154                     licenseModelId,
155                     version
156                 });
157                 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
158                     licenseModelId,
159                     version
160                 });
161                 return ItemsHelper.checkItemStatus(dispatch, {
162                     itemId: licenseModelId,
163                     versionId: version.id
164                 });
165             });
166         }
167     },
168
169     deleteLicenseAgreement(
170         dispatch,
171         { licenseModelId, licenseAgreementId, version }
172     ) {
173         return deleteLicenseAgreement(
174             licenseModelId,
175             licenseAgreementId,
176             version
177         ).then(() => {
178             dispatch({
179                 type: licenseAgreementActionTypes.DELETE_LICENSE_AGREEMENT,
180                 licenseAgreementId
181             });
182             return ItemsHelper.checkItemStatus(dispatch, {
183                 itemId: licenseModelId,
184                 versionId: version.id
185             });
186         });
187     },
188
189     selectLicenseAgreementEditorTab(dispatch, { tab }) {
190         dispatch({
191             type: licenseAgreementActionTypes.licenseAgreementEditor.SELECT_TAB,
192             tab
193         });
194     }
195 };