Skip to content

Commit 3139f3d

Browse files
Refactor client
1 parent 21f01c4 commit 3139f3d

File tree

5 files changed

+377
-349
lines changed

5 files changed

+377
-349
lines changed
Lines changed: 316 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,316 @@
1+
const moment = require('moment');
2+
const BinarySocket = require('../../app/base/socket');
3+
const SocketCache = require('../../app/base/socket_cache');
4+
const isCryptocurrency = require('../../app/common/currency').isCryptocurrency;
5+
const LocalStore = require('../storage').LocalStore;
6+
const State = require('../storage').State;
7+
const getPropertyValue = require('../utility').getPropertyValue;
8+
const isEmptyObject = require('../utility').isEmptyObject;
9+
10+
const ClientBase = (() => {
11+
const storage_key = 'client.accounts';
12+
let client_object = {};
13+
let current_loginid;
14+
15+
const init = () => {
16+
current_loginid = LocalStore.get('active_loginid');
17+
client_object = getAllAccountsObject();
18+
};
19+
20+
const isLoggedIn = () => (
21+
!isEmptyObject(getAllAccountsObject()) &&
22+
get('loginid') &&
23+
get('token')
24+
);
25+
26+
const isValidLoginid = () => {
27+
if (!isLoggedIn()) return true;
28+
const valid_login_ids = new RegExp('^(MX|MF|VRTC|MLT|CR|FOG)[0-9]+$', 'i');
29+
return getAllLoginids().every(loginid => valid_login_ids.test(loginid));
30+
};
31+
32+
/**
33+
* Stores the client information in local variable and localStorage
34+
*
35+
* @param {String} key The property name to set
36+
* @param {String|Number|Object} value The regarding value
37+
* @param {String|null} loginid The account to set the value for
38+
*/
39+
const set = (key, value, loginid = current_loginid) => {
40+
if (key === 'loginid' && value !== current_loginid) {
41+
LocalStore.set('active_loginid', value);
42+
current_loginid = value;
43+
} else {
44+
if (!(loginid in client_object)) {
45+
client_object[loginid] = {};
46+
}
47+
client_object[loginid][key] = value;
48+
LocalStore.setObject(storage_key, client_object);
49+
}
50+
};
51+
52+
/**
53+
* Returns the client information
54+
*
55+
* @param {String|null} key The property name to return the value from, if missing returns the account object
56+
* @param {String|null} loginid The account to return the value from
57+
*/
58+
const get = (key, loginid = current_loginid) => {
59+
let value;
60+
if (key === 'loginid') {
61+
value = loginid || LocalStore.get('active_loginid');
62+
} else {
63+
const current_client = client_object[loginid] || getAllAccountsObject()[loginid] || client_object;
64+
65+
value = key ? current_client[key] : current_client;
66+
}
67+
if (!Array.isArray(value) && (+value === 1 || +value === 0 || value === 'true' || value === 'false')) {
68+
value = JSON.parse(value || false);
69+
}
70+
return value;
71+
};
72+
73+
const getAllAccountsObject = () => LocalStore.getObject(storage_key);
74+
75+
const getAllLoginids = () => Object.keys(getAllAccountsObject());
76+
77+
const getAccountType = (loginid = current_loginid) => {
78+
let account_type;
79+
if (/^VR/.test(loginid)) account_type = 'virtual';
80+
else if (/^MF/.test(loginid)) account_type = 'financial';
81+
else if (/^MLT/.test(loginid)) account_type = 'gaming';
82+
return account_type;
83+
};
84+
85+
const isAccountOfType = (type, loginid = current_loginid, only_enabled = false) => {
86+
const this_type = getAccountType(loginid);
87+
return ((
88+
(type === 'virtual' && this_type === 'virtual') ||
89+
(type === 'real' && this_type !== 'virtual') ||
90+
type === this_type) &&
91+
(only_enabled ? !get('is_disabled', loginid) : true));
92+
};
93+
94+
const getAccountOfType = (type, only_enabled) => {
95+
const id = getAllLoginids().find(loginid => isAccountOfType(type, loginid, only_enabled));
96+
return id ? $.extend({ loginid: id }, get(null, id)) : {};
97+
};
98+
99+
const hasAccountType = (type, only_enabled) => !isEmptyObject(getAccountOfType(type, only_enabled));
100+
101+
// only considers currency of real money accounts
102+
// @param {String} type = crypto|fiat
103+
const hasCurrencyType = (type) => {
104+
const loginids = getAllLoginids();
105+
if (type === 'crypto') {
106+
// find if has crypto currency account
107+
return loginids.find(loginid =>
108+
!get('is_virtual', loginid) && isCryptocurrency(get('currency', loginid)));
109+
}
110+
// else find if have fiat currency account
111+
return loginids.find(loginid =>
112+
!get('is_virtual', loginid) && !isCryptocurrency(get('currency', loginid)));
113+
};
114+
115+
const types_map = {
116+
virtual : 'Virtual',
117+
gaming : 'Gaming',
118+
financial: 'Investment',
119+
};
120+
121+
const getAccountTitle = loginid => types_map[getAccountType(loginid)] || 'Real';
122+
123+
const responseAuthorize = (response) => {
124+
const authorize = response.authorize;
125+
set('email', authorize.email);
126+
set('currency', authorize.currency);
127+
set('is_virtual', +authorize.is_virtual);
128+
set('session_start', parseInt(moment().valueOf() / 1000));
129+
set('landing_company_shortcode', authorize.landing_company_name);
130+
updateAccountList(authorize.account_list);
131+
};
132+
133+
const updateAccountList = (account_list) => {
134+
account_list.forEach((account) => {
135+
set('excluded_until', account.excluded_until || '', account.loginid);
136+
Object.keys(account).forEach((param) => {
137+
const param_to_set = param === 'country' ? 'residence' : param;
138+
const value_to_set = typeof account[param] === 'undefined' ? '' : account[param];
139+
if (param_to_set !== 'loginid') {
140+
set(param_to_set, value_to_set, account.loginid);
141+
}
142+
});
143+
});
144+
};
145+
146+
const shouldAcceptTnc = () => {
147+
if (get('is_virtual')) return false;
148+
const website_tnc_version = State.getResponse('website_status.terms_conditions_version');
149+
const client_tnc_status = State.getResponse('get_settings.client_tnc_status');
150+
return typeof client_tnc_status !== 'undefined' && client_tnc_status !== website_tnc_version;
151+
};
152+
153+
const clearAllAccounts = () => {
154+
current_loginid = undefined;
155+
client_object = {};
156+
LocalStore.setObject(storage_key, client_object);
157+
};
158+
159+
const setNewAccount = (options) => {
160+
if (!options.email || !options.loginid || !options.token) {
161+
return false;
162+
}
163+
164+
SocketCache.clear();
165+
localStorage.setItem('GTM_new_account', '1');
166+
167+
set('token', options.token, options.loginid);
168+
set('email', options.email, options.loginid);
169+
set('is_virtual', +options.is_virtual, options.loginid);
170+
set('loginid', options.loginid);
171+
172+
return true;
173+
};
174+
175+
const sendLogoutRequest = (show_login_page) => {
176+
if (show_login_page) {
177+
sessionStorage.setItem('showLoginPage', 1);
178+
}
179+
BinarySocket.send({ logout: '1' });
180+
};
181+
182+
const currentLandingCompany = () => {
183+
const landing_company_response = State.getResponse('landing_company') || {};
184+
const this_shortcode = get('landing_company_shortcode');
185+
const landing_company_prop = Object.keys(landing_company_response).find((key) => (
186+
this_shortcode === landing_company_response[key].shortcode
187+
));
188+
return landing_company_response[landing_company_prop] || {};
189+
};
190+
191+
const shouldCompleteTax = () => isAccountOfType('financial') && !/crs_tin_information/.test((State.getResponse('get_account_status') || {}).status);
192+
193+
const getMT5AccountType = group => (group ? group.replace('\\', '_').replace(/_(\d+|master)/, '') : ''); // remove manager id or master distinction from group
194+
195+
const getBasicUpgradeInfo = () => {
196+
const upgradeable_landing_companies = State.getResponse('authorize.upgradeable_landing_companies');
197+
198+
let can_open_multi = false;
199+
let type,
200+
can_upgrade_to;
201+
202+
if ((upgradeable_landing_companies || []).length) {
203+
const current_landing_company = get('landing_company_shortcode');
204+
205+
can_open_multi = upgradeable_landing_companies.indexOf(current_landing_company) !== -1;
206+
207+
// only show upgrade message to landing companies other than current
208+
const canUpgrade = (...landing_companies) => landing_companies.find(landing_company => (
209+
landing_company !== current_landing_company &&
210+
upgradeable_landing_companies.indexOf(landing_company) !== -1
211+
));
212+
213+
can_upgrade_to = canUpgrade('costarica', 'iom', 'malta', 'maltainvest', 'japan');
214+
if (can_upgrade_to) {
215+
type = can_upgrade_to === 'maltainvest' ? 'financial' : 'real';
216+
}
217+
}
218+
219+
return {
220+
type,
221+
can_upgrade: !!can_upgrade_to,
222+
can_upgrade_to,
223+
can_open_multi,
224+
};
225+
};
226+
227+
const getLandingCompanyValue = (loginid, landing_company, key) => {
228+
let landing_company_object;
229+
if (loginid.financial || isAccountOfType('financial', loginid)) {
230+
landing_company_object = getPropertyValue(landing_company, 'financial_company');
231+
} else if (loginid.real || isAccountOfType('real', loginid)) {
232+
landing_company_object = getPropertyValue(landing_company, 'gaming_company');
233+
234+
// handle accounts that don't have gaming company
235+
if (!landing_company_object) {
236+
landing_company_object = getPropertyValue(landing_company, 'financial_company');
237+
}
238+
} else {
239+
const financial_company = (getPropertyValue(landing_company, 'financial_company') || {})[key] || [];
240+
const gaming_company = (getPropertyValue(landing_company, 'gaming_company') || {})[key] || [];
241+
landing_company_object = financial_company.concat(gaming_company);
242+
return landing_company_object;
243+
}
244+
return (landing_company_object || {})[key];
245+
};
246+
247+
248+
// API_V3: send a list of accounts the client can transfer to
249+
const canTransferFunds = (account) => {
250+
if (account) {
251+
// this specific account can be used to transfer funds to
252+
return canTransferFundsTo(account.loginid);
253+
}
254+
// at least one account can be used to transfer funds to
255+
return Object.keys(client_object).some(loginid => canTransferFundsTo(loginid));
256+
};
257+
258+
const canTransferFundsTo = (to_loginid) => {
259+
if (to_loginid === current_loginid || get('is_virtual', to_loginid) || get('is_virtual') || get('is_disabled', to_loginid)) {
260+
return false;
261+
}
262+
const from_currency = get('currency');
263+
const to_currency = get('currency', to_loginid);
264+
if (!from_currency || !to_currency) {
265+
return false;
266+
}
267+
// only transfer to other accounts that have the same currency as current account if one is maltainvest and one is malta
268+
if (from_currency === to_currency) {
269+
// these landing companies are allowed to transfer funds to each other if they have the same currency
270+
const same_cur_allowed = {
271+
maltainvest: 'malta',
272+
malta : 'maltainvest',
273+
};
274+
const from_landing_company = get('landing_company_shortcode');
275+
const to_landing_company = get('landing_company_shortcode', to_loginid);
276+
// if same_cur_allowed[from_landing_company] is undefined and to_landing_company is also undefined, it will return true
277+
// so we should compare '' === undefined instead
278+
return (same_cur_allowed[from_landing_company] || '') === to_landing_company;
279+
}
280+
// or for other clients if current account is cryptocurrency it should only transfer to fiat currencies and vice versa
281+
const is_from_crypto = isCryptocurrency(from_currency);
282+
const is_to_crypto = isCryptocurrency(to_currency);
283+
return (is_from_crypto ? !is_to_crypto : is_to_crypto);
284+
};
285+
286+
const hasCostaricaAccount = () => !!(getAllLoginids().find(loginid => /^CR/.test(loginid)));
287+
288+
return {
289+
init,
290+
isLoggedIn,
291+
isValidLoginid,
292+
set,
293+
get,
294+
getAllLoginids,
295+
getAccountType,
296+
isAccountOfType,
297+
getAccountOfType,
298+
hasAccountType,
299+
hasCurrencyType,
300+
getAccountTitle,
301+
responseAuthorize,
302+
shouldAcceptTnc,
303+
clearAllAccounts,
304+
setNewAccount,
305+
sendLogoutRequest,
306+
currentLandingCompany,
307+
shouldCompleteTax,
308+
getMT5AccountType,
309+
getBasicUpgradeInfo,
310+
getLandingCompanyValue,
311+
canTransferFunds,
312+
hasCostaricaAccount,
313+
};
314+
})();
315+
316+
module.exports = ClientBase;

src/javascript/_common/storage.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,29 @@ CookieStorage.prototype = {
167167
},
168168
};
169169

170+
const removeCookies = (...cookie_names) => {
171+
const domains = [
172+
`.${document.domain.split('.').slice(-2).join('.')}`,
173+
`.${document.domain}`,
174+
];
175+
176+
let parent_path = window.location.pathname.split('/', 2)[1];
177+
if (parent_path !== '') {
178+
parent_path = `/${parent_path}`;
179+
}
180+
181+
cookie_names.forEach((c) => {
182+
Cookies.remove(c, { path: '/', domain: domains[0] });
183+
Cookies.remove(c, { path: '/', domain: domains[1] });
184+
Cookies.remove(c);
185+
if (new RegExp(c).test(document.cookie) && parent_path) {
186+
Cookies.remove(c, { path: parent_path, domain: domains[0] });
187+
Cookies.remove(c, { path: parent_path, domain: domains[1] });
188+
Cookies.remove(c, { path: parent_path });
189+
}
190+
});
191+
};
192+
170193
let SessionStore,
171194
LocalStore;
172195

@@ -187,6 +210,7 @@ if (!SessionStore) {
187210
module.exports = {
188211
isStorageSupported,
189212
CookieStorage,
213+
removeCookies,
190214
State,
191215
SessionStore,
192216
LocalStore,

0 commit comments

Comments
 (0)