Browse Source

Refactoring

pull/3109/head
Thomas Kaul 1 year ago
parent
commit
888c4c8588
  1. 92
      apps/api/src/app/portfolio/portfolio.service.ts

92
apps/api/src/app/portfolio/portfolio.service.ts

@ -439,15 +439,33 @@ export class PortfolioService {
portfolioItemsNow[position.symbol] = position; portfolioItemsNow[position.symbol] = position;
} }
for (const item of currentPositions.positions) { for (const {
if (item.quantity.lte(0)) { currency,
firstBuyDate,
grossPerformance,
grossPerformanceWithCurrencyEffect,
grossPerformancePercentage,
grossPerformancePercentageWithCurrencyEffect,
investment,
marketPrice,
marketPriceInBaseCurrency,
netPerformance,
netPerformancePercentage,
netPerformancePercentageWithCurrencyEffect,
netPerformanceWithCurrencyEffect,
quantity,
symbol,
tags,
transactionCount
} of currentPositions.positions) {
if (quantity.eq(0)) {
// Ignore positions without any quantity // Ignore positions without any quantity
continue; continue;
} }
const value = item.quantity.mul(item.marketPriceInBaseCurrency ?? 0); const value = quantity.mul(marketPriceInBaseCurrency ?? 0);
const symbolProfile = symbolProfileMap[item.symbol]; const symbolProfile = symbolProfileMap[symbol];
const dataProviderResponse = dataProviderResponses[item.symbol]; const dataProviderResponse = dataProviderResponses[symbol];
const markets: PortfolioPosition['markets'] = { const markets: PortfolioPosition['markets'] = {
[UNKNOWN_KEY]: 0, [UNKNOWN_KEY]: 0,
@ -519,40 +537,39 @@ export class PortfolioService {
.toNumber(); .toNumber();
} }
holdings[item.symbol] = { holdings[symbol] = {
currency,
markets, markets,
marketsAdvanced, marketsAdvanced,
marketPrice,
symbol,
tags,
transactionCount,
allocationInPercentage: filteredValueInBaseCurrency.eq(0) allocationInPercentage: filteredValueInBaseCurrency.eq(0)
? 0 ? 0
: value.div(filteredValueInBaseCurrency).toNumber(), : value.div(filteredValueInBaseCurrency).toNumber(),
assetClass: symbolProfile.assetClass, assetClass: symbolProfile.assetClass,
assetSubClass: symbolProfile.assetSubClass, assetSubClass: symbolProfile.assetSubClass,
countries: symbolProfile.countries, countries: symbolProfile.countries,
currency: item.currency,
dataSource: symbolProfile.dataSource, dataSource: symbolProfile.dataSource,
dateOfFirstActivity: parseDate(item.firstBuyDate), dateOfFirstActivity: parseDate(firstBuyDate),
grossPerformance: item.grossPerformance?.toNumber() ?? 0, grossPerformance: grossPerformance?.toNumber() ?? 0,
grossPerformancePercent: grossPerformancePercent: grossPerformancePercentage?.toNumber() ?? 0,
item.grossPerformancePercentage?.toNumber() ?? 0,
grossPerformancePercentWithCurrencyEffect: grossPerformancePercentWithCurrencyEffect:
item.grossPerformancePercentageWithCurrencyEffect?.toNumber() ?? 0, grossPerformancePercentageWithCurrencyEffect?.toNumber() ?? 0,
grossPerformanceWithCurrencyEffect: grossPerformanceWithCurrencyEffect:
item.grossPerformanceWithCurrencyEffect?.toNumber() ?? 0, grossPerformanceWithCurrencyEffect?.toNumber() ?? 0,
investment: item.investment.toNumber(), investment: investment.toNumber(),
marketPrice: item.marketPrice,
marketState: dataProviderResponse?.marketState ?? 'delayed', marketState: dataProviderResponse?.marketState ?? 'delayed',
name: symbolProfile.name, name: symbolProfile.name,
netPerformance: item.netPerformance?.toNumber() ?? 0, netPerformance: netPerformance?.toNumber() ?? 0,
netPerformancePercent: item.netPerformancePercentage?.toNumber() ?? 0, netPerformancePercent: netPerformancePercentage?.toNumber() ?? 0,
netPerformancePercentWithCurrencyEffect: netPerformancePercentWithCurrencyEffect:
item.netPerformancePercentageWithCurrencyEffect?.toNumber() ?? 0, netPerformancePercentageWithCurrencyEffect?.toNumber() ?? 0,
netPerformanceWithCurrencyEffect: netPerformanceWithCurrencyEffect:
item.netPerformanceWithCurrencyEffect?.toNumber() ?? 0, netPerformanceWithCurrencyEffect?.toNumber() ?? 0,
quantity: item.quantity.toNumber(), quantity: quantity.toNumber(),
sectors: symbolProfile.sectors, sectors: symbolProfile.sectors,
symbol: item.symbol,
tags: item.tags,
transactionCount: item.transactionCount,
url: symbolProfile.url, url: symbolProfile.url,
valueInBaseCurrency: value.toNumber() valueInBaseCurrency: value.toNumber()
}; };
@ -1772,10 +1789,14 @@ export class PortfolioService {
const items = Object.keys(holdings) const items = Object.keys(holdings)
.filter((symbol) => { .filter((symbol) => {
return isUUID(symbol) && holdings[symbol].dataSource === 'MANUAL'; return (
isUUID(symbol) &&
holdings[symbol].dataSource === 'MANUAL' &&
holdings[symbol].valueInBaseCurrency > 0
);
}) })
.map((symbol) => { .map((symbol) => {
return holdings[symbol].valueInBaseCurrency; return Math.abs(holdings[symbol].valueInBaseCurrency);
}) })
.reduce( .reduce(
(previous, current) => new Big(previous).plus(current), (previous, current) => new Big(previous).plus(current),
@ -1783,11 +1804,22 @@ export class PortfolioService {
) )
.toNumber(); .toNumber();
const liabilities = this.getSumOfActivityType({ const liabilities = Object.keys(holdings)
activities, .filter((symbol) => {
userCurrency, return (
activityType: 'LIABILITY' isUUID(symbol) &&
}).toNumber(); holdings[symbol].dataSource === 'MANUAL' &&
holdings[symbol].valueInBaseCurrency < 0
);
})
.map((symbol) => {
return Math.abs(holdings[symbol].valueInBaseCurrency);
})
.reduce(
(previous, current) => new Big(previous).plus(current),
new Big(0)
)
.toNumber();
const totalBuy = this.getSumOfActivityType({ const totalBuy = this.getSumOfActivityType({
userCurrency, userCurrency,

Loading…
Cancel
Save