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'; |
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'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 22 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 23 | function baseUrl(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 24 | const restPrefix = Configuration.get('restPrefix'); |
| 25 | const { id: versionId } = version; |
| 26 | return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/feature-groups`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 27 | } |
| 28 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 29 | function fetchFeatureGroup(licenseModelId, featureGroupId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 30 | return RestAPIUtil.fetch( |
| 31 | `${baseUrl(licenseModelId, version)}/${featureGroupId}` |
| 32 | ); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 35 | function fetchFeatureGroupsList(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 36 | return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 37 | } |
| 38 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 39 | function deleteFeatureGroup(licenseModelId, featureGroupId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 40 | return RestAPIUtil.destroy( |
| 41 | `${baseUrl(licenseModelId, version)}/${featureGroupId}` |
| 42 | ); |
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 addFeatureGroup(licenseModelId, featureGroup, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 46 | return RestAPIUtil.post(baseUrl(licenseModelId, version), { |
| 47 | name: featureGroup.name, |
| 48 | description: featureGroup.description, |
| 49 | partNumber: featureGroup.partNumber, |
| 50 | manufacturerReferenceNumber: featureGroup.manufacturerReferenceNumber, |
| 51 | addedLicenseKeyGroupsIds: featureGroup.licenseKeyGroupsIds, |
| 52 | addedEntitlementPoolsIds: featureGroup.entitlementPoolsIds |
| 53 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 56 | function updateFeatureGroup( |
| 57 | licenseModelId, |
| 58 | previousFeatureGroup, |
| 59 | featureGroup, |
| 60 | version |
| 61 | ) { |
| 62 | const { licenseKeyGroupsIds = [] } = featureGroup; |
| 63 | const { |
| 64 | licenseKeyGroupsIds: prevLicenseKeyGroupsIds = [] |
| 65 | } = previousFeatureGroup; |
| 66 | const { entitlementPoolsIds = [] } = featureGroup; |
| 67 | const { |
| 68 | entitlementPoolsIds: prevEntitlementPoolsIds = [] |
| 69 | } = previousFeatureGroup; |
| 70 | return RestAPIUtil.put( |
| 71 | `${baseUrl(licenseModelId, version)}/${featureGroup.id}`, |
| 72 | { |
| 73 | name: featureGroup.name, |
| 74 | description: featureGroup.description, |
| 75 | partNumber: featureGroup.partNumber, |
| 76 | manufacturerReferenceNumber: |
| 77 | featureGroup.manufacturerReferenceNumber, |
| 78 | addedLicenseKeyGroupsIds: licenseKeyGroupsIds.filter( |
| 79 | licenseKeyGroupId => |
| 80 | prevLicenseKeyGroupsIds.indexOf(licenseKeyGroupId) === -1 |
| 81 | ), |
| 82 | removedLicenseKeyGroupsIds: prevLicenseKeyGroupsIds.filter( |
| 83 | prevLicenseKeyGroupId => |
| 84 | licenseKeyGroupsIds.indexOf(prevLicenseKeyGroupId) === -1 |
| 85 | ), |
| 86 | addedEntitlementPoolsIds: entitlementPoolsIds.filter( |
| 87 | entitlementPoolId => |
| 88 | prevEntitlementPoolsIds.indexOf(entitlementPoolId) === -1 |
| 89 | ), |
| 90 | removedEntitlementPoolsIds: prevEntitlementPoolsIds.filter( |
| 91 | prevEntitlementPoolId => |
| 92 | entitlementPoolsIds.indexOf(prevEntitlementPoolId) === -1 |
| 93 | ) |
| 94 | } |
| 95 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | export default { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 99 | fetchFeatureGroup(dispatch, { licenseModelId, featureGroupId, version }) { |
| 100 | return fetchFeatureGroup(licenseModelId, featureGroupId, version); |
| 101 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 102 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 103 | fetchFeatureGroupsList(dispatch, { licenseModelId, version }) { |
| 104 | return fetchFeatureGroupsList(licenseModelId, version).then(response => |
| 105 | dispatch({ |
| 106 | type: featureGroupsActionConstants.FEATURE_GROUPS_LIST_LOADED, |
| 107 | response |
| 108 | }) |
| 109 | ); |
| 110 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 111 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 112 | deleteFeatureGroup(dispatch, { licenseModelId, featureGroupId, version }) { |
| 113 | return deleteFeatureGroup(licenseModelId, featureGroupId, version).then( |
| 114 | () => { |
| 115 | dispatch({ |
| 116 | type: featureGroupsActionConstants.DELETE_FEATURE_GROUPS, |
| 117 | featureGroupId |
| 118 | }); |
| 119 | return ItemsHelper.checkItemStatus(dispatch, { |
| 120 | itemId: licenseModelId, |
| 121 | versionId: version.id |
| 122 | }); |
| 123 | } |
| 124 | ); |
| 125 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 126 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 127 | saveFeatureGroup( |
| 128 | dispatch, |
| 129 | { licenseModelId, previousFeatureGroup, featureGroup, version } |
| 130 | ) { |
| 131 | if (previousFeatureGroup) { |
| 132 | return updateFeatureGroup( |
| 133 | licenseModelId, |
| 134 | previousFeatureGroup, |
| 135 | featureGroup, |
| 136 | version |
| 137 | ).then(() => { |
| 138 | dispatch({ |
| 139 | type: featureGroupsActionConstants.EDIT_FEATURE_GROUPS, |
| 140 | featureGroup |
| 141 | }); |
| 142 | EntitlementPoolsActionHelper.fetchEntitlementPoolsList( |
| 143 | dispatch, |
| 144 | { licenseModelId, version } |
| 145 | ); |
| 146 | LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList( |
| 147 | dispatch, |
| 148 | { licenseModelId, version } |
| 149 | ); |
| 150 | return ItemsHelper.checkItemStatus(dispatch, { |
| 151 | itemId: licenseModelId, |
| 152 | versionId: version.id |
| 153 | }); |
| 154 | }); |
| 155 | } else { |
| 156 | return addFeatureGroup(licenseModelId, featureGroup, version).then( |
| 157 | response => { |
| 158 | dispatch({ |
| 159 | type: featureGroupsActionConstants.ADD_FEATURE_GROUPS, |
| 160 | featureGroup: { |
| 161 | ...featureGroup, |
| 162 | id: response.value, |
| 163 | referencingLicenseAgreements: [] |
| 164 | } |
| 165 | }); |
| 166 | EntitlementPoolsActionHelper.fetchEntitlementPoolsList( |
| 167 | dispatch, |
| 168 | { licenseModelId, version } |
| 169 | ); |
| 170 | LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList( |
| 171 | dispatch, |
| 172 | { licenseModelId, version } |
| 173 | ); |
| 174 | return ItemsHelper.checkItemStatus(dispatch, { |
| 175 | itemId: licenseModelId, |
| 176 | versionId: version.id |
| 177 | }); |
| 178 | } |
| 179 | ); |
| 180 | } |
| 181 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 182 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 183 | selectEntitlementPoolsEditorTab(dispatch, { tab }) { |
| 184 | dispatch({ |
| 185 | type: featureGroupsActionConstants.featureGroupsEditor.SELECT_TAB, |
| 186 | tab |
| 187 | }); |
| 188 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 189 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 190 | openFeatureGroupsEditor( |
| 191 | dispatch, |
| 192 | { featureGroup, licenseModelId, version } |
| 193 | ) { |
| 194 | return Promise.all([ |
| 195 | EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, { |
| 196 | licenseModelId, |
| 197 | version |
| 198 | }), |
| 199 | LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, { |
| 200 | licenseModelId, |
| 201 | version |
| 202 | }) |
| 203 | ]).then(() => { |
| 204 | dispatch({ |
| 205 | type: featureGroupsActionConstants.featureGroupsEditor.OPEN, |
| 206 | featureGroup |
| 207 | }); |
| 208 | }); |
| 209 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 210 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 211 | closeFeatureGroupsEditor(dispatch) { |
| 212 | dispatch({ |
| 213 | type: featureGroupsActionConstants.featureGroupsEditor.CLOSE |
| 214 | }); |
| 215 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 216 | }; |