blob: f8693e15bee61a35e51216ac5c7ba40428b1137c [file] [log] [blame]
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +02001import { actionTypes } from './UserNotificationsConstants.js';
talig8e9c0652017-12-20 14:30:43 +02002import i18n from 'nfvo-utils/i18n/i18n.js';
3import Configuration from 'sdc-app/config/Configuration.js';
4import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +02005import WebSocketUtil, { websocketUrl } from 'nfvo-utils/WebSocketUtil.js';
6import { actionsEnum as VersionControllerActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
talig8e9c0652017-12-20 14:30:43 +02007import 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';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020010import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
11import { SyncStates } from 'sdc-app/common/merge/MergeEditorConstants.js';
talig8e9c0652017-12-20 14:30:43 +020012
13function baseUrl() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020014 const restPrefix = Configuration.get('restPrefix');
15 return `${restPrefix}/v1.0/notifications`;
talig8e9c0652017-12-20 14:30:43 +020016}
17
18function fetch() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020019 return RestAPIUtil.fetch(baseUrl());
talig8e9c0652017-12-20 14:30:43 +020020}
21
22function updateNotification(notificationId) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020023 return RestAPIUtil.put(`${baseUrl()}/${notificationId}`);
talig8e9c0652017-12-20 14:30:43 +020024}
25
26function updateLastSeenNotification(notificationId) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020027 return RestAPIUtil.put(`${baseUrl()}/last-seen/${notificationId}`);
talig8e9c0652017-12-20 14:30:43 +020028}
29
30function loadPrevNotifications(lastScanned, endOfPage) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020031 return RestAPIUtil.fetch(
32 `${baseUrl()}?LAST_DELIVERED_EVENT_ID=${lastScanned}&END_OF_PAGE_EVENT_ID=${endOfPage}`
33 );
talig8e9c0652017-12-20 14:30:43 +020034}
35
36const INITIAL_LAST_SCANNED = '00000000-0000-1000-8080-808080808080';
37
38const UserNotificationsActionHelper = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020039 notificationsFirstHandling(dispatch) {
40 console.log('Websocket Url: ', websocketUrl);
41 UserNotificationsActionHelper.fetchUserNotificationsList(dispatch).then(
42 ({ lastScanned }) => {
43 WebSocketUtil.open(websocketUrl, {
44 lastScanned: lastScanned || INITIAL_LAST_SCANNED
45 });
46 }
47 );
48 },
talig8e9c0652017-12-20 14:30:43 +020049
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020050 fetchUserNotificationsList(dispatch) {
51 return fetch().then(result => {
52 dispatch({
53 type: actionTypes.LOAD_NOTIFICATIONS,
54 result
55 });
56 return Promise.resolve({ lastScanned: result.lastScanned });
57 });
58 },
talig8e9c0652017-12-20 14:30:43 +020059
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020060 loadPreviousNotifications(dispatch, { lastScanned, endOfPage }) {
61 loadPrevNotifications(lastScanned, endOfPage).then(result =>
62 dispatch({
63 type: actionTypes.LOAD_PREV_NOTIFICATIONS,
64 result
65 })
66 );
67 },
talig8e9c0652017-12-20 14:30:43 +020068
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020069 notifyAboutConflicts(
70 dispatch,
71 { itemId, itemName, version, currentScreen }
72 ) {
73 let { props } = currentScreen;
74 let currentItemId = props.softwareProductId || props.licenseModelId;
75 let currentVersion = props.version;
76 if (currentItemId === itemId && currentVersion.id === version.id) {
77 MergeEditorActionHelper.analyzeSyncResult(dispatch, {
78 itemId,
79 version
80 }).then(() => ScreensHelper.loadScreen(dispatch, currentScreen));
81 } else {
82 dispatch({
83 type: modalActionTypes.GLOBAL_MODAL_WARNING,
84 data: {
85 title: i18n('Conflicts'),
86 msg: i18n(
87 'There are conflicts in {itemName} version {versionName} that you have to resolve',
88 {
89 itemName: itemName.toUpperCase(),
90 versionName: version.versionName
91 }
92 ),
93 cancelButtonText: i18n('OK')
94 }
95 });
96 }
97 },
talig8e9c0652017-12-20 14:30:43 +020098
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020099 syncItem(
100 dispatch,
101 { itemId, itemName, versionId, versionName, currentScreen }
102 ) {
103 let version = { id: versionId, versionName };
104 ItemsHelper.fetchVersion({ itemId, versionId }).then(response => {
105 let inMerge =
106 response &&
107 response.state &&
108 response.state.synchronizationState === SyncStates.MERGE;
109 if (!inMerge) {
110 ItemsHelper.performVCAction({
111 itemId,
112 version,
113 action: VersionControllerActionsEnum.SYNC
114 }).then(() => {
115 return ItemsHelper.fetchVersion({ itemId, versionId }).then(
116 response => {
117 let inMerge =
118 response &&
119 response.state &&
120 response.state.synchronizationState ===
121 SyncStates.MERGE;
122 if (!inMerge) {
123 return ScreensHelper.loadScreen(
124 dispatch,
125 currentScreen
126 );
127 } else {
128 return this.notifyAboutConflicts(dispatch, {
129 itemId,
130 itemName,
131 version,
132 currentScreen
133 });
134 }
135 }
136 );
137 });
138 } else {
139 this.notifyAboutConflicts(dispatch, {
140 itemId,
141 itemName,
142 version,
143 currentScreen
144 });
145 }
146 });
147 },
talig8e9c0652017-12-20 14:30:43 +0200148
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200149 updateNotification(dispatch, { notificationForUpdate }) {
150 updateNotification(notificationForUpdate.eventId).then(response => {
151 if (
152 response.status === 'Success' &&
153 Object.keys(response.errors).length === 0
154 ) {
155 dispatch({
156 type: actionTypes.UPDATE_READ_NOTIFICATION,
157 notificationForUpdate
158 });
159 }
160 });
161 },
talig8e9c0652017-12-20 14:30:43 +0200162
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200163 updateLastSeenNotification(dispatch, { notificationId }) {
164 updateLastSeenNotification(notificationId).then(response => {
165 if (
166 response.status === 'Success' &&
167 Object.keys(response.errors).length === 0
168 ) {
169 dispatch({ type: actionTypes.RESET_NEW_NOTIFICATIONS });
170 }
171 });
172 }
talig8e9c0652017-12-20 14:30:43 +0200173};
174
175export default UserNotificationsActionHelper;