48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { MILLISECONDS_PER_SECOND } from '../../common/time';
|
|
import { TbankAccount } from './tbank.client';
|
|
|
|
const accountTypes = {
|
|
ACCOUNT_TYPE_TINKOFF: { type: 'BROKERAGE', name: 'Брокерский счет' },
|
|
ACCOUNT_TYPE_TINKOFF_IIS: {
|
|
type: 'INDIVIDUAL_INVESTMENT',
|
|
name: 'ИИС',
|
|
},
|
|
ACCOUNT_TYPE_INVEST_BOX: { type: 'INVEST_BOX', name: 'Инвесткопилка' },
|
|
ACCOUNT_TYPE_INVEST_FUND: {
|
|
type: 'MONEY_MARKET_FUND',
|
|
name: 'Фонд денежного рынка',
|
|
},
|
|
} as const;
|
|
|
|
type AccountType = keyof typeof accountTypes;
|
|
export type SupportedTbankAccount = {
|
|
id: string;
|
|
name: string;
|
|
openedOn: null | string;
|
|
type: (typeof accountTypes)[AccountType]['type'];
|
|
};
|
|
|
|
export function supportedTbankAccounts(
|
|
accounts: TbankAccount[],
|
|
): SupportedTbankAccount[] {
|
|
return accounts.flatMap((account) => {
|
|
const details = accountTypes[account.type as AccountType];
|
|
if (!details) return [];
|
|
return [
|
|
{
|
|
id: account.id,
|
|
name: account.name.trim() || details.name,
|
|
type: details.type,
|
|
openedOn: openedOn(account.openedDate?.seconds),
|
|
},
|
|
];
|
|
});
|
|
}
|
|
|
|
function openedOn(seconds: number | string | undefined): null | string {
|
|
if (seconds === undefined) return null;
|
|
const date = new Date(Number(seconds) * MILLISECONDS_PER_SECOND);
|
|
if (Number.isNaN(date.valueOf())) return null;
|
|
return date.toISOString().slice(0, 10);
|
|
}
|