aboutsummaryrefslogtreecommitdiffstats
path: root/test/csit/plans/dcaegen2
AgeCommit message (Expand)AuthorFilesLines
2018-03-07Update image tagLusheng Ji1-1/+1
2017-09-27Add dcaegen2 test cases for HTTPSpswang1-1/+2
2017-09-25Rename dcae directory to dcaegen2pswang3-0/+59
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
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;