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'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 32 | import { actionsEnum as VersionControllerActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js'; |
svishnev | ea5e43c | 2018-04-15 09:06:57 +0300 | [diff] [blame^] | 33 | import { |
| 34 | itemStatus, |
| 35 | versionStatus |
| 36 | } from 'sdc-app/common/helpers/ItemsHelperConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 37 | |
| 38 | function baseUrl() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 39 | const restPrefix = Configuration.get('restPrefix'); |
| 40 | return `${restPrefix}/v1.0/vendor-license-models/`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | function fetchLicenseModels() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 44 | return RestAPIUtil.fetch( |
svishnev | ea5e43c | 2018-04-15 09:06:57 +0300 | [diff] [blame^] | 45 | `${baseUrl()}?versionFilter=${versionStatus.DRAFT}` |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 46 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | function fetchFinalizedLicenseModels() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 50 | return RestAPIUtil.fetch( |
svishnev | ea5e43c | 2018-04-15 09:06:57 +0300 | [diff] [blame^] | 51 | `${baseUrl()}?versionFilter=${versionStatus.CERTIFIED}` |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 52 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 53 | } |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 54 | function fetchArchivedLicenseModels() { |
svishnev | ea5e43c | 2018-04-15 09:06:57 +0300 | [diff] [blame^] | 55 | return RestAPIUtil.fetch(`${baseUrl()}?Status=${itemStatus.ARCHIVED}`); |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 56 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 57 | function fetchLicenseModelById(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 58 | const { id: versionId } = version; |
| 59 | return RestAPIUtil.fetch( |
| 60 | `${baseUrl()}${licenseModelId}/versions/${versionId}` |
| 61 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 62 | } |
| 63 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 64 | function putLicenseModel(licenseModel) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 65 | 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 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 77 | } |
| 78 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 79 | function putLicenseModelAction({ itemId, action, version }) { |
| 80 | const { id: versionId } = version; |
| 81 | return RestAPIUtil.put( |
| 82 | `${baseUrl()}${itemId}/versions/${versionId}/actions`, |
| 83 | { action: action } |
| 84 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | const LicenseModelActionHelper = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 88 | fetchLicenseModels(dispatch) { |
| 89 | return fetchLicenseModels().then(response => { |
| 90 | dispatch({ |
| 91 | type: actionTypes.LICENSE_MODELS_LIST_LOADED, |
| 92 | response |
| 93 | }); |
| 94 | }); |
| 95 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 96 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 97 | fetchFinalizedLicenseModels(dispatch) { |
| 98 | return fetchFinalizedLicenseModels().then(response => |
| 99 | dispatch({ |
| 100 | type: actionTypes.FINALIZED_LICENSE_MODELS_LIST_LOADED, |
| 101 | response |
| 102 | }) |
| 103 | ); |
| 104 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 105 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 106 | fetchArchivedLicenseModels(dispatch) { |
| 107 | return fetchArchivedLicenseModels().then(response => |
| 108 | dispatch({ |
| 109 | type: actionTypes.ARCHIVED_LICENSE_MODELS_LIST_LOADED, |
| 110 | response |
| 111 | }) |
| 112 | ); |
| 113 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 114 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 115 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 123 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 124 | 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 | }, |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 144 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 145 | 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 | }, |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 175 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 176 | 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 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 203 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 204 | performVCAction(dispatch, { licenseModelId, action, version, comment }) { |
| 205 | return MergeEditorActionHelper.analyzeSyncResult(dispatch, { |
| 206 | itemId: licenseModelId, |
| 207 | version |
| 208 | }).then(({ inMerge, isDirty, updatedVersion }) => { |
| 209 | if ( |
svishnev | ea5e43c | 2018-04-15 09:06:57 +0300 | [diff] [blame^] | 210 | (updatedVersion.status === versionStatus.CERTIFIED || |
| 211 | updatedVersion.archivedStatus === versionStatus.ARCHIVED) && |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 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 = |
svishnev | ea5e43c | 2018-04-15 09:06:57 +0300 | [diff] [blame^] | 220 | updatedVersion.archivedStatus === versionStatus.ARCHIVED |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 221 | ? 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 269 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 270 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | export default LicenseModelActionHelper; |