From d28a2d88fb1717f3de9c5c2b5a4eb55fad8bbc4e Mon Sep 17 00:00:00 2001 From: adrienne-rio Date: Tue, 24 Sep 2024 15:58:49 +0800 Subject: [PATCH] chore: generate test link --- src/javascript/_common/analytics.js | 9 +- src/javascript/_common/base/client_base.js | 272 +++++++++------------ 2 files changed, 125 insertions(+), 156 deletions(-) diff --git a/src/javascript/_common/analytics.js b/src/javascript/_common/analytics.js index 6539482af83..597cee90d63 100644 --- a/src/javascript/_common/analytics.js +++ b/src/javascript/_common/analytics.js @@ -4,9 +4,9 @@ const Analytics = (() => { const init = () => { if (process.env.RUDDERSTACK_KEY && process.env.GROWTHBOOK_CLIENT_KEY && process.env.GROWTHBOOK_DECRYPTION_KEY) { DerivAnalytics.Analytics.initialise({ - growthbookKey: process.env.GROWTHBOOK_CLIENT_KEY, // optional key to enable A/B tests + growthbookKey : process.env.GROWTHBOOK_CLIENT_KEY, // optional key to enable A/B tests growthbookDecryptionKey: process.env.GROWTHBOOK_DECRYPTION_KEY, // optional key to enable A/B tests - rudderstackKey: process.env.RUDDERSTACK_KEY, + rudderstackKey : process.env.RUDDERSTACK_KEY, }); } }; @@ -21,15 +21,18 @@ const Analytics = (() => { if (DerivAnalytics.Analytics?.getInstances()?.ab) { return [DerivAnalytics.Analytics?.getFeatureValue(featureFlag, resolvedDefaultValue), true]; } + + return null; }; const setGrowthbookOnChange = onChange => { const isGBLoaded = isGrowthbookLoaded(); if (!isGBLoaded) return null; - DerivAnalytics.Analytics?.getInstances().ab.GrowthBook?.setRenderer(() => { + const onChangeRenderer = DerivAnalytics.Analytics?.getInstances().ab.GrowthBook?.setRenderer(() => { onChange?.(); }); + return onChangeRenderer; }; return { diff --git a/src/javascript/_common/base/client_base.js b/src/javascript/_common/base/client_base.js index 731a7597cdb..8e9f10fb1d7 100644 --- a/src/javascript/_common/base/client_base.js +++ b/src/javascript/_common/base/client_base.js @@ -1,12 +1,12 @@ -const moment = require('moment'); -const isCryptocurrency = require('./currency_base').isCryptocurrency; -const SocketCache = require('./socket_cache'); -const AuthClient = require('../auth'); -const localize = require('../localize').localize; -const LocalStore = require('../storage').LocalStore; -const State = require('../storage').State; -const getPropertyValue = require('../utility').getPropertyValue; -const isEmptyObject = require('../utility').isEmptyObject; +const moment = require('moment'); +const isCryptocurrency = require('./currency_base').isCryptocurrency; +const SocketCache = require('./socket_cache'); +const AuthClient = require('../auth'); +const localize = require('../localize').localize; +const LocalStore = require('../storage').LocalStore; +const State = require('../storage').State; +const getPropertyValue = require('../utility').getPropertyValue; +const isEmptyObject = require('../utility').isEmptyObject; const getAllowedLocalStorageOrigin = require('../url').getAllowedLocalStorageOrigin; const ClientBase = (() => { @@ -18,10 +18,14 @@ const ClientBase = (() => { const init = () => { current_loginid = LocalStore.get('active_loginid'); - client_object = getAllAccountsObject(); + client_object = getAllAccountsObject(); }; - const isLoggedIn = () => !isEmptyObject(getAllAccountsObject()) && get('loginid') && get('token'); + const isLoggedIn = () => ( + !isEmptyObject(getAllAccountsObject()) && + get('loginid') && + get('token') + ); const isValidLoginid = () => { if (!isLoggedIn()) return true; @@ -37,6 +41,9 @@ const ClientBase = (() => { * @param {String|null} loginid The account to set the value for */ const set = (key, value, loginid = current_loginid) => { + const isOAuth2Enabled = AuthClient.isOAuth2Enabled(); + if (isOAuth2Enabled) return; + if (key === 'loginid' && value !== current_loginid) { syncWithDerivApp(value, client_object); LocalStore.set('active_loginid', value); @@ -75,7 +82,7 @@ const ClientBase = (() => { return value; }; - const setTotalBalance = (amount, currency) => (total_balance = { amount, currency }); + const setTotalBalance = (amount, currency) => total_balance = { amount, currency }; const getTotalBalance = () => total_balance; @@ -85,20 +92,19 @@ const ClientBase = (() => { const getAccountType = (loginid = current_loginid) => { let account_type; - if (/^VR/.test(loginid)) account_type = 'virtual'; - else if (/^MF/.test(loginid)) account_type = 'financial'; + if (/^VR/.test(loginid)) account_type = 'virtual'; + else if (/^MF/.test(loginid)) account_type = 'financial'; else if (/^MLT|MX/.test(loginid)) account_type = 'gaming'; return account_type; }; const isAccountOfType = (type, loginid = current_loginid, only_enabled = false) => { - const this_type = getAccountType(loginid); - return ( - ((type === 'virtual' && this_type === 'virtual') || - (type === 'real' && this_type !== 'virtual') || - type === this_type) && - (only_enabled ? !get('is_disabled', loginid) : true) - ); + const this_type = getAccountType(loginid); + return (( + (type === 'virtual' && this_type === 'virtual') || + (type === 'real' && this_type !== 'virtual') || + type === this_type) && + (only_enabled ? !get('is_disabled', loginid) : true)); }; const getAccountOfType = (type, only_enabled) => { @@ -110,20 +116,22 @@ const ClientBase = (() => { // only considers currency of real money accounts // @param {String} type = crypto|fiat - const hasCurrencyType = type => { + const hasCurrencyType = (type) => { const loginids = getAllLoginids(); if (type === 'crypto') { // find if has crypto currency account - return loginids.find(loginid => !get('is_virtual', loginid) && isCryptocurrency(get('currency', loginid))); + return loginids.find(loginid => + !get('is_virtual', loginid) && isCryptocurrency(get('currency', loginid))); } // else find if have fiat currency account - return loginids.find(loginid => !get('is_virtual', loginid) && !isCryptocurrency(get('currency', loginid))); + return loginids.find(loginid => + !get('is_virtual', loginid) && !isCryptocurrency(get('currency', loginid))); }; const hasOnlyCurrencyType = (type = 'fiat') => { const loginids = getAllLoginids(); const real_loginid = /^(MX|MF|MLT|CR|FOG)[0-9]+$/i; - const only_real_loginids = loginids.filter(loginid => real_loginid.test(loginid)); + const only_real_loginids = loginids.filter((loginid) => real_loginid.test(loginid)); if (type === 'crypto') { return only_real_loginids.every(loginid => isCryptocurrency(get('currency', loginid))); } @@ -131,12 +139,10 @@ const ClientBase = (() => { return only_real_loginids.every(loginid => !get('currency', loginid)); } - return only_real_loginids.every( - loginid => get('currency', loginid) && !isCryptocurrency(get('currency', loginid)) - ); + return only_real_loginids.every(loginid => get('currency', loginid) && !isCryptocurrency(get('currency', loginid))); }; - const isWalletsAccount = loginid => { + const isWalletsAccount = (loginid) => { if (typeof loginid === 'undefined') { return false; } @@ -146,18 +152,17 @@ const ClientBase = (() => { } return account_object.account_category === 'wallet'; }; - - const hasWalletsAccount = () => - Object.values(getAllAccountsObject()).some(account => account.account_category === 'wallet'); + + const hasWalletsAccount = () => Object.values(getAllAccountsObject()).some(account => account.account_category === 'wallet'); const TypesMapConfig = (() => { let types_map_config; const initTypesMap = () => ({ - default: localize('Real'), + default : localize('Real'), financial: localize('Investment'), - gaming: localize('Gaming'), - virtual: localize('Virtual'), + gaming : localize('Gaming'), + virtual : localize('Virtual'), }); return { @@ -172,10 +177,10 @@ const ClientBase = (() => { const getAccountTitle = loginid => { const types_map = TypesMapConfig.get(); - return types_map[getAccountType(loginid)] || types_map.default; + return (types_map[getAccountType(loginid)] || types_map.default); }; - const responseAuthorize = response => { + const responseAuthorize = (response) => { const authorize = response.authorize; const local_currency_config = {}; const local_currencies = Object.keys(authorize.local_currencies); @@ -184,9 +189,9 @@ const ClientBase = (() => { local_currency_config.decimal_places = +authorize.local_currencies[local_currency_config.currency].fractional_digits; } - set('email', authorize.email); - set('country', authorize.country); - set('currency', authorize.currency); + set('email', authorize.email); + set('country', authorize.country); + set('currency', authorize.currency); set('is_virtual', +authorize.is_virtual); set('session_start', parseInt(moment().valueOf() / 1000)); set('landing_company_shortcode', authorize.landing_company_name); @@ -195,10 +200,10 @@ const ClientBase = (() => { updateAccountList(authorize.account_list); }; - const updateAccountList = account_list => { - account_list.forEach(account => { + const updateAccountList = (account_list) => { + account_list.forEach((account) => { set('excluded_until', account.excluded_until || '', account.loginid); - Object.keys(account).forEach(param => { + Object.keys(account).forEach((param) => { const param_to_set = param === 'country' ? 'residence' : param; const value_to_set = typeof account[param] === 'undefined' ? '' : account[param]; if (param_to_set !== 'loginid') { @@ -211,17 +216,17 @@ const ClientBase = (() => { const shouldAcceptTnc = () => { if (get('is_virtual')) return false; const website_tnc_version = State.getResponse('website_status.terms_conditions_version'); - const client_tnc_status = State.getResponse('get_settings.client_tnc_status'); + const client_tnc_status = State.getResponse('get_settings.client_tnc_status'); return typeof client_tnc_status !== 'undefined' && client_tnc_status !== website_tnc_version; }; const clearAllAccounts = () => { current_loginid = undefined; - client_object = {}; + client_object = {}; LocalStore.setObject(storage_key, client_object); }; - const setNewAccount = options => { + const setNewAccount = (options) => { if (!options.email || !options.loginid || !options.token) { return false; } @@ -229,25 +234,24 @@ const ClientBase = (() => { SocketCache.clear(); localStorage.setItem('GTM_new_account', '1'); - set('token', options.token, options.loginid); - set('email', options.email, options.loginid); + set('token', options.token, options.loginid); + set('email', options.email, options.loginid); set('is_virtual', +options.is_virtual, options.loginid); - set('loginid', options.loginid); + set('loginid', options.loginid); return true; }; const currentLandingCompany = () => { const landing_company_response = State.getResponse('landing_company') || {}; - const this_shortcode = get('landing_company_shortcode'); - const landing_company_prop = Object.keys(landing_company_response).find( - key => this_shortcode === landing_company_response[key].shortcode - ); + const this_shortcode = get('landing_company_shortcode'); + const landing_company_prop = Object.keys(landing_company_response).find((key) => ( + this_shortcode === landing_company_response[key].shortcode + )); return landing_company_response[landing_company_prop] || {}; }; - const shouldCompleteTax = () => - isAccountOfType('financial') && + const shouldCompleteTax = () => isAccountOfType('financial') && !/crs_tin_information/.test((State.getResponse('get_account_status') || {}).status); const isAuthenticationAllowed = () => { @@ -270,17 +274,17 @@ const ClientBase = (() => { gaming: { financial: { short: localize('Synthetic'), - full: is_demo ? localize('Demo Synthetic') : localize('Real Synthetic'), + full : is_demo ? localize('Demo Synthetic') : localize('Real Synthetic'), }, }, financial: { financial: { short: localize('Financial'), - full: is_demo ? localize('Demo Financial') : localize('Real Financial'), + full : is_demo ? localize('Demo Financial') : localize('Real Financial'), }, financial_stp: { short: localize('Financial STP'), - full: is_demo ? localize('Demo Financial STP') : localize('Real Financial STP'), + full : is_demo ? localize('Demo Financial STP') : localize('Real Financial STP'), }, }, }; @@ -300,34 +304,26 @@ const ClientBase = (() => { const current_landing_company = get('landing_company_shortcode'); let allowed_currencies = []; if (current_loginid) { - allowed_currencies = getLandingCompanyValue( - current_loginid, - landing_company_obj, - 'legal_allowed_currencies' - ); + allowed_currencies = getLandingCompanyValue(current_loginid, landing_company_obj, 'legal_allowed_currencies'); } // create multiple accounts only available for landing companies with legal_allowed_currencies - can_open_multi = !!( - upgradeable_landing_companies.indexOf(current_landing_company) !== -1 && - allowed_currencies && - allowed_currencies.length - ); + can_open_multi = !!(upgradeable_landing_companies.indexOf(current_landing_company) !== -1 && + (allowed_currencies && allowed_currencies.length)); // only show upgrade message to landing companies other than current const canUpgrade = (...landing_companies) => { - const result = landing_companies.filter( - landing_company => - landing_company !== current_landing_company && - upgradeable_landing_companies.indexOf(landing_company) !== -1 - ); + const result = landing_companies.filter(landing_company => ( + landing_company !== current_landing_company && + upgradeable_landing_companies.indexOf(landing_company) !== -1 + )); return result.length ? result : []; }; can_upgrade_to = canUpgrade('iom', 'svg', 'malta', 'maltainvest'); if (can_upgrade_to.length) { - type = can_upgrade_to.map(landing_company_shortcode => - landing_company_shortcode === 'maltainvest' ? 'financial' : 'real' + type = can_upgrade_to.map( + landing_company_shortcode => landing_company_shortcode === 'maltainvest' ? 'financial' : 'real', ); } } @@ -353,11 +349,12 @@ const ClientBase = (() => { } } else { const financial_company = (getPropertyValue(landing_company, 'financial_company') || {})[key] || []; - const gaming_company = (getPropertyValue(landing_company, 'gaming_company') || {})[key] || []; + const gaming_company = (getPropertyValue(landing_company, 'gaming_company') || {})[key] || []; - landing_company_object = Array.isArray(financial_company) - ? financial_company.concat(gaming_company) - : $.extend({}, financial_company, gaming_company); + landing_company_object = Array.isArray(financial_company) ? + financial_company.concat(gaming_company) + : + $.extend({}, financial_company, gaming_company); return landing_company_object; } @@ -367,13 +364,15 @@ const ClientBase = (() => { const getRiskAssessment = () => { const status = State.getResponse('get_account_status.status'); - return isAccountOfType('financial') - ? /(financial_assessment|trading_experience)_not_complete/.test(status) - : /financial_assessment_not_complete/.test(status); + return ( + isAccountOfType('financial') ? + /(financial_assessment|trading_experience)_not_complete/.test(status) : + /financial_assessment_not_complete/.test(status) + ); }; // API_V3: send a list of accounts the client can transfer to - const canTransferFunds = account => { + const canTransferFunds = (account) => { if (account) { // this specific account can be used to transfer funds to return canTransferFundsTo(account.loginid); @@ -382,17 +381,13 @@ const ClientBase = (() => { return Object.keys(client_object).some(loginid => canTransferFundsTo(loginid)); }; - const canTransferFundsTo = to_loginid => { - if ( - to_loginid === current_loginid || - get('is_virtual', to_loginid) || - get('is_virtual') || - get('is_disabled', to_loginid) - ) { + const canTransferFundsTo = (to_loginid) => { + if (to_loginid === current_loginid || get('is_virtual', to_loginid) || get('is_virtual') || + get('is_disabled', to_loginid)) { return false; } const from_currency = get('currency'); - const to_currency = get('currency', to_loginid); + const to_currency = get('currency', to_loginid); if (!from_currency || !to_currency) { return false; } @@ -401,26 +396,26 @@ const ClientBase = (() => { // these landing companies are allowed to transfer funds to each other if they have the same currency const same_cur_allowed = { maltainvest: 'malta', - malta: 'maltainvest', + malta : 'maltainvest', }; const from_landing_company = get('landing_company_shortcode'); - const to_landing_company = get('landing_company_shortcode', to_loginid); + const to_landing_company = get('landing_company_shortcode', to_loginid); // if same_cur_allowed[from_landing_company] is undefined and to_landing_company is also undefined, it will return true // so we should compare '' === undefined instead return (same_cur_allowed[from_landing_company] || '') === to_landing_company; } // or for other clients if current account is cryptocurrency it should only transfer to fiat currencies and vice versa const is_from_crypto = isCryptocurrency(from_currency); - const is_to_crypto = isCryptocurrency(to_currency); - return is_from_crypto ? !is_to_crypto : is_to_crypto; + const is_to_crypto = isCryptocurrency(to_currency); + return (is_from_crypto ? !is_to_crypto : is_to_crypto); }; - const hasSvgAccount = () => !!getAllLoginids().find(loginid => /^CR/.test(loginid)); + const hasSvgAccount = () => !!(getAllLoginids().find(loginid => /^CR/.test(loginid))); const canChangeCurrency = (statement, mt5_login_list, is_current = true) => { - const currency = get('currency'); - const has_no_mt5 = !mt5_login_list || !mt5_login_list.length; - const has_no_transaction = statement.count === 0 && statement.transactions.length === 0; + const currency = get('currency'); + const has_no_mt5 = !mt5_login_list || !mt5_login_list.length; + const has_no_transaction = (statement.count === 0 && statement.transactions.length === 0); const has_account_criteria = has_no_transaction && has_no_mt5; // Current API requirements for currently logged-in user successfully changing their account's currency: @@ -428,14 +423,11 @@ const ClientBase = (() => { // 2. User must not have any MT5 account // 3. Not be a crypto account // 4. Not be a virtual account - return is_current - ? currency && !get('is_virtual') && has_account_criteria && !isCryptocurrency(currency) - : has_account_criteria; + return is_current ? currency && !get('is_virtual') && has_account_criteria && !isCryptocurrency(currency) : has_account_criteria; }; const isMF = () => { - const landing_company_shortcode = - get('landing_company_shortcode') || State.getResponse('landing_company.gaming_company.shortcode'); + const landing_company_shortcode = get('landing_company_shortcode') || State.getResponse('landing_company.gaming_company.shortcode'); return landing_company_shortcode === 'maltainvest'; }; @@ -472,19 +464,13 @@ const ClientBase = (() => { const financial_restricted_countries = financial_company_shortcode === 'svg' && !gaming_company_shortcode; const CFDs_restricted_countries = gaming_company_shortcode === 'svg' && !financial_company_shortcode; - + const restricted_countries = financial_company_shortcode === 'svg' || (gaming_company_shortcode === 'svg' && financial_company_shortcode !== 'maltainvest'); - + const high_risk = financial_company_shortcode === 'svg' && gaming_company_shortcode === 'svg'; - return ( - high_risk || - restricted_countries || - risk_classification === 'high' || - financial_restricted_countries || - CFDs_restricted_countries - ); + return high_risk || restricted_countries || risk_classification === 'high' || financial_restricted_countries || CFDs_restricted_countries; } return false; @@ -499,72 +485,52 @@ const ClientBase = (() => { if (landing_companies.gaming_company) { gaming_company_shortcode = landing_companies.gaming_company.shortcode; } - const low_risk_landing_company = - financial_company_shortcode === 'maltainvest' && gaming_company_shortcode === 'svg'; - return ( - low_risk_landing_company || - (upgradeable_landing_companies.include('svg') && upgradeable_landing_companies.include('maltainvest')) - ); + const low_risk_landing_company = financial_company_shortcode === 'maltainvest' && gaming_company_shortcode === 'svg'; + return low_risk_landing_company || (upgradeable_landing_companies.include('svg') && upgradeable_landing_companies.include('maltainvest')); } return false; }; const syncWithDerivApp = (active_loginid, client_accounts) => { - const isHydraEnabled = AuthClient.isOAuth2Enabled(); - - if (isHydraEnabled) return; - const iframe_window = document.getElementById('localstorage-sync'); const origin = getAllowedLocalStorageOrigin(); if (!iframe_window || !origin) return; - if (document.readyState === 'complete') { + if (document.readyState === 'complete'){ iframe_window.onload = () => { // Keep client.accounts in sync (in case user wasn't logged in). if (iframe_window.src === `${origin}/localstorage-sync.html`) { - iframe_window.contentWindow.postMessage( - { - key: 'client.accounts', - value: JSON.stringify(client_accounts), - }, - origin - ); - iframe_window.contentWindow.postMessage( - { - key: 'active_loginid', - value: active_loginid, - }, - origin - ); + iframe_window.contentWindow.postMessage({ + key : 'client.accounts', + value: JSON.stringify(client_accounts), + }, origin); + iframe_window.contentWindow.postMessage({ + key : 'active_loginid', + value: active_loginid, + }, origin); } }; return; } - if (!has_readystate_listener) { + if (!has_readystate_listener){ has_readystate_listener = true; document.addEventListener('readystatechange', () => { iframe_window.onload = () => { // Keep client.accounts in sync (in case user wasn't logged in). if (iframe_window.src === `${origin}/localstorage-sync.html`) { - iframe_window.contentWindow.postMessage( - { - key: 'client.accounts', - value: JSON.stringify(client_accounts), - }, - origin - ); - iframe_window.contentWindow.postMessage( - { - key: 'active_loginid', - value: active_loginid, - }, - origin - ); + iframe_window.contentWindow.postMessage({ + key : 'client.accounts', + value: JSON.stringify(client_accounts), + }, origin); + iframe_window.contentWindow.postMessage({ + key : 'active_loginid', + value: active_loginid, + }, origin); } }; });