Browse Source

feat(client): resolve type errors

pull/7076/head
KenTandrian 4 weeks ago
parent
commit
5bb0b36e37
  1. 176
      apps/client/src/app/pages/portfolio/allocations/allocations-page.component.ts
  2. 2
      libs/common/src/lib/interfaces/holding.interface.ts

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

@ -12,7 +12,7 @@ import {
User User
} from '@ghostfolio/common/interfaces'; } from '@ghostfolio/common/interfaces';
import { hasPermission, permissions } from '@ghostfolio/common/permissions'; import { hasPermission, permissions } from '@ghostfolio/common/permissions';
import { Market, MarketAdvanced } from '@ghostfolio/common/types'; import { MarketAdvanced } from '@ghostfolio/common/types';
import { translate } from '@ghostfolio/ui/i18n'; import { translate } from '@ghostfolio/ui/i18n';
import { GfPortfolioProportionChartComponent } from '@ghostfolio/ui/portfolio-proportion-chart'; import { GfPortfolioProportionChartComponent } from '@ghostfolio/ui/portfolio-proportion-chart';
import { GfPremiumIndicatorComponent } from '@ghostfolio/ui/premium-indicator'; import { GfPremiumIndicatorComponent } from '@ghostfolio/ui/premium-indicator';
@ -83,9 +83,7 @@ export class GfAllocationsPageComponent implements OnInit {
> & { etfProvider: string; exchange?: string; value: number }; > & { etfProvider: string; exchange?: string; value: number };
}; };
public isLoading = false; public isLoading = false;
public markets: { public markets: PortfolioDetails['markets'];
[key in Market]: { id: Market; valueInPercentage: number };
};
public marketsAdvanced: { public marketsAdvanced: {
[key in MarketAdvanced]: { [key in MarketAdvanced]: {
id: MarketAdvanced; id: MarketAdvanced;
@ -187,7 +185,7 @@ export class GfAllocationsPageComponent implements OnInit {
public onAccountChartClicked({ accountId }: { accountId: string }) { public onAccountChartClicked({ accountId }: { accountId: string }) {
if (accountId && accountId !== UNKNOWN_KEY) { if (accountId && accountId !== UNKNOWN_KEY) {
this.router.navigate([], { void this.router.navigate([], {
queryParams: { accountId, accountDetailDialog: true } queryParams: { accountId, accountDetailDialog: true }
}); });
} }
@ -195,7 +193,7 @@ export class GfAllocationsPageComponent implements OnInit {
public onSymbolChartClicked({ dataSource, symbol }: AssetProfileIdentifier) { public onSymbolChartClicked({ dataSource, symbol }: AssetProfileIdentifier) {
if (dataSource && symbol) { if (dataSource && symbol) {
this.router.navigate([], { void this.router.navigate([], {
queryParams: { dataSource, symbol, holdingDetailDialog: true } queryParams: { dataSource, symbol, holdingDetailDialog: true }
}); });
} }
@ -226,9 +224,9 @@ export class GfAllocationsPageComponent implements OnInit {
name name
}: { }: {
assetSubClass: PortfolioPosition['assetProfile']['assetSubClass']; assetSubClass: PortfolioPosition['assetProfile']['assetSubClass'];
name: string; name?: string;
}) { }) {
if (assetSubClass === 'ETF') { if (assetSubClass === 'ETF' && name) {
const [firstWord] = name.split(' '); const [firstWord] = name.split(' ');
return firstWord; return firstWord;
} }
@ -298,7 +296,7 @@ export class GfAllocationsPageComponent implements OnInit {
this.platforms = {}; this.platforms = {};
this.portfolioDetails = { this.portfolioDetails = {
accounts: {}, accounts: {},
createdAt: undefined, createdAt: new Date(),
holdings: {}, holdings: {},
platforms: {}, platforms: {},
summary: undefined summary: undefined
@ -327,7 +325,7 @@ export class GfAllocationsPageComponent implements OnInit {
let value = 0; let value = 0;
if (this.showValuesInPercentage()) { if (this.showValuesInPercentage()) {
value = valueInPercentage; value = valueInPercentage ?? 0;
} else { } else {
value = valueInBaseCurrency; value = valueInBaseCurrency;
} }
@ -347,18 +345,18 @@ export class GfAllocationsPageComponent implements OnInit {
if (this.showValuesInPercentage()) { if (this.showValuesInPercentage()) {
value = position.allocationInPercentage; value = position.allocationInPercentage;
} else { } else {
value = position.valueInBaseCurrency; value = position.valueInBaseCurrency ?? 0;
} }
this.holdings[symbol] = { this.holdings[symbol] = {
value, value,
assetClass: assetClass:
position.assetProfile.assetClass || (UNKNOWN_KEY as AssetClass), position.assetProfile.assetClass || (UNKNOWN_KEY as AssetClass),
assetClassLabel: position.assetProfile.assetClassLabel || UNKNOWN_KEY, assetClassLabel: position.assetProfile.assetClassLabel ?? UNKNOWN_KEY,
assetSubClass: assetSubClass:
position.assetProfile.assetSubClass || (UNKNOWN_KEY as AssetSubClass), position.assetProfile.assetSubClass || (UNKNOWN_KEY as AssetSubClass),
assetSubClassLabel: assetSubClassLabel:
position.assetProfile.assetSubClassLabel || UNKNOWN_KEY, position.assetProfile.assetSubClassLabel ?? UNKNOWN_KEY,
currency: this.extractCurrency(position.assetProfile), currency: this.extractCurrency(position.assetProfile),
etfProvider: this.extractEtfProvider({ etfProvider: this.extractEtfProvider({
assetSubClass: position.assetProfile.assetSubClass, assetSubClass: position.assetProfile.assetSubClass,
@ -373,53 +371,45 @@ export class GfAllocationsPageComponent implements OnInit {
if (position.assetProfile.countries.length > 0) { if (position.assetProfile.countries.length > 0) {
for (const country of position.assetProfile.countries) { for (const country of position.assetProfile.countries) {
const { code, continent, weight } = country; const { code, continent, weight } = country;
const val =
if (this.continents[continent]?.value) { (isNumber(position.valueInBaseCurrency)
this.continents[continent].value += ? position.valueInBaseCurrency
weight * : position.valueInPercentage) ?? 0;
(isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency const continentData = this.continents[continent];
: position.valueInPercentage); if (continentData) {
continentData.value += weight * val;
} else { } else {
this.continents[continent] = { this.continents[continent] = {
name: translate(continent), name: translate(continent),
value: value: weight * val
weight *
(isNumber(position.valueInBaseCurrency)
? this.portfolioDetails.holdings[symbol].valueInBaseCurrency
: this.portfolioDetails.holdings[symbol].valueInPercentage)
}; };
} }
if (this.countries[code]?.value) { const countryData = this.countries[code];
this.countries[code].value += if (countryData) {
weight * countryData.value += weight * val;
(isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency
: position.valueInPercentage);
} else { } else {
this.countries[code] = { this.countries[code] = {
name: getCountryName({ code }), name: getCountryName({ code }),
value: value: weight * val
weight *
(isNumber(position.valueInBaseCurrency)
? this.portfolioDetails.holdings[symbol].valueInBaseCurrency
: this.portfolioDetails.holdings[symbol].valueInPercentage)
}; };
} }
} }
} else { } else {
this.continents[UNKNOWN_KEY].value += isNumber( const val =
position.valueInBaseCurrency (isNumber(position.valueInBaseCurrency)
) ? position.valueInBaseCurrency
? this.portfolioDetails.holdings[symbol].valueInBaseCurrency : position.valueInPercentage) ?? 0;
: this.portfolioDetails.holdings[symbol].valueInPercentage;
const continentData = this.continents[UNKNOWN_KEY];
this.countries[UNKNOWN_KEY].value += isNumber( if (continentData) {
position.valueInBaseCurrency continentData.value += val;
) }
? this.portfolioDetails.holdings[symbol].valueInBaseCurrency const countryData = this.countries[UNKNOWN_KEY];
: this.portfolioDetails.holdings[symbol].valueInPercentage; if (countryData) {
countryData.value += val;
}
} }
if (position.assetProfile.holdings.length > 0) { if (position.assetProfile.holdings.length > 0) {
@ -429,21 +419,17 @@ export class GfAllocationsPageComponent implements OnInit {
valueInBaseCurrency valueInBaseCurrency
} of position.assetProfile.holdings) { } of position.assetProfile.holdings) {
const normalizedAssetName = this.normalizeAssetName(name); const normalizedAssetName = this.normalizeAssetName(name);
const val = isNumber(valueInBaseCurrency)
? valueInBaseCurrency
: allocationInPercentage * (position.valueInPercentage ?? 0);
if (this.topHoldingsMap[normalizedAssetName]?.value) { const holdingData = this.topHoldingsMap[normalizedAssetName];
this.topHoldingsMap[normalizedAssetName].value += isNumber( if (holdingData) {
valueInBaseCurrency holdingData.value += val;
)
? valueInBaseCurrency
: allocationInPercentage *
this.portfolioDetails.holdings[symbol].valueInPercentage;
} else { } else {
this.topHoldingsMap[normalizedAssetName] = { this.topHoldingsMap[normalizedAssetName] = {
name, name,
value: isNumber(valueInBaseCurrency) value: val
? valueInBaseCurrency
: allocationInPercentage *
this.portfolioDetails.holdings[symbol].valueInPercentage
}; };
} }
} }
@ -452,30 +438,31 @@ export class GfAllocationsPageComponent implements OnInit {
if (position.assetProfile.sectors.length > 0) { if (position.assetProfile.sectors.length > 0) {
for (const sector of position.assetProfile.sectors) { for (const sector of position.assetProfile.sectors) {
const { name, weight } = sector; const { name, weight } = sector;
const val =
if (this.sectors[name]?.value) { (isNumber(position.valueInBaseCurrency)
this.sectors[name].value += ? position.valueInBaseCurrency
weight * : position.valueInPercentage) ?? 0;
(isNumber(position.valueInBaseCurrency)
? position.valueInBaseCurrency const sectorData = this.sectors[name];
: position.valueInPercentage); if (sectorData) {
sectorData.value += weight * val;
} else { } else {
this.sectors[name] = { this.sectors[name] = {
name: translate(name), name: translate(name),
value: value: weight * val
weight *
(isNumber(position.valueInBaseCurrency)
? this.portfolioDetails.holdings[symbol].valueInBaseCurrency
: this.portfolioDetails.holdings[symbol].valueInPercentage)
}; };
} }
} }
} else { } else {
this.sectors[UNKNOWN_KEY].value += isNumber( const val =
position.valueInBaseCurrency (isNumber(position.valueInBaseCurrency)
) ? position.valueInBaseCurrency
? this.portfolioDetails.holdings[symbol].valueInBaseCurrency : position.valueInPercentage) ?? 0;
: this.portfolioDetails.holdings[symbol].valueInPercentage;
const sectorData = this.sectors[UNKNOWN_KEY];
if (sectorData) {
sectorData.value += val;
}
} }
if (this.holdings[symbol].assetSubClass === 'ETF') { if (this.holdings[symbol].assetSubClass === 'ETF') {
@ -484,23 +471,26 @@ export class GfAllocationsPageComponent implements OnInit {
this.symbols[prettifySymbol(symbol)] = { this.symbols[prettifySymbol(symbol)] = {
dataSource: position.assetProfile.dataSource, dataSource: position.assetProfile.dataSource,
name: position.assetProfile.name, name: position.assetProfile.name ?? '',
symbol: prettifySymbol(symbol), symbol: prettifySymbol(symbol),
value: isNumber(position.valueInBaseCurrency) value:
? position.valueInBaseCurrency (isNumber(position.valueInBaseCurrency)
: position.valueInPercentage ? position.valueInBaseCurrency
: position.valueInPercentage) ?? 0
}; };
} }
this.markets = this.portfolioDetails.markets; this.markets = this.portfolioDetails.markets;
Object.values(this.portfolioDetails.marketsAdvanced).forEach( if (this.portfolioDetails.marketsAdvanced) {
({ id, valueInBaseCurrency, valueInPercentage }) => { Object.values(this.portfolioDetails.marketsAdvanced).forEach(
this.marketsAdvanced[id].value = isNumber(valueInBaseCurrency) ({ id, valueInBaseCurrency, valueInPercentage }) => {
? valueInBaseCurrency this.marketsAdvanced[id].value = isNumber(valueInBaseCurrency)
: valueInPercentage; ? valueInBaseCurrency
} : valueInPercentage;
); }
);
}
for (const [ for (const [
id, id,
@ -509,7 +499,7 @@ export class GfAllocationsPageComponent implements OnInit {
let value = 0; let value = 0;
if (this.showValuesInPercentage()) { if (this.showValuesInPercentage()) {
value = valueInPercentage; value = valueInPercentage ?? 0;
} else { } else {
value = valueInBaseCurrency; value = valueInBaseCurrency;
} }
@ -522,12 +512,11 @@ export class GfAllocationsPageComponent implements OnInit {
} }
this.topHoldings = Object.values(this.topHoldingsMap) this.topHoldings = Object.values(this.topHoldingsMap)
.map(({ name, value }) => { .map(({ name, value }): HoldingWithParents => {
if (this.showValuesInPercentage()) { if (this.showValuesInPercentage()) {
return { return {
name, name,
allocationInPercentage: value, allocationInPercentage: value
valueInBaseCurrency: null
}; };
} }
@ -547,11 +536,12 @@ export class GfAllocationsPageComponent implements OnInit {
} }
); );
return currentParentHolding return currentParentHolding &&
isNumber(currentParentHolding.valueInBaseCurrency)
? { ? {
allocationInPercentage: allocationInPercentage:
currentParentHolding.valueInBaseCurrency / value, currentParentHolding.valueInBaseCurrency / value,
name: holding.assetProfile.name, name: holding.assetProfile.name ?? '',
position: holding, position: holding,
symbol: prettifySymbol(symbol), symbol: prettifySymbol(symbol),
valueInBaseCurrency: valueInBaseCurrency:
@ -611,7 +601,7 @@ export class GfAllocationsPageComponent implements OnInit {
.afterClosed() .afterClosed()
.pipe(takeUntilDestroyed(this.destroyRef)) .pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(() => { .subscribe(() => {
this.router.navigate(['.'], { relativeTo: this.route }); void this.router.navigate(['.'], { relativeTo: this.route });
}); });
} }

2
libs/common/src/lib/interfaces/holding.interface.ts

@ -1,5 +1,5 @@
export interface Holding { export interface Holding {
allocationInPercentage: number; allocationInPercentage: number;
name: string; name: string;
valueInBaseCurrency: number; valueInBaseCurrency?: number;
} }

Loading…
Cancel
Save