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