Skip to content

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

5.2 KB
Raw Blame
import { LogPerformance } from '@ghostfolio/api/interceptors/performance-logging/performance-logging.interceptor';
import { BenchmarkService } from '@ghostfolio/api/services/benchmark/benchmark.service';
import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service';
import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service';
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
import { DataGatheringService } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.service';
import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service';
import { getAssetProfileIdentifier } from '@ghostfolio/common/helper';
import {
  AssetProfileIdentifier,
  WatchlistResponse
} from '@ghostfolio/common/interfaces';

import { BadRequestException, Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';

@Injectable()
export class WatchlistService {
  public constructor(
    private readonly benchmarkService: BenchmarkService,
    private readonly dataGatheringService: DataGatheringService,
    private readonly dataProviderService: DataProviderService,
    private readonly marketDataService: MarketDataService,
    private readonly prismaService: PrismaService,
    private readonly symbolProfileService: SymbolProfileService
  ) {}

  public async createWatchlistItem({
    dataSource,
    symbol,
    userId
  }: { userId: string } & AssetProfileIdentifier): Promise<void> {
    const symbolProfile = await this.prismaService.symbolProfile.findUnique({
      where: {
        dataSource_symbol: { dataSource, symbol }
      }
    });

    if (!symbolProfile) {
      const assetProfiles = await this.dataProviderService.getAssetProfiles([
        { dataSource, symbol }
      ]);

      const assetProfile =
        assetProfiles[getAssetProfileIdentifier({ dataSource, symbol })];

      if (!assetProfile?.currency) {
        throw new BadRequestException(
          `Asset profile not found for ${symbol} (${dataSource})`
        );
      }

      await this.symbolProfileService.add(
        assetProfile as Prisma.SymbolProfileCreateInput
      );
    }

    await this.dataGatheringService.gatherSymbol({
      dataSource,
      symbol
    });

    await this.prismaService.user.update({
      data: {
        watchlist: {
          connect: {
            dataSource_symbol: { dataSource, symbol }
          }
        }
      },
      where: { id: userId }
    });
  }

  public async deleteWatchlistItem({
    dataSource,
    symbol,
    userId
  }: { userId: string } & AssetProfileIdentifier) {
    await this.prismaService.user.update({
      data: {
        watchlist: {
          disconnect: {
            dataSource_symbol: { dataSource, symbol }
          }
        }
      },
      where: { id: userId }
    });
  }

  @LogPerformance
  public async getWatchlistItems(
    userId: string
  ): Promise<WatchlistResponse['watchlist']> {
    const user = await this.prismaService.user.findUnique({
      select: {
        watchlist: {
          select: { dataSource: true, symbol: true }
        }
      },
      where: { id: userId }
    });

    const [assetProfiles, quotes] = await Promise.all([
      this.symbolProfileService.getSymbolProfiles(user.watchlist),
      this.dataProviderService.getQuotes({
        items: user.watchlist.map(({ dataSource, symbol }) => {
          return { dataSource, symbol };
        })
      })
    ]);

    const watchlist = await Promise.all(
      user.watchlist.map(async ({ dataSource, symbol }) => {
        const assetProfile = assetProfiles.find((profile) => {
          return profile.dataSource === dataSource && profile.symbol === symbol;
        });

        const [allTimeHigh, trends] = await Promise.all([
          this.marketDataService.getMax({
            dataSource,
            symbol
          }),
          this.benchmarkService.getBenchmarkTrends({ dataSource, symbol })
        ]);

        const performancePercent =
          this.benchmarkService.calculateChangeInPercentage(
            allTimeHigh?.marketPrice,
            quotes[getAssetProfileIdentifier({ dataSource, symbol })]
              ?.marketPrice
          );

        return {
          dataSource,
          symbol,
          marketCondition:
            this.benchmarkService.getMarketCondition(performancePercent),
          name: assetProfile?.name,
          performances: {
            allTimeHigh: {
              date: allTimeHigh?.date,
              performancePercent:
                this.benchmarkService.getPerformancePercentFromAllTimeHigh(
                  performancePercent
                )
            }
          },
          trend50d: trends.trend50d,
          trend200d: trends.trend200d
        };
      })
    );

    return watchlist.sort((a, b) => {
      return (a.name ?? '').localeCompare(b.name ?? '');
    });
  }

  public async hasWatchlistItem({
    dataSource,
    symbol,
    userId
  }: { userId: string } & AssetProfileIdentifier): Promise<boolean> {
    const assetProfile = await this.prismaService.symbolProfile.findFirst({
      where: {
        dataSource,
        symbol,
        watchedBy: { some: { id: userId } }
      }
    });

    return !!assetProfile;
  }
}