blob: 3ac3efd4b15242f7477d451ed2afd49266ca30e3 [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
KrupaNagabhushan881438c2022-04-04 17:49:50 +01007 *
svishnevea5e43c2018-04-15 09:06:57 +03008 * http://www.apache.org/licenses/LICENSE-2.0
KrupaNagabhushan881438c2022-04-04 17:49:50 +01009 *
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 objectPropsToUrlString from 'nfvo-utils/objectPropsToUrlString.js';
22
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
talig8e9c0652017-12-20 14:30:43 +020028function baseUrl() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020029 const restPrefix = Configuration.get('restPrefix');
30 return `${restPrefix}/v1.0/items`;
talig8e9c0652017-12-20 14:30:43 +020031}
32
33const ItemsHelper = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020034 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 },
talig8e9c0652017-12-20 14:30:43 +020047
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020048 fetchVersions({ itemId }) {
49 return RestAPIUtil.fetch(`${baseUrl()}/${itemId}/versions`);
50 },
talig8e9c0652017-12-20 14:30:43 +020051
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020052 fetchVersion({ itemId, versionId }) {
53 return RestAPIUtil.fetch(
54 `${baseUrl()}/${itemId}/versions/${versionId}`
55 );
56 },
talig8e9c0652017-12-20 14:30:43 +020057
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020058 fetchActivityLog({ itemId, versionId }) {
59 return RestAPIUtil.fetch(
60 `${baseUrl()}/${itemId}/versions/${versionId}/activity-logs`
61 );
62 },
talig8e9c0652017-12-20 14:30:43 +020063
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020064 fetchUsers({ itemId }) {
65 return RestAPIUtil.fetch(`${baseUrl()}/${itemId}/permissions`);
66 },
talig8e9c0652017-12-20 14:30:43 +020067
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020068 updateContributors({ itemId, removedUsersIds, addedUsersIds }) {
69 return RestAPIUtil.put(
70 `${baseUrl()}/${itemId}/permissions/${permissionTypes.CONTRIBUTOR}`,
71 { removedUsersIds, addedUsersIds }
72 );
73 },
talig8e9c0652017-12-20 14:30:43 +020074
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020075 changeOwner({ itemId, ownerId }) {
76 return RestAPIUtil.put(
77 `${baseUrl()}/${itemId}/permissions/${permissionTypes.OWNER}`,
78 { removedUsersIds: [], addedUsersIds: [ownerId] }
79 );
80 },
talig8e9c0652017-12-20 14:30:43 +020081
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020082 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 });
talig8e9c0652017-12-20 14:30:43 +020094
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020095 return Promise.resolve({ ...response, archivedStatus: item.status });
96 },
svishnev091edfd2018-03-19 12:15:19 +020097
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020098 fetchItem(itemId) {
svishnev57c5c4a2018-04-22 14:14:31 +030099 return RestAPIUtil.fetch(`${baseUrl()}/${itemId}`);
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200100 },
svishnev091edfd2018-03-19 12:15:19 +0200101
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200102 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 });
svishnevea5e43c2018-04-15 09:06:57 +0300111 },
112
113 fetchItems(filterData) {
114 const str = objectPropsToUrlString(filterData);
svishnev57c5c4a2018-04-22 14:14:31 +0300115 return RestAPIUtil.fetch(`${baseUrl()}?${str}`);
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200116 }
talig8e9c0652017-12-20 14:30:43 +0200117};
118
119export default ItemsHelper;