blob: 036aaaa68691d92dd678373a5ad34e4682c3dffe [file] [log] [blame]
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03001/*
2 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03008 * http://www.apache.org/licenses/LICENSE-2.0
AviZi280f8012017-06-09 02:39:56 +03009 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17import Configuration from 'sdc-app/config/Configuration.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020018import { actionTypes as licenseAgreementActionTypes } from './LicenseAgreementConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020019import FeatureGroupsActionHelper from 'sdc-app/onboarding/licenseModel/featureGroups/FeatureGroupsActionHelper.js';
talig8e9c0652017-12-20 14:30:43 +020020import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030021import {
22 actionTypes as modalActionTypes,
23 modalSizes
24} from 'nfvo-components/modal/GlobalModalConstants.js';
25import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
26import i18n from 'nfvo-utils/i18n/i18n.js';
Michael Landoefa037d2017-02-19 12:57:33 +020027
AviZi280f8012017-06-09 02:39:56 +030028function baseUrl(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020029 const restPrefix = Configuration.get('restPrefix');
30 const { id: versionId } = version;
31 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/license-agreements`;
Michael Landoefa037d2017-02-19 12:57:33 +020032}
33
34function fetchLicenseAgreementList(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020035 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020036}
37
talig8e9c0652017-12-20 14:30:43 +020038function fetchLicenseAgreement(licenseModelId, licenseAgreementId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020039 return RestAPIUtil.fetch(
40 `${baseUrl(licenseModelId, version)}/${licenseAgreementId}`
41 );
talig8e9c0652017-12-20 14:30:43 +020042}
43
AviZi280f8012017-06-09 02:39:56 +030044function postLicenseAgreement(licenseModelId, licenseAgreement, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020045 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
46 name: licenseAgreement.name,
47 description: licenseAgreement.description,
48 licenseTerm: licenseAgreement.licenseTerm,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020049 addedFeatureGroupsIds: licenseAgreement.featureGroupsIds
50 });
Michael Landoefa037d2017-02-19 12:57:33 +020051}
52
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020053function putLicenseAgreement(
54 licenseModelId,
55 previousLicenseAgreement,
56 licenseAgreement,
57 version
58) {
59 const { featureGroupsIds = [] } = licenseAgreement;
60 const {
61 featureGroupsIds: prevFeatureGroupsIds = []
62 } = previousLicenseAgreement;
63 return RestAPIUtil.put(
64 `${baseUrl(licenseModelId, version)}/${licenseAgreement.id}`,
65 {
66 name: licenseAgreement.name,
67 description: licenseAgreement.description,
68 licenseTerm: licenseAgreement.licenseTerm,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020069 addedFeatureGroupsIds: featureGroupsIds.filter(
70 featureGroupId =>
71 prevFeatureGroupsIds.indexOf(featureGroupId) === -1
72 ),
73 removedFeatureGroupsIds: prevFeatureGroupsIds.filter(
74 prevFeatureGroupsId =>
75 featureGroupsIds.indexOf(prevFeatureGroupsId) === -1
76 )
77 }
78 );
Michael Landoefa037d2017-02-19 12:57:33 +020079}
80
AviZi280f8012017-06-09 02:39:56 +030081function deleteLicenseAgreement(licenseModelId, licenseAgreementId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020082 return RestAPIUtil.destroy(
83 `${baseUrl(licenseModelId, version)}/${licenseAgreementId}`
84 );
Michael Landoefa037d2017-02-19 12:57:33 +020085}
86
87export default {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020088 fetchLicenseAgreementList(dispatch, { licenseModelId, version }) {
89 return fetchLicenseAgreementList(licenseModelId, version).then(
90 response =>
91 dispatch({
92 type:
93 licenseAgreementActionTypes.LICENSE_AGREEMENT_LIST_LOADED,
94 response
95 })
96 );
97 },
Michael Landoefa037d2017-02-19 12:57:33 +020098
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020099 fetchLicenseAgreement(
100 dispatch,
101 { licenseModelId, licenseAgreementId, version }
102 ) {
103 return fetchLicenseAgreement(
104 licenseModelId,
105 licenseAgreementId,
106 version
107 );
108 },
Michael Landoefa037d2017-02-19 12:57:33 +0200109
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200110 openLicenseAgreementEditor(
111 dispatch,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300112 { licenseModelId, licenseAgreement, version, isReadOnlyMode }
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200113 ) {
114 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
115 licenseModelId,
116 version
117 });
118 dispatch({
119 type: licenseAgreementActionTypes.licenseAgreementEditor.OPEN,
120 licenseAgreement
121 });
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300122 dispatch({
123 type: modalActionTypes.GLOBAL_MODAL_SHOW,
124 data: {
125 modalComponentName: modalContentMapper.LA_EDITOR,
126 modalComponentProps: {
127 version,
128 licenseModelId,
129 isReadOnlyMode,
130 size: modalSizes.LARGE
131 },
132 title:
133 licenseModelId && version && licenseAgreement
134 ? i18n('Edit License Agreement')
135 : i18n('Create New License Agreement')
136 }
137 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200138 },
talig8e9c0652017-12-20 14:30:43 +0200139
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200140 closeLicenseAgreementEditor(dispatch) {
141 dispatch({
142 type: licenseAgreementActionTypes.licenseAgreementEditor.CLOSE
143 });
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300144 dispatch({
145 type: modalActionTypes.GLOBAL_MODAL_CLOSE
146 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200147 },
Michael Landoefa037d2017-02-19 12:57:33 +0200148
svishnev57c5c4a2018-04-22 14:14:31 +0300149 async saveLicenseAgreement(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200150 dispatch,
151 { licenseModelId, previousLicenseAgreement, licenseAgreement, version }
152 ) {
153 if (previousLicenseAgreement) {
svishnev57c5c4a2018-04-22 14:14:31 +0300154 await putLicenseAgreement(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200155 licenseModelId,
156 previousLicenseAgreement,
157 licenseAgreement,
158 version
svishnev57c5c4a2018-04-22 14:14:31 +0300159 );
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200160 } else {
svishnev57c5c4a2018-04-22 14:14:31 +0300161 await postLicenseAgreement(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200162 licenseModelId,
163 licenseAgreement,
164 version
svishnev57c5c4a2018-04-22 14:14:31 +0300165 );
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200166 }
svishnev57c5c4a2018-04-22 14:14:31 +0300167 await this.fetchLicenseAgreementList(dispatch, {
168 licenseModelId,
169 version
170 });
171 await FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
172 licenseModelId,
173 version
174 });
175
176 return ItemsHelper.checkItemStatus(dispatch, {
177 itemId: licenseModelId,
178 versionId: version.id
179 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200180 },
Michael Landoefa037d2017-02-19 12:57:33 +0200181
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200182 deleteLicenseAgreement(
183 dispatch,
184 { licenseModelId, licenseAgreementId, version }
185 ) {
186 return deleteLicenseAgreement(
187 licenseModelId,
188 licenseAgreementId,
189 version
190 ).then(() => {
191 dispatch({
192 type: licenseAgreementActionTypes.DELETE_LICENSE_AGREEMENT,
193 licenseAgreementId
194 });
195 return ItemsHelper.checkItemStatus(dispatch, {
196 itemId: licenseModelId,
197 versionId: version.id
198 });
199 });
200 },
Michael Landoefa037d2017-02-19 12:57:33 +0200201
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200202 selectLicenseAgreementEditorTab(dispatch, { tab }) {
203 dispatch({
204 type: licenseAgreementActionTypes.licenseAgreementEditor.SELECT_TAB,
205 tab
206 });
207 }
Michael Landoefa037d2017-02-19 12:57:33 +0200208};