Skip to content

Commit

Permalink
Merge branch 'migrate_svg_to_bvi_dvl' into svg/update-flag-for-open-o…
Browse files Browse the repository at this point in the history
…rder-positions
  • Loading branch information
utkarsha-deriv committed Oct 20, 2023
2 parents adf1c3b + 430dd2f commit 81eb929
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 21 deletions.
52 changes: 51 additions & 1 deletion packages/cfd/src/Containers/cfd-password-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
CFD_PLATFORMS,
getCFDPlatformNames,
getAuthenticationStatusInfo,
getFormattedJurisdictionMarketTypes,
getCFDPlatformLabel,
getErrorMessages,
getFormattedJurisdictionCode,
Expand Down Expand Up @@ -659,6 +660,7 @@ const CFDPasswordModal = observer(({ form_error, platform }: TCFDPasswordModalPr
submitMt5Password,
submitCFDPassword,
new_account_response,
migrated_mt5_accounts,
} = useCfdStore();

const history = useHistory();
Expand Down Expand Up @@ -796,7 +798,14 @@ const CFDPasswordModal = observer(({ form_error, platform }: TCFDPasswordModalPr
(!has_cfd_error || is_password_error || is_password_reset);

const should_show_success =
!has_cfd_error && is_cfd_success_dialog_enabled && is_cfd_password_modal_enabled && is_password_modal_exited;
!has_cfd_error &&
is_cfd_success_dialog_enabled &&
is_cfd_password_modal_enabled &&
is_password_modal_exited &&
!is_mt5_migration_modal_enabled;

const should_show_migration_success =
!has_cfd_error && is_mt5_migration_modal_enabled && is_cfd_success_dialog_enabled && is_password_modal_exited;

const should_show_sent_email_modal = is_sent_email_modal_open && is_password_modal_exited;

Expand Down Expand Up @@ -923,6 +932,30 @@ const CFDPasswordModal = observer(({ form_error, platform }: TCFDPasswordModalPr
);
};

const getMigrationSubmitText = () => {
const list = migrated_mt5_accounts.map(account => {
const [to_account_type] = Object.keys(account);
const [to_jurisdiction] = Object.values(account);

return `${getCFDPlatformNames(CFD_PLATFORMS.MT5)} ${getFormattedJurisdictionMarketTypes(
to_account_type
)} ${getFormattedJurisdictionCode(to_jurisdiction)}`;
});

return (
<Localize
i18n_default_text="We've upgraded your MT5 account(s) by moving them to the {{eligible_account_migrate}} jurisdiction. Use your <0>{{migrated_accounts}}</0> new login ID and MT5 password to start trading."
values={{
eligible_account_migrate: getFormattedJurisdictionCode(
migrated_mt5_accounts.map(account => Object.values(account))[0]
),
migrated_accounts: list.join(' and '), // [MT5 Derived Vanuatu and MT5 Financial Vanuatu]
}}
components={[<strong key={0} />]}
/>
);
};

const cfd_password_form = (
<CFDPasswordForm
is_bvi={is_bvi}
Expand Down Expand Up @@ -1018,6 +1051,23 @@ const CFDPasswordModal = observer(({ form_error, platform }: TCFDPasswordModalPr
width={isMobile() ? '32.8rem' : 'auto'}
is_medium_button={isMobile()}
/>
{should_show_migration_success && (
<SuccessDialog
is_open={should_show_migration_success}
toggleModal={closeModal}
onCancel={closeModal}
onSubmit={closeModal}
classNameMessage='cfd-password-modal__message'
message={getMigrationSubmitText()}
icon={<Icon icon='IcMt5MigrationSuccess' size={128} />}
icon_size='xlarge'
text_submit={localize('OK')}
has_cancel={false}
has_close_icon={false}
width={isMobile() ? '32.8rem' : 'auto'}
is_medium_button={isMobile()}
/>
)}
<SentEmailModal
is_open={should_show_sent_email_modal}
identifier_title='trading_password'
Expand Down
121 changes: 101 additions & 20 deletions packages/cfd/src/Stores/Modules/CFD/cfd-store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { action, computed, observable, reaction, runInAction, makeObservable, override } from 'mobx';
import { getAccountListKey, getAccountTypeFields, CFD_PLATFORMS, WS, Jurisdiction } from '@deriv/shared';
import {
getAccountListKey,
getAccountTypeFields,
CFD_PLATFORMS,
WS,
Jurisdiction,
JURISDICTION_MARKET_TYPES,
} from '@deriv/shared';
import BaseStore from '../../base-store';
import { getDxCompanies, getMtCompanies, getDerivezCompanies } from './Helpers/cfd-config';

Expand Down Expand Up @@ -49,6 +56,8 @@ export default class CFDStore extends BaseStore {
real_financial_accounts_existing_data = [];
real_swapfree_accounts_existing_data = [];

migrated_mt5_accounts = [];

constructor({ root_store }) {
super({ root_store });

Expand All @@ -75,6 +84,7 @@ export default class CFDStore extends BaseStore {
dxtrade_tokens: observable,
ctrader_tokens: observable,
derivez_tokens: observable,
migrated_mt5_accounts: observable,
account_title: computed,
current_list: computed,
has_created_account_for_selected_jurisdiction: computed,
Expand All @@ -89,6 +99,7 @@ export default class CFDStore extends BaseStore {
disableCFDPasswordModal: action.bound,
enableCFDPasswordModal: action.bound,
getName: action.bound,
migrateMT5Accounts: action.bound,
openMT5Account: action.bound,
openCFDAccount: action.bound,
beginRealSignupForMt5: action.bound,
Expand Down Expand Up @@ -373,6 +384,79 @@ export default class CFDStore extends BaseStore {
// First name is not set when user has no real account
return first_name ? [first_name, title].join(' ') : title;
}
async migrateMT5Accounts(values, actions) {
const account_to_migrate = this.root_store.client.mt5_login_list.filter(
acc => acc.landing_company_short === Jurisdiction.SVG && !!acc.eligible_to_migrate
);
const promises = account_to_migrate.map(account => {
const { eligible_to_migrate } = account;
const [type, shortcode] = Object.entries(eligible_to_migrate)[0];
const account_type = {
category: 'real',
type,
};
this.migrated_mt5_accounts = [...this.migrated_mt5_accounts, { ...eligible_to_migrate }];
return this.requestMigrateAccount(values, shortcode, account_type);
});

try {
const results = await Promise.all(promises);
const has_error = results.find(result => result.error);

if (!has_error) {
actions.setStatus({ success: true });
actions.setSubmitting(false);
this.setError(false);
this.setCFDSuccessDialog(true);
await this.getAccountStatus(CFD_PLATFORMS.MT5);

const mt5_login_list_response = await WS.authorized.mt5LoginList();
this.root_store.client.responseMt5LoginList(mt5_login_list_response);

WS.transferBetweenAccounts();
this.root_store.client.responseMT5TradingServers(await WS.tradingServers(CFD_PLATFORMS.MT5));
} else {
await this.getAccountStatus(CFD_PLATFORMS.MT5);
this.clearCFDError();
this.setMT5MigrationError(has_error?.error?.message);
this.root_store.ui.toggleMT5MigrationModal();
}
} catch (error) {
// At least one request has failed
// eslint-disable-next-line no-console
console.warn('One or more MT5 migration requests failed:', error);
}
}

requestMigrateAccount(values, shortcode, account_type) {
const name = this.getName();
const leverage = this.mt5_companies[account_type.category][account_type.type].leverage;
const type_request = getAccountTypeFields(account_type);
const { address_line_1, address_line_2, address_postcode, address_city, address_state, country_code, phone } =
this.root_store.client.account_settings;

return WS.mt5NewAccount({
mainPassword: values.password,
email: this.root_store.client.email_address,
leverage,
name,
address: address_line_1 || address_line_2,
city: address_city,
country: country_code,
phone,
state: address_state,
zipCode: address_postcode,
migrate: 1,
...(values.server ? { server: values.server } : {}),
...(shortcode ? { company: shortcode } : {}),
...(shortcode !== Jurisdiction.LABUAN
? type_request
: {
account_type: JURISDICTION_MARKET_TYPES.FINANCIAL,
mt5_account_type: 'financial_stp',
}),
});
}

openMT5Account(values) {
const name = this.getName();
Expand Down Expand Up @@ -515,28 +599,25 @@ export default class CFDStore extends BaseStore {
}

this.resetFormErrors();
const response = await this.openMT5Account(values);
if (!response.error) {
actions.setStatus({ success: true });
actions.setSubmitting(false);
this.setError(false);
this.setCFDSuccessDialog(true);
await this.getAccountStatus(CFD_PLATFORMS.MT5);

const mt5_login_list_response = await WS.authorized.mt5LoginList();
this.root_store.client.responseMt5LoginList(mt5_login_list_response);

WS.transferBetweenAccounts(); // get the list of updated accounts for transfer in cashier
this.root_store.client.responseMT5TradingServers(await WS.tradingServers(CFD_PLATFORMS.MT5));
this.setCFDNewAccount(response.mt5_new_account);
if (this.root_store.ui.is_mt5_migration_modal_enabled) {
await this.migrateMT5Accounts(values, actions);
} else {
await this.getAccountStatus(CFD_PLATFORMS.MT5);
const response = await this.openMT5Account(values);
if (!response.error) {
actions.setStatus({ success: true });
actions.setSubmitting(false);
this.setError(false);
this.setCFDSuccessDialog(true);
await this.getAccountStatus(CFD_PLATFORMS.MT5);

if (this.root_store.ui.is_mt5_migration_modal_enabled) {
this.clearCFDError();
this.setMT5MigrationError(response?.error?.message);
this.root_store.ui.toggleMT5MigrationModal();
const mt5_login_list_response = await WS.authorized.mt5LoginList();
this.root_store.client.responseMt5LoginList(mt5_login_list_response);

WS.transferBetweenAccounts(); // get the list of updated accounts for transfer in cashier
this.root_store.client.responseMT5TradingServers(await WS.tradingServers(CFD_PLATFORMS.MT5));
this.setCFDNewAccount(response.mt5_new_account);
} else {
await this.getAccountStatus(CFD_PLATFORMS.MT5);
this.setError(true, response.error);
actions.resetForm({});
actions.setSubmitting(false);
Expand Down
1 change: 1 addition & 0 deletions packages/cfd/src/types/cfd-store.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type TCFDStore = {
};
sendVerifyEmail: () => Promise<VerifyEmailResponse>;
account_title: string;
migrated_mt5_accounts: Record<string, string>[];
disableCFDPasswordModal: () => void;
error_message: string;
error_type?: string;
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/components/icon/icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ import './mt5/ic-mt5-high-leverage.svg';
import './mt5/ic-mt5-liquid-risk.svg';
import './mt5/ic-mt5-logo.svg';
import './mt5/ic-mt5-margin-trading.svg';
import './mt5/ic-mt5-migration-success.svg';
import './mt5/ic-mt5-one-password.svg';
import './mt5/ic-mt5-open-markets.svg';
import './mt5/ic-mt5-password-updated.svg';
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/components/stories/icon/icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ export const icons =
'IcMt5LiquidRisk',
'IcMt5Logo',
'IcMt5MarginTrading',
'IcMt5MigrationSuccess',
'IcMt5OnePassword',
'IcMt5OpenMarkets',
'IcMt5PasswordUpdated',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const getFormattedJurisdictionMarketTypes = (jurisdiction_market_type: st
let formatted_market_type = '';

switch (jurisdiction_market_type) {
case 'synthetic': // need to remove this once we have the correct market type from BE
case JURISDICTION_MARKET_TYPES.DERIVED:
formatted_market_type = localize('Derived');
break;
Expand Down
2 changes: 2 additions & 0 deletions packages/stores/src/mockStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ const mock = (): TStores & { is_mock: boolean } => {
},
modules: {
cfd: {
setMT5MigrationError: jest.fn(),
migrated_mt5_accounts: [],
mt5_migration_error: '',
enableCFDPasswordModal: jest.fn(),
setJurisdictionSelectedShortcode: jest.fn(),
Expand Down

0 comments on commit 81eb929

Please sign in to comment.