71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
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<typeof decimal>,
|
|
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<typeof decimal>,
|
|
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<typeof decimal>,
|
|
value: string,
|
|
currency: string | undefined,
|
|
): UpcomingEventDto['projectedAmount'] {
|
|
const normalizedCurrency = currency?.trim().toUpperCase();
|
|
if (!normalizedCurrency) return null;
|
|
return {
|
|
currency: normalizedCurrency,
|
|
value: formatDecimal(multiply(quantity, decimalFromString(value))),
|
|
};
|
|
}
|