refactor: organize backend by business responsibility
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import {
|
||||
add,
|
||||
formatDecimal,
|
||||
isZero,
|
||||
multiply,
|
||||
} from '../../../../common/decimal';
|
||||
import type { Money } from '../../../../common/money.types';
|
||||
import { decimal } from '../../../../integrations/tbank/decimal';
|
||||
|
||||
import {
|
||||
TbankPortfolio,
|
||||
TbankPositions,
|
||||
} from '../../../../integrations/tbank/tbank.api';
|
||||
|
||||
function totalsByCurrency(
|
||||
positions: NonNullable<TbankPortfolio['positions']>,
|
||||
money: NonNullable<TbankPositions['money']>,
|
||||
): Money[] {
|
||||
const totals = new Map<string, ReturnType<typeof decimal>>();
|
||||
const addTotal = (
|
||||
currency: string | undefined,
|
||||
amount: ReturnType<typeof decimal>,
|
||||
) => {
|
||||
const normalizedCurrency = currency?.trim().toUpperCase();
|
||||
if (!normalizedCurrency || isZero(amount)) return;
|
||||
totals.set(
|
||||
normalizedCurrency,
|
||||
add(totals.get(normalizedCurrency) ?? decimal({}), amount),
|
||||
);
|
||||
};
|
||||
|
||||
for (const value of money) addTotal(value.currency, decimal(value));
|
||||
for (const position of positions) {
|
||||
const price = position.currentPrice;
|
||||
if (!price) continue;
|
||||
addTotal(
|
||||
price.currency,
|
||||
multiply(decimal(position.quantity ?? {}), decimal(price)),
|
||||
);
|
||||
}
|
||||
|
||||
return [...totals]
|
||||
.filter(([, value]) => !isZero(value))
|
||||
.map(([currency, value]) => ({ currency, value: formatDecimal(value) }));
|
||||
}
|
||||
|
||||
export { totalsByCurrency };
|
||||
Reference in New Issue
Block a user