AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 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 | * |
| 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 licenseKeyGroupsConstants } from './LicenseKeyGroupsConstants.js'; |
| 19 | import { actionTypes as limitEditorActions } from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js'; |
| 20 | import { default as getValue, getStrValue } from 'nfvo-utils/getValue.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 i18n from 'nfvo-utils/i18n/i18n.js'; |
| 23 | import { |
| 24 | actionTypes as modalActionTypes, |
| 25 | modalSizes |
| 26 | } from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 27 | import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.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}/license-key-groups`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | function fetchLicenseKeyGroupsList(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 deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 40 | return RestAPIUtil.destroy( |
| 41 | `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}` |
| 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 postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 46 | return RestAPIUtil.post(baseUrl(licenseModelId, version), { |
| 47 | name: licenseKeyGroup.name, |
| 48 | description: licenseKeyGroup.description, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 49 | type: licenseKeyGroup.type, |
| 50 | increments: licenseKeyGroup.increments, |
| 51 | thresholdValue: licenseKeyGroup.thresholdValue, |
| 52 | thresholdUnits: getValue(licenseKeyGroup.thresholdUnits), |
| 53 | startDate: licenseKeyGroup.startDate, |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 54 | expiryDate: licenseKeyGroup.expiryDate, |
| 55 | manufacturerReferenceNumber: licenseKeyGroup.manufacturerReferenceNumber |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 56 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 57 | } |
| 58 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 59 | function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 60 | return RestAPIUtil.put( |
| 61 | `${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`, |
| 62 | { |
| 63 | name: licenseKeyGroup.name, |
| 64 | description: licenseKeyGroup.description, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 65 | type: licenseKeyGroup.type, |
| 66 | increments: licenseKeyGroup.increments, |
| 67 | thresholdValue: licenseKeyGroup.thresholdValue, |
| 68 | thresholdUnits: getValue(licenseKeyGroup.thresholdUnits), |
| 69 | startDate: licenseKeyGroup.startDate, |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 70 | expiryDate: licenseKeyGroup.expiryDate, |
| 71 | manufacturerReferenceNumber: |
| 72 | licenseKeyGroup.manufacturerReferenceNumber |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 73 | } |
| 74 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 77 | function fetchLimitsList(licenseModelId, licenseKeyGroupId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 78 | return RestAPIUtil.fetch( |
| 79 | `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits` |
| 80 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | function deleteLimit(licenseModelId, licenseKeyGroupId, version, limitId) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 84 | return RestAPIUtil.destroy( |
| 85 | `${baseUrl( |
| 86 | licenseModelId, |
| 87 | version |
| 88 | )}/${licenseKeyGroupId}/limits/${limitId}` |
| 89 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | function postLimit(licenseModelId, licenseKeyGroupId, version, limit) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 93 | return RestAPIUtil.post( |
| 94 | `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`, |
| 95 | { |
| 96 | name: limit.name, |
| 97 | type: limit.type, |
| 98 | description: limit.description, |
| 99 | metric: getStrValue(limit.metric), |
| 100 | value: limit.value, |
| 101 | unit: getStrValue(limit.unit), |
| 102 | aggregationFunction: getValue(limit.aggregationFunction), |
| 103 | time: getValue(limit.time) |
| 104 | } |
| 105 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | function putLimit(licenseModelId, licenseKeyGroupId, version, limit) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 109 | return RestAPIUtil.put( |
| 110 | `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${ |
| 111 | limit.id |
| 112 | }`, |
| 113 | { |
| 114 | name: limit.name, |
| 115 | type: limit.type, |
| 116 | description: limit.description, |
| 117 | metric: getStrValue(limit.metric), |
| 118 | value: limit.value, |
| 119 | unit: getStrValue(limit.unit), |
| 120 | aggregationFunction: getValue(limit.aggregationFunction), |
| 121 | time: getValue(limit.time) |
| 122 | } |
| 123 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 124 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 125 | |
| 126 | export default { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 127 | fetchLicenseKeyGroupsList(dispatch, { licenseModelId, version }) { |
| 128 | return fetchLicenseKeyGroupsList(licenseModelId, version).then( |
| 129 | response => |
| 130 | dispatch({ |
| 131 | type: |
| 132 | licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED, |
| 133 | response |
| 134 | }) |
| 135 | ); |
| 136 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 137 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 138 | openLicenseKeyGroupsEditor( |
| 139 | dispatch, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 140 | { licenseKeyGroup, licenseModelId, version, isReadOnlyMode } = {} |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 141 | ) { |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 142 | if (licenseModelId && version && licenseKeyGroup) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 143 | this.fetchLimits(dispatch, { |
| 144 | licenseModelId, |
| 145 | version, |
| 146 | licenseKeyGroup |
| 147 | }); |
| 148 | } |
| 149 | dispatch({ |
| 150 | type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN, |
| 151 | licenseKeyGroup |
| 152 | }); |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 153 | dispatch({ |
| 154 | type: modalActionTypes.GLOBAL_MODAL_SHOW, |
| 155 | data: { |
| 156 | modalComponentName: modalContentMapper.LKG_EDITOR, |
| 157 | modalComponentProps: { |
| 158 | version, |
| 159 | licenseModelId, |
| 160 | isReadOnlyMode, |
| 161 | size: modalSizes.LARGE |
| 162 | }, |
| 163 | title: |
| 164 | licenseModelId && version && licenseKeyGroup |
| 165 | ? i18n('Edit License Key Group') |
| 166 | : i18n('Create New License Key Group') |
| 167 | } |
| 168 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 169 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 170 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 171 | closeLicenseKeyGroupEditor(dispatch) { |
| 172 | dispatch({ |
| 173 | type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE |
| 174 | }); |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 175 | dispatch({ |
| 176 | type: modalActionTypes.GLOBAL_MODAL_CLOSE |
| 177 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 178 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 179 | |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 180 | async saveLicenseKeyGroup( |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 181 | dispatch, |
| 182 | { licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version } |
| 183 | ) { |
| 184 | if (previousLicenseKeyGroup) { |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 185 | await putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 186 | } else { |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 187 | await postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 188 | } |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 189 | await ItemsHelper.checkItemStatus(dispatch, { |
| 190 | itemId: licenseModelId, |
| 191 | versionId: version.id |
| 192 | }); |
| 193 | await this.fetchLicenseKeyGroupsList(dispatch, { |
| 194 | licenseModelId, |
| 195 | version |
| 196 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 197 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 198 | |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 199 | async deleteLicenseKeyGroup( |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 200 | dispatch, |
| 201 | { licenseModelId, licenseKeyGroupId, version } |
| 202 | ) { |
svishnev | 8f13330 | 2018-08-06 23:08:39 +0300 | [diff] [blame] | 203 | await deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version); |
| 204 | await ItemsHelper.checkItemStatus(dispatch, { |
| 205 | itemId: licenseModelId, |
| 206 | versionId: version.id |
| 207 | }); |
| 208 | await this.fetchLicenseKeyGroupsList(dispatch, { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 209 | licenseModelId, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 210 | version |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 211 | }); |
| 212 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 213 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 214 | hideDeleteConfirm(dispatch) { |
| 215 | dispatch({ |
| 216 | type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM, |
| 217 | licenseKeyGroupToDelete: false |
| 218 | }); |
| 219 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 220 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 221 | openDeleteLicenseAgreementConfirm(dispatch, { licenseKeyGroup }) { |
| 222 | dispatch({ |
| 223 | type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM, |
| 224 | licenseKeyGroupToDelete: licenseKeyGroup |
| 225 | }); |
| 226 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 227 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 228 | fetchLimits(dispatch, { licenseModelId, version, licenseKeyGroup }) { |
| 229 | return fetchLimitsList( |
| 230 | licenseModelId, |
| 231 | licenseKeyGroup.id, |
| 232 | version |
| 233 | ).then(response => { |
| 234 | dispatch({ |
| 235 | type: |
| 236 | licenseKeyGroupsConstants.licenseKeyGroupsEditor |
| 237 | .LIMITS_LIST_LOADED, |
| 238 | response |
| 239 | }); |
| 240 | }); |
| 241 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 242 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 243 | submitLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) { |
| 244 | const promise = limit.id |
| 245 | ? putLimit(licenseModelId, licenseKeyGroup.id, version, limit) |
| 246 | : postLimit(licenseModelId, licenseKeyGroup.id, version, limit); |
| 247 | return promise.then(() => { |
| 248 | dispatch({ |
| 249 | type: limitEditorActions.CLOSE |
| 250 | }); |
| 251 | this.fetchLimits(dispatch, { |
| 252 | licenseModelId, |
| 253 | version, |
| 254 | licenseKeyGroup |
| 255 | }); |
| 256 | return ItemsHelper.checkItemStatus(dispatch, { |
| 257 | itemId: licenseModelId, |
| 258 | versionId: version.id |
| 259 | }); |
| 260 | }); |
| 261 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 262 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 263 | deleteLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) { |
| 264 | return deleteLimit( |
| 265 | licenseModelId, |
| 266 | licenseKeyGroup.id, |
| 267 | version, |
| 268 | limit.id |
| 269 | ).then(() => { |
| 270 | this.fetchLimits(dispatch, { |
| 271 | licenseModelId, |
| 272 | version, |
| 273 | licenseKeyGroup |
| 274 | }); |
| 275 | return ItemsHelper.checkItemStatus(dispatch, { |
| 276 | itemId: licenseModelId, |
| 277 | versionId: version.id |
| 278 | }); |
| 279 | }); |
| 280 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 281 | }; |