<commit_before>const MetaTraderConfig = require('./metatrader.config');
const MetaTraderUI     = require('./metatrader.ui');
const BinarySocket     = require('../../socket');
const Client           = require('../../../base/client');
const localize         = require('../../../base/localize').localize;
const Validation       = require('../../../common_functions/form_validation');

const MetaTrader = (() => {
    'use strict';

    const types_info   = MetaTraderConfig.types_info;
    const actions_info = MetaTraderConfig.actions_info;
    const fields       = MetaTraderConfig.fields;

    let has_financial_company,
        has_gaming_company;

    const onLoad = () => {
        BinarySocket.wait('landing_company').then((response) => {
            if (isEligible(response)) {
                updateEnabledStatus('gaming', has_gaming_company);
                updateEnabledStatus('financial', has_financial_company);
                getAllAccountsInfo();
                MetaTraderUI.init(submit);
            } else {
                MetaTraderUI.displayPageError(localize('Sorry, this feature is not available.'));
            }
        });
    };

    const isEligible = (landing_company_response) => {
        let is_eligible = false;
        if (!landing_company_response.error) {
            const lc              = landing_company_response.landing_company;
            has_financial_company = lc.hasOwnProperty('mt_financial_company') && lc.mt_financial_company.shortcode === 'vanuatu';
            has_gaming_company    = lc.hasOwnProperty('mt_gaming_company') && lc.mt_gaming_company.shortcode === 'costarica';
            if (lc.hasOwnProperty('financial_company') && lc.financial_company.shortcode === 'costarica' &&
                (has_financial_company || has_gaming_company)) {
                is_eligible = true;
            }
        }
        return is_eligible;
    };

    const updateEnabledStatus = (account_type, is_enabled) => {
        Object.keys(types_info).forEach((acc_type) => {
            if (types_info[acc_type].account_type === account_type) {
                types_info[acc_type].is_enabled = is_enabled;
            }
        });
    };

    const getAllAccountsInfo = () => {
        BinarySocket.wait('mt5_login_list').then((response) => {
            // Ignore old accounts which are not linked to any group
            const mt5_login_list = (response.mt5_login_list || []).filter(obj => obj.group);

            // Update account info
            mt5_login_list.forEach((obj) => {
                const acc_type = Client.getMT5AccountType(obj.group);
                if (acc_type) {
                    types_info[acc_type].account_info = { login: obj.login };
                    getAccountDetails(obj.login, acc_type);
                }
            });

            Client.set('mt5_account', getDefaultAccount(mt5_login_list));

            // Update types with no account
            Object.keys(types_info).forEach((acc_type) => {
                if (!types_info[acc_type].account_info) {
                    MetaTraderUI.updateAccount(acc_type);
                }
            });
        });
    };

    const getDefaultAccount = login_list => (
        Object.keys(types_info).indexOf(location.hash.substring(1)) >= 0 ? location.hash.substring(1) :
            Client.get('mt5_account') ||
            (login_list && login_list.length ?
                Client.getMT5AccountType(
                    ((login_list.find(login => /real/.test(login.group)) || login_list.find(login => /demo/.test(login.group))) || {}).group) :
                'demo_vanuatu_cent')
    );

    const getAccountDetails = (login, acc_type) => {
        BinarySocket.send({
            mt5_get_settings: 1,
            login           : login,
        }).then((response) => {
            if (response.mt5_get_settings) {
                types_info[acc_type].account_info = response.mt5_get_settings;
                MetaTraderUI.updateAccount(acc_type);
            }<commit_msg>Ignore also deprecated accounts which still are being returned via WS<commit_after>const MetaTraderConfig = require('./metatrader.config');
const MetaTraderUI     = require('./metatrader.ui');
const BinarySocket     = require('../../socket');
const Client           = require('../../../base/client');
const localize         = require('../../../base/localize').localize;
const Validation       = require('../../../common_functions/form_validation');

const MetaTrader = (() => {
    'use strict';

    const types_info   = MetaTraderConfig.types_info;
    const actions_info = MetaTraderConfig.actions_info;
    const fields       = MetaTraderConfig.fields;

    let has_financial_company,
        has_gaming_company;

    const onLoad = () => {
        BinarySocket.wait('landing_company').then((response) => {
            if (isEligible(response)) {
                updateEnabledStatus('gaming', has_gaming_company);
                updateEnabledStatus('financial', has_financial_company);
                getAllAccountsInfo();
                MetaTraderUI.init(submit);
            } else {
                MetaTraderUI.displayPageError(localize('Sorry, this feature is not available.'));
            }
        });
    };

    const isEligible = (landing_company_response) => {
        let is_eligible = false;
        if (!landing_company_response.error) {
            const lc              = landing_company_response.landing_company;
            has_financial_company = lc.hasOwnProperty('mt_financial_company') && lc.mt_financial_company.shortcode === 'vanuatu';
            has_gaming_company    = lc.hasOwnProperty('mt_gaming_company') && lc.mt_gaming_company.shortcode === 'costarica';
            if (lc.hasOwnProperty('financial_company') && lc.financial_company.shortcode === 'costarica' &&
                (has_financial_company || has_gaming_company)) {
                is_eligible = true;
            }
        }
        return is_eligible;
    };

    const updateEnabledStatus = (account_type, is_enabled) => {
        Object.keys(types_info).forEach((acc_type) => {
            if (types_info[acc_type].account_type === account_type) {
                types_info[acc_type].is_enabled = is_enabled;
            }
        });
    };

    const getAllAccountsInfo = () => {
        BinarySocket.wait('mt5_login_list').then((response) => {
            // Ignore old accounts which are not linked to any group or has deprecated group
            const mt5_login_list = (response.mt5_login_list || []).filter(obj => (
                obj.group && Client.getMT5AccountType(obj.group) in types_info
            ));

            // Update account info
            mt5_login_list.forEach((obj) => {
                const acc_type = Client.getMT5AccountType(obj.group);
                types_info[acc_type].account_info = { login: obj.login };
                getAccountDetails(obj.login, acc_type);
            });

            Client.set('mt5_account', getDefaultAccount(mt5_login_list));

            // Update types with no account
            Object.keys(types_info).forEach((acc_type) => {
                if (!types_info[acc_type].account_info) {
                    MetaTraderUI.updateAccount(acc_type);
                }
            });
        });
    };

    const getDefaultAccount = login_list => (
        Object.keys(types_info).indexOf(location.hash.substring(1)) >= 0 ? location.hash.substring(1) :
            Client.get('mt5_account') ||
            (login_list && login_list.length ?
                Client.getMT5AccountType(
                    ((login_list.find(login => /real/.test(login.group)) || login_list.find(login => /demo/.test(login.group))) || {}).group) :
                'demo_vanuatu_cent')
    );

    const getAccountDetails = (login, acc_type) => {
        BinarySocket.send({
            mt5_get_settings: 1,
            login           : login,
        }).then((response) => {
            if (response.mt5_get_settings) {
                types_info[acc_type].account_info = response.mt5_get_settings;
                MetaTraderUI.updateAccount(acc_type);
            }
{'fraction_non_alphanumeric': 0.12458451311091961, 'fraction_numerical': 0.004062538471008248, 'mean_word_length': 2.133050520632472, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5563932', 'n_tokens_mistral': 2323, 'n_tokens_neox': 2241, 'n_words': 345}