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'; |
| 18 | import {actionTypes as licenseAgreementActionTypes} from './LicenseAgreementConstants.js'; |
| 19 | import FeatureGroupsActionHelper from 'sdc-app/onboarding/licenseModel/featureGroups/FeatureGroupsActionHelper.js'; |
| 20 | import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js'; |
| 21 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 22 | function baseUrl(licenseModelId, version) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 23 | const restPrefix = Configuration.get('restPrefix'); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 29 | return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 30 | } |
| 31 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 32 | function postLicenseAgreement(licenseModelId, licenseAgreement, version) { |
| 33 | return RestAPIUtil.post(baseUrl(licenseModelId, version), { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 34 | name: licenseAgreement.name, |
| 35 | description: licenseAgreement.description, |
| 36 | licenseTerm: licenseAgreement.licenseTerm, |
| 37 | requirementsAndConstrains: licenseAgreement.requirementsAndConstrains, |
| 38 | addedFeatureGroupsIds: licenseAgreement.featureGroupsIds |
| 39 | }); |
| 40 | } |
| 41 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 42 | function putLicenseAgreement(licenseModelId, previousLicenseAgreement, licenseAgreement, version) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 43 | const {featureGroupsIds = []} = licenseAgreement; |
| 44 | const {featureGroupsIds: prevFeatureGroupsIds = []} = previousLicenseAgreement; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 45 | return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${licenseAgreement.id}`, { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 46 | name: licenseAgreement.name, |
| 47 | description: licenseAgreement.description, |
| 48 | licenseTerm: licenseAgreement.licenseTerm, |
| 49 | requirementsAndConstrains: licenseAgreement.requirementsAndConstrains, |
| 50 | addedFeatureGroupsIds: featureGroupsIds.filter(featureGroupId => prevFeatureGroupsIds.indexOf(featureGroupId) === -1), |
| 51 | removedFeatureGroupsIds: prevFeatureGroupsIds.filter(prevFeatureGroupsId => featureGroupsIds.indexOf(prevFeatureGroupsId) === -1) |
| 52 | }); |
| 53 | } |
| 54 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 55 | function deleteLicenseAgreement(licenseModelId, licenseAgreementId, version) { |
| 56 | return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${licenseAgreementId}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | export default { |
| 60 | |
| 61 | fetchLicenseAgreementList(dispatch, {licenseModelId, version}) { |
| 62 | return fetchLicenseAgreementList(licenseModelId, version).then(response => dispatch({ |
| 63 | type: licenseAgreementActionTypes.LICENSE_AGREEMENT_LIST_LOADED, |
| 64 | response |
| 65 | })); |
| 66 | }, |
| 67 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 68 | openLicenseAgreementEditor(dispatch, {licenseModelId, licenseAgreement, version}) { |
| 69 | FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {licenseModelId, version}); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 70 | dispatch({ |
| 71 | type: licenseAgreementActionTypes.licenseAgreementEditor.OPEN, |
| 72 | licenseAgreement |
| 73 | }); |
| 74 | }, |
| 75 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 76 | closeLicenseAgreementEditor(dispatch) { |
| 77 | dispatch({ |
| 78 | type: licenseAgreementActionTypes.licenseAgreementEditor.CLOSE |
| 79 | }); |
| 80 | }, |
| 81 | |
| 82 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 83 | saveLicenseAgreement(dispatch, {licenseModelId, previousLicenseAgreement, licenseAgreement, version}) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 84 | if (previousLicenseAgreement) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 85 | return putLicenseAgreement(licenseModelId, previousLicenseAgreement, licenseAgreement, version).then(() => { |
ilanap | cb1d483 | 2017-09-27 11:41:19 +0300 | [diff] [blame] | 86 | this.fetchLicenseAgreementList(dispatch, {licenseModelId, version}); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 87 | }); |
| 88 | } |
| 89 | else { |
ilanap | cb1d483 | 2017-09-27 11:41:19 +0300 | [diff] [blame] | 90 | return postLicenseAgreement(licenseModelId, licenseAgreement, version).then(() => { |
| 91 | this.fetchLicenseAgreementList(dispatch, {licenseModelId, version}); |
| 92 | FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {licenseModelId, version}); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 93 | }); |
| 94 | } |
| 95 | }, |
| 96 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 97 | deleteLicenseAgreement(dispatch, {licenseModelId, licenseAgreementId, version}) { |
| 98 | return deleteLicenseAgreement(licenseModelId, licenseAgreementId, version).then(() => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 99 | dispatch({ |
| 100 | type: licenseAgreementActionTypes.DELETE_LICENSE_AGREEMENT, |
| 101 | licenseAgreementId |
| 102 | }); |
| 103 | }); |
| 104 | }, |
| 105 | |
| 106 | selectLicenseAgreementEditorTab(dispatch, {tab}) { |
| 107 | dispatch({ |
| 108 | type: licenseAgreementActionTypes.licenseAgreementEditor.SELECT_TAB, |
| 109 | tab |
| 110 | }); |
| 111 | }, |
| 112 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 113 | switchVersion(dispatch, {licenseModelId, version}) { |
| 114 | LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version}).then(() => { |
| 115 | this.fetchLicenseAgreementList(dispatch, {licenseModelId, version}); |
| 116 | }); |
| 117 | } |
| 118 | }; |