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 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'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 21 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 22 | function baseUrl(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 23 | const restPrefix = Configuration.get('restPrefix'); |
| 24 | const { id: versionId } = version; |
| 25 | return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/license-agreements`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | function fetchLicenseAgreementList(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 29 | return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 30 | } |
| 31 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 32 | function fetchLicenseAgreement(licenseModelId, licenseAgreementId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 33 | return RestAPIUtil.fetch( |
| 34 | `${baseUrl(licenseModelId, version)}/${licenseAgreementId}` |
| 35 | ); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 36 | } |
| 37 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 38 | function postLicenseAgreement(licenseModelId, licenseAgreement, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 39 | return RestAPIUtil.post(baseUrl(licenseModelId, version), { |
| 40 | name: licenseAgreement.name, |
| 41 | description: licenseAgreement.description, |
| 42 | licenseTerm: licenseAgreement.licenseTerm, |
| 43 | requirementsAndConstrains: licenseAgreement.requirementsAndConstrains, |
| 44 | addedFeatureGroupsIds: licenseAgreement.featureGroupsIds |
| 45 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 48 | function putLicenseAgreement( |
| 49 | licenseModelId, |
| 50 | previousLicenseAgreement, |
| 51 | licenseAgreement, |
| 52 | version |
| 53 | ) { |
| 54 | const { featureGroupsIds = [] } = licenseAgreement; |
| 55 | const { |
| 56 | featureGroupsIds: prevFeatureGroupsIds = [] |
| 57 | } = previousLicenseAgreement; |
| 58 | return RestAPIUtil.put( |
| 59 | `${baseUrl(licenseModelId, version)}/${licenseAgreement.id}`, |
| 60 | { |
| 61 | name: licenseAgreement.name, |
| 62 | description: licenseAgreement.description, |
| 63 | licenseTerm: licenseAgreement.licenseTerm, |
| 64 | requirementsAndConstrains: |
| 65 | licenseAgreement.requirementsAndConstrains, |
| 66 | addedFeatureGroupsIds: featureGroupsIds.filter( |
| 67 | featureGroupId => |
| 68 | prevFeatureGroupsIds.indexOf(featureGroupId) === -1 |
| 69 | ), |
| 70 | removedFeatureGroupsIds: prevFeatureGroupsIds.filter( |
| 71 | prevFeatureGroupsId => |
| 72 | featureGroupsIds.indexOf(prevFeatureGroupsId) === -1 |
| 73 | ) |
| 74 | } |
| 75 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 76 | } |
| 77 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 78 | function deleteLicenseAgreement(licenseModelId, licenseAgreementId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 79 | return RestAPIUtil.destroy( |
| 80 | `${baseUrl(licenseModelId, version)}/${licenseAgreementId}` |
| 81 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | export default { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 85 | fetchLicenseAgreementList(dispatch, { licenseModelId, version }) { |
| 86 | return fetchLicenseAgreementList(licenseModelId, version).then( |
| 87 | response => |
| 88 | dispatch({ |
| 89 | type: |
| 90 | licenseAgreementActionTypes.LICENSE_AGREEMENT_LIST_LOADED, |
| 91 | response |
| 92 | }) |
| 93 | ); |
| 94 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 95 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 96 | fetchLicenseAgreement( |
| 97 | dispatch, |
| 98 | { licenseModelId, licenseAgreementId, version } |
| 99 | ) { |
| 100 | return fetchLicenseAgreement( |
| 101 | licenseModelId, |
| 102 | licenseAgreementId, |
| 103 | version |
| 104 | ); |
| 105 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 106 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 107 | openLicenseAgreementEditor( |
| 108 | dispatch, |
| 109 | { licenseModelId, licenseAgreement, version } |
| 110 | ) { |
| 111 | FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, { |
| 112 | licenseModelId, |
| 113 | version |
| 114 | }); |
| 115 | dispatch({ |
| 116 | type: licenseAgreementActionTypes.licenseAgreementEditor.OPEN, |
| 117 | licenseAgreement |
| 118 | }); |
| 119 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 120 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 121 | closeLicenseAgreementEditor(dispatch) { |
| 122 | dispatch({ |
| 123 | type: licenseAgreementActionTypes.licenseAgreementEditor.CLOSE |
| 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 | saveLicenseAgreement( |
| 128 | dispatch, |
| 129 | { licenseModelId, previousLicenseAgreement, licenseAgreement, version } |
| 130 | ) { |
| 131 | if (previousLicenseAgreement) { |
| 132 | return putLicenseAgreement( |
| 133 | licenseModelId, |
| 134 | previousLicenseAgreement, |
| 135 | licenseAgreement, |
| 136 | version |
| 137 | ).then(() => { |
| 138 | this.fetchLicenseAgreementList(dispatch, { |
| 139 | licenseModelId, |
| 140 | version |
| 141 | }); |
| 142 | return ItemsHelper.checkItemStatus(dispatch, { |
| 143 | itemId: licenseModelId, |
| 144 | versionId: version.id |
| 145 | }); |
| 146 | }); |
| 147 | } else { |
| 148 | return postLicenseAgreement( |
| 149 | licenseModelId, |
| 150 | licenseAgreement, |
| 151 | version |
| 152 | ).then(() => { |
| 153 | this.fetchLicenseAgreementList(dispatch, { |
| 154 | licenseModelId, |
| 155 | version |
| 156 | }); |
| 157 | FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, { |
| 158 | licenseModelId, |
| 159 | version |
| 160 | }); |
| 161 | return ItemsHelper.checkItemStatus(dispatch, { |
| 162 | itemId: licenseModelId, |
| 163 | versionId: version.id |
| 164 | }); |
| 165 | }); |
| 166 | } |
| 167 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 168 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 169 | deleteLicenseAgreement( |
| 170 | dispatch, |
| 171 | { licenseModelId, licenseAgreementId, version } |
| 172 | ) { |
| 173 | return deleteLicenseAgreement( |
| 174 | licenseModelId, |
| 175 | licenseAgreementId, |
| 176 | version |
| 177 | ).then(() => { |
| 178 | dispatch({ |
| 179 | type: licenseAgreementActionTypes.DELETE_LICENSE_AGREEMENT, |
| 180 | licenseAgreementId |
| 181 | }); |
| 182 | return ItemsHelper.checkItemStatus(dispatch, { |
| 183 | itemId: licenseModelId, |
| 184 | versionId: version.id |
| 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 | selectLicenseAgreementEditorTab(dispatch, { tab }) { |
| 190 | dispatch({ |
| 191 | type: licenseAgreementActionTypes.licenseAgreementEditor.SELECT_TAB, |
| 192 | tab |
| 193 | }); |
| 194 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 195 | }; |