Browse Source

Refactoring

pull/2171/head
Thomas 2 years ago
parent
commit
43d85d23b9
  1. 3
      apps/api/src/app/portfolio/portfolio.controller.ts
  2. 15
      apps/api/src/app/portfolio/portfolio.service.ts
  3. 87
      apps/client/src/app/pages/portfolio/allocations/allocations-page.component.ts

3
apps/api/src/app/portfolio/portfolio.controller.ts

@ -179,6 +179,9 @@ export class PortfolioController {
countries: hasDetails ? portfolioPosition.countries : [], countries: hasDetails ? portfolioPosition.countries : [],
currency: hasDetails ? portfolioPosition.currency : undefined, currency: hasDetails ? portfolioPosition.currency : undefined,
markets: hasDetails ? portfolioPosition.markets : undefined, markets: hasDetails ? portfolioPosition.markets : undefined,
marketsAdvanced: hasDetails
? portfolioPosition.marketsAdvanced
: undefined,
sectors: hasDetails ? portfolioPosition.sectors : [] sectors: hasDetails ? portfolioPosition.sectors : []
}; };
} }

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

@ -1535,7 +1535,13 @@ export class PortfolioService {
); );
} }
private getLiabilities(activities: OrderWithAccount[]) { private getLiabilities({
activities,
userCurrency
}: {
activities: OrderWithAccount[];
userCurrency: string;
}) {
return activities return activities
.filter(({ type }) => { .filter(({ type }) => {
return type === TypeOfOrder.LIABILITY; return type === TypeOfOrder.LIABILITY;
@ -1544,7 +1550,7 @@ export class PortfolioService {
return this.exchangeRateDataService.toCurrency( return this.exchangeRateDataService.toCurrency(
new Big(quantity).mul(unitPrice).toNumber(), new Big(quantity).mul(unitPrice).toNumber(),
SymbolProfile.currency, SymbolProfile.currency,
this.request.user.Settings.settings.baseCurrency userCurrency
); );
}) })
.reduce( .reduce(
@ -1654,7 +1660,10 @@ export class PortfolioService {
const fees = this.getFees({ activities, userCurrency }).toNumber(); const fees = this.getFees({ activities, userCurrency }).toNumber();
const firstOrderDate = activities[0]?.date; const firstOrderDate = activities[0]?.date;
const items = this.getItems(activities).toNumber(); const items = this.getItems(activities).toNumber();
const liabilities = this.getLiabilities(activities).toNumber(); const liabilities = this.getLiabilities({
activities,
userCurrency
}).toNumber();
const totalBuy = this.getTotalByType(activities, userCurrency, 'BUY'); const totalBuy = this.getTotalByType(activities, userCurrency, 'BUY');
const totalSell = this.getTotalByType(activities, userCurrency, 'SELL'); const totalSell = this.getTotalByType(activities, userCurrency, 'SELL');

87
apps/client/src/app/pages/portfolio/allocations/allocations-page.component.ts

@ -357,58 +357,96 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
} }
this.markets.developedMarkets.value += this.markets.developedMarkets.value +=
position.markets.developedMarkets * position.valueInBaseCurrency; position.markets.developedMarkets *
(isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency
: position.valueInPercentage);
this.markets.emergingMarkets.value += this.markets.emergingMarkets.value +=
position.markets.emergingMarkets * position.valueInBaseCurrency; position.markets.emergingMarkets *
(isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency
: position.valueInPercentage);
this.markets.otherMarkets.value += this.markets.otherMarkets.value +=
position.markets.otherMarkets * position.valueInBaseCurrency; position.markets.otherMarkets *
(isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency
: position.valueInPercentage);
this.marketsAdvanced.asiaPacific.value += this.marketsAdvanced.asiaPacific.value +=
position.marketsAdvanced.asiaPacific * position.valueInBaseCurrency; position.marketsAdvanced.asiaPacific *
(isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency
: position.valueInPercentage);
this.marketsAdvanced.emergingMarkets.value += this.marketsAdvanced.emergingMarkets.value +=
position.marketsAdvanced.emergingMarkets * position.marketsAdvanced.emergingMarkets *
position.valueInBaseCurrency; (isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency
: position.valueInPercentage);
this.marketsAdvanced.europe.value += this.marketsAdvanced.europe.value +=
position.marketsAdvanced.europe * position.valueInBaseCurrency; position.marketsAdvanced.europe *
(isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency
: position.valueInPercentage);
this.marketsAdvanced.japan.value += this.marketsAdvanced.japan.value +=
position.marketsAdvanced.japan * position.valueInBaseCurrency; position.marketsAdvanced.japan *
(isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency
: position.valueInPercentage);
this.marketsAdvanced.northAmerica.value += this.marketsAdvanced.northAmerica.value +=
position.marketsAdvanced.northAmerica * position.marketsAdvanced.northAmerica *
position.valueInBaseCurrency; (isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency
: position.valueInPercentage);
for (const country of position.countries) { for (const country of position.countries) {
const { code, continent, name, weight } = country; const { code, continent, name, weight } = country;
if (this.continents[continent]?.value) { if (this.continents[continent]?.value) {
this.continents[continent].value += this.continents[continent].value +=
weight * position.valueInBaseCurrency; weight *
(isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency
: position.valueInPercentage);
} else { } else {
this.continents[continent] = { this.continents[continent] = {
name: continent, name: continent,
value: value:
weight * weight *
this.portfolioDetails.holdings[symbol].valueInBaseCurrency (isNumber(position.valueInBaseCurrency)
? this.portfolioDetails.holdings[symbol].valueInBaseCurrency
: this.portfolioDetails.holdings[symbol].valueInPercentage)
}; };
} }
if (this.countries[code]?.value) { if (this.countries[code]?.value) {
this.countries[code].value += this.countries[code].value +=
weight * position.valueInBaseCurrency; weight *
(isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency
: position.valueInPercentage);
} else { } else {
this.countries[code] = { this.countries[code] = {
name, name,
value: value:
weight * weight *
this.portfolioDetails.holdings[symbol].valueInBaseCurrency (isNumber(position.valueInBaseCurrency)
? this.portfolioDetails.holdings[symbol].valueInBaseCurrency
: this.portfolioDetails.holdings[symbol].valueInPercentage)
}; };
} }
} }
} else { } else {
this.continents[UNKNOWN_KEY].value += this.continents[UNKNOWN_KEY].value += isNumber(
this.portfolioDetails.holdings[symbol].valueInBaseCurrency; position.valueInBaseCurrency
)
? this.portfolioDetails.holdings[symbol].valueInBaseCurrency
: this.portfolioDetails.holdings[symbol].valueInPercentage;
this.countries[UNKNOWN_KEY].value += this.countries[UNKNOWN_KEY].value += isNumber(
this.portfolioDetails.holdings[symbol].valueInBaseCurrency; position.valueInBaseCurrency
)
? this.portfolioDetails.holdings[symbol].valueInBaseCurrency
: this.portfolioDetails.holdings[symbol].valueInPercentage;
} }
if (position.sectors.length > 0) { if (position.sectors.length > 0) {
@ -416,19 +454,28 @@ export class AllocationsPageComponent implements OnDestroy, OnInit {
const { name, weight } = sector; const { name, weight } = sector;
if (this.sectors[name]?.value) { if (this.sectors[name]?.value) {
this.sectors[name].value += weight * position.valueInBaseCurrency; this.sectors[name].value +=
weight *
(isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency
: position.valueInPercentage);
} else { } else {
this.sectors[name] = { this.sectors[name] = {
name, name,
value: value:
weight * weight *
this.portfolioDetails.holdings[symbol].valueInBaseCurrency (isNumber(position.valueInBaseCurrency)
? this.portfolioDetails.holdings[symbol].valueInBaseCurrency
: this.portfolioDetails.holdings[symbol].valueInPercentage)
}; };
} }
} }
} else { } else {
this.sectors[UNKNOWN_KEY].value += this.sectors[UNKNOWN_KEY].value += isNumber(
this.portfolioDetails.holdings[symbol].valueInBaseCurrency; position.valueInBaseCurrency
)
? this.portfolioDetails.holdings[symbol].valueInBaseCurrency
: this.portfolioDetails.holdings[symbol].valueInPercentage;
} }
} }

Loading…
Cancel
Save