blob: d1d3a77d9255e9f84be091503ede77574c7d367d [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';
18import {actionTypes} from './LicenseModelConstants.js';
AviZi280f8012017-06-09 02:39:56 +030019import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020020import {actionsEnum as vcActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
21import 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';
svishnev091edfd2018-03-19 12:15:19 +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';
28import {modalContentMapper} from 'sdc-app/common/modal/ModalContentMapper.js';
29import {CommitModalType} from 'nfvo-components/panel/versionController/components/CommitCommentModal.jsx';
30import versionPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js';
31import {itemTypes} from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js';
32import {catalogItemStatuses} from 'sdc-app/onboarding/onboard/onboardingCatalog/OnboardingCatalogConstants.js';
33import {actionsEnum as VersionControllerActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020034
35function baseUrl() {
36 const restPrefix = Configuration.get('restPrefix');
37 return `${restPrefix}/v1.0/vendor-license-models/`;
38}
39
40function fetchLicenseModels() {
svishnev091edfd2018-03-19 12:15:19 +020041 return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=${catalogItemStatuses.DRAFT}`);
Michael Landoefa037d2017-02-19 12:57:33 +020042}
43
44function fetchFinalizedLicenseModels() {
svishnev091edfd2018-03-19 12:15:19 +020045 return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=${catalogItemStatuses.CERTIFIED}`);
Michael Landoefa037d2017-02-19 12:57:33 +020046}
svishnev091edfd2018-03-19 12:15:19 +020047function fetchArchivedLicenseModels() {
48 return RestAPIUtil.fetch(`${baseUrl()}?Status=${catalogItemStatuses.ARCHIVED}`);
49}
Michael Landoefa037d2017-02-19 12:57:33 +020050function fetchLicenseModelById(licenseModelId, version) {
AviZi280f8012017-06-09 02:39:56 +030051 const {id: versionId} = version;
52 return RestAPIUtil.fetch(`${baseUrl()}${licenseModelId}/versions/${versionId}`);
Michael Landoefa037d2017-02-19 12:57:33 +020053}
54
AviZi280f8012017-06-09 02:39:56 +030055function putLicenseModel(licenseModel) {
56 let {id, vendorName, description, iconRef, version: {id: versionId}} = licenseModel;
57 return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}`, {
58 vendorName,
59 description,
60 iconRef
61 });
62}
63
talig8e9c0652017-12-20 14:30:43 +020064function putLicenseModelAction({itemId, action, version}) {
65 const {id: versionId} = version;
66 return RestAPIUtil.put(`${baseUrl()}${itemId}/versions/${versionId}/actions`, {action: action});
Michael Landoefa037d2017-02-19 12:57:33 +020067}
68
69const LicenseModelActionHelper = {
70
71 fetchLicenseModels(dispatch) {
72 return fetchLicenseModels().then(response => {
73 dispatch({
74 type: actionTypes.LICENSE_MODELS_LIST_LOADED,
75 response
76 });
77 });
78 },
79
80 fetchFinalizedLicenseModels(dispatch) {
81 return fetchFinalizedLicenseModels().then(response => dispatch({
82 type: actionTypes.FINALIZED_LICENSE_MODELS_LIST_LOADED,
83 response
84 }));
85
86 },
87
svishnev091edfd2018-03-19 12:15:19 +020088 fetchArchivedLicenseModels(dispatch) {
89 return fetchArchivedLicenseModels().then(response => dispatch({
90 type: actionTypes.ARCHIVED_LICENSE_MODELS_LIST_LOADED,
91 response
92 }));
93
94 },
95
Michael Landoefa037d2017-02-19 12:57:33 +020096 fetchLicenseModelById(dispatch, {licenseModelId, version}) {
talig8e9c0652017-12-20 14:30:43 +020097
98 return fetchLicenseModelById(licenseModelId, version).then(response => {
Michael Landoefa037d2017-02-19 12:57:33 +020099 dispatch({
100 type: actionTypes.LICENSE_MODEL_LOADED,
AviZi280f8012017-06-09 02:39:56 +0300101 response: {...response, version}
Michael Landoefa037d2017-02-19 12:57:33 +0200102 });
103 });
104 },
105
AviZi280f8012017-06-09 02:39:56 +0300106 fetchLicenseModelItems(dispatch, {licenseModelId, version}) {
107 return Promise.all([
108 LicenseAgreementActionHelper.fetchLicenseAgreementList(dispatch, {licenseModelId, version}),
109 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {licenseModelId, version}),
110 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {licenseModelId, version}),
111 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version})
112 ]);
113 },
114
talig8e9c0652017-12-20 14:30:43 +0200115 manageSubmitAction(dispatch, {licenseModelId, version, isDirty}) {
116 if(isDirty) {
117 const onCommit = comment => {
118 return this.performVCAction(dispatch, {licenseModelId, action: vcActionsEnum.COMMIT, version, comment}).then(() => {
119 return this.performSubmitAction(dispatch, {licenseModelId, version});
120 });
121 };
122 dispatch({
123 type: modalActionTypes.GLOBAL_MODAL_SHOW,
124 data: {
125 modalComponentName: modalContentMapper.COMMIT_COMMENT,
126 modalComponentProps: {
127 onCommit,
128 type: CommitModalType.COMMIT_SUBMIT
129 },
130 title: i18n('Commit & Submit')
131 }
132 });
133 return Promise.reject();
134 }
135 return this.performSubmitAction(dispatch, {licenseModelId, version});
136 },
137
138 performSubmitAction(dispatch, {licenseModelId, version}) {
139 return putLicenseModelAction({itemId: licenseModelId, action: vcActionsEnum.SUBMIT, version}).then(() => {
140 return ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id}).then(updatedVersion => {
Michael Landoefa037d2017-02-19 12:57:33 +0200141 dispatch({
AviZi280f8012017-06-09 02:39:56 +0300142 type: modalActionTypes.GLOBAL_MODAL_SUCCESS,
143 data: {
talig8e9c0652017-12-20 14:30:43 +0200144 title: i18n('Submit Succeeded'),
AviZi280f8012017-06-09 02:39:56 +0300145 msg: i18n('This license model successfully submitted'),
talig8e9c0652017-12-20 14:30:43 +0200146 cancelButtonText: i18n('OK'),
AviZi280f8012017-06-09 02:39:56 +0300147 timeout: 2000
148 }
Michael Landoefa037d2017-02-19 12:57:33 +0200149 });
talig8e9c0652017-12-20 14:30:43 +0200150 versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId});
151 return Promise.resolve(updatedVersion);
152 });
AviZi280f8012017-06-09 02:39:56 +0300153 });
154 },
155
talig8e9c0652017-12-20 14:30:43 +0200156 performVCAction(dispatch, {licenseModelId, action, version, comment}) {
157 return MergeEditorActionHelper.analyzeSyncResult(dispatch, {itemId: licenseModelId, version}).then(({inMerge, isDirty, updatedVersion}) => {
svishnev091edfd2018-03-19 12:15:19 +0200158 if ( (updatedVersion.status === catalogItemStatuses.CERTIFIED || updatedVersion.archivedStatus === catalogItemStatuses.ARCHIVED) &&
talig8e9c0652017-12-20 14:30:43 +0200159 (action === VersionControllerActionsEnum.COMMIT || action === VersionControllerActionsEnum.SYNC)) {
160 versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId});
svishnev091edfd2018-03-19 12:15:19 +0200161 const msg = updatedVersion.archivedStatus === catalogItemStatuses.ARCHIVED ? i18n('Item was Archived') : i18n('Item version already Certified');
talig8e9c0652017-12-20 14:30:43 +0200162 dispatch({
163 type: modalActionTypes.GLOBAL_MODAL_WARNING,
164 data: {
165 title: i18n('Commit error'),
svishnev091edfd2018-03-19 12:15:19 +0200166 msg,
talig8e9c0652017-12-20 14:30:43 +0200167 cancelButtonText: i18n('Cancel')
168 }
169 });
170 return Promise.resolve(updatedVersion);
171 }
172 if (!inMerge) {
173 if(action === vcActionsEnum.SUBMIT) {
174 return this.manageSubmitAction(dispatch, {licenseModelId, version, isDirty});
175 }
176 else {
177 return ItemsHelper.performVCAction({itemId: licenseModelId, action, version, comment}).then(() => {
178 versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId});
179 if (action === vcActionsEnum.SYNC) {
180 return MergeEditorActionHelper.analyzeSyncResult(dispatch, {itemId: licenseModelId, version}).then(({updatedVersion}) => {
181 return Promise.resolve(updatedVersion);
182 });
183 } else {
184 return ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
185 }
186 });
187 }
188 }
AviZi280f8012017-06-09 02:39:56 +0300189 });
190 },
191
192 saveLicenseModel(dispatch, {licenseModel}) {
193 return putLicenseModel(licenseModel).then(() => {
194 dispatch({
AviZi280f8012017-06-09 02:39:56 +0300195 type: actionTypes.LICENSE_MODEL_LOADED,
196 response: licenseModel
197 });
talig8e9c0652017-12-20 14:30:43 +0200198 const {id, version: {id: versionId}} = licenseModel;
199 return ItemsHelper.checkItemStatus(dispatch, {itemId: id, versionId}).then(updatedVersion => {
200 if (updatedVersion.status !== licenseModel.version.status) {
201 versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModel.id});
202 }
203 });
Michael Landoefa037d2017-02-19 12:57:33 +0200204 });
205 }
AviZi280f8012017-06-09 02:39:56 +0300206
Michael Landoefa037d2017-02-19 12:57:33 +0200207};
208
209export default LicenseModelActionHelper;