@@ -4,10 +4,14 @@ const moment = require('moment');
44const generateBirthDate = require ( './attach_dom/birth_date_picker' ) ;
55const FormManager = require ( './form_manager' ) ;
66const BinaryPjax = require ( '../base/binary_pjax' ) ;
7- const Client = require ( '../base/client ' ) ;
7+ const Header = require ( '../base/header ' ) ;
88const BinarySocket = require ( '../base/socket' ) ;
9+ const Client = require ( '../base/client' ) ;
910const professionalClient = require ( '../pages/user/account/settings/professional_client' ) ;
11+ const param = require ( '../../_common/url' ) . param ;
12+ const ClientBase = require ( '../../_common/base/client_base' ) ;
1013const CommonFunctions = require ( '../../_common/common_functions' ) ;
14+ const getElementById = require ( '../../_common/common_functions' ) . getElementById ;
1115const localize = require ( '../../_common/localize' ) . localize ;
1216const State = require ( '../../_common/storage' ) . State ;
1317const toISOFormat = require ( '../../_common/string_util' ) . toISOFormat ;
@@ -16,14 +20,47 @@ const urlFor = require('../../_common/url').urlFor;
1620const getPropertyValue = require ( '../../_common/utility' ) . getPropertyValue ;
1721
1822const AccountOpening = ( ( ) => {
23+
24+ const excluded_countries = [ 'im' , 'au' , 'sg' , 'no' ] ;
25+
26+ const getSinupPageLink = ( upgrade_info , account_type ) => {
27+ const get_settings = State . getResponse ( 'get_settings' ) ;
28+ const country_code = get_settings . country_code ;
29+ if ( excluded_countries . includes ( country_code ) ) { // old flow
30+ return urlFor ( upgrade_info . upgrade_links [ account_type ] ) ;
31+ } // new flow
32+ return urlFor ( '/new_account/real_account' , `account_type=${ account_type } ` ) ;
33+
34+ } ;
35+
1936 const redirectAccount = ( ) => {
2037 const upgrade_info = Client . getUpgradeInfo ( ) ;
21-
2238 if ( ! upgrade_info . can_upgrade ) {
2339 BinaryPjax . loadPreviousUrl ( ) ;
2440 return - 1 ;
2541 }
42+ const country_code = State . getResponse ( 'get_settings' ) . country_code ;
2643
44+ if ( window . location . pathname . includes ( '/real_account' ) ) { // new signup flow
45+ if ( excluded_countries . includes ( country_code ) ) {
46+ BinaryPjax . load ( 'user/accounts' ) ;
47+ }
48+ const real_account_signup_target = param ( 'account_type' ) ;
49+ const is_in_correct_path = upgrade_info . can_upgrade_to . some ( lc => lc === real_account_signup_target ) ;
50+ if ( ! is_in_correct_path ) {
51+ const upgradable_accounts_count = upgrade_info . can_upgrade_to . length ;
52+ if ( upgradable_accounts_count > 1 ) {
53+ BinaryPjax . load ( 'user/accounts' ) ;
54+ } else if ( upgradable_accounts_count === 1 ) {
55+ window . location . replace ( urlFor ( '/new_account/real_account' , `account_type=${ upgrade_info . can_upgrade_to [ 0 ] } ` ) ) ;
56+ }
57+ return 1 ;
58+ }
59+ return 0 ;
60+ } // old signup flow (only for excluded countries)
61+ if ( ! excluded_countries . includes ( country_code ) ) {
62+ BinaryPjax . load ( 'user/accounts' ) ;
63+ }
2764 if ( ! upgrade_info . is_current_path ) {
2865 const upgradable_accounts_count = Object . keys ( upgrade_info . upgrade_links ) . length ;
2966 if ( upgradable_accounts_count > 1 ) {
@@ -34,6 +71,17 @@ const AccountOpening = (() => {
3471 return 1 ;
3572 }
3673 return 0 ;
74+
75+ } ;
76+
77+ const showResponseError = ( response ) => {
78+ getElementById ( 'loading' ) . setVisibility ( 0 ) ;
79+ getElementById ( 'real_account_wrapper' ) . setVisibility ( 1 ) ;
80+ const $notice_box = $ ( '#client_message' ) . find ( '.notice-msg' ) ;
81+ $ ( '#submit-message' ) . empty ( ) ;
82+ $notice_box . text ( response . msg_type === 'sanity_check' ? localize ( 'There was some invalid character in an input field.' ) : response . error . message ) . end ( )
83+ . setVisibility ( 1 ) ;
84+ $ . scrollTo ( $notice_box , 500 , { offset : - 150 } ) ;
3785 } ;
3886
3987 const populateForm = ( form_id , getValidations , is_financial ) => {
@@ -205,21 +253,6 @@ const AccountOpening = (() => {
205253 }
206254 } ;
207255
208- const handleTaxIdentificationNumber = ( ) => {
209- BinarySocket . wait ( 'get_settings' ) . then ( ( response ) => {
210- const tax_identification_number = response . get_settings . tax_identification_number ;
211- if ( tax_identification_number ) {
212- $ ( '#lbl_tax_identification_number' ) . text ( tax_identification_number ) ;
213- CommonFunctions . getElementById ( 'row_lbl_tax_identification_number' ) . setVisibility ( 1 ) ;
214- $ ( '#tax_identification_number' )
215- . val ( tax_identification_number ) // Set value for validation
216- . attr ( { 'data-force' : true , 'data-value' : tax_identification_number } ) ;
217- } else {
218- CommonFunctions . getElementById ( 'row_tax_identification_number' ) . setVisibility ( 1 ) ;
219- }
220- } ) ;
221- } ;
222-
223256 const handleState = ( states_list , form_id , getValidations ) => {
224257 const address_state_id = '#address_state' ;
225258 BinarySocket . wait ( 'get_settings' ) . then ( ( response ) => {
@@ -256,6 +289,7 @@ const AccountOpening = (() => {
256289 }
257290 } ) ;
258291 } ;
292+
259293 const handleNewAccount = ( response , message_type ) => {
260294 if ( response . error ) {
261295 const error_message = response . error . message ;
@@ -275,6 +309,21 @@ const AccountOpening = (() => {
275309 }
276310 } ;
277311
312+ const handleTaxIdentificationNumber = ( ) => {
313+ BinarySocket . wait ( 'get_settings' ) . then ( ( response ) => {
314+ const tax_identification_number = response . get_settings . tax_identification_number ;
315+ if ( tax_identification_number ) {
316+ $ ( '#lbl_tax_identification_number' ) . text ( tax_identification_number ) ;
317+ CommonFunctions . getElementById ( 'row_lbl_tax_identification_number' ) . setVisibility ( 1 ) ;
318+ $ ( '#tax_identification_number' )
319+ . val ( tax_identification_number ) // Set value for validation
320+ . attr ( { 'data-force' : true , 'data-value' : tax_identification_number } ) ;
321+ } else {
322+ CommonFunctions . getElementById ( 'row_tax_identification_number' ) . setVisibility ( 1 ) ;
323+ }
324+ } ) ;
325+ } ;
326+
278327 const commonValidations = ( ) => {
279328 const residence = Client . get ( 'residence' ) ;
280329 const req = [
@@ -313,7 +362,7 @@ const AccountOpening = (() => {
313362 id = $ ( this ) . attr ( 'id' ) ;
314363 if ( ! / ^ ( t n c | a d d r e s s _ s t a t e | c h k _ p r o f e s s i o n a l | c h k _ t a x _ i d | c i t i z e n ) $ / . test ( id ) ) {
315364 validation = { selector : `#${ id } ` , validations : [ 'req' ] } ;
316- if ( id === 'not_pep ' ) {
365+ if ( id === 'pep_declaration ' ) {
317366 validation . exclude_request = 1 ;
318367 validation . validations = [ [ 'req' , { message : localize ( 'Please confirm that you are not a politically exposed person.' ) } ] ] ;
319368 }
@@ -323,6 +372,54 @@ const AccountOpening = (() => {
323372 return validations ;
324373 } ;
325374
375+ const setCurrencyForFinancialAccount = async ( currency_to_set ) => {
376+ sessionStorage . removeItem ( 'new_financial_account_set_currency' ) ;
377+ await BinarySocket . wait ( 'authorize' ) ;
378+ const response = await BinarySocket . send ( { set_account_currency : currency_to_set } ) ;
379+ if ( response . error ) {
380+ showResponseError ( response ) ;
381+ } else {
382+ Client . set ( 'currency' , currency_to_set ) ;
383+ BinarySocket . send ( { balance : 1 } ) ;
384+ BinarySocket . send ( { payout_currencies : 1 } , { forced : true } ) ;
385+ Header . displayAccountStatus ( ) ;
386+ setTimeout ( ( ) => { window . location . replace ( urlFor ( 'user/set-currency' ) || urlFor ( 'trading' ) ) ; } , 500 ) ; // need to redirect not using pjax
387+ }
388+ } ;
389+
390+ const createNewAccount = async ( account_details , submit_button ) => {
391+ FormManager . disableButton ( submit_button ) ;
392+ const is_maltainvest_account = ! ! account_details . new_account_maltainvest ;
393+ account_details . client_type = $ ( '#chk_professional' ) . is ( ':checked' ) ? 'professional' : 'retail' ;
394+ delete account_details . tax_identification_confirm ;
395+ delete account_details . tnc ;
396+ delete account_details . pep_declaration ;
397+ delete account_details . fs_professional ;
398+
399+ // Set currency after account is created for Maltainvest only
400+ if ( is_maltainvest_account && account_details . currency ) {
401+ const currency = account_details . currency ;
402+ delete account_details . currency ;
403+ sessionStorage . setItem ( 'new_financial_account_set_currency' , currency ) ;
404+ }
405+
406+ const response = await BinarySocket . send ( account_details ) ;
407+ if ( response . error ) {
408+ if ( response . error . code === 'show risk disclaimer' ) return true ;
409+ showResponseError ( response ) ;
410+ } else {
411+ localStorage . setItem ( 'is_new_account' , 1 ) ;
412+ const email = Client . get ( 'email' ) ;
413+ const loginid = response [ is_maltainvest_account ? 'new_account_maltainvest' : 'new_account_real' ] . client_id ;
414+ const token = response [ is_maltainvest_account ? 'new_account_maltainvest' : 'new_account_real' ] . oauth_token ;
415+ ClientBase . setNewAccount ( { email, loginid, token } ) ;
416+ if ( is_maltainvest_account ) window . location . reload ( ) ;
417+ else window . location . replace ( urlFor ( 'user/set-currency' ) ) ;
418+ }
419+ FormManager . enableButton ( submit_button ) ;
420+ return false ;
421+ } ;
422+
326423 const showHidePulser = ( should_show ) => { $ ( '.upgrademessage' ) . children ( 'a' ) . setVisibility ( should_show ) ; } ;
327424
328425 const registerPepToggle = ( ) => {
@@ -334,13 +431,17 @@ const AccountOpening = (() => {
334431 } ;
335432
336433 return {
337- redirectAccount,
338- populateForm,
339- handleNewAccount,
340434 commonValidations,
435+ createNewAccount,
436+ excluded_countries,
437+ getSinupPageLink,
438+ handleNewAccount,
439+ populateForm,
440+ redirectAccount,
441+ registerPepToggle,
341442 selectCheckboxValidation,
443+ setCurrencyForFinancialAccount,
342444 showHidePulser,
343- registerPepToggle,
344445 } ;
345446} ) ( ) ;
346447
0 commit comments