blob: be33af7910c9a53a305ba517de22562d053e8540 [file] [log] [blame]
svishnev091edfd2018-03-19 12:15:19 +02001/*
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
svishnev091edfd2018-03-19 12:15:19 +02007 *
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,
svishnev091edfd2018-03-19 12:15:19 +020012 * 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 } from './LicenseModelConstants.js';
19import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
20import { actionsEnum as vcActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020021import i18n from 'nfvo-utils/i18n/i18n.js';
AviZi280f8012017-06-09 02:39:56 +030022import LicenseAgreementActionHelper from './licenseAgreement/LicenseAgreementActionHelper.js';
23import FeatureGroupsActionHelper from './featureGroups/FeatureGroupsActionHelper.js';
24import EntitlementPoolsActionHelper from './entitlementPools/EntitlementPoolsActionHelper.js';
25import LicenseKeyGroupsActionHelper from './licenseKeyGroups/LicenseKeyGroupsActionHelper.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020026import { default as ItemsHelper } from 'sdc-app/common/helpers/ItemsHelper.js';
talig8e9c0652017-12-20 14:30:43 +020027import MergeEditorActionHelper from 'sdc-app/common/merge/MergeEditorActionHelper.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020028import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
29import { CommitModalType } from 'nfvo-components/panel/versionController/components/CommitCommentModal.jsx';
talig8e9c0652017-12-20 14:30:43 +020030import versionPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020031import { itemTypes } from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020032import { actionsEnum as VersionControllerActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
svishnevea5e43c2018-04-15 09:06:57 +030033import {
34 itemStatus,
35 versionStatus
36} from 'sdc-app/common/helpers/ItemsHelperConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020037
38function baseUrl() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020039 const restPrefix = Configuration.get('restPrefix');
40 return `${restPrefix}/v1.0/vendor-license-models/`;
Michael Landoefa037d2017-02-19 12:57:33 +020041}
42
43function fetchLicenseModels() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020044 return RestAPIUtil.fetch(
svishnevea5e43c2018-04-15 09:06:57 +030045 `${baseUrl()}?versionFilter=${versionStatus.DRAFT}`
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020046 );
Michael Landoefa037d2017-02-19 12:57:33 +020047}
48
49function fetchFinalizedLicenseModels() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020050 return RestAPIUtil.fetch(
svishnevea5e43c2018-04-15 09:06:57 +030051 `${baseUrl()}?versionFilter=${versionStatus.CERTIFIED}`
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020052 );
Michael Landoefa037d2017-02-19 12:57:33 +020053}
svishnev091edfd2018-03-19 12:15:19 +020054function fetchArchivedLicenseModels() {
svishnevea5e43c2018-04-15 09:06:57 +030055 return RestAPIUtil.fetch(`${baseUrl()}?Status=${itemStatus.ARCHIVED}`);
svishnev091edfd2018-03-19 12:15:19 +020056}
Michael Landoefa037d2017-02-19 12:57:33 +020057function fetchLicenseModelById(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020058 const { id: versionId } = version;
59 return RestAPIUtil.fetch(
60 `${baseUrl()}${licenseModelId}/versions/${versionId}`
61 );
Michael Landoefa037d2017-02-19 12:57:33 +020062}
63
AviZi280f8012017-06-09 02:39:56 +030064function putLicenseModel(licenseModel) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020065 let {
66 id,
67 vendorName,
68 description,
69 iconRef,
70 version: { id: versionId }
71 } = licenseModel;
72 return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}`, {
73 vendorName,
74 description,
75 iconRef
76 });
AviZi280f8012017-06-09 02:39:56 +030077}
78
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020079function putLicenseModelAction({ itemId, action, version }) {
80 const { id: versionId } = version;
81 return RestAPIUtil.put(
82 `${baseUrl()}${itemId}/versions/${versionId}/actions`,
83 { action: action }
84 );
Michael Landoefa037d2017-02-19 12:57:33 +020085}
86
87const LicenseModelActionHelper = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020088 fetchLicenseModels(dispatch) {
89 return fetchLicenseModels().then(response => {
90 dispatch({
91 type: actionTypes.LICENSE_MODELS_LIST_LOADED,
92 response
93 });
94 });
95 },
Michael Landoefa037d2017-02-19 12:57:33 +020096
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020097 fetchFinalizedLicenseModels(dispatch) {
98 return fetchFinalizedLicenseModels().then(response =>
99 dispatch({
100 type: actionTypes.FINALIZED_LICENSE_MODELS_LIST_LOADED,
101 response
102 })
103 );
104 },
Michael Landoefa037d2017-02-19 12:57:33 +0200105
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200106 fetchArchivedLicenseModels(dispatch) {
107 return fetchArchivedLicenseModels().then(response =>
108 dispatch({
109 type: actionTypes.ARCHIVED_LICENSE_MODELS_LIST_LOADED,
110 response
111 })
112 );
113 },
Michael Landoefa037d2017-02-19 12:57:33 +0200114
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200115 fetchLicenseModelById(dispatch, { licenseModelId, version }) {
116 return fetchLicenseModelById(licenseModelId, version).then(response => {
117 dispatch({
118 type: actionTypes.LICENSE_MODEL_LOADED,
119 response: { ...response, version }
120 });
121 });
122 },
Michael Landoefa037d2017-02-19 12:57:33 +0200123
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200124 fetchLicenseModelItems(dispatch, { licenseModelId, version }) {
125 return Promise.all([
126 LicenseAgreementActionHelper.fetchLicenseAgreementList(dispatch, {
127 licenseModelId,
128 version
129 }),
130 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
131 licenseModelId,
132 version
133 }),
134 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
135 licenseModelId,
136 version
137 }),
138 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {
139 licenseModelId,
140 version
141 })
142 ]);
143 },
svishnev091edfd2018-03-19 12:15:19 +0200144
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200145 manageSubmitAction(dispatch, { licenseModelId, version, isDirty }) {
146 if (isDirty) {
147 const onCommit = comment => {
148 return this.performVCAction(dispatch, {
149 licenseModelId,
150 action: vcActionsEnum.COMMIT,
151 version,
152 comment
153 }).then(() => {
154 return this.performSubmitAction(dispatch, {
155 licenseModelId,
156 version
157 });
158 });
159 };
160 dispatch({
161 type: modalActionTypes.GLOBAL_MODAL_SHOW,
162 data: {
163 modalComponentName: modalContentMapper.COMMIT_COMMENT,
164 modalComponentProps: {
165 onCommit,
166 type: CommitModalType.COMMIT_SUBMIT
167 },
168 title: i18n('Commit & Submit')
169 }
170 });
171 return Promise.reject();
172 }
173 return this.performSubmitAction(dispatch, { licenseModelId, version });
174 },
svishnev091edfd2018-03-19 12:15:19 +0200175
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200176 performSubmitAction(dispatch, { licenseModelId, version }) {
177 return putLicenseModelAction({
178 itemId: licenseModelId,
179 action: vcActionsEnum.SUBMIT,
180 version
181 }).then(() => {
182 return ItemsHelper.checkItemStatus(dispatch, {
183 itemId: licenseModelId,
184 versionId: version.id
185 }).then(updatedVersion => {
186 dispatch({
187 type: modalActionTypes.GLOBAL_MODAL_SUCCESS,
188 data: {
189 title: i18n('Submit Succeeded'),
190 msg: i18n('This license model successfully submitted'),
191 cancelButtonText: i18n('OK'),
192 timeout: 2000
193 }
194 });
195 versionPageActionHelper.fetchVersions(dispatch, {
196 itemType: itemTypes.LICENSE_MODEL,
197 itemId: licenseModelId
198 });
199 return Promise.resolve(updatedVersion);
200 });
201 });
202 },
talig8e9c0652017-12-20 14:30:43 +0200203
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200204 performVCAction(dispatch, { licenseModelId, action, version, comment }) {
205 return MergeEditorActionHelper.analyzeSyncResult(dispatch, {
206 itemId: licenseModelId,
207 version
208 }).then(({ inMerge, isDirty, updatedVersion }) => {
209 if (
svishnevea5e43c2018-04-15 09:06:57 +0300210 (updatedVersion.status === versionStatus.CERTIFIED ||
211 updatedVersion.archivedStatus === versionStatus.ARCHIVED) &&
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200212 (action === VersionControllerActionsEnum.COMMIT ||
213 action === VersionControllerActionsEnum.SYNC)
214 ) {
215 versionPageActionHelper.fetchVersions(dispatch, {
216 itemType: itemTypes.LICENSE_MODEL,
217 itemId: licenseModelId
218 });
219 const msg =
svishnevea5e43c2018-04-15 09:06:57 +0300220 updatedVersion.archivedStatus === versionStatus.ARCHIVED
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200221 ? i18n('Item was Archived')
222 : i18n('Item version already Certified');
223 dispatch({
224 type: modalActionTypes.GLOBAL_MODAL_WARNING,
225 data: {
226 title: i18n('Commit error'),
227 msg,
228 cancelButtonText: i18n('Cancel')
229 }
230 });
231 return Promise.resolve(updatedVersion);
232 }
233 if (!inMerge) {
234 if (action === vcActionsEnum.SUBMIT) {
235 return this.manageSubmitAction(dispatch, {
236 licenseModelId,
237 version,
238 isDirty
239 });
240 } else {
241 return ItemsHelper.performVCAction({
242 itemId: licenseModelId,
243 action,
244 version,
245 comment
246 }).then(() => {
247 versionPageActionHelper.fetchVersions(dispatch, {
248 itemType: itemTypes.LICENSE_MODEL,
249 itemId: licenseModelId
250 });
251 if (action === vcActionsEnum.SYNC) {
252 return MergeEditorActionHelper.analyzeSyncResult(
253 dispatch,
254 { itemId: licenseModelId, version }
255 ).then(({ updatedVersion }) => {
256 return Promise.resolve(updatedVersion);
257 });
258 } else {
259 return ItemsHelper.checkItemStatus(dispatch, {
260 itemId: licenseModelId,
261 versionId: version.id
262 });
263 }
264 });
265 }
266 }
267 });
268 },
Michael Landoefa037d2017-02-19 12:57:33 +0200269
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200270 saveLicenseModel(dispatch, { licenseModel }) {
271 return putLicenseModel(licenseModel).then(() => {
272 dispatch({
273 type: actionTypes.LICENSE_MODEL_LOADED,
274 response: licenseModel
275 });
276 const { id, version: { id: versionId } } = licenseModel;
277 return ItemsHelper.checkItemStatus(dispatch, {
278 itemId: id,
279 versionId
280 }).then(updatedVersion => {
281 if (updatedVersion.status !== licenseModel.version.status) {
282 versionPageActionHelper.fetchVersions(dispatch, {
283 itemType: itemTypes.LICENSE_MODEL,
284 itemId: licenseModel.id
285 });
286 }
287 });
288 });
289 }
Michael Landoefa037d2017-02-19 12:57:33 +0200290};
291
292export default LicenseModelActionHelper;