blob: 574aa0f3fc44845c8b5d932f4748a14090380e86 [file] [log] [blame]
talig8e9c0652017-12-20 14:30:43 +02001import {actionTypes} from './UserNotificationsConstants.js';
2import i18n from 'nfvo-utils/i18n/i18n.js';
3import Configuration from 'sdc-app/config/Configuration.js';
4import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
5import WebSocketUtil, {websocketUrl} from 'nfvo-utils/WebSocketUtil.js';
6import {actionsEnum as VersionControllerActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
7import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
8import ScreensHelper from 'sdc-app/common/helpers/ScreensHelper.js';
9import MergeEditorActionHelper from 'sdc-app/common/merge/MergeEditorActionHelper.js';
10import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
11import {SyncStates} from 'sdc-app/common/merge/MergeEditorConstants.js';
12
13function baseUrl() {
14 const restPrefix = Configuration.get('restPrefix');
15 return `${restPrefix}/v1.0/notifications`;
16}
17
18function fetch() {
19 return RestAPIUtil.fetch(baseUrl());
20}
21
22function updateNotification(notificationId) {
23 return RestAPIUtil.put(`${baseUrl()}/${notificationId}`);
24}
25
26function updateLastSeenNotification(notificationId) {
27 return RestAPIUtil.put(`${baseUrl()}/last-seen/${notificationId}`);
28}
29
30function loadPrevNotifications(lastScanned, endOfPage) {
31 return RestAPIUtil.fetch(`${baseUrl()}?LAST_DELIVERED_EVENT_ID=${lastScanned}&END_OF_PAGE_EVENT_ID=${endOfPage}`);
32}
33
34const INITIAL_LAST_SCANNED = '00000000-0000-1000-8080-808080808080';
35
36const UserNotificationsActionHelper = {
37 notificationsFirstHandling(dispatch) {
38 console.log('Websocket Url: ', websocketUrl);
39 UserNotificationsActionHelper.fetchUserNotificationsList(dispatch).then(({lastScanned}) => {
40 WebSocketUtil.open(websocketUrl, {lastScanned: lastScanned || INITIAL_LAST_SCANNED});
41 });
42 },
43
44 fetchUserNotificationsList(dispatch) {
45 return fetch().then(result => {
46 dispatch({
47 type: actionTypes.LOAD_NOTIFICATIONS,
48 result
49 });
50 return Promise.resolve({lastScanned: result.lastScanned});
51 });
52 },
53
54 loadPreviousNotifications(dispatch, {lastScanned, endOfPage}) {
55 loadPrevNotifications(lastScanned, endOfPage).then(result => dispatch({
56 type: actionTypes.LOAD_PREV_NOTIFICATIONS,
57 result
58 }));
59 },
60
61 notifyAboutConflicts(dispatch, {itemId, itemName, version, currentScreen}) {
62 let {props} = currentScreen;
63 let currentItemId = props.softwareProductId || props.licenseModelId;
64 let currentVersion = props.version;
65 if(currentItemId === itemId && currentVersion.id === version.id) {
66 MergeEditorActionHelper.analyzeSyncResult(dispatch, {itemId, version}).then(() => ScreensHelper.loadScreen(dispatch, currentScreen));
67 }
68 else {
69 dispatch({
70 type: modalActionTypes.GLOBAL_MODAL_WARNING,
71 data: {
72 title: i18n('Conflicts'),
73 msg: i18n('There are conflicts in {itemName} version {versionName} that you have to resolve', {itemName: itemName.toUpperCase(), versionName: version.versionName}),
74 cancelButtonText: i18n('OK')
75 }
76 });
77 }
78 },
79
80 syncItem(dispatch, {itemId, itemName, versionId, versionName, currentScreen}) {
81 let version = {id: versionId, versionName};
82 ItemsHelper.fetchVersion({itemId, versionId}).then(response => {
83 let inMerge = response && response.state && response.state.synchronizationState === SyncStates.MERGE;
84 if (!inMerge) {
85 ItemsHelper.performVCAction({itemId, version, action: VersionControllerActionsEnum.SYNC}).then(() => {
86 return ItemsHelper.fetchVersion({itemId, versionId}).then(response => {
87 let inMerge = response && response.state && response.state.synchronizationState === SyncStates.MERGE;
88 if (!inMerge) {
89 return ScreensHelper.loadScreen(dispatch, currentScreen);
90 }
91 else {
92 return this.notifyAboutConflicts(dispatch, {itemId, itemName, version, currentScreen});
93 }
94 });
95 });
96 }
97 else {
98 this.notifyAboutConflicts(dispatch, {itemId, itemName, version, currentScreen});
99 }
100 });
101 },
102
103 updateNotification(dispatch, {notificationForUpdate}) {
104 updateNotification(notificationForUpdate.eventId).then(response => {
105 if(response.status === 'Success' && Object.keys(response.errors).length === 0) {
106 dispatch({
107 type: actionTypes.UPDATE_READ_NOTIFICATION,
108 notificationForUpdate
109 });
110 }
111 });
112 },
113
114 updateLastSeenNotification(dispatch, {notificationId}) {
115 updateLastSeenNotification(notificationId).then(response => {
116 if (response.status === 'Success' && Object.keys(response.errors).length === 0) {
117 dispatch({type: actionTypes.RESET_NEW_NOTIFICATIONS});
118 }
119 });
120 }
121};
122
123export default UserNotificationsActionHelper;