blob: 05affe981fe6f1db99fed9e67831f23f3205a67c [file] [log] [blame]
talig8e9c0652017-12-20 14:30:43 +02001/*!
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 */
16import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17import Configuration from 'sdc-app/config/Configuration.js';
18import {permissionTypes} from 'sdc-app/onboarding/permissions/PermissionsConstants.js';
19import {actionsEnum as VersionControllerActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
20import {actionTypes as onboardingActionTypes} from 'sdc-app/onboarding/OnboardingConstants.js';
svishnev091edfd2018-03-19 12:15:19 +020021import restToggle from 'sdc-app/features/restToggle.js';
22import {featureToggleNames} from 'sdc-app/features/FeaturesConstants.js';
23export const archiveActions = {
24 ARCHIVE: 'ARCHIVE',
25 RESTORE: 'RESTORE'
26};
27
28export const itemStatus = {
29 ARCHIVED: 'ARCHIVED',
30 DRAFT: 'Draft',
31 CERTIFIED: 'Certified'
32};
33
talig8e9c0652017-12-20 14:30:43 +020034
35function baseUrl() {
36 const restPrefix = Configuration.get('restPrefix');
37 return `${restPrefix}/v1.0/items`;
38}
39
40const 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
svishnev091edfd2018-03-19 12:15:19 +020074 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}
talig8e9c0652017-12-20 14:30:43 +020085 });
86
svishnev091edfd2018-03-19 12:15:19 +020087 return Promise.resolve({...response, archivedStatus: item.status});
talig8e9c0652017-12-20 14:30:43 +020088 },
svishnev091edfd2018-03-19 12:15:19 +020089
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 }
talig8e9c0652017-12-20 14:30:43 +0200104};
105
106export default ItemsHelper;