import type { MoexBondEvent } from '#src/integrations/moex/moex.api'; import type { TbankDividend } from '#src/integrations/tbank/tbank.api'; import { decimalFromString, formatDecimal, multiply, } from '#src/common/decimal'; import { ISO_DATE_LENGTH, ISO_DATE_START, MILLISECONDS_PER_SECOND } from '#src/common/time'; import { decimal } from '#src/integrations/tbank/decimal'; import { UpcomingEventDto } from './upcoming-events.dto'; export function dividendEvent( metadata: Pick< UpcomingEventDto, 'displayName' | 'figi' | 'instrumentUid' | 'isin' | 'logoName' | 'ticker' >, quantity: ReturnType, event: TbankDividend, ): UpcomingEventDto { return { ...metadata, type: 'dividend', date: new Date(Number(event.recordDate?.seconds) * MILLISECONDS_PER_SECOND) .toISOString() .slice(ISO_DATE_START, ISO_DATE_LENGTH), dateKind: 'registry', projectedAmount: event.dividendNet ? projectedMoney( quantity, formatDecimal(decimal(event.dividendNet)), event.dividendNet.currency, ) : null, }; } export function bondEvent( metadata: Pick< UpcomingEventDto, 'displayName' | 'figi' | 'instrumentUid' | 'isin' | 'logoName' | 'ticker' >, quantity: ReturnType, event: MoexBondEvent, ): UpcomingEventDto { return { ...metadata, type: event.type, date: event.date, dateKind: 'event', projectedAmount: 'value' in event ? projectedMoney(quantity, event.value, event.currency) : null, }; } function projectedMoney( quantity: ReturnType, value: string, currency: string | undefined, ): UpcomingEventDto['projectedAmount'] { // RUB-only: projected income in other currencies is filtered out. if (currency?.trim().toUpperCase() !== 'RUB') return null; return { currency: 'RUB', value: formatDecimal(multiply(quantity, decimalFromString(value))), }; }