Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ameerul /Task 86076 Use disabled from website_status instead of calling p2p_advertiser_info on load of deriv-app. #7875

Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d4523aa
chore: cherry picked changes
Jan 17, 2023
ab7549f
chore: fixed tests
ameerul-deriv Mar 10, 2023
e236096
chore: left comment in test
ameerul-deriv Mar 10, 2023
695539d
chore: fixed typing issues
ameerul-deriv Mar 10, 2023
17e50e0
Merge branch 'master' into task-86076-use-disabled-website-status
ameerul-deriv Mar 10, 2023
63771ac
chore: fixed tests
ameerul-deriv Mar 13, 2023
337f126
Merge branch 'task-86076-use-disabled-website-status' of github.com:a…
ameerul-deriv Mar 13, 2023
c7ff941
Merge branch 'master' of github.com:binary-com/deriv-app into task-86…
ameerul-deriv Mar 13, 2023
601cb23
chore: check if p2p is disabled using get_account_status
ameerul-deriv Mar 13, 2023
9a442c1
chore: removed commented code
ameerul-deriv Mar 13, 2023
6354d84
chore: added p2p is disabled cookie
ameerul-deriv Mar 13, 2023
aeb5d0f
Merge branch 'master' of github.com:binary-com/deriv-app into task-86…
ameerul-deriv Mar 14, 2023
ba14a72
chore: fixed tests
ameerul-deriv Mar 14, 2023
8a75d8d
chore: implemented suggestions
ameerul-deriv Mar 14, 2023
8f7625c
chore: added suggestions
ameerul-deriv Mar 14, 2023
eb19492
Merge branch 'master' of github.com:binary-com/deriv-app into task-86…
ameerul-deriv Mar 15, 2023
8224bc2
chore: added farzins changes to types.ts for @deriv/api-types, checke…
ameerul-deriv Mar 16, 2023
2ff6e3a
Merge branch 'master' of github.com:binary-com/deriv-app into task-86…
ameerul-deriv Mar 16, 2023
32aa830
chore: fixed test issue and website_status call after authorize on in…
ameerul-deriv Mar 16, 2023
37924e7
chore: fixed tests
ameerul-deriv Mar 17, 2023
9fd1b25
chore: fixed tests
ameerul-deriv Mar 17, 2023
4a390be
Merge branch 'master' into task-86076-use-disabled-website-status
ameerul-deriv Mar 20, 2023
ae65412
chore: empty commit
ameerul-deriv Mar 20, 2023
7666733
chore: changed localstorage-sync
ameerul-deriv Mar 20, 2023
d2a47d2
chore: fixed website_status issues
ameerul-deriv Mar 21, 2023
4ff670d
chore: removed setIsLoggingIn(false)
ameerul-deriv Mar 21, 2023
1df0213
chore: added TODO
ameerul-deriv Mar 21, 2023
b5e03dc
chore: empty commit
ameerul-deriv Mar 21, 2023
d26de39
Merge branch 'master' of github.com:binary-com/deriv-app into task-86…
ameerul-deriv Mar 21, 2023
2123417
chore: fixed window not loading for new user
ameerul-deriv Mar 21, 2023
d0842c3
Merge branch 'master' into task-86076-use-disabled-website-status
jim-deriv Mar 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 13 additions & 46 deletions packages/cashier/src/stores/__tests__/general-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,36 +119,29 @@ describe('GeneralStore', () => {
// expect(general_store.is_p2p_enabled).toBeTruthy();
// });

it('should not show p2p in cashier onboarding if p2p_advertiser_error is equal to "RestrictedCountry"', () => {
general_store.setP2pAdvertiserError('RestrictedCountry');
general_store.showP2pInCashierOnboarding();

expect(general_store.show_p2p_in_cashier_onboarding).toBeFalsy();
});

it('should not show p2p in cashier onboarding if the user has accounts with fiat currency, but has not account with USD currency', () => {
it('should not show p2p in cashier onboarding if the user has accounts with fiat currency, but has not account with USD currency', async () => {
general_store.root_store.client.account_list = [{ title: 'EUR' }];
general_store.showP2pInCashierOnboarding();
await general_store.showP2pInCashierOnboarding();

expect(general_store.show_p2p_in_cashier_onboarding).toBeFalsy();
});

it('should not show p2p in cashier onboarding if the user has accounts with fiat currency, but has not account with USD currency', () => {
it('should not show p2p in cashier onboarding if the user has accounts with fiat currency, but has not account with USD currency', async () => {
general_store.root_store.client.account_list = [{ title: 'EUR' }];
general_store.showP2pInCashierOnboarding();
await general_store.showP2pInCashierOnboarding();

expect(general_store.show_p2p_in_cashier_onboarding).toBeFalsy();
});

it('should show p2p in cashier onboarding if the user account is not virtual, there is no p2p_advertiser_error and he has USD account', () => {
general_store.showP2pInCashierOnboarding();
it('should show p2p in cashier onboarding if the user account is not virtual, there is no p2p_advertiser_error and he has USD account', async () => {
await general_store.showP2pInCashierOnboarding();

expect(general_store.show_p2p_in_cashier_onboarding).toBeTruthy();
});

it('should show p2p in cashier onboarding if the user account is not virtual, there is no p2p_advertiser_error and he has not fiat currency accounts', () => {
it('should show p2p in cashier onboarding if the user account is not virtual, there is no p2p_advertiser_error and he has not fiat currency accounts', async () => {
general_store.root_store.client.account_list = [{ title: 'BTC' }];
general_store.showP2pInCashierOnboarding();
await general_store.showP2pInCashierOnboarding();

expect(general_store.show_p2p_in_cashier_onboarding).toBeTruthy();
});
Expand Down Expand Up @@ -262,47 +255,20 @@ describe('GeneralStore', () => {
});

it('should perform proper init invocation when is_logged_in is equal to true', async () => {
const spyGetAdvertizerError = jest.spyOn(general_store, 'getAdvertizerError');
const spyCheckP2pStatus = jest.spyOn(general_store, 'checkP2pStatus');
general_store.root_store.client.is_logged_in = true;
general_store.init();

await waitFor(() => {
expect(spyGetAdvertizerError).toHaveBeenCalledTimes(1);
expect(spyCheckP2pStatus).toHaveBeenCalledTimes(1);
});
expect(spyCheckP2pStatus).toHaveBeenCalledTimes(1);

// Don't remove eslint here as WS.wait is expected to be called 2 times from init and checkP2pStatus
// eslint-disable-next-line testing-library/await-async-utils
expect(general_store.WS.wait).toHaveBeenCalledTimes(1);
expect(general_store.WS.wait).toHaveBeenCalledTimes(2);
expect(general_store.root_store.modules.cashier.withdraw.check10kLimit).toHaveBeenCalledTimes(1);
});

it('should set advertiser error', async () => {
const spySetP2pAdvertiserError = jest.spyOn(general_store, 'setP2pAdvertiserError');
await general_store.getAdvertizerError();

expect(spySetP2pAdvertiserError).toHaveBeenCalledWith('advertiser_error');
});

it('should set p2p advertiser error', () => {
general_store.setP2pAdvertiserError('p2p_advertiser_error');

expect(general_store.p2p_advertiser_error).toBe('p2p_advertiser_error');
});

it('should set is_p2p_visible equal to false, if there is a "RestrictedCountry" p2p advertiser error', () => {
general_store.setP2pAdvertiserError('RestrictedCountry');
general_store.checkP2pStatus();

expect(general_store.is_p2p_visible).toBeFalsy();
});

it('should set is_p2p_visible equal to false, if there is a "RestrictedCurrency" p2p advertiser error', () => {
general_store.setP2pAdvertiserError('RestrictedCurrency');
general_store.checkP2pStatus();

expect(general_store.is_p2p_visible).toBeFalsy();
});

balakrishna-deriv marked this conversation as resolved.
Show resolved Hide resolved
it('should set is_p2p_visible equal to false, if there is a virtual account', () => {
general_store.root_store.client.is_virtual = true;
general_store.checkP2pStatus();
Expand Down Expand Up @@ -417,6 +383,7 @@ describe('GeneralStore', () => {
jest.spyOn(window, 'window', 'get').mockImplementation(() => ({
location: {
pathname: routes.cashier_p2p,
hostname: 'localhost.binary.sx',
},
}));
general_store.setIsP2pVisible(false);
Expand Down
37 changes: 15 additions & 22 deletions packages/cashier/src/stores/general-store.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { action, computed, observable, reaction, when, makeObservable } from 'mobx';
import { isCryptocurrency, isEmptyObject, getPropertyValue, routes, ContentFlag } from '@deriv/shared';
import { isCryptocurrency, isEmptyObject, routes, ContentFlag, CookieStorage } from '@deriv/shared';
import Constants from 'Constants/constants';
import BaseStore from './base-store';
import PaymentAgentStore from './payment-agent-store';
import type { TRootStore, TWebSocket } from 'Types';
import { AccountStatusResponse } from '@deriv/api-types';
ameerul-deriv marked this conversation as resolved.
Show resolved Hide resolved

export default class GeneralStore extends BaseStore {
constructor(public WS: TWebSocket, public root_store: TRootStore) {
Expand All @@ -16,7 +17,6 @@ export default class GeneralStore extends BaseStore {
checkP2pStatus: action.bound,
continueRoute: action.bound,
deposit_target: observable,
getAdvertizerError: action.bound,
has_set_currency: observable,
init: action.bound,
is_cashier_locked: computed,
Expand All @@ -30,7 +30,6 @@ export default class GeneralStore extends BaseStore {
onMountCashierOnboarding: action.bound,
onMountCommon: action.bound,
onRemount: observable,
p2p_advertiser_error: observable,
p2p_notification_count: observable,
p2p_unseen_notifications: computed,
percentage: observable,
Expand All @@ -47,7 +46,6 @@ export default class GeneralStore extends BaseStore {
setLoading: action.bound,
setNotificationCount: action.bound,
setOnRemount: action.bound,
setP2pAdvertiserError: action.bound,
setShouldShowAllAvailableCurrencies: action.bound,
should_percentage_reset: observable,
should_set_currency_modal_title_change: observable,
Expand Down Expand Up @@ -89,7 +87,6 @@ export default class GeneralStore extends BaseStore {
is_p2p_visible = false;
is_populating_values = false;
onRemount: VoidFunction = () => this;
p2p_advertiser_error?: string = undefined;
p2p_notification_count = 0;
percentage = 0;
payment_agent: PaymentAgentStore | null = null;
Expand Down Expand Up @@ -135,16 +132,18 @@ export default class GeneralStore extends BaseStore {
return unseen_notifications.length;
}

showP2pInCashierOnboarding(): void {
async showP2pInCashierOnboarding(): Promise<void> {
const { account_list, is_virtual } = this.root_store.client;

const is_p2p_restricted = this.p2p_advertiser_error === 'RestrictedCountry';
const get_account_status = this.WS.wait('get_account_status');

const is_p2p_disabled = (await get_account_status)?.get_account_status?.p2p_status === 'none';
const has_usd_currency = account_list.some(account => account.title === 'USD');
const has_user_fiat_currency = account_list.some(
account => !isCryptocurrency(account.title) && account.title !== 'Real'
account => account?.title && account.title !== 'Real' && !isCryptocurrency(account?.title)
);

if (is_p2p_restricted || is_virtual || (has_user_fiat_currency && !has_usd_currency)) {
if (is_p2p_disabled || is_virtual || (has_user_fiat_currency && !has_usd_currency)) {
this.show_p2p_in_cashier_onboarding = false;
} else {
this.show_p2p_in_cashier_onboarding = true;
Expand Down Expand Up @@ -243,7 +242,6 @@ export default class GeneralStore extends BaseStore {
await this.WS.wait('get_settings');

if (is_logged_in) {
await this.getAdvertizerError();
if (!switched) {
this.checkP2pStatus();
payment_agent.setPaymentAgentList().then(payment_agent.filterPaymentAgentList);
Expand All @@ -258,19 +256,11 @@ export default class GeneralStore extends BaseStore {
}
}

async getAdvertizerError() {
const advertiser_info = await this.WS.authorized.p2pAdvertiserInfo?.();
this.setP2pAdvertiserError(getPropertyValue(advertiser_info, ['error', 'code']));
}
async checkP2pStatus(): Promise<void> {
const get_account_status = this.WS.wait('get_account_status');

setP2pAdvertiserError(value: string): void {
this.p2p_advertiser_error = value;
}

checkP2pStatus(): void {
const advertiser_error = this.p2p_advertiser_error;
const is_p2p_restricted = advertiser_error === 'RestrictedCountry' || advertiser_error === 'RestrictedCurrency';
this.setIsP2pVisible(!(is_p2p_restricted || this.root_store.client.is_virtual));
const is_p2p_disabled = (await get_account_status)?.get_account_status?.p2p_status === 'none';
this.setIsP2pVisible(!(is_p2p_disabled || this.root_store.client.is_virtual));
}

async onMountCommon(should_remount?: boolean) {
Expand Down Expand Up @@ -341,6 +331,9 @@ export default class GeneralStore extends BaseStore {
this.root_store.modules.cashier.account_prompt_dialog.last_location ?? routes.cashier_deposit
);
}

const p2p_cookie = new (CookieStorage as any)('is_p2p_disabled');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ameerul-deriv Why as any? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used as any as there are other similar code snippets, and I had just followed the same...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2023-03-14 at 11 57 44 AM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@niloofar-deriv @jim-deriv Any idea why? CookieStorage has been migrated to TS in this commit, But the type and all the usage don't match with each other, Should we update the types or the usage is wrong? 🤔

p2p_cookie.set('is_p2p_disabled', !is_p2p_visible);
}

get is_cashier_locked(): boolean {
Expand Down
3 changes: 2 additions & 1 deletion packages/cashier/src/types/websocket.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
P2PAdvertInfo,
TransferBetweenAccountsResponse,
} from '@deriv/api-types';
import { TSocketEndpointNames, TSocketResponse } from '@deriv/api/types';
ameerul-deriv marked this conversation as resolved.
Show resolved Hide resolved
import type { TTransactionItem } from 'Types';

export type TCashierPayments = {
Expand Down Expand Up @@ -87,5 +88,5 @@ export type TWebSocket = {
tradingPlatformAccountsList: (platform: string) => {
trading_platform_accounts: (DetailsOfEachMT5Loginid & { account_id: string })[];
};
wait: (value: string) => Promise<unknown>;
wait: <T extends TSocketEndpointNames>(value: T) => Promise<TSocketResponse<T>>;
};
4 changes: 2 additions & 2 deletions packages/core/src/Services/socket-general.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const BinarySocketGeneral = (() => {
client_store.logout();
return;
}
WS.subscribeWebsiteStatus(ResponseHandlers.websiteStatus);
// Waiting for authorize call to be completed so BE can use residence instead of IP
WS.wait('authorize').then(() => WS.subscribeWebsiteStatus(ResponseHandlers.websiteStatus));
ServerTime.init(() => common_store.setServerTime(ServerTime.get()));
common_store.setIsSocketOpened(true);
}
Expand Down Expand Up @@ -187,7 +188,6 @@ const BinarySocketGeneral = (() => {
'trading_platform_password_reset',
'trading_platform_investor_password_reset',
'new_account_virtual',
'p2p_advertiser_info',
'portfolio',
'proposal_open_contract',
'change_email',
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/_common/base/socket_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,6 @@ const BinarySocketBase = (() => {

const cancelContract = contract_id => deriv_api.send({ cancel: contract_id });

const p2pAdvertiserInfo = () => deriv_api.send({ p2p_advertiser_info: 1 });

const fetchLoginHistory = limit =>
deriv_api.send({
login_history: 1,
Expand Down Expand Up @@ -444,7 +442,6 @@ const BinarySocketBase = (() => {
newAccountVirtual,
newAccountReal,
newAccountRealMaltaInvest,
p2pAdvertiserInfo,
p2pSubscribe,
profitTable,
statement,
Expand Down