summaryrefslogtreecommitdiffstats
path: root/openecomp-ui/src/sdc-app/onboarding/userNotifications/UserNotificationsActionHelper.js
blob: 574aa0f3fc44845c8b5d932f4748a14090380e86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import {actionTypes} from './UserNotificationsConstants.js';
import i18n from 'nfvo-utils/i18n/i18n.js';
import Configuration from 'sdc-app/config/Configuration.js';
import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
import WebSocketUtil, {websocketUrl} from 'nfvo-utils/WebSocketUtil.js';
import {actionsEnum as VersionControllerActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
import ScreensHelper from 'sdc-app/common/helpers/ScreensHelper.js';
import MergeEditorActionHelper from 'sdc-app/common/merge/MergeEditorActionHelper.js';
import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
import {SyncStates} from 'sdc-app/common/merge/MergeEditorConstants.js';

function baseUrl() {
	const restPrefix = Configuration.get('restPrefix');
	return `${restPrefix}/v1.0/notifications`;
}

function fetch() {
	return RestAPIUtil.fetch(baseUrl());
}

function updateNotification(notificationId) {
	return RestAPIUtil.put(`${baseUrl()}/${notificationId}`);
}

function updateLastSeenNotification(notificationId) {
	return RestAPIUtil.put(`${baseUrl()}/last-seen/${notificationId}`);
}

function loadPrevNotifications(lastScanned, endOfPage) {
	return RestAPIUtil.fetch(`${baseUrl()}?LAST_DELIVERED_EVENT_ID=${lastScanned}&END_OF_PAGE_EVENT_ID=${endOfPage}`);
}

const INITIAL_LAST_SCANNED = '00000000-0000-1000-8080-808080808080';

const UserNotificationsActionHelper = {
	notificationsFirstHandling(dispatch) {
		console.log('Websocket Url: ', websocketUrl);
		UserNotificationsActionHelper.fetchUserNotificationsList(dispatch).then(({lastScanned}) => {
			WebSocketUtil.open(websocketUrl, {lastScanned: lastScanned || INITIAL_LAST_SCANNED});
		});
	},

	fetchUserNotificationsList(dispatch) {
		return fetch().then(result => {
			dispatch({
				type: actionTypes.LOAD_NOTIFICATIONS,
				result
			});
			return Promise.resolve({lastScanned: result.lastScanned});
		});
	},

	loadPreviousNotifications(dispatch, {lastScanned, endOfPage}) {
		loadPrevNotifications(lastScanned, endOfPage).then(result => dispatch({
			type: actionTypes.LOAD_PREV_NOTIFICATIONS,
			result
		}));
	},

	notifyAboutConflicts(dispatch, {itemId, itemName, version, currentScreen}) {
		let {props} = currentScreen;
		let currentItemId = props.softwareProductId || props.licenseModelId;
		let currentVersion = props.version;
		if(currentItemId === itemId && currentVersion.id === version.id) {
			MergeEditorActionHelper.analyzeSyncResult(dispatch, {itemId, version}).then(() => ScreensHelper.loadScreen(dispatch, currentScreen));
		}
		else {
			dispatch({
				type: modalActionTypes.GLOBAL_MODAL_WARNING,
				data: {
					title: i18n('Conflicts'),
					msg: i18n('There are conflicts in {itemName} version {versionName} that you have to resolve', {itemName: itemName.toUpperCase(), versionName: version.versionName}),
					cancelButtonText: i18n('OK')
				}
			});
		}
	},

	syncItem(dispatch, {itemId, itemName, versionId, versionName, currentScreen}) {
		let version = {id: versionId, versionName};
		ItemsHelper.fetchVersion({itemId, versionId}).then(response => {
			let inMerge = response && response.state && response.state.synchronizationState === SyncStates.MERGE;
			if (!inMerge) {
				ItemsHelper.performVCAction({itemId, version, action: VersionControllerActionsEnum.SYNC}).then(() => {
					return ItemsHelper.fetchVersion({itemId, versionId}).then(response => {
						let inMerge = response && response.state && response.state.synchronizationState === SyncStates.MERGE;
						if (!inMerge) {
							return ScreensHelper.loadScreen(dispatch, currentScreen);
						}
						else {
							return this.notifyAboutConflicts(dispatch, {itemId, itemName, version, currentScreen});
						}
					});
				});
			}
			else {
				this.notifyAboutConflicts(dispatch, {itemId, itemName, version, currentScreen});
			}
		});
	},

	updateNotification(dispatch, {notificationForUpdate}) {
		updateNotification(notificationForUpdate.eventId).then(response => {
			if(response.status === 'Success' && Object.keys(response.errors).length === 0) {
				dispatch({
					type: actionTypes.UPDATE_READ_NOTIFICATION,
					notificationForUpdate
				});
			}
		});
	},

	updateLastSeenNotification(dispatch, {notificationId}) {
		updateLastSeenNotification(notificationId).then(response => {
			if (response.status === 'Success' && Object.keys(response.errors).length === 0) {
				dispatch({type: actionTypes.RESET_NEW_NOTIFICATIONS});
			}
		});
	}
};

export default UserNotificationsActionHelper;