blob: feceba57260aa22c32dabc1dd93d6cdcf9cd3500 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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,
AviZi280f8012017-06-09 02:39:56 +030012 * 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.
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';
Michael Landoefa037d2017-02-19 12:57:33 +020021
AviZi280f8012017-06-09 02:39:56 +030022function baseUrl(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020023 const restPrefix = Configuration.get('restPrefix');
24 const { id: versionId } = version;
25 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/license-agreements`;
Michael Landoefa037d2017-02-19 12:57:33 +020026}
27
28function fetchLicenseAgreementList(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020029 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020030}
31
talig8e9c0652017-12-20 14:30:43 +020032function fetchLicenseAgreement(licenseModelId, licenseAgreementId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020033 return RestAPIUtil.fetch(
34 `${baseUrl(licenseModelId, version)}/${licenseAgreementId}`
35 );
talig8e9c0652017-12-20 14:30:43 +020036}
37
AviZi280f8012017-06-09 02:39:56 +030038function postLicenseAgreement(licenseModelId, licenseAgreement, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020039 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
40 name: licenseAgreement.name,
41 description: licenseAgreement.description,
42 licenseTerm: licenseAgreement.licenseTerm,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020043 addedFeatureGroupsIds: licenseAgreement.featureGroupsIds
44 });
Michael Landoefa037d2017-02-19 12:57:33 +020045}
46
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020047function putLicenseAgreement(
48 licenseModelId,
49 previousLicenseAgreement,
50 licenseAgreement,
51 version
52) {
53 const { featureGroupsIds = [] } = licenseAgreement;
54 const {
55 featureGroupsIds: prevFeatureGroupsIds = []
56 } = previousLicenseAgreement;
57 return RestAPIUtil.put(
58 `${baseUrl(licenseModelId, version)}/${licenseAgreement.id}`,
59 {
60 name: licenseAgreement.name,
61 description: licenseAgreement.description,
62 licenseTerm: licenseAgreement.licenseTerm,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020063 addedFeatureGroupsIds: featureGroupsIds.filter(
64 featureGroupId =>
65 prevFeatureGroupsIds.indexOf(featureGroupId) === -1
66 ),
67 removedFeatureGroupsIds: prevFeatureGroupsIds.filter(
68 prevFeatureGroupsId =>
69 featureGroupsIds.indexOf(prevFeatureGroupsId) === -1
70 )
71 }
72 );
Michael Landoefa037d2017-02-19 12:57:33 +020073}
74
AviZi280f8012017-06-09 02:39:56 +030075function deleteLicenseAgreement(licenseModelId, licenseAgreementId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020076 return RestAPIUtil.destroy(
77 `${baseUrl(licenseModelId, version)}/${licenseAgreementId}`
78 );
Michael Landoefa037d2017-02-19 12:57:33 +020079}
80
81export default {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020082 fetchLicenseAgreementList(dispatch, { licenseModelId, version }) {
83 return fetchLicenseAgreementList(licenseModelId, version).then(
84 response =>
85 dispatch({
86 type:
87 licenseAgreementActionTypes.LICENSE_AGREEMENT_LIST_LOADED,
88 response
89 })
90 );
91 },
Michael Landoefa037d2017-02-19 12:57:33 +020092
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020093 fetchLicenseAgreement(
94 dispatch,
95 { licenseModelId, licenseAgreementId, version }
96 ) {
97 return fetchLicenseAgreement(
98 licenseModelId,
99 licenseAgreementId,
100 version
101 );
102 },
Michael Landoefa037d2017-02-19 12:57:33 +0200103
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200104 openLicenseAgreementEditor(
105 dispatch,
106 { licenseModelId, licenseAgreement, version }
107 ) {
108 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
109 licenseModelId,
110 version
111 });
112 dispatch({
113 type: licenseAgreementActionTypes.licenseAgreementEditor.OPEN,
114 licenseAgreement
115 });
116 },
talig8e9c0652017-12-20 14:30:43 +0200117
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200118 closeLicenseAgreementEditor(dispatch) {
119 dispatch({
120 type: licenseAgreementActionTypes.licenseAgreementEditor.CLOSE
121 });
122 },
Michael Landoefa037d2017-02-19 12:57:33 +0200123
svishnev57c5c4a2018-04-22 14:14:31 +0300124 async saveLicenseAgreement(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200125 dispatch,
126 { licenseModelId, previousLicenseAgreement, licenseAgreement, version }
127 ) {
128 if (previousLicenseAgreement) {
svishnev57c5c4a2018-04-22 14:14:31 +0300129 await putLicenseAgreement(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200130 licenseModelId,
131 previousLicenseAgreement,
132 licenseAgreement,
133 version
svishnev57c5c4a2018-04-22 14:14:31 +0300134 );
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200135 } else {
svishnev57c5c4a2018-04-22 14:14:31 +0300136 await postLicenseAgreement(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200137 licenseModelId,
138 licenseAgreement,
139 version
svishnev57c5c4a2018-04-22 14:14:31 +0300140 );
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200141 }
svishnev57c5c4a2018-04-22 14:14:31 +0300142 await this.fetchLicenseAgreementList(dispatch, {
143 licenseModelId,
144 version
145 });
146 await FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
147 licenseModelId,
148 version
149 });
150
151 return ItemsHelper.checkItemStatus(dispatch, {
152 itemId: licenseModelId,
153 versionId: version.id
154 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200155 },
Michael Landoefa037d2017-02-19 12:57:33 +0200156
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200157 deleteLicenseAgreement(
158 dispatch,
159 { licenseModelId, licenseAgreementId, version }
160 ) {
161 return deleteLicenseAgreement(
162 licenseModelId,
163 licenseAgreementId,
164 version
165 ).then(() => {
166 dispatch({
167 type: licenseAgreementActionTypes.DELETE_LICENSE_AGREEMENT,
168 licenseAgreementId
169 });
170 return ItemsHelper.checkItemStatus(dispatch, {
171 itemId: licenseModelId,
172 versionId: version.id
173 });
174 });
175 },
Michael Landoefa037d2017-02-19 12:57:33 +0200176
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200177 selectLicenseAgreementEditorTab(dispatch, { tab }) {
178 dispatch({
179 type: licenseAgreementActionTypes.licenseAgreementEditor.SELECT_TAB,
180 tab
181 });
182 }
Michael Landoefa037d2017-02-19 12:57:33 +0200183};