blob: 99ecae98872c9a3031ef651c215daf9781113c67 [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';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020018import { 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';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020022import { featureToggleNames } from 'sdc-app/features/FeaturesConstants.js';
svishnev091edfd2018-03-19 12:15:19 +020023export const archiveActions = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020024 ARCHIVE: 'ARCHIVE',
25 RESTORE: 'RESTORE'
svishnev091edfd2018-03-19 12:15:19 +020026};
27
28export const itemStatus = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020029 ARCHIVED: 'ARCHIVED',
30 DRAFT: 'Draft',
31 CERTIFIED: 'Certified'
svishnev091edfd2018-03-19 12:15:19 +020032};
33
talig8e9c0652017-12-20 14:30:43 +020034function baseUrl() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020035 const restPrefix = Configuration.get('restPrefix');
36 return `${restPrefix}/v1.0/items`;
talig8e9c0652017-12-20 14:30:43 +020037}
38
39const ItemsHelper = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020040 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 },
talig8e9c0652017-12-20 14:30:43 +020053
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020054 fetchVersions({ itemId }) {
55 return RestAPIUtil.fetch(`${baseUrl()}/${itemId}/versions`);
56 },
talig8e9c0652017-12-20 14:30:43 +020057
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020058 fetchVersion({ itemId, versionId }) {
59 return RestAPIUtil.fetch(
60 `${baseUrl()}/${itemId}/versions/${versionId}`
61 );
62 },
talig8e9c0652017-12-20 14:30:43 +020063
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020064 fetchActivityLog({ itemId, versionId }) {
65 return RestAPIUtil.fetch(
66 `${baseUrl()}/${itemId}/versions/${versionId}/activity-logs`
67 );
68 },
talig8e9c0652017-12-20 14:30:43 +020069
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020070 fetchUsers({ itemId }) {
71 return RestAPIUtil.fetch(`${baseUrl()}/${itemId}/permissions`);
72 },
talig8e9c0652017-12-20 14:30:43 +020073
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020074 updateContributors({ itemId, removedUsersIds, addedUsersIds }) {
75 return RestAPIUtil.put(
76 `${baseUrl()}/${itemId}/permissions/${permissionTypes.CONTRIBUTOR}`,
77 { removedUsersIds, addedUsersIds }
78 );
79 },
talig8e9c0652017-12-20 14:30:43 +020080
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020081 changeOwner({ itemId, ownerId }) {
82 return RestAPIUtil.put(
83 `${baseUrl()}/${itemId}/permissions/${permissionTypes.OWNER}`,
84 { removedUsersIds: [], addedUsersIds: [ownerId] }
85 );
86 },
talig8e9c0652017-12-20 14:30:43 +020087
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020088 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 });
talig8e9c0652017-12-20 14:30:43 +0200100
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200101 return Promise.resolve({ ...response, archivedStatus: item.status });
102 },
svishnev091edfd2018-03-19 12:15:19 +0200103
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200104 fetchItem(itemId) {
105 return restToggle({
106 restFunction: () => RestAPIUtil.fetch(`${baseUrl()}/${itemId}`),
107 featureName: featureToggleNames.ARCHIVE_ITEM,
108 mockResult: {}
109 });
110 },
svishnev091edfd2018-03-19 12:15:19 +0200111
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200112 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 }
talig8e9c0652017-12-20 14:30:43 +0200122};
123
124export default ItemsHelper;