talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 1 | /*! |
| 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 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. |
| 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 | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 21 | import restToggle from 'sdc-app/features/restToggle.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 22 | import { featureToggleNames } from 'sdc-app/features/FeaturesConstants.js'; |
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 | |
| 28 | export const itemStatus = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 29 | ARCHIVED: 'ARCHIVED', |
| 30 | DRAFT: 'Draft', |
| 31 | CERTIFIED: 'Certified' |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 34 | function baseUrl() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 35 | const restPrefix = Configuration.get('restPrefix'); |
| 36 | return `${restPrefix}/v1.0/items`; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | const ItemsHelper = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 40 | performVCAction({ itemId, version, action, comment }) { |
| 41 | const { id: versionId } = version; |
| 42 | const data = { |
| 43 | action, |
| 44 | ...(action === VersionControllerActionsEnum.COMMIT && { |
| 45 | commitRequest: { message: comment } |
| 46 | }) |
| 47 | }; |
| 48 | return RestAPIUtil.put( |
| 49 | `${baseUrl()}/${itemId}/versions/${versionId}/actions`, |
| 50 | data |
| 51 | ); |
| 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 | fetchVersions({ itemId }) { |
| 55 | return RestAPIUtil.fetch(`${baseUrl()}/${itemId}/versions`); |
| 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 | fetchVersion({ itemId, versionId }) { |
| 59 | return RestAPIUtil.fetch( |
| 60 | `${baseUrl()}/${itemId}/versions/${versionId}` |
| 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 | fetchActivityLog({ itemId, versionId }) { |
| 65 | return RestAPIUtil.fetch( |
| 66 | `${baseUrl()}/${itemId}/versions/${versionId}/activity-logs` |
| 67 | ); |
| 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 | fetchUsers({ itemId }) { |
| 71 | return RestAPIUtil.fetch(`${baseUrl()}/${itemId}/permissions`); |
| 72 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 73 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 74 | updateContributors({ itemId, removedUsersIds, addedUsersIds }) { |
| 75 | return RestAPIUtil.put( |
| 76 | `${baseUrl()}/${itemId}/permissions/${permissionTypes.CONTRIBUTOR}`, |
| 77 | { removedUsersIds, addedUsersIds } |
| 78 | ); |
| 79 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 80 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 81 | changeOwner({ itemId, ownerId }) { |
| 82 | return RestAPIUtil.put( |
| 83 | `${baseUrl()}/${itemId}/permissions/${permissionTypes.OWNER}`, |
| 84 | { removedUsersIds: [], addedUsersIds: [ownerId] } |
| 85 | ); |
| 86 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 87 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 88 | async checkItemStatus(dispatch, { itemId, versionId }) { |
| 89 | const response = await ItemsHelper.fetchVersion({ itemId, versionId }); |
| 90 | let state = (response && response.state) || {}; |
| 91 | const { baseId, description, id, name, status } = response; |
| 92 | const item = await ItemsHelper.fetchItem(itemId); |
| 93 | dispatch({ |
| 94 | type: onboardingActionTypes.UPDATE_ITEM_STATUS, |
| 95 | itemState: state, |
| 96 | itemStatus: response.status, |
| 97 | archivedStatus: item.status, |
| 98 | updatedVersion: { baseId, description, id, name, status } |
| 99 | }); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 100 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 101 | return Promise.resolve({ ...response, archivedStatus: item.status }); |
| 102 | }, |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 103 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 104 | fetchItem(itemId) { |
| 105 | return restToggle({ |
| 106 | restFunction: () => RestAPIUtil.fetch(`${baseUrl()}/${itemId}`), |
| 107 | featureName: featureToggleNames.ARCHIVE_ITEM, |
| 108 | mockResult: {} |
| 109 | }); |
| 110 | }, |
svishnev | 091edfd | 2018-03-19 12:15:19 +0200 | [diff] [blame] | 111 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 112 | archiveItem(itemId) { |
| 113 | return RestAPIUtil.put(`${baseUrl()}/${itemId}/actions`, { |
| 114 | action: archiveActions.ARCHIVE |
| 115 | }); |
| 116 | }, |
| 117 | restoreItem(itemId) { |
| 118 | return RestAPIUtil.put(`${baseUrl()}/${itemId}/actions`, { |
| 119 | action: archiveActions.RESTORE |
| 120 | }); |
| 121 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | export default ItemsHelper; |