Skip to content

Commit

Permalink
[WALL] Jim/WALL-5218/send all accounts with their tokens to os (#17738)
Browse files Browse the repository at this point in the history
* chore: send all accounts with their tokens

* chore: change os_auth_token to os_auth_tokens

* ci: fix failing tests

* ci: fix failing tests
  • Loading branch information
jim-deriv authored Dec 5, 2024
1 parent 7ff089d commit dfd40d8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import React, { useEffect } from 'react';
import Cookies from 'js-cookie';
import { useHistory } from 'react-router-dom';
import { useActiveWalletAccount, useCreateWallet, useIsEuRegion } from '@deriv/api-v2';
import { useCreateWallet, useIsEuRegion } from '@deriv/api-v2';
import { LabelPairedCheckMdFillIcon, LabelPairedPlusMdFillIcon } from '@deriv/quill-icons';
import { getAccountsFromLocalStorage } from '@deriv/utils';
import { Analytics } from '@deriv-com/analytics';
import { Localize, useTranslations } from '@deriv-com/translations';
import { Button, useDevice } from '@deriv-com/ui';
import { URLConstants } from '@deriv-com/utils';
import { LANDING_COMPANIES } from '../../constants/constants';
import { isProduction, OUT_SYSTEMS_TRADERSHUB } from '../../helpers/urls';
import { redirectToOutSystems } from '../../helpers/urls';
import useSyncLocalStorageClientAccounts from '../../hooks/useSyncLocalStorageClientAccounts';
import useWalletAccountSwitcher from '../../hooks/useWalletAccountSwitcher';
import { TWalletCarouselItem } from '../../types';
Expand All @@ -32,7 +27,6 @@ const WalletsAddMoreCardBanner: React.FC<TWalletCarouselItem> = ({
const { addWalletAccountToLocalStorage } = useSyncLocalStorageClientAccounts();
const { localize } = useTranslations();
const { data: isEuRegion } = useIsEuRegion();
const { data: activeWallet } = useActiveWalletAccount();

useEffect(
() => {
Expand Down Expand Up @@ -69,35 +63,6 @@ const WalletsAddMoreCardBanner: React.FC<TWalletCarouselItem> = ({
]
);

const redirectToOutSystems = () => {
// redirect to OS Tradershub if feature is enabled
const isOutSystemsRealAccountCreationEnabled = Analytics?.getFeatureValue(
'trigger_os_real_account_creation',
false
);
if (isOutSystemsRealAccountCreationEnabled) {
const clientAccounts = getAccountsFromLocalStorage() ?? {};
const loginid = activeWallet?.loginid ?? '';
if (!loginid) return;
const authToken = clientAccounts[loginid].token;
if (!authToken) return;
const expires = new Date(new Date().getTime() + 1 * 60 * 1000); // 1 minute

Cookies.set('os_auth_token', authToken, { domain: URLConstants.baseDomain, expires });

const params = new URLSearchParams({
action: 'real-account-signup',
currency: currency ?? '',
target: LANDING_COMPANIES.MALTAINVEST,
});
const baseUrl = isProduction() ? OUT_SYSTEMS_TRADERSHUB.PRODUCTION : OUT_SYSTEMS_TRADERSHUB.STAGING;

const redirectURL = new URL(`${baseUrl}/redirect`);
redirectURL.search = params.toString();
return (window.location.href = redirectURL.toString());
}
};

return (
<div className='wallets-add-more__banner'>
<div className='wallets-add-more__banner-header'>
Expand All @@ -120,7 +85,7 @@ const WalletsAddMoreCardBanner: React.FC<TWalletCarouselItem> = ({
if (!currency) return;

if (isEuRegion) {
return redirectToOutSystems();
return redirectToOutSystems(currency);
}

const createAccountResponse = await mutateAsync({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useActiveWalletAccount, useCreateWallet, useIsEuRegion } from '@deriv/a
import { Analytics } from '@deriv-com/analytics';
import { useDevice } from '@deriv-com/ui';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { isProduction } from '../../../helpers/urls';
import useSyncLocalStorageClientAccounts from '../../../hooks/useSyncLocalStorageClientAccounts';
import useWalletAccountSwitcher from '../../../hooks/useWalletAccountSwitcher';
import { ModalProvider } from '../../ModalProvider';
Expand Down Expand Up @@ -50,7 +49,7 @@ jest.mock('@deriv/utils', () => ({
}));

jest.mock('../../../helpers/urls', () => ({
isProduction: jest.fn(),
...jest.requireActual('../../../helpers/urls'),
OUT_SYSTEMS_TRADERSHUB: {
PRODUCTION: 'https://hub.deriv.com/tradershub',
STAGING: 'https://staging-hub.deriv.com/tradershub',
Expand Down Expand Up @@ -263,7 +262,6 @@ describe('WalletsAddMoreCardBanner', () => {
});

it('redirects to OutSystems staging for EU users on staging', async () => {
(isProduction as jest.Mock).mockReturnValue(false);
(useIsEuRegion as jest.Mock).mockReturnValue({ data: true });
(useActiveWalletAccount as jest.Mock).mockReturnValue({
data: { loginid: 'VRW1' },
Expand All @@ -290,7 +288,8 @@ describe('WalletsAddMoreCardBanner', () => {
});

it('redirects to OutSystems production for EU users on production', async () => {
(isProduction as jest.Mock).mockReturnValue(true);
const ORIGINAL_NODE_ENV = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
(useIsEuRegion as jest.Mock).mockReturnValue({ data: true });
(useActiveWalletAccount as jest.Mock).mockReturnValue({
data: { loginid: 'VRW1' },
Expand All @@ -314,5 +313,6 @@ describe('WalletsAddMoreCardBanner', () => {
Object.defineProperty(window, 'location', {
value: originalWindowLocation,
});
process.env.NODE_ENV = ORIGINAL_NODE_ENV;
});
});
4 changes: 4 additions & 0 deletions packages/wallets/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './constants';
export * from './errorCodes';
export * from './icons';
export * from './password';
41 changes: 38 additions & 3 deletions packages/wallets/src/helpers/urls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { AppIDConstants, LocalStorageUtils, URLUtils } from '@deriv-com/utils';
import Cookies from 'js-cookie';
import { getAccountsFromLocalStorage } from '@deriv/utils';
import { Analytics } from '@deriv-com/analytics';
import { LocalStorageUtils, URLConstants, URLUtils } from '@deriv-com/utils';
import { LANDING_COMPANIES } from '../constants/constants';

const isBrowser = () => typeof window !== 'undefined';

Expand Down Expand Up @@ -84,8 +88,7 @@ export const isStaging = (domain = window.location.hostname) => {
};

export const isProduction = () => {
const allDomains = Object.keys(AppIDConstants.domainAppId).map(domain => `(www\\.)?${domain.replace('.', '\\.')}`);
return new RegExp(`^(${allDomains.join('|')})$`, 'i').test(window.location.hostname);
return process.env.NODE_ENV === 'production';
};

/**
Expand Down Expand Up @@ -120,3 +123,35 @@ export const OUT_SYSTEMS_TRADERSHUB = Object.freeze({
PRODUCTION: `https://hub.${domainUrl}/tradershub`,
STAGING: `https://staging-hub.${domainUrl}/tradershub`,
});

export const redirectToOutSystems = (currency = '') => {
// redirect to OS Tradershub if feature is enabled
const isOutSystemsRealAccountCreationEnabled = Analytics?.getFeatureValue(
'trigger_os_real_account_creation',
false
);

if (isOutSystemsRealAccountCreationEnabled) {
const clientAccounts = getAccountsFromLocalStorage() ?? {};
if (!Object.keys(clientAccounts).length) return;
const accountsWithTokens: Record<string, unknown> = {};
Object.keys(clientAccounts).forEach(loginid => {
const account = clientAccounts[loginid];
accountsWithTokens[loginid] = { token: account.token };
});
const expires = new Date(new Date().getTime() + 1 * 60 * 1000); // 1 minute

Cookies.set('os_auth_tokens', JSON.stringify(accountsWithTokens), { domain: URLConstants.baseDomain, expires });

const params = new URLSearchParams({
action: 'real-account-signup',
...(currency ? { currency } : {}),
target: LANDING_COMPANIES.MALTAINVEST,
});
const baseUrl = isProduction() ? OUT_SYSTEMS_TRADERSHUB.PRODUCTION : OUT_SYSTEMS_TRADERSHUB.STAGING;

const redirectURL = new URL(`${baseUrl}/redirect`);
redirectURL.search = params.toString();
return (window.location.href = redirectURL.toString());
}
};
3 changes: 2 additions & 1 deletion packages/wallets/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = function (env) {
externals: [
{
'@deriv/api-v2': '@deriv/api-v2',
'@deriv/quill-icons': `@deriv/quill-icons`,
'@deriv/quill-icons': '@deriv/quill-icons',
'@deriv/shared': '@deriv/shared',
'@deriv/utils': '@deriv/utils',
'@deriv-com/analytics': '@deriv-com/analytics',
Expand Down Expand Up @@ -204,6 +204,7 @@ module.exports = function (env) {
plugins: [
new DefinePlugin({
'process.env.CROWDIN_URL': JSON.stringify('https://translations.deriv.com'),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'process.env.REMOTE_CONFIG_URL': JSON.stringify(process.env.REMOTE_CONFIG_URL),
'process.env.WALLETS_TRANSLATION_PATH': JSON.stringify('deriv-app-wallets/staging'),
}),
Expand Down

0 comments on commit dfd40d8

Please sign in to comment.