Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

22.6 KB
Raw Blame
import { ImpersonationStorageService } from '@ghostfolio/client/services/impersonation-storage.service';
import { UserService } from '@ghostfolio/client/services/user/user.service';
import {
  DEFAULT_PAGE_SIZE,
  E_MAIL_LINE_BREAK,
  NUMERICAL_PRECISION_THRESHOLD_3_FIGURES,
  NUMERICAL_PRECISION_THRESHOLD_4_FIGURES,
  TAG_ID_DRAFT
} from '@ghostfolio/common/config';
import { CreateOrderDto } from '@ghostfolio/common/dtos';
import {
  DATE_FORMAT,
  downloadAsFile,
  getCountryName
} from '@ghostfolio/common/helper';
import {
  Activity,
  DataProviderInfo,
  EnhancedAssetProfile,
  Filter,
  LineChartItem,
  NullableLineChartItem,
  User
} from '@ghostfolio/common/interfaces';
import { hasPermission, permissions } from '@ghostfolio/common/permissions';
import { internalRoutes } from '@ghostfolio/common/routes/routes';
import { AccountWithValue } from '@ghostfolio/common/types';
import type { MarketData, Tag } from '@ghostfolio/prisma/browser';
import { GfAccountsTableComponent } from '@ghostfolio/ui/accounts-table';
import { GfActivitiesTableComponent } from '@ghostfolio/ui/activities-table';
import { GfDataProviderCreditsComponent } from '@ghostfolio/ui/data-provider-credits';
import { GfDialogFooterComponent } from '@ghostfolio/ui/dialog-footer';
import { GfDialogHeaderComponent } from '@ghostfolio/ui/dialog-header';
import { GfHistoricalMarketDataEditorComponent } from '@ghostfolio/ui/historical-market-data-editor';
import { translate } from '@ghostfolio/ui/i18n';
import { GfLineChartComponent } from '@ghostfolio/ui/line-chart';
import { GfPortfolioProportionChartComponent } from '@ghostfolio/ui/portfolio-proportion-chart';
import { GfPremiumIndicatorComponent } from '@ghostfolio/ui/premium-indicator';
import { DataService } from '@ghostfolio/ui/services';
import { GfTagsSelectorComponent } from '@ghostfolio/ui/tags-selector';
import { GfValueComponent } from '@ghostfolio/ui/value';

import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  DestroyRef,
  OnInit,
  inject
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import {
  FormBuilder,
  FormControl,
  FormGroup,
  ReactiveFormsModule
} from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatChipsModule } from '@angular/material/chips';
import {
  MAT_DIALOG_DATA,
  MatDialogModule,
  MatDialogRef
} from '@angular/material/dialog';
import { MatFormFieldModule } from '@angular/material/form-field';
import { PageEvent } from '@angular/material/paginator';
import { SortDirection } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { MatTabsModule } from '@angular/material/tabs';
import { NavigationStart, Router, RouterModule } from '@angular/router';
import { IonIcon } from '@ionic/angular/standalone';
import { isUUID } from 'class-validator';
import { format, isSameMonth, isToday, parseISO } from 'date-fns';
import { addIcons } from 'ionicons';
import {
  arrowDownCircleOutline,
  createOutline,
  flagOutline,
  readerOutline,
  serverOutline,
  swapVerticalOutline,
  walletOutline
} from 'ionicons/icons';
import { isNumber, round, uniqBy } from 'lodash-es';
import { NgxSkeletonLoaderModule } from 'ngx-skeleton-loader';
import { filter, switchMap } from 'rxjs/operators';

import {
  HoldingDetailDialogParams,
  HoldingDetailDialogResult
} from './interfaces/interfaces';

@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { class: 'd-flex flex-column h-100' },
  imports: [
    GfAccountsTableComponent,
    GfActivitiesTableComponent,
    GfDataProviderCreditsComponent,
    GfDialogFooterComponent,
    GfDialogHeaderComponent,
    GfHistoricalMarketDataEditorComponent,
    GfLineChartComponent,
    GfPortfolioProportionChartComponent,
    GfPremiumIndicatorComponent,
    GfTagsSelectorComponent,
    GfValueComponent,
    IonIcon,
    MatButtonModule,
    MatChipsModule,
    MatDialogModule,
    MatFormFieldModule,
    MatTabsModule,
    NgxSkeletonLoaderModule,
    ReactiveFormsModule,
    RouterModule
  ],
  selector: 'gf-holding-detail-dialog',
  styleUrls: ['./holding-detail-dialog.component.scss'],
  templateUrl: 'holding-detail-dialog.html'
})
export class GfHoldingDetailDialogComponent implements OnInit {
  protected accounts: AccountWithValue[];
  protected activitiesCount: number;
  protected assetClass: string;
  protected assetProfile: Pick<
    EnhancedAssetProfile,
    | 'assetClass'
    | 'assetSubClass'
    | 'countries'
    | 'currency'
    | 'dataSource'
    | 'isin'
    | 'name'
    | 'sectors'
    | 'symbol'
    | 'userId'
  >;
  protected assetSubClass: string;
  protected averagePrice: number;
  protected averagePricePrecision = 2;
  protected benchmarkDataItems: NullableLineChartItem[];
  protected readonly benchmarkLabel = $localize`Average Unit Price`;
  protected countries: {
    [code: string]: { name: string; value: number };
  };
  protected dataProviderInfo: DataProviderInfo;
  protected dataSource: MatTableDataSource<Activity>;
  protected dateOfFirstActivity: Date;
  protected dividendInBaseCurrency: number;
  protected dividendInBaseCurrencyPrecision = 2;
  protected dividendYieldPercentWithCurrencyEffect: number;
  protected feeInBaseCurrency: number;
  protected readonly getCountryName = getCountryName;
  protected hasPermissionToCreateOwnTag: boolean;
  protected hasPermissionToReadMarketDataOfOwnAssetProfile: boolean;
  protected historicalDataItems: LineChartItem[];
  protected holdingForm: FormGroup<{
    tags: FormControl<Tag[] | null>;
  }>;
  protected investmentInBaseCurrencyWithCurrencyEffect: number;
  protected investmentInBaseCurrencyWithCurrencyEffectPrecision = 2;
  protected isLoading = true;
  protected isOwnAssetProfile: boolean;
  protected readonly isUUID = isUUID;
  protected marketDataItems: MarketData[] = [];
  protected marketPrice: number;
  protected marketPriceMax: number;
  protected marketPriceMaxPrecision = 2;
  protected marketPriceMin: number;
  protected marketPriceMinPrecision = 2;
  protected marketPricePrecision = 2;
  protected netPerformancePercentWithCurrencyEffect: number;
  protected netPerformancePercentWithCurrencyEffectPrecision = 2;
  protected netPerformanceWithCurrencyEffect: number;
  protected netPerformanceWithCurrencyEffectPrecision = 2;
  protected pageIndex = 0;
  protected readonly pageSize = DEFAULT_PAGE_SIZE;
  protected quantity: number;
  protected quantityPrecision = 2;
  protected reportDataGlitchMailHref: string;
  protected readonly round = round;
  protected readonly routerLinkAdminControlMarketDataUpdate =
    internalRoutes.adminControl.subRoutes.marketData.subRoutes.update
      .routerLink;
  protected sectors: {
    [name: string]: { name: string; value: number };
  };
  protected sortColumn = 'date';
  protected sortDirection: SortDirection = 'desc';
  protected tagsAvailable: Tag[];
  protected tagsOfAccounts: Tag[];
  protected readonly translate = translate;
  protected user: User;
  protected value: number;

  protected readonly data = inject<HoldingDetailDialogParams>(MAT_DIALOG_DATA);
  protected readonly dialogRef =
    inject<
      MatDialogRef<GfHoldingDetailDialogComponent, HoldingDetailDialogResult>
    >(MatDialogRef);

  private tags: Tag[];

  private readonly changeDetectorRef = inject(ChangeDetectorRef);
  private readonly dataService = inject(DataService);
  private readonly destroyRef = inject(DestroyRef);
  private readonly formBuilder = inject(FormBuilder);
  private readonly impersonationStorageService = inject(
    ImpersonationStorageService
  );
  private readonly router = inject(Router);
  private readonly userService = inject(UserService);

  public constructor() {
    this.router.events
      .pipe(
        filter((event) => {
          return event instanceof NavigationStart;
        }),
        takeUntilDestroyed(this.destroyRef)
      )
      .subscribe(() => {
        this.dialogRef.close({ isNavigating: true });
      });

    addIcons({
      arrowDownCircleOutline,
      createOutline,
      flagOutline,
      readerOutline,
      serverOutline,
      swapVerticalOutline,
      walletOutline
    });
  }

  public ngOnInit() {
    const filters = this.getActivityFilters();

    this.holdingForm = this.formBuilder.group({
      tags: new FormControl<Tag[]>([])
    });

    this.holdingForm.controls.tags.valueChanges
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe((tags: Tag[]) => {
        const newTag = tags.find(({ id }) => {
          return id === undefined;
        });

        if (newTag && this.hasPermissionToCreateOwnTag) {
          this.dataService
            .postTag({ ...newTag, userId: this.user.id })
            .pipe(
              switchMap((createdTag) => {
                return this.dataService.putHoldingTags({
                  dataSource: this.data.dataSource,
                  symbol: this.data.symbol,
                  tags: [
                    ...tags.filter(({ id }) => {
                      return id !== undefined;
                    }),
                    createdTag
                  ]
                });
              }),
              switchMap(() => {
                return this.userService.get(true);
              }),
              takeUntilDestroyed(this.destroyRef)
            )
            .subscribe();
        } else {
          this.dataService
            .putHoldingTags({
              tags,
              dataSource: this.data.dataSource,
              symbol: this.data.symbol
            })
            .pipe(takeUntilDestroyed(this.destroyRef))
            .subscribe();
        }
      });

    this.dataService
      .fetchAccounts({
        filters
      })
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(({ accounts }) => {
        this.accounts = accounts;

        this.tagsOfAccounts = uniqBy(
          accounts.flatMap(({ tags }) => {
            return tags ?? [];
          }),
          'id'
        ).map((tag) => {
          return {
            ...tag,
            name: translate(tag.name)
          };
        });

        this.changeDetectorRef.markForCheck();
      });

    this.fetchActivities(filters);

    this.dataService
      .fetchHoldingDetail({
        dataSource: this.data.dataSource,
        symbol: this.data.symbol
      })
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(
        ({
          activitiesCount,
          assetProfile,
          averagePrice,
          dataProviderInfo,
          dateOfFirstActivity,
          dividendInBaseCurrency,
          dividendYieldPercentWithCurrencyEffect,
          feeInBaseCurrency,
          historicalData,
          investmentInBaseCurrencyWithCurrencyEffect,
          marketPrice,
          marketPriceMax,
          marketPriceMin,
          netPerformancePercentWithCurrencyEffect,
          netPerformanceWithCurrencyEffect,
          quantity,
          tags,
          value
        }) => {
          this.activitiesCount = activitiesCount;
          this.assetProfile = assetProfile;
          this.averagePrice = averagePrice;

          if (
            this.averagePrice >= NUMERICAL_PRECISION_THRESHOLD_4_FIGURES &&
            this.data.deviceType === 'mobile'
          ) {
            this.averagePricePrecision = 0;
          }

          this.benchmarkDataItems = [];
          this.countries = {};
          this.dataProviderInfo = dataProviderInfo;

          if (dateOfFirstActivity) {
            this.dateOfFirstActivity = dateOfFirstActivity;
          }

          this.dividendInBaseCurrency = dividendInBaseCurrency;

          if (
            this.data.deviceType === 'mobile' &&
            this.dividendInBaseCurrency >=
              NUMERICAL_PRECISION_THRESHOLD_4_FIGURES
          ) {
            this.dividendInBaseCurrencyPrecision = 0;
          }

          this.dividendYieldPercentWithCurrencyEffect =
            dividendYieldPercentWithCurrencyEffect;

          this.feeInBaseCurrency = feeInBaseCurrency;

          this.hasPermissionToReadMarketDataOfOwnAssetProfile = hasPermission(
            this.user?.permissions,
            permissions.readMarketDataOfOwnAssetProfile
          );

          this.historicalDataItems = historicalData.map(
            ({ averagePrice, date, marketPrice }) => {
              this.benchmarkDataItems.push({
                date,
                value: isNumber(averagePrice) ? averagePrice : null
              });

              return {
                date,
                value: marketPrice ?? 0
              };
            }
          );

          this.investmentInBaseCurrencyWithCurrencyEffect =
            investmentInBaseCurrencyWithCurrencyEffect;

          if (
            this.data.deviceType === 'mobile' &&
            this.investmentInBaseCurrencyWithCurrencyEffect >=
              NUMERICAL_PRECISION_THRESHOLD_4_FIGURES
          ) {
            this.investmentInBaseCurrencyWithCurrencyEffectPrecision = 0;
          }

          this.isOwnAssetProfile =
            assetProfile?.dataSource === 'MANUAL' &&
            assetProfile?.userId === this.user?.id;

          this.marketPrice = marketPrice;
          this.marketPriceMax = marketPriceMax;

          if (
            this.data.deviceType === 'mobile' &&
            this.marketPriceMax >= NUMERICAL_PRECISION_THRESHOLD_4_FIGURES
          ) {
            this.marketPriceMaxPrecision = 0;
          }

          this.marketPriceMin = marketPriceMin;

          if (
            this.data.deviceType === 'mobile' &&
            this.marketPriceMin >= NUMERICAL_PRECISION_THRESHOLD_4_FIGURES
          ) {
            this.marketPriceMinPrecision = 0;
          }

          if (
            this.data.deviceType === 'mobile' &&
            this.marketPrice >= NUMERICAL_PRECISION_THRESHOLD_4_FIGURES
          ) {
            this.marketPricePrecision = 0;
          }

          this.netPerformancePercentWithCurrencyEffect =
            netPerformancePercentWithCurrencyEffect;

          if (
            this.data.deviceType === 'mobile' &&
            this.netPerformancePercentWithCurrencyEffect >=
              NUMERICAL_PRECISION_THRESHOLD_3_FIGURES
          ) {
            this.netPerformancePercentWithCurrencyEffectPrecision = 0;
          }

          this.netPerformanceWithCurrencyEffect =
            netPerformanceWithCurrencyEffect;

          if (
            this.data.deviceType === 'mobile' &&
            this.netPerformanceWithCurrencyEffect >=
              NUMERICAL_PRECISION_THRESHOLD_4_FIGURES
          ) {
            this.netPerformanceWithCurrencyEffectPrecision = 0;
          }

          this.quantity = quantity;

          if (Number.isInteger(this.quantity)) {
            this.quantityPrecision = 0;
          } else if (assetProfile?.assetSubClass === 'CRYPTOCURRENCY') {
            if (this.quantity < 10) {
              this.quantityPrecision = 8;
            } else if (this.quantity < 1000) {
              this.quantityPrecision = 6;
            } else if (this.quantity >= 10000000) {
              this.quantityPrecision = 0;
            }
          }

          this.sectors = {};

          this.tags = tags.map((tag) => {
            return {
              ...tag,
              name: translate(tag.name)
            };
          });

          this.holdingForm.setValue({ tags: this.tags }, { emitEvent: false });

          this.value = value;

          const reportDataGlitchSubject = `Ghostfolio Data Glitch Report${
            this.assetProfile?.symbol ? ` (${this.assetProfile.symbol})` : ''
          }`;

          this.reportDataGlitchMailHref = `mailto:[email protected]?subject=${reportDataGlitchSubject}&body=${[
            'Hello',
            '',
            'I would like to report a data glitch for',
            '',
            `Symbol: ${this.assetProfile?.symbol}`,
            `Data Source: ${this.assetProfile?.dataSource}`,
            '',
            'Additional notes:',
            '',
            'Can you please take a look?',
            '',
            'Kind regards'
          ].join(E_MAIL_LINE_BREAK)}`;

          if (this.assetProfile?.assetClass) {
            this.assetClass = translate(this.assetProfile?.assetClass);
          }

          if (this.assetProfile?.assetSubClass) {
            this.assetSubClass = translate(this.assetProfile?.assetSubClass);
          }

          if (this.assetProfile?.countries?.length > 0) {
            for (const country of this.assetProfile.countries) {
              this.countries[country.code] = {
                name: getCountryName({ code: country.code }),
                value: country.weight
              };
            }
          }

          if (this.assetProfile?.sectors?.length > 0) {
            for (const sector of this.assetProfile.sectors) {
              this.sectors[sector.name] = {
                name: translate(sector.name),
                value: sector.weight
              };
            }
          }

          if (isToday(this.dateOfFirstActivity)) {
            // Add average price
            this.historicalDataItems.push({
              date: this.dateOfFirstActivity.toISOString(),
              value: this.averagePrice
            });

            // Add benchmark 1
            this.benchmarkDataItems.push({
              date: this.dateOfFirstActivity.toISOString(),
              value: averagePrice
            });

            // Add market price
            this.historicalDataItems.push({
              date: new Date().toISOString(),
              value: this.marketPrice
            });

            // Add benchmark 2
            this.benchmarkDataItems.push({
              date: new Date().toISOString(),
              value: averagePrice
            });
          } else {
            // Add market price
            this.historicalDataItems.push({
              date: format(new Date(), DATE_FORMAT),
              value: this.marketPrice
            });

            // Add benchmark
            this.benchmarkDataItems.push({
              date: format(new Date(), DATE_FORMAT),
              value: averagePrice
            });
          }

          if (
            this.benchmarkDataItems[0]?.value === undefined &&
            isSameMonth(this.dateOfFirstActivity, new Date())
          ) {
            this.benchmarkDataItems[0].value = this.averagePrice;
          }

          this.benchmarkDataItems = this.benchmarkDataItems.map(
            ({ date, value }) => {
              return {
                date,
                value: value === 0 ? null : value
              };
            }
          );

          if (
            this.hasPermissionToReadMarketDataOfOwnAssetProfile &&
            this.isOwnAssetProfile
          ) {
            this.fetchMarketData();
          }

          this.isLoading = false;

          this.changeDetectorRef.markForCheck();
        }
      );

    this.userService.stateChanged
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe((state) => {
        if (state?.user) {
          this.user = state.user;

          // A tag created during an impersonation belongs to the authenticated
          // user, hence it cannot be assigned to the data of the impersonated
          // user
          this.hasPermissionToCreateOwnTag =
            !this.impersonationStorageService.getId() &&
            hasPermission(this.user?.permissions, permissions.createOwnTag);

          this.tagsAvailable =
            this.user?.tags
              ?.filter(({ id }) => {
                // The "DRAFT" tag qualifies an individual activity and cannot
                // be assigned to all activities of a holding
                return id !== TAG_ID_DRAFT;
              })
              .map((tag) => {
                return {
                  ...tag,
                  name: translate(tag.name)
                };
              }) ?? [];

          this.changeDetectorRef.markForCheck();
        }
      });
  }

  protected onChangePage(page: PageEvent) {
    this.pageIndex = page.pageIndex;

    this.fetchActivities();
  }

  protected onClose() {
    this.dialogRef.close();
  }

  protected onCloseHolding() {
    const today = new Date();

    const activity: CreateOrderDto = {
      accountId: this.accounts.length === 1 ? this.accounts[0].id : undefined,
      comment: undefined,
      currency: this.assetProfile?.currency ?? '',
      dataSource: this.assetProfile?.dataSource,
      date: today.toISOString(),
      fee: 0,
      quantity: this.quantity,
      symbol: this.assetProfile?.symbol ?? '',
      tags: this.tags.map(({ id }) => {
        return id;
      }),
      type: 'SELL',
      unitPrice: this.marketPrice
    };

    this.dataService
      .postActivity(activity)
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(() => {
        this.userService.get(true).subscribe();

        this.navigateToActivities();
      });
  }

  protected onExport() {
    const activityIds = this.dataSource.data.map(({ id }) => {
      return id;
    });

    this.dataService
      .fetchExport({ activityIds })
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe((data) => {
        downloadAsFile({
          content: data,
          fileName: `ghostfolio-export-${this.assetProfile?.symbol}-${format(
            parseISO(data.meta.date),
            'yyyyMMddHHmm'
          )}.json`,
          format: 'json'
        });
      });
  }

  protected onMarketDataChanged(withRefresh = false) {
    if (withRefresh) {
      this.fetchMarketData();
    }
  }

  private fetchActivities(filters: Filter[] = this.getActivityFilters()) {
    this.dataService
      .fetchActivities({
        filters,
        skip: this.pageIndex * this.pageSize,
        sortColumn: this.sortColumn,
        sortDirection: this.sortDirection,
        take: this.pageSize
      })
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(({ activities }) => {
        this.dataSource = new MatTableDataSource(activities);

        this.changeDetectorRef.markForCheck();
      });
  }

  private fetchMarketData() {
    this.dataService
      .fetchMarketDataBySymbol({
        dataSource: this.data.dataSource,
        symbol: this.data.symbol
      })
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe(({ marketData }) => {
        this.marketDataItems = marketData;

        this.historicalDataItems = this.marketDataItems.map(
          ({ date, marketPrice }) => {
            return {
              date: format(date, DATE_FORMAT),
              value: marketPrice
            };
          }
        );

        this.changeDetectorRef.markForCheck();
      });
  }

  private getActivityFilters(): Filter[] {
    return [
      { id: this.data.dataSource, type: 'DATA_SOURCE' },
      { id: this.data.symbol, type: 'SYMBOL' }
    ];
  }

  private navigateToActivities() {
    this.router.navigate(
      internalRoutes.portfolio.subRoutes.activities.routerLink
    );

    this.dialogRef.close();
  }
}