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 licenseAgreementActionTypes } from './LicenseAgreementConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 19 | import FeatureGroupsActionHelper from 'sdc-app/onboarding/licenseModel/featureGroups/FeatureGroupsActionHelper.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 20 | import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js'; |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 21 | import { |
| 22 | actionTypes as modalActionTypes, |
| 23 | modalSizes |
| 24 | } from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 25 | import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js'; |
| 26 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 27 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 28 | function baseUrl(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 29 | const restPrefix = Configuration.get('restPrefix'); |
| 30 | const { id: versionId } = version; |
| 31 | return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/license-agreements`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | function fetchLicenseAgreementList(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 35 | return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 36 | } |
| 37 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 38 | function fetchLicenseAgreement(licenseModelId, licenseAgreementId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 39 | return RestAPIUtil.fetch( |
| 40 | `${baseUrl(licenseModelId, version)}/${licenseAgreementId}` |
| 41 | ); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 42 | } |
| 43 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 44 | function postLicenseAgreement(licenseModelId, licenseAgreement, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 45 | return RestAPIUtil.post(baseUrl(licenseModelId, version), { |
| 46 | name: licenseAgreement.name, |
| 47 | description: licenseAgreement.description, |
| 48 | licenseTerm: licenseAgreement.licenseTerm, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 49 | addedFeatureGroupsIds: licenseAgreement.featureGroupsIds |
| 50 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 53 | function putLicenseAgreement( |
| 54 | licenseModelId, |
| 55 | previousLicenseAgreement, |
| 56 | licenseAgreement, |
| 57 | version |
| 58 | ) { |
| 59 | const { featureGroupsIds = [] } = licenseAgreement; |
| 60 | const { |
| 61 | featureGroupsIds: prevFeatureGroupsIds = [] |
| 62 | } = previousLicenseAgreement; |
| 63 | return RestAPIUtil.put( |
| 64 | `${baseUrl(licenseModelId, version)}/${licenseAgreement.id}`, |
| 65 | { |
| 66 | name: licenseAgreement.name, |
| 67 | description: licenseAgreement.description, |
| 68 | licenseTerm: licenseAgreement.licenseTerm, |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 69 | addedFeatureGroupsIds: featureGroupsIds.filter( |
| 70 | featureGroupId => |
| 71 | prevFeatureGroupsIds.indexOf(featureGroupId) === -1 |
| 72 | ), |
| 73 | removedFeatureGroupsIds: prevFeatureGroupsIds.filter( |
| 74 | prevFeatureGroupsId => |
| 75 | featureGroupsIds.indexOf(prevFeatureGroupsId) === -1 |
| 76 | ) |
| 77 | } |
| 78 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 79 | } |
| 80 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 81 | function deleteLicenseAgreement(licenseModelId, licenseAgreementId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 82 | return RestAPIUtil.destroy( |
| 83 | `${baseUrl(licenseModelId, version)}/${licenseAgreementId}` |
| 84 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | export default { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 88 | fetchLicenseAgreementList(dispatch, { licenseModelId, version }) { |
| 89 | return fetchLicenseAgreementList(licenseModelId, version).then( |
| 90 | response => |
| 91 | dispatch({ |
| 92 | type: |
| 93 | licenseAgreementActionTypes.LICENSE_AGREEMENT_LIST_LOADED, |
| 94 | response |
| 95 | }) |
| 96 | ); |
| 97 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 98 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 99 | fetchLicenseAgreement( |
| 100 | dispatch, |
| 101 | { licenseModelId, licenseAgreementId, version } |
| 102 | ) { |
| 103 | return fetchLicenseAgreement( |
| 104 | licenseModelId, |
| 105 | licenseAgreementId, |
| 106 | version |
| 107 | ); |
| 108 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 109 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 110 | openLicenseAgreementEditor( |
| 111 | dispatch, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 112 | { licenseModelId, licenseAgreement, version, isReadOnlyMode } |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 113 | ) { |
| 114 | FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, { |
| 115 | licenseModelId, |
| 116 | version |
| 117 | }); |
| 118 | dispatch({ |
| 119 | type: licenseAgreementActionTypes.licenseAgreementEditor.OPEN, |
| 120 | licenseAgreement |
| 121 | }); |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 122 | dispatch({ |
| 123 | type: modalActionTypes.GLOBAL_MODAL_SHOW, |
| 124 | data: { |
| 125 | modalComponentName: modalContentMapper.LA_EDITOR, |
| 126 | modalComponentProps: { |
| 127 | version, |
| 128 | licenseModelId, |
| 129 | isReadOnlyMode, |
| 130 | size: modalSizes.LARGE |
| 131 | }, |
| 132 | title: |
| 133 | licenseModelId && version && licenseAgreement |
| 134 | ? i18n('Edit License Agreement') |
| 135 | : i18n('Create New License Agreement') |
| 136 | } |
| 137 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 138 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 139 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 140 | closeLicenseAgreementEditor(dispatch) { |
| 141 | dispatch({ |
| 142 | type: licenseAgreementActionTypes.licenseAgreementEditor.CLOSE |
| 143 | }); |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 144 | dispatch({ |
| 145 | type: modalActionTypes.GLOBAL_MODAL_CLOSE |
| 146 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 147 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 148 | |
svishnev | 57c5c4a | 2018-04-22 14:14:31 +0300 | [diff] [blame] | 149 | async saveLicenseAgreement( |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 150 | dispatch, |
| 151 | { licenseModelId, previousLicenseAgreement, licenseAgreement, version } |
| 152 | ) { |
| 153 | if (previousLicenseAgreement) { |
svishnev | 57c5c4a | 2018-04-22 14:14:31 +0300 | [diff] [blame] | 154 | await putLicenseAgreement( |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 155 | licenseModelId, |
| 156 | previousLicenseAgreement, |
| 157 | licenseAgreement, |
| 158 | version |
svishnev | 57c5c4a | 2018-04-22 14:14:31 +0300 | [diff] [blame] | 159 | ); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 160 | } else { |
svishnev | 57c5c4a | 2018-04-22 14:14:31 +0300 | [diff] [blame] | 161 | await postLicenseAgreement( |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 162 | licenseModelId, |
| 163 | licenseAgreement, |
| 164 | version |
svishnev | 57c5c4a | 2018-04-22 14:14:31 +0300 | [diff] [blame] | 165 | ); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 166 | } |
svishnev | 57c5c4a | 2018-04-22 14:14:31 +0300 | [diff] [blame] | 167 | await this.fetchLicenseAgreementList(dispatch, { |
| 168 | licenseModelId, |
| 169 | version |
| 170 | }); |
| 171 | await FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, { |
| 172 | licenseModelId, |
| 173 | version |
| 174 | }); |
| 175 | |
| 176 | return ItemsHelper.checkItemStatus(dispatch, { |
| 177 | itemId: licenseModelId, |
| 178 | versionId: version.id |
| 179 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 180 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 181 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 182 | deleteLicenseAgreement( |
| 183 | dispatch, |
| 184 | { licenseModelId, licenseAgreementId, version } |
| 185 | ) { |
| 186 | return deleteLicenseAgreement( |
| 187 | licenseModelId, |
| 188 | licenseAgreementId, |
| 189 | version |
| 190 | ).then(() => { |
| 191 | dispatch({ |
| 192 | type: licenseAgreementActionTypes.DELETE_LICENSE_AGREEMENT, |
| 193 | licenseAgreementId |
| 194 | }); |
| 195 | return ItemsHelper.checkItemStatus(dispatch, { |
| 196 | itemId: licenseModelId, |
| 197 | versionId: version.id |
| 198 | }); |
| 199 | }); |
| 200 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 201 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 202 | selectLicenseAgreementEditorTab(dispatch, { tab }) { |
| 203 | dispatch({ |
| 204 | type: licenseAgreementActionTypes.licenseAgreementEditor.SELECT_TAB, |
| 205 | tab |
| 206 | }); |
| 207 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 208 | }; |