Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 7 | * |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [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 as featureGroupsActionConstants } from './FeatureGroupsConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 19 | import EntitlementPoolsActionHelper from 'sdc-app/onboarding/licenseModel/entitlementPools/EntitlementPoolsActionHelper.js'; |
| 20 | import LicenseKeyGroupsActionHelper from 'sdc-app/onboarding/licenseModel/licenseKeyGroups/LicenseKeyGroupsActionHelper.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 21 | import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js'; |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 22 | import { |
| 23 | actionTypes as modalActionTypes, |
| 24 | modalSizes |
| 25 | } from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 26 | import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js'; |
| 27 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 28 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 29 | function baseUrl(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 30 | const restPrefix = Configuration.get('restPrefix'); |
| 31 | const { id: versionId } = version; |
| 32 | return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/feature-groups`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 33 | } |
| 34 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 35 | function fetchFeatureGroup(licenseModelId, featureGroupId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 36 | return RestAPIUtil.fetch( |
| 37 | `${baseUrl(licenseModelId, version)}/${featureGroupId}` |
| 38 | ); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 41 | function fetchFeatureGroupsList(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 42 | return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 43 | } |
| 44 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 45 | function deleteFeatureGroup(licenseModelId, featureGroupId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 46 | return RestAPIUtil.destroy( |
| 47 | `${baseUrl(licenseModelId, version)}/${featureGroupId}` |
| 48 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 49 | } |
| 50 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 51 | function addFeatureGroup(licenseModelId, featureGroup, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 52 | return RestAPIUtil.post(baseUrl(licenseModelId, version), { |
| 53 | name: featureGroup.name, |
| 54 | description: featureGroup.description, |
| 55 | partNumber: featureGroup.partNumber, |
| 56 | manufacturerReferenceNumber: featureGroup.manufacturerReferenceNumber, |
| 57 | addedLicenseKeyGroupsIds: featureGroup.licenseKeyGroupsIds, |
| 58 | addedEntitlementPoolsIds: featureGroup.entitlementPoolsIds |
| 59 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 62 | function updateFeatureGroup( |
| 63 | licenseModelId, |
| 64 | previousFeatureGroup, |
| 65 | featureGroup, |
| 66 | version |
| 67 | ) { |
| 68 | const { licenseKeyGroupsIds = [] } = featureGroup; |
| 69 | const { |
| 70 | licenseKeyGroupsIds: prevLicenseKeyGroupsIds = [] |
| 71 | } = previousFeatureGroup; |
| 72 | const { entitlementPoolsIds = [] } = featureGroup; |
| 73 | const { |
| 74 | entitlementPoolsIds: prevEntitlementPoolsIds = [] |
| 75 | } = previousFeatureGroup; |
| 76 | return RestAPIUtil.put( |
| 77 | `${baseUrl(licenseModelId, version)}/${featureGroup.id}`, |
| 78 | { |
| 79 | name: featureGroup.name, |
| 80 | description: featureGroup.description, |
| 81 | partNumber: featureGroup.partNumber, |
| 82 | manufacturerReferenceNumber: |
| 83 | featureGroup.manufacturerReferenceNumber, |
| 84 | addedLicenseKeyGroupsIds: licenseKeyGroupsIds.filter( |
| 85 | licenseKeyGroupId => |
| 86 | prevLicenseKeyGroupsIds.indexOf(licenseKeyGroupId) === -1 |
| 87 | ), |
| 88 | removedLicenseKeyGroupsIds: prevLicenseKeyGroupsIds.filter( |
| 89 | prevLicenseKeyGroupId => |
| 90 | licenseKeyGroupsIds.indexOf(prevLicenseKeyGroupId) === -1 |
| 91 | ), |
| 92 | addedEntitlementPoolsIds: entitlementPoolsIds.filter( |
| 93 | entitlementPoolId => |
| 94 | prevEntitlementPoolsIds.indexOf(entitlementPoolId) === -1 |
| 95 | ), |
| 96 | removedEntitlementPoolsIds: prevEntitlementPoolsIds.filter( |
| 97 | prevEntitlementPoolId => |
| 98 | entitlementPoolsIds.indexOf(prevEntitlementPoolId) === -1 |
| 99 | ) |
| 100 | } |
| 101 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | export default { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 105 | fetchFeatureGroup(dispatch, { licenseModelId, featureGroupId, version }) { |
| 106 | return fetchFeatureGroup(licenseModelId, featureGroupId, version); |
| 107 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 108 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 109 | fetchFeatureGroupsList(dispatch, { licenseModelId, version }) { |
| 110 | return fetchFeatureGroupsList(licenseModelId, version).then(response => |
| 111 | dispatch({ |
| 112 | type: featureGroupsActionConstants.FEATURE_GROUPS_LIST_LOADED, |
| 113 | response |
| 114 | }) |
| 115 | ); |
| 116 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 117 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 118 | deleteFeatureGroup(dispatch, { licenseModelId, featureGroupId, version }) { |
| 119 | return deleteFeatureGroup(licenseModelId, featureGroupId, version).then( |
| 120 | () => { |
| 121 | dispatch({ |
| 122 | type: featureGroupsActionConstants.DELETE_FEATURE_GROUPS, |
| 123 | featureGroupId |
| 124 | }); |
| 125 | return ItemsHelper.checkItemStatus(dispatch, { |
| 126 | itemId: licenseModelId, |
| 127 | versionId: version.id |
| 128 | }); |
| 129 | } |
| 130 | ); |
| 131 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 132 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 133 | saveFeatureGroup( |
| 134 | dispatch, |
| 135 | { licenseModelId, previousFeatureGroup, featureGroup, version } |
| 136 | ) { |
| 137 | if (previousFeatureGroup) { |
| 138 | return updateFeatureGroup( |
| 139 | licenseModelId, |
| 140 | previousFeatureGroup, |
| 141 | featureGroup, |
| 142 | version |
| 143 | ).then(() => { |
| 144 | dispatch({ |
| 145 | type: featureGroupsActionConstants.EDIT_FEATURE_GROUPS, |
| 146 | featureGroup |
| 147 | }); |
| 148 | EntitlementPoolsActionHelper.fetchEntitlementPoolsList( |
| 149 | dispatch, |
| 150 | { licenseModelId, version } |
| 151 | ); |
| 152 | LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList( |
| 153 | dispatch, |
| 154 | { licenseModelId, version } |
| 155 | ); |
| 156 | return ItemsHelper.checkItemStatus(dispatch, { |
| 157 | itemId: licenseModelId, |
| 158 | versionId: version.id |
| 159 | }); |
| 160 | }); |
| 161 | } else { |
| 162 | return addFeatureGroup(licenseModelId, featureGroup, version).then( |
| 163 | response => { |
| 164 | dispatch({ |
| 165 | type: featureGroupsActionConstants.ADD_FEATURE_GROUPS, |
| 166 | featureGroup: { |
| 167 | ...featureGroup, |
| 168 | id: response.value, |
| 169 | referencingLicenseAgreements: [] |
| 170 | } |
| 171 | }); |
| 172 | EntitlementPoolsActionHelper.fetchEntitlementPoolsList( |
| 173 | dispatch, |
| 174 | { licenseModelId, version } |
| 175 | ); |
| 176 | LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList( |
| 177 | dispatch, |
| 178 | { licenseModelId, version } |
| 179 | ); |
| 180 | return ItemsHelper.checkItemStatus(dispatch, { |
| 181 | itemId: licenseModelId, |
| 182 | versionId: version.id |
| 183 | }); |
| 184 | } |
| 185 | ); |
| 186 | } |
| 187 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 188 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 189 | selectEntitlementPoolsEditorTab(dispatch, { tab }) { |
| 190 | dispatch({ |
| 191 | type: featureGroupsActionConstants.featureGroupsEditor.SELECT_TAB, |
| 192 | tab |
| 193 | }); |
| 194 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 195 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 196 | openFeatureGroupsEditor( |
| 197 | dispatch, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 198 | { featureGroup, licenseModelId, version, isReadOnlyMode } |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 199 | ) { |
| 200 | return Promise.all([ |
| 201 | EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, { |
| 202 | licenseModelId, |
| 203 | version |
| 204 | }), |
| 205 | LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, { |
| 206 | licenseModelId, |
| 207 | version |
| 208 | }) |
| 209 | ]).then(() => { |
| 210 | dispatch({ |
| 211 | type: featureGroupsActionConstants.featureGroupsEditor.OPEN, |
| 212 | featureGroup |
| 213 | }); |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 214 | dispatch({ |
| 215 | type: featureGroupsActionConstants.featureGroupsEditor.OPEN, |
| 216 | featureGroup |
| 217 | }); |
| 218 | dispatch({ |
| 219 | type: modalActionTypes.GLOBAL_MODAL_SHOW, |
| 220 | data: { |
| 221 | modalComponentName: modalContentMapper.FG_EDITOR, |
| 222 | modalComponentProps: { |
| 223 | version, |
| 224 | licenseModelId, |
| 225 | isReadOnlyMode, |
| 226 | size: modalSizes.LARGE |
| 227 | }, |
| 228 | title: |
| 229 | licenseModelId && version && featureGroup |
| 230 | ? i18n('Edit Feature Group') |
| 231 | : i18n('Create New Feature Group') |
| 232 | } |
| 233 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 234 | }); |
| 235 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 236 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 237 | closeFeatureGroupsEditor(dispatch) { |
| 238 | dispatch({ |
| 239 | type: featureGroupsActionConstants.featureGroupsEditor.CLOSE |
| 240 | }); |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 241 | dispatch({ |
| 242 | type: modalActionTypes.GLOBAL_MODAL_CLOSE |
| 243 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 244 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 245 | }; |