59 lines
1.6 KiB
TypeScript
59 lines
1.6 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;
|
|
type: (typeof accountTypes)[AccountType]['type'];
|
|
openedOn: string | null;
|
|
};
|
|
|
|
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),
|
|
},
|
|
];
|
|
});
|
|
}
|
|
|
|
export function accessibleTbankAccount(
|
|
accounts: TbankAccount[],
|
|
accountId: string,
|
|
): TbankAccount | null {
|
|
return (
|
|
accounts.find(
|
|
(account) => account.id === accountId && account.type in accountTypes,
|
|
) ?? null
|
|
);
|
|
}
|
|
|
|
function openedOn(seconds: string | number | undefined): string | null {
|
|
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);
|
|
}
|