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