import { ISO_DATE_LENGTH, ISO_DATE_START, MILLISECONDS_PER_SECOND } from '../../common/time'; import { TbankAccountType } from './tbank-protocol'; import { TbankAccount } from './tbank.client'; const accountTypes = { [TbankAccountType.Tinkoff]: { type: 'BROKERAGE', name: 'Брокерский счет' }, [TbankAccountType.TinkoffIis]: { type: 'INDIVIDUAL_INVESTMENT', name: 'ИИС', }, [TbankAccountType.InvestBox]: { type: 'INVEST_BOX', name: 'Инвесткопилка' }, [TbankAccountType.InvestFund]: { 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(ISO_DATE_START, ISO_DATE_LENGTH); }