AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [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, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 13 | * or implied. See the License for the specific language governing |
| 14 | * permissions and 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'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 26 | import ItemsHelper from '../../common/helpers/ItemsHelper.js'; |
| 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() { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 41 | return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=Draft`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | function fetchFinalizedLicenseModels() { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 45 | return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=Certified`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | function fetchLicenseModelById(licenseModelId, version) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 49 | const {id: versionId} = version; |
| 50 | return RestAPIUtil.fetch(`${baseUrl()}${licenseModelId}/versions/${versionId}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 51 | } |
| 52 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 53 | function putLicenseModel(licenseModel) { |
| 54 | let {id, vendorName, description, iconRef, version: {id: versionId}} = licenseModel; |
| 55 | return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}`, { |
| 56 | vendorName, |
| 57 | description, |
| 58 | iconRef |
| 59 | }); |
| 60 | } |
| 61 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 62 | function putLicenseModelAction({itemId, action, version}) { |
| 63 | const {id: versionId} = version; |
| 64 | return RestAPIUtil.put(`${baseUrl()}${itemId}/versions/${versionId}/actions`, {action: action}); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | const LicenseModelActionHelper = { |
| 68 | |
| 69 | fetchLicenseModels(dispatch) { |
| 70 | return fetchLicenseModels().then(response => { |
| 71 | dispatch({ |
| 72 | type: actionTypes.LICENSE_MODELS_LIST_LOADED, |
| 73 | response |
| 74 | }); |
| 75 | }); |
| 76 | }, |
| 77 | |
| 78 | fetchFinalizedLicenseModels(dispatch) { |
| 79 | return fetchFinalizedLicenseModels().then(response => dispatch({ |
| 80 | type: actionTypes.FINALIZED_LICENSE_MODELS_LIST_LOADED, |
| 81 | response |
| 82 | })); |
| 83 | |
| 84 | }, |
| 85 | |
| 86 | fetchLicenseModelById(dispatch, {licenseModelId, version}) { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 87 | |
| 88 | return fetchLicenseModelById(licenseModelId, version).then(response => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 89 | dispatch({ |
| 90 | type: actionTypes.LICENSE_MODEL_LOADED, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 91 | response: {...response, version} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 92 | }); |
| 93 | }); |
| 94 | }, |
| 95 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 96 | fetchLicenseModelItems(dispatch, {licenseModelId, version}) { |
| 97 | return Promise.all([ |
| 98 | LicenseAgreementActionHelper.fetchLicenseAgreementList(dispatch, {licenseModelId, version}), |
| 99 | FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {licenseModelId, version}), |
| 100 | EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {licenseModelId, version}), |
| 101 | LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version}) |
| 102 | ]); |
| 103 | }, |
| 104 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 105 | manageSubmitAction(dispatch, {licenseModelId, version, isDirty}) { |
| 106 | if(isDirty) { |
| 107 | const onCommit = comment => { |
| 108 | return this.performVCAction(dispatch, {licenseModelId, action: vcActionsEnum.COMMIT, version, comment}).then(() => { |
| 109 | return this.performSubmitAction(dispatch, {licenseModelId, version}); |
| 110 | }); |
| 111 | }; |
| 112 | dispatch({ |
| 113 | type: modalActionTypes.GLOBAL_MODAL_SHOW, |
| 114 | data: { |
| 115 | modalComponentName: modalContentMapper.COMMIT_COMMENT, |
| 116 | modalComponentProps: { |
| 117 | onCommit, |
| 118 | type: CommitModalType.COMMIT_SUBMIT |
| 119 | }, |
| 120 | title: i18n('Commit & Submit') |
| 121 | } |
| 122 | }); |
| 123 | return Promise.reject(); |
| 124 | } |
| 125 | return this.performSubmitAction(dispatch, {licenseModelId, version}); |
| 126 | }, |
| 127 | |
| 128 | performSubmitAction(dispatch, {licenseModelId, version}) { |
| 129 | return putLicenseModelAction({itemId: licenseModelId, action: vcActionsEnum.SUBMIT, version}).then(() => { |
| 130 | return ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id}).then(updatedVersion => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 131 | dispatch({ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 132 | type: modalActionTypes.GLOBAL_MODAL_SUCCESS, |
| 133 | data: { |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 134 | title: i18n('Submit Succeeded'), |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 135 | msg: i18n('This license model successfully submitted'), |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 136 | cancelButtonText: i18n('OK'), |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 137 | timeout: 2000 |
| 138 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 139 | }); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 140 | versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId}); |
| 141 | return Promise.resolve(updatedVersion); |
| 142 | }); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 143 | }); |
| 144 | }, |
| 145 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 146 | performVCAction(dispatch, {licenseModelId, action, version, comment}) { |
| 147 | return MergeEditorActionHelper.analyzeSyncResult(dispatch, {itemId: licenseModelId, version}).then(({inMerge, isDirty, updatedVersion}) => { |
| 148 | if (updatedVersion.status === catalogItemStatuses.CERTIFIED && |
| 149 | (action === VersionControllerActionsEnum.COMMIT || action === VersionControllerActionsEnum.SYNC)) { |
| 150 | versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId}); |
| 151 | dispatch({ |
| 152 | type: modalActionTypes.GLOBAL_MODAL_WARNING, |
| 153 | data: { |
| 154 | title: i18n('Commit error'), |
| 155 | msg: i18n('Item version already Certified'), |
| 156 | cancelButtonText: i18n('Cancel') |
| 157 | } |
| 158 | }); |
| 159 | return Promise.resolve(updatedVersion); |
| 160 | } |
| 161 | if (!inMerge) { |
| 162 | if(action === vcActionsEnum.SUBMIT) { |
| 163 | return this.manageSubmitAction(dispatch, {licenseModelId, version, isDirty}); |
| 164 | } |
| 165 | else { |
| 166 | return ItemsHelper.performVCAction({itemId: licenseModelId, action, version, comment}).then(() => { |
| 167 | versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId}); |
| 168 | if (action === vcActionsEnum.SYNC) { |
| 169 | return MergeEditorActionHelper.analyzeSyncResult(dispatch, {itemId: licenseModelId, version}).then(({updatedVersion}) => { |
| 170 | return Promise.resolve(updatedVersion); |
| 171 | }); |
| 172 | } else { |
| 173 | return ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id}); |
| 174 | } |
| 175 | }); |
| 176 | } |
| 177 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 178 | }); |
| 179 | }, |
| 180 | |
| 181 | saveLicenseModel(dispatch, {licenseModel}) { |
| 182 | return putLicenseModel(licenseModel).then(() => { |
| 183 | dispatch({ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 184 | type: actionTypes.LICENSE_MODEL_LOADED, |
| 185 | response: licenseModel |
| 186 | }); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 187 | const {id, version: {id: versionId}} = licenseModel; |
| 188 | return ItemsHelper.checkItemStatus(dispatch, {itemId: id, versionId}).then(updatedVersion => { |
| 189 | if (updatedVersion.status !== licenseModel.version.status) { |
| 190 | versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModel.id}); |
| 191 | } |
| 192 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 193 | }); |
| 194 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 195 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | export default LicenseModelActionHelper; |