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