attachment page navigation fix
[sdc.git] / openecomp-ui / src / sdc-app / common / helpers / ItemsHelper.js
1 /*
2  * Copyright © 2016-2018 European Support Limited
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 or implied.
13  * See the License for the specific language governing permissions and
14  * 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';
21 import objectPropsToUrlString from 'nfvo-utils/objectPropsToUrlString.js';
22
23 export const archiveActions = {
24     ARCHIVE: 'ARCHIVE',
25     RESTORE: 'RESTORE'
26 };
27
28 function baseUrl() {
29     const restPrefix = Configuration.get('restPrefix');
30     return `${restPrefix}/v1.0/items`;
31 }
32
33 const ItemsHelper = {
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     },
47
48     fetchVersions({ itemId }) {
49         return RestAPIUtil.fetch(`${baseUrl()}/${itemId}/versions`);
50     },
51
52     fetchVersion({ itemId, versionId }) {
53         return RestAPIUtil.fetch(
54             `${baseUrl()}/${itemId}/versions/${versionId}`
55         );
56     },
57
58     fetchActivityLog({ itemId, versionId }) {
59         return RestAPIUtil.fetch(
60             `${baseUrl()}/${itemId}/versions/${versionId}/activity-logs`
61         );
62     },
63
64     fetchUsers({ itemId }) {
65         return RestAPIUtil.fetch(`${baseUrl()}/${itemId}/permissions`);
66     },
67
68     updateContributors({ itemId, removedUsersIds, addedUsersIds }) {
69         return RestAPIUtil.put(
70             `${baseUrl()}/${itemId}/permissions/${permissionTypes.CONTRIBUTOR}`,
71             { removedUsersIds, addedUsersIds }
72         );
73     },
74
75     changeOwner({ itemId, ownerId }) {
76         return RestAPIUtil.put(
77             `${baseUrl()}/${itemId}/permissions/${permissionTypes.OWNER}`,
78             { removedUsersIds: [], addedUsersIds: [ownerId] }
79         );
80     },
81
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         });
94
95         return Promise.resolve({ ...response, archivedStatus: item.status });
96     },
97
98     fetchItem(itemId) {
99         return RestAPIUtil.fetch(`${baseUrl()}/${itemId}`);
100     },
101
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         });
111     },
112
113     fetchItems(filterData) {
114         const str = objectPropsToUrlString(filterData);
115         return RestAPIUtil.fetch(`${baseUrl()}?${str}`);
116     }
117 };
118
119 export default ItemsHelper;