svishnev | ea5e43c | 2018-04-15 09:06:57 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright © 2016-2018 European Support Limited |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 3 | * |
| 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 |
svishnev | ea5e43c | 2018-04-15 09:06:57 +0300 | [diff] [blame^] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
talig | 8e9c065 | 2017-12-20 14:30:43 +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, |
svishnev | ea5e43c | 2018-04-15 09:06:57 +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. |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 15 | */ |
| 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 { permissionTypes } from 'sdc-app/onboarding/permissions/PermissionsConstants.js'; |
| 19 | import { actionsEnum as VersionControllerActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js'; |
| 20 | import { actionTypes as onboardingActionTypes } from 'sdc-app/onboarding/OnboardingConstants.js'; |
svishnev | ea5e43c | 2018-04-15 09:06:57 +0300 | [diff] [blame^] | 21 | import { restToggle } from 'sdc-app/features/featureToggleUtils.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 22 | import { featureToggleNames } from 'sdc-app/features/FeaturesConstants.js'; |
svishnev | ea5e43c | 2018-04-15 09:06:57 +0300 | [diff] [blame^] | 23 | import objectPropsToUrlString from 'nfvo-utils/objectPropsToUrlString.js'; |
| 24 | |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 25 | export const archiveActions = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 26 | ARCHIVE: 'ARCHIVE', |
| 27 | RESTORE: 'RESTORE' |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 30 | function baseUrl() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 31 | const restPrefix = Configuration.get('restPrefix'); |
| 32 | return `${restPrefix}/v1.0/items`; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | const ItemsHelper = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 36 | performVCAction({ itemId, version, action, comment }) { |
| 37 | const { id: versionId } = version; |
| 38 | const data = { |
| 39 | action, |
| 40 | ...(action === VersionControllerActionsEnum.COMMIT && { |
| 41 | commitRequest: { message: comment } |
| 42 | }) |
| 43 | }; |
| 44 | return RestAPIUtil.put( |
| 45 | `${baseUrl()}/${itemId}/versions/${versionId}/actions`, |
| 46 | data |
| 47 | ); |
| 48 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 49 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 50 | fetchVersions({ itemId }) { |
| 51 | return RestAPIUtil.fetch(`${baseUrl()}/${itemId}/versions`); |
| 52 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 53 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 54 | fetchVersion({ itemId, versionId }) { |
| 55 | return RestAPIUtil.fetch( |
| 56 | `${baseUrl()}/${itemId}/versions/${versionId}` |
| 57 | ); |
| 58 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 59 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 60 | fetchActivityLog({ itemId, versionId }) { |
| 61 | return RestAPIUtil.fetch( |
| 62 | `${baseUrl()}/${itemId}/versions/${versionId}/activity-logs` |
| 63 | ); |
| 64 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 65 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 66 | fetchUsers({ itemId }) { |
| 67 | return RestAPIUtil.fetch(`${baseUrl()}/${itemId}/permissions`); |
| 68 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 69 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 70 | updateContributors({ itemId, removedUsersIds, addedUsersIds }) { |
| 71 | return RestAPIUtil.put( |
| 72 | `${baseUrl()}/${itemId}/permissions/${permissionTypes.CONTRIBUTOR}`, |
| 73 | { removedUsersIds, addedUsersIds } |
| 74 | ); |
| 75 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 76 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 77 | changeOwner({ itemId, ownerId }) { |
| 78 | return RestAPIUtil.put( |
| 79 | `${baseUrl()}/${itemId}/permissions/${permissionTypes.OWNER}`, |
| 80 | { removedUsersIds: [], addedUsersIds: [ownerId] } |
| 81 | ); |
| 82 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 83 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 84 | async checkItemStatus(dispatch, { itemId, versionId }) { |
| 85 | const response = await ItemsHelper.fetchVersion({ itemId, versionId }); |
| 86 | let state = (response && response.state) || {}; |
| 87 | const { baseId, description, id, name, status } = response; |
| 88 | const item = await ItemsHelper.fetchItem(itemId); |
| 89 | dispatch({ |
| 90 | type: onboardingActionTypes.UPDATE_ITEM_STATUS, |
| 91 | itemState: state, |
| 92 | itemStatus: response.status, |
| 93 | archivedStatus: item.status, |
| 94 | updatedVersion: { baseId, description, id, name, status } |
| 95 | }); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 96 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 97 | return Promise.resolve({ ...response, archivedStatus: item.status }); |
| 98 | }, |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 99 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 100 | fetchItem(itemId) { |
| 101 | return restToggle({ |
| 102 | restFunction: () => RestAPIUtil.fetch(`${baseUrl()}/${itemId}`), |
| 103 | featureName: featureToggleNames.ARCHIVE_ITEM, |
| 104 | mockResult: {} |
| 105 | }); |
| 106 | }, |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 107 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 108 | archiveItem(itemId) { |
| 109 | return RestAPIUtil.put(`${baseUrl()}/${itemId}/actions`, { |
| 110 | action: archiveActions.ARCHIVE |
| 111 | }); |
| 112 | }, |
| 113 | restoreItem(itemId) { |
| 114 | return RestAPIUtil.put(`${baseUrl()}/${itemId}/actions`, { |
| 115 | action: archiveActions.RESTORE |
| 116 | }); |
svishnev | ea5e43c | 2018-04-15 09:06:57 +0300 | [diff] [blame^] | 117 | }, |
| 118 | |
| 119 | fetchItems(filterData) { |
| 120 | const str = objectPropsToUrlString(filterData); |
| 121 | return restToggle({ |
| 122 | restFunction: () => RestAPIUtil.fetch(`${baseUrl()}?${str}`), |
| 123 | featureName: featureToggleNames.FILTER, |
| 124 | mockResult: { results: [] } |
| 125 | }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 126 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | export default ItemsHelper; |