async function saveConnectionState()

in Frontend/main.js [49:102]


async function saveConnectionState(connection, agent, arr) {
    //This function appends the current connection status to object specified
    let connectionIndex = arr.findIndex(con => con.id === connection.connectionId);
    let tempConnection = arr[connectionIndex] || {};
    tempConnection['history'] = tempConnection['history'] || [];
    //log current status from streams.js library
    let connectionState = connection.getState();
    let isActive = connection.isActive();
    let endpoint = connection.getAddress();
    let type = connection.getType();
    let id = connection.connectionId;
    let endpointCountryIso;
    result = await Promise.all([connectionState, isActive, endpoint, type]);

    //log all status changes
    tempConnection['history'].push({
        'status': connectionState['type'],
        'timestamp': connectionState['timestamp'],
        'active': isActive
    });

    //log the first time connection reaches connecting, connected, disconnected states
    if (!tempConnection['connectingTimestamp'] && connectionState['type'] == 'connecting') tempConnection['connectingTimestamp'] = connectionState['timestamp'];
    if (!tempConnection['connectedTimestamp'] && connectionState['type'] == 'connected') tempConnection['connectedTimestamp'] = connectionState['timestamp'];
    if (!tempConnection['disconnectedTimestamp'] && connectionState['type'] == 'disconnected') tempConnection['disconnectedTimestamp'] = connectionState['timestamp'];
    
    tempConnection['type'] = type;
    tempConnection['active'] = isActive;
    tempConnection['lastUpdate'] = connectionState['timestamp'];
    tempConnection['status'] = connectionState['type'];
    tempConnection['endpoint'] = endpoint;
    tempConnection['id'] = id;
    if (agent) tempConnection['agentStatus'] = agent.getConfiguration();
    //Get connection country ISO code
    try { 
        if (type=='agent') {
             endpointCountryIso = window.libphonenumber.parsePhoneNumber(tempConnection.agentStatus.extension).country;  
             tempConnection.endpoint.phoneNumber = tempConnection.agentStatus.extension;
        }
        else {
             endpointCountryIso = window.libphonenumber.parsePhoneNumber(endpoint.phoneNumber).country; 
        }
        tempConnection['endpointCountryISO'] = endpointCountryIso;
    } catch (err) {
        console.log(err);
    }
    connectionIndex = arr.findIndex(con => con.id === connection.connectionId);
    if (connectionIndex >= 0) {
        arr[connectionIndex] = tempConnection;
    } else {
        arr.push(tempConnection);
    }
    return true;
}