Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 1 | import { actionTypes } from './UserNotificationsConstants.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 2 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
| 3 | import Configuration from 'sdc-app/config/Configuration.js'; |
| 4 | import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 5 | import WebSocketUtil, { websocketUrl } from 'nfvo-utils/WebSocketUtil.js'; |
| 6 | import { actionsEnum as VersionControllerActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 7 | import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js'; |
| 8 | import ScreensHelper from 'sdc-app/common/helpers/ScreensHelper.js'; |
| 9 | import MergeEditorActionHelper from 'sdc-app/common/merge/MergeEditorActionHelper.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 10 | import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 11 | import { SyncStates } from 'sdc-app/common/merge/MergeEditorConstants.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 12 | |
| 13 | function baseUrl() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 14 | const restPrefix = Configuration.get('restPrefix'); |
| 15 | return `${restPrefix}/v1.0/notifications`; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | function fetch() { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 19 | return RestAPIUtil.fetch(baseUrl()); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | function updateNotification(notificationId) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 23 | return RestAPIUtil.put(`${baseUrl()}/${notificationId}`); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | function updateLastSeenNotification(notificationId) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 27 | return RestAPIUtil.put(`${baseUrl()}/last-seen/${notificationId}`); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | function loadPrevNotifications(lastScanned, endOfPage) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 31 | return RestAPIUtil.fetch( |
| 32 | `${baseUrl()}?LAST_DELIVERED_EVENT_ID=${lastScanned}&END_OF_PAGE_EVENT_ID=${endOfPage}` |
| 33 | ); |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | const INITIAL_LAST_SCANNED = '00000000-0000-1000-8080-808080808080'; |
| 37 | |
| 38 | const UserNotificationsActionHelper = { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 39 | 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 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 49 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 50 | 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 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 59 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 60 | loadPreviousNotifications(dispatch, { lastScanned, endOfPage }) { |
| 61 | loadPrevNotifications(lastScanned, endOfPage).then(result => |
| 62 | dispatch({ |
| 63 | type: actionTypes.LOAD_PREV_NOTIFICATIONS, |
| 64 | result |
| 65 | }) |
| 66 | ); |
| 67 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 68 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 69 | 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 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 98 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 99 | 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 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 148 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 149 | 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 | }, |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 162 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 163 | 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 | } |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | export default UserNotificationsActionHelper; |