svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016-2018 European Support Limited |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 12 | * 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js'; |
| 17 | import Configuration from 'sdc-app/config/Configuration.js'; |
| 18 | import {actionTypes} from './LicenseModelConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 19 | import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 20 | import {actionsEnum as vcActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js'; |
| 21 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 22 | import LicenseAgreementActionHelper from './licenseAgreement/LicenseAgreementActionHelper.js'; |
| 23 | import FeatureGroupsActionHelper from './featureGroups/FeatureGroupsActionHelper.js'; |
| 24 | import EntitlementPoolsActionHelper from './entitlementPools/EntitlementPoolsActionHelper.js'; |
| 25 | import LicenseKeyGroupsActionHelper from './licenseKeyGroups/LicenseKeyGroupsActionHelper.js'; |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 26 | import {default as ItemsHelper} from 'sdc-app/common/helpers/ItemsHelper.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 27 | import MergeEditorActionHelper from 'sdc-app/common/merge/MergeEditorActionHelper.js'; |
| 28 | import {modalContentMapper} from 'sdc-app/common/modal/ModalContentMapper.js'; |
| 29 | import {CommitModalType} from 'nfvo-components/panel/versionController/components/CommitCommentModal.jsx'; |
| 30 | import versionPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js'; |
| 31 | import {itemTypes} from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js'; |
| 32 | import {catalogItemStatuses} from 'sdc-app/onboarding/onboard/onboardingCatalog/OnboardingCatalogConstants.js'; |
| 33 | import {actionsEnum as VersionControllerActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 34 | |
| 35 | function baseUrl() { |
| 36 | const restPrefix = Configuration.get('restPrefix'); |
| 37 | return `${restPrefix}/v1.0/vendor-license-models/`; |
| 38 | } |
| 39 | |
| 40 | function fetchLicenseModels() { |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 41 | return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=${catalogItemStatuses.DRAFT}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | function fetchFinalizedLicenseModels() { |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 45 | return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=${catalogItemStatuses.CERTIFIED}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 46 | } |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 47 | function fetchArchivedLicenseModels() { |
| 48 | return RestAPIUtil.fetch(`${baseUrl()}?Status=${catalogItemStatuses.ARCHIVED}`); |
| 49 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 50 | function fetchLicenseModelById(licenseModelId, version) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 51 | const {id: versionId} = version; |
| 52 | return RestAPIUtil.fetch(`${baseUrl()}${licenseModelId}/versions/${versionId}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 53 | } |
| 54 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 55 | function 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 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 64 | function putLicenseModelAction({itemId, action, version}) { |
| 65 | const {id: versionId} = version; |
| 66 | return RestAPIUtil.put(`${baseUrl()}${itemId}/versions/${versionId}/actions`, {action: action}); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | const 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 | |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 88 | fetchArchivedLicenseModels(dispatch) { |
| 89 | return fetchArchivedLicenseModels().then(response => dispatch({ |
| 90 | type: actionTypes.ARCHIVED_LICENSE_MODELS_LIST_LOADED, |
| 91 | response |
| 92 | })); |
| 93 | |
| 94 | }, |
| 95 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 96 | fetchLicenseModelById(dispatch, {licenseModelId, version}) { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 97 | |
| 98 | return fetchLicenseModelById(licenseModelId, version).then(response => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 99 | dispatch({ |
| 100 | type: actionTypes.LICENSE_MODEL_LOADED, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 101 | response: {...response, version} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 102 | }); |
| 103 | }); |
| 104 | }, |
| 105 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 106 | 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 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 115 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 141 | dispatch({ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 142 | type: modalActionTypes.GLOBAL_MODAL_SUCCESS, |
| 143 | data: { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 144 | title: i18n('Submit Succeeded'), |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 145 | msg: i18n('This license model successfully submitted'), |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 146 | cancelButtonText: i18n('OK'), |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 147 | timeout: 2000 |
| 148 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 149 | }); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 150 | versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId}); |
| 151 | return Promise.resolve(updatedVersion); |
| 152 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 153 | }); |
| 154 | }, |
| 155 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 156 | performVCAction(dispatch, {licenseModelId, action, version, comment}) { |
| 157 | return MergeEditorActionHelper.analyzeSyncResult(dispatch, {itemId: licenseModelId, version}).then(({inMerge, isDirty, updatedVersion}) => { |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 158 | if ( (updatedVersion.status === catalogItemStatuses.CERTIFIED || updatedVersion.archivedStatus === catalogItemStatuses.ARCHIVED) && |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 159 | (action === VersionControllerActionsEnum.COMMIT || action === VersionControllerActionsEnum.SYNC)) { |
| 160 | versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId}); |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 161 | const msg = updatedVersion.archivedStatus === catalogItemStatuses.ARCHIVED ? i18n('Item was Archived') : i18n('Item version already Certified'); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 162 | dispatch({ |
| 163 | type: modalActionTypes.GLOBAL_MODAL_WARNING, |
| 164 | data: { |
| 165 | title: i18n('Commit error'), |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 166 | msg, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 167 | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 189 | }); |
| 190 | }, |
| 191 | |
| 192 | saveLicenseModel(dispatch, {licenseModel}) { |
| 193 | return putLicenseModel(licenseModel).then(() => { |
| 194 | dispatch({ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 195 | type: actionTypes.LICENSE_MODEL_LOADED, |
| 196 | response: licenseModel |
| 197 | }); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 198 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 204 | }); |
| 205 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 206 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | export default LicenseModelActionHelper; |