blob: ae5c2707b69873fc49d5570c01de7dbacea16ec4 [file] [log] [blame]
svishnevea5e43c2018-04-15 09:06:57 +03001/*
2 * Copyright © 2016-2018 European Support Limited
talig8e9c0652017-12-20 14:30:43 +02003 *
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
svishnevea5e43c2018-04-15 09:06:57 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
talig8e9c0652017-12-20 14:30:43 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
svishnevea5e43c2018-04-15 09:06:57 +030012 * 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.
talig8e9c0652017-12-20 14:30:43 +020015 */
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';
svishnevea5e43c2018-04-15 09:06:57 +030021import { restToggle } from 'sdc-app/features/featureToggleUtils.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020022import { featureToggleNames } from 'sdc-app/features/FeaturesConstants.js';
svishnevea5e43c2018-04-15 09:06:57 +030023import objectPropsToUrlString from 'nfvo-utils/objectPropsToUrlString.js';
24
svishnev091edfd2018-03-19 12:15:19 +020025export const archiveActions = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020026 ARCHIVE: 'ARCHIVE',
27 RESTORE: 'RESTORE'
svishnev091edfd2018-03-19 12:15:19 +020028};
29
talig8e9c0652017-12-20 14:30:43 +020030function baseUrl() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020031 const restPrefix = Configuration.get('restPrefix');
32 return `${restPrefix}/v1.0/items`;
talig8e9c0652017-12-20 14:30:43 +020033}
34
35const ItemsHelper = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020036 performVCAction({ itemId, version, action, comment }) {
37 const { id: versionId } = version;
38 const data = {
39 action,
40 ...(action === VersionControllerActionsEnum.COMMIT && {
41 commitRequest: { message: comment }
42 })
43 };
44 return RestAPIUtil.put(
45 `${baseUrl()}/${itemId}/versions/${versionId}/actions`,
46 data
47 );
48 },
talig8e9c0652017-12-20 14:30:43 +020049
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020050 fetchVersions({ itemId }) {
51 return RestAPIUtil.fetch(`${baseUrl()}/${itemId}/versions`);
52 },
talig8e9c0652017-12-20 14:30:43 +020053
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020054 fetchVersion({ itemId, versionId }) {
55 return RestAPIUtil.fetch(
56 `${baseUrl()}/${itemId}/versions/${versionId}`
57 );
58 },
talig8e9c0652017-12-20 14:30:43 +020059
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020060 fetchActivityLog({ itemId, versionId }) {
61 return RestAPIUtil.fetch(
62 `${baseUrl()}/${itemId}/versions/${versionId}/activity-logs`
63 );
64 },
talig8e9c0652017-12-20 14:30:43 +020065
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020066 fetchUsers({ itemId }) {
67 return RestAPIUtil.fetch(`${baseUrl()}/${itemId}/permissions`);
68 },
talig8e9c0652017-12-20 14:30:43 +020069
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020070 updateContributors({ itemId, removedUsersIds, addedUsersIds }) {
71 return RestAPIUtil.put(
72 `${baseUrl()}/${itemId}/permissions/${permissionTypes.CONTRIBUTOR}`,
73 { removedUsersIds, addedUsersIds }
74 );
75 },
talig8e9c0652017-12-20 14:30:43 +020076
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020077 changeOwner({ itemId, ownerId }) {
78 return RestAPIUtil.put(
79 `${baseUrl()}/${itemId}/permissions/${permissionTypes.OWNER}`,
80 { removedUsersIds: [], addedUsersIds: [ownerId] }
81 );
82 },
talig8e9c0652017-12-20 14:30:43 +020083
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020084 async checkItemStatus(dispatch, { itemId, versionId }) {
85 const response = await ItemsHelper.fetchVersion({ itemId, versionId });
86 let state = (response && response.state) || {};
87 const { baseId, description, id, name, status } = response;
88 const item = await ItemsHelper.fetchItem(itemId);
89 dispatch({
90 type: onboardingActionTypes.UPDATE_ITEM_STATUS,
91 itemState: state,
92 itemStatus: response.status,
93 archivedStatus: item.status,
94 updatedVersion: { baseId, description, id, name, status }
95 });
talig8e9c0652017-12-20 14:30:43 +020096
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020097 return Promise.resolve({ ...response, archivedStatus: item.status });
98 },
svishnev091edfd2018-03-19 12:15:19 +020099
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200100 fetchItem(itemId) {
101 return restToggle({
102 restFunction: () => RestAPIUtil.fetch(`${baseUrl()}/${itemId}`),
103 featureName: featureToggleNames.ARCHIVE_ITEM,
104 mockResult: {}
105 });
106 },
svishnev091edfd2018-03-19 12:15:19 +0200107
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200108 archiveItem(itemId) {
109 return RestAPIUtil.put(`${baseUrl()}/${itemId}/actions`, {
110 action: archiveActions.ARCHIVE
111 });
112 },
113 restoreItem(itemId) {
114 return RestAPIUtil.put(`${baseUrl()}/${itemId}/actions`, {
115 action: archiveActions.RESTORE
116 });
svishnevea5e43c2018-04-15 09:06:57 +0300117 },
118
119 fetchItems(filterData) {
120 const str = objectPropsToUrlString(filterData);
121 return restToggle({
122 restFunction: () => RestAPIUtil.fetch(`${baseUrl()}?${str}`),
123 featureName: featureToggleNames.FILTER,
124 mockResult: { results: [] }
125 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200126 }
talig8e9c0652017-12-20 14:30:43 +0200127};
128
129export default ItemsHelper;