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'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 18 | import { actionTypes } from './LicenseModelConstants.js'; |
| 19 | import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 20 | import { actionsEnum as vcActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +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'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 28 | import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js'; |
| 29 | import { CommitModalType } from 'nfvo-components/panel/versionController/components/CommitCommentModal.jsx'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 30 | import versionPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 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() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 36 | const restPrefix = Configuration.get('restPrefix'); |
| 37 | return `${restPrefix}/v1.0/vendor-license-models/`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | function fetchLicenseModels() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 41 | return RestAPIUtil.fetch( |
| 42 | `${baseUrl()}?versionFilter=${catalogItemStatuses.DRAFT}` |
| 43 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | function fetchFinalizedLicenseModels() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 47 | return RestAPIUtil.fetch( |
| 48 | `${baseUrl()}?versionFilter=${catalogItemStatuses.CERTIFIED}` |
| 49 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 50 | } |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 51 | function fetchArchivedLicenseModels() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 52 | return RestAPIUtil.fetch( |
| 53 | `${baseUrl()}?Status=${catalogItemStatuses.ARCHIVED}` |
| 54 | ); |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 55 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 56 | function fetchLicenseModelById(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 57 | const { id: versionId } = version; |
| 58 | return RestAPIUtil.fetch( |
| 59 | `${baseUrl()}${licenseModelId}/versions/${versionId}` |
| 60 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 61 | } |
| 62 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 63 | function putLicenseModel(licenseModel) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 64 | let { |
| 65 | id, |
| 66 | vendorName, |
| 67 | description, |
| 68 | iconRef, |
| 69 | version: { id: versionId } |
| 70 | } = licenseModel; |
| 71 | return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}`, { |
| 72 | vendorName, |
| 73 | description, |
| 74 | iconRef |
| 75 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 76 | } |
| 77 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 78 | function putLicenseModelAction({ itemId, action, version }) { |
| 79 | const { id: versionId } = version; |
| 80 | return RestAPIUtil.put( |
| 81 | `${baseUrl()}${itemId}/versions/${versionId}/actions`, |
| 82 | { action: action } |
| 83 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | const LicenseModelActionHelper = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 87 | fetchLicenseModels(dispatch) { |
| 88 | return fetchLicenseModels().then(response => { |
| 89 | dispatch({ |
| 90 | type: actionTypes.LICENSE_MODELS_LIST_LOADED, |
| 91 | response |
| 92 | }); |
| 93 | }); |
| 94 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 95 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 96 | fetchFinalizedLicenseModels(dispatch) { |
| 97 | return fetchFinalizedLicenseModels().then(response => |
| 98 | dispatch({ |
| 99 | type: actionTypes.FINALIZED_LICENSE_MODELS_LIST_LOADED, |
| 100 | response |
| 101 | }) |
| 102 | ); |
| 103 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 104 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 105 | fetchArchivedLicenseModels(dispatch) { |
| 106 | return fetchArchivedLicenseModels().then(response => |
| 107 | dispatch({ |
| 108 | type: actionTypes.ARCHIVED_LICENSE_MODELS_LIST_LOADED, |
| 109 | response |
| 110 | }) |
| 111 | ); |
| 112 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 113 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 114 | fetchLicenseModelById(dispatch, { licenseModelId, version }) { |
| 115 | return fetchLicenseModelById(licenseModelId, version).then(response => { |
| 116 | dispatch({ |
| 117 | type: actionTypes.LICENSE_MODEL_LOADED, |
| 118 | response: { ...response, version } |
| 119 | }); |
| 120 | }); |
| 121 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 122 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 123 | fetchLicenseModelItems(dispatch, { licenseModelId, version }) { |
| 124 | return Promise.all([ |
| 125 | LicenseAgreementActionHelper.fetchLicenseAgreementList(dispatch, { |
| 126 | licenseModelId, |
| 127 | version |
| 128 | }), |
| 129 | FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, { |
| 130 | licenseModelId, |
| 131 | version |
| 132 | }), |
| 133 | EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, { |
| 134 | licenseModelId, |
| 135 | version |
| 136 | }), |
| 137 | LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, { |
| 138 | licenseModelId, |
| 139 | version |
| 140 | }) |
| 141 | ]); |
| 142 | }, |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 143 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 144 | manageSubmitAction(dispatch, { licenseModelId, version, isDirty }) { |
| 145 | if (isDirty) { |
| 146 | const onCommit = comment => { |
| 147 | return this.performVCAction(dispatch, { |
| 148 | licenseModelId, |
| 149 | action: vcActionsEnum.COMMIT, |
| 150 | version, |
| 151 | comment |
| 152 | }).then(() => { |
| 153 | return this.performSubmitAction(dispatch, { |
| 154 | licenseModelId, |
| 155 | version |
| 156 | }); |
| 157 | }); |
| 158 | }; |
| 159 | dispatch({ |
| 160 | type: modalActionTypes.GLOBAL_MODAL_SHOW, |
| 161 | data: { |
| 162 | modalComponentName: modalContentMapper.COMMIT_COMMENT, |
| 163 | modalComponentProps: { |
| 164 | onCommit, |
| 165 | type: CommitModalType.COMMIT_SUBMIT |
| 166 | }, |
| 167 | title: i18n('Commit & Submit') |
| 168 | } |
| 169 | }); |
| 170 | return Promise.reject(); |
| 171 | } |
| 172 | return this.performSubmitAction(dispatch, { licenseModelId, version }); |
| 173 | }, |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 174 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 175 | performSubmitAction(dispatch, { licenseModelId, version }) { |
| 176 | return putLicenseModelAction({ |
| 177 | itemId: licenseModelId, |
| 178 | action: vcActionsEnum.SUBMIT, |
| 179 | version |
| 180 | }).then(() => { |
| 181 | return ItemsHelper.checkItemStatus(dispatch, { |
| 182 | itemId: licenseModelId, |
| 183 | versionId: version.id |
| 184 | }).then(updatedVersion => { |
| 185 | dispatch({ |
| 186 | type: modalActionTypes.GLOBAL_MODAL_SUCCESS, |
| 187 | data: { |
| 188 | title: i18n('Submit Succeeded'), |
| 189 | msg: i18n('This license model successfully submitted'), |
| 190 | cancelButtonText: i18n('OK'), |
| 191 | timeout: 2000 |
| 192 | } |
| 193 | }); |
| 194 | versionPageActionHelper.fetchVersions(dispatch, { |
| 195 | itemType: itemTypes.LICENSE_MODEL, |
| 196 | itemId: licenseModelId |
| 197 | }); |
| 198 | return Promise.resolve(updatedVersion); |
| 199 | }); |
| 200 | }); |
| 201 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 202 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 203 | performVCAction(dispatch, { licenseModelId, action, version, comment }) { |
| 204 | return MergeEditorActionHelper.analyzeSyncResult(dispatch, { |
| 205 | itemId: licenseModelId, |
| 206 | version |
| 207 | }).then(({ inMerge, isDirty, updatedVersion }) => { |
| 208 | if ( |
| 209 | (updatedVersion.status === catalogItemStatuses.CERTIFIED || |
| 210 | updatedVersion.archivedStatus === |
| 211 | catalogItemStatuses.ARCHIVED) && |
| 212 | (action === VersionControllerActionsEnum.COMMIT || |
| 213 | action === VersionControllerActionsEnum.SYNC) |
| 214 | ) { |
| 215 | versionPageActionHelper.fetchVersions(dispatch, { |
| 216 | itemType: itemTypes.LICENSE_MODEL, |
| 217 | itemId: licenseModelId |
| 218 | }); |
| 219 | const msg = |
| 220 | updatedVersion.archivedStatus === |
| 221 | catalogItemStatuses.ARCHIVED |
| 222 | ? i18n('Item was Archived') |
| 223 | : i18n('Item version already Certified'); |
| 224 | dispatch({ |
| 225 | type: modalActionTypes.GLOBAL_MODAL_WARNING, |
| 226 | data: { |
| 227 | title: i18n('Commit error'), |
| 228 | msg, |
| 229 | cancelButtonText: i18n('Cancel') |
| 230 | } |
| 231 | }); |
| 232 | return Promise.resolve(updatedVersion); |
| 233 | } |
| 234 | if (!inMerge) { |
| 235 | if (action === vcActionsEnum.SUBMIT) { |
| 236 | return this.manageSubmitAction(dispatch, { |
| 237 | licenseModelId, |
| 238 | version, |
| 239 | isDirty |
| 240 | }); |
| 241 | } else { |
| 242 | return ItemsHelper.performVCAction({ |
| 243 | itemId: licenseModelId, |
| 244 | action, |
| 245 | version, |
| 246 | comment |
| 247 | }).then(() => { |
| 248 | versionPageActionHelper.fetchVersions(dispatch, { |
| 249 | itemType: itemTypes.LICENSE_MODEL, |
| 250 | itemId: licenseModelId |
| 251 | }); |
| 252 | if (action === vcActionsEnum.SYNC) { |
| 253 | return MergeEditorActionHelper.analyzeSyncResult( |
| 254 | dispatch, |
| 255 | { itemId: licenseModelId, version } |
| 256 | ).then(({ updatedVersion }) => { |
| 257 | return Promise.resolve(updatedVersion); |
| 258 | }); |
| 259 | } else { |
| 260 | return ItemsHelper.checkItemStatus(dispatch, { |
| 261 | itemId: licenseModelId, |
| 262 | versionId: version.id |
| 263 | }); |
| 264 | } |
| 265 | }); |
| 266 | } |
| 267 | } |
| 268 | }); |
| 269 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 270 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 271 | saveLicenseModel(dispatch, { licenseModel }) { |
| 272 | return putLicenseModel(licenseModel).then(() => { |
| 273 | dispatch({ |
| 274 | type: actionTypes.LICENSE_MODEL_LOADED, |
| 275 | response: licenseModel |
| 276 | }); |
| 277 | const { id, version: { id: versionId } } = licenseModel; |
| 278 | return ItemsHelper.checkItemStatus(dispatch, { |
| 279 | itemId: id, |
| 280 | versionId |
| 281 | }).then(updatedVersion => { |
| 282 | if (updatedVersion.status !== licenseModel.version.status) { |
| 283 | versionPageActionHelper.fetchVersions(dispatch, { |
| 284 | itemType: itemTypes.LICENSE_MODEL, |
| 285 | itemId: licenseModel.id |
| 286 | }); |
| 287 | } |
| 288 | }); |
| 289 | }); |
| 290 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | export default LicenseModelActionHelper; |