Skip to content

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

29.5 KB
Raw Blame
import { getMaskedGhostfolioDataSource } from '@ghostfolio/api/helper/data-source.helper';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { DataProviderInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface';
import { ImportValidationError } from '@ghostfolio/api/services/import/errors/import-validation.error';
import { ImportDataDto } from '@ghostfolio/api/services/import/import-data.dto';
import { MarketDataService } from '@ghostfolio/api/services/market-data/market-data.service';
import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';
import { PropertyService } from '@ghostfolio/api/services/property/property.service';
import { RedisCacheService } from '@ghostfolio/api/services/redis-cache/redis-cache.service';
import {
  DEFAULT_CURRENCY,
  DERIVED_CURRENCIES,
  NON_INVESTMENT_ACTIVITY_TYPES,
  PROPERTY_API_KEY_GHOSTFOLIO,
  PROPERTY_DATA_SOURCE_MAPPING
} from '@ghostfolio/common/config';
import { CreateOrderDto } from '@ghostfolio/common/dtos';
import { SubscriptionType } from '@ghostfolio/common/enums';
import {
  DATE_FORMAT,
  formatAssetProfileName,
  getAssetProfileIdentifier,
  getCurrencyFromSymbol,
  getStartOfUtcDate,
  isCurrency,
  isDerivedCurrency,
  isValidCustomAssetProfileSymbol,
  isValidSearchQuery
} from '@ghostfolio/common/helper';
import {
  AssetProfileIdentifier,
  DataProviderHistoricalResponse,
  DataProviderResponse,
  LookupItem,
  LookupResponse,
  MarketDataOfMarketsResponse
} from '@ghostfolio/common/interfaces';
import type { Granularity, UserWithSettings } from '@ghostfolio/common/types';

import { utc } from '@date-fns/utc';
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { DataSource, MarketData, Prisma, SymbolProfile } from '@prisma/client';
import { Big } from 'big.js';
import { eachDayOfInterval, format, isValid } from 'date-fns';
import { groupBy, isEmpty, isNumber, omit, uniqWith } from 'lodash-es';
import ms from 'ms';

import { AssetProfileInvalidError } from './errors/asset-profile-invalid.error';

@Injectable()
export class DataProviderService implements OnModuleInit {
  private readonly logger = new Logger(DataProviderService.name);

  private dataProviderMapping: { [dataProviderName: string]: string } = {};

  public constructor(
    private readonly configurationService: ConfigurationService,
    @Inject('DataProviderInterfaces')
    private readonly dataProviderInterfaces: DataProviderInterface[],
    private readonly marketDataService: MarketDataService,
    private readonly prismaService: PrismaService,
    private readonly propertyService: PropertyService,
    private readonly redisCacheService: RedisCacheService
  ) {}

  public async onModuleInit() {
    this.dataProviderMapping =
      (await this.propertyService.getByKey<{
        [dataProviderName: string]: string;
      }>(PROPERTY_DATA_SOURCE_MAPPING)) ?? {};
  }

  public async checkQuote(dataSource: DataSource) {
    const dataProvider = this.getDataProvider(dataSource);
    const symbol = dataProvider.getTestSymbol();

    const quotes = await this.getQuotes({
      items: [
        {
          dataSource,
          symbol
        }
      ],
      requestTimeout: ms('30 seconds'),
      useCache: false
    });

    if (
      quotes[getAssetProfileIdentifier({ dataSource, symbol })]?.marketPrice > 0
    ) {
      return true;
    }

    return false;
  }

  public async getAssetProfiles(items: AssetProfileIdentifier[]): Promise<{
    [assetProfileIdentifier: string]: Partial<SymbolProfile>;
  }> {
    const response: {
      [assetProfileIdentifier: string]: Partial<SymbolProfile>;
    } = {};

    const itemsGroupedByDataSource = groupBy(items, ({ dataSource }) => {
      return dataSource;
    });

    const promises: Promise<void>[] = [];

    for (const [dataSource, assetProfileIdentifiers] of Object.entries(
      itemsGroupedByDataSource
    )) {
      const symbols = assetProfileIdentifiers.map(({ symbol }) => {
        return symbol;
      });

      for (const symbol of symbols) {
        const promise = Promise.resolve(
          this.getDataProvider(DataSource[dataSource]).getAssetProfile({
            symbol
          })
        );

        promises.push(
          promise.then((assetProfile) => {
            if (isCurrency(assetProfile?.currency)) {
              response[
                getAssetProfileIdentifier({
                  symbol,
                  dataSource: DataSource[dataSource]
                })
              ] = {
                ...assetProfile,
                symbol,
                name: formatAssetProfileName(assetProfile)
              };
            }
          })
        );
      }
    }

    try {
      await Promise.all(promises);

      if (isEmpty(response)) {
        throw new AssetProfileInvalidError(
          'No valid asset profiles have been found'
        );
      }
    } catch (error) {
      this.logger.error(error.message);

      throw error;
    }

    return response;
  }

  /**
   * Gives the data provider of the data source, or the data provider which
   * the data source mapping gives for this data source
   */
  public getDataProvider(providerName: DataSource) {
    const mappedDataProviderInterface = this.dataProviderInterfaces.find(
      (dataProviderInterface) => {
        return (
          dataProviderInterface.getName() ===
          this.dataProviderMapping[providerName]
        );
      }
    );

    if (mappedDataProviderInterface) {
      return mappedDataProviderInterface;
    }

    const dataProviderInterface = this.dataProviderInterfaces.find(
      (currentDataProviderInterface) => {
        return currentDataProviderInterface.getName() === providerName;
      }
    );

    if (dataProviderInterface) {
      return dataProviderInterface;
    }

    throw new Error('No data provider has been found.');
  }

  public getDataSourceForExchangeRates(): DataSource {
    return DataSource[
      this.configurationService.get('DATA_SOURCE_EXCHANGE_RATES')
    ];
  }

  public getDataSourceForFearAndGreedIndexStocks(): DataSource {
    return DataSource[
      this.configurationService.get('DATA_SOURCE_FEAR_AND_GREED_INDEX_STOCKS')
    ];
  }

  public getDataSourceForImport(): DataSource {
    return DataSource[this.configurationService.get('DATA_SOURCE_IMPORT')];
  }

  public async getDataSources(): Promise<DataSource[]> {
    const dataSources: DataSource[] = this.configurationService
      .get('DATA_SOURCES')
      .map((dataSource) => {
        return DataSource[dataSource];
      });

    if (await this.isDataProviderGhostfolioConfigured()) {
      dataSources.push('GHOSTFOLIO');
    }

    return dataSources.sort();
  }

  public async validateActivities({
    activitiesDto,
    assetProfilesWithMarketDataDto,
    maxActivitiesToImport,
    subscription,
    userId
  }: {
    activitiesDto: Pick<
      Partial<CreateOrderDto>,
      'currency' | 'dataSource' | 'symbol' | 'type'
    >[];
    assetProfilesWithMarketDataDto?: ImportDataDto['assetProfiles'];
    maxActivitiesToImport: number;
    subscription: UserWithSettings['subscription'];
    userId: string;
  }) {
    if (activitiesDto?.length > maxActivitiesToImport) {
      throw new ImportValidationError(
        `Too many activities (${maxActivitiesToImport} at most)`
      );
    }

    const assetProfiles: {
      [assetProfileIdentifier: string]: Partial<SymbolProfile>;
    } = {};

    const dataSources = await this.getDataSources();
    const ghostfolioDataSources = this.configurationService.get(
      'DATA_SOURCES_GHOSTFOLIO_DATA_PROVIDER'
    );

    for (const [
      index,
      { currency, dataSource, symbol, type }
    ] of activitiesDto.entries()) {
      const activityPath =
        maxActivitiesToImport === 1 ? 'activity' : `activities.${index}`;
      const maskedDataSource = getMaskedGhostfolioDataSource({
        dataSource,
        ghostfolioDataSources
      });

      if (!dataSources.includes(dataSource)) {
        throw new ImportValidationError(
          `${activityPath}.dataSource ("${dataSource}") is not valid`
        );
      }

      if (
        dataSource !== DataSource.MANUAL &&
        isValidCustomAssetProfileSymbol(symbol)
      ) {
        throw new ImportValidationError(
          `${activityPath}.symbol ("${symbol}") is not valid for the data source ("${maskedDataSource}")`
        );
      }

      const assetProfileIdentifier = getAssetProfileIdentifier({
        dataSource,
        symbol
      });

      if (this.isPremiumRequired({ dataSource, subscription })) {
        // The user can add activities to an asset profile of a premium data
        // provider which the user already has, for example after the Premium
        // subscription has expired. The stored asset profile is taken, so
        // that the validation sends no request to the data provider.
        const assetProfileOfUser =
          assetProfiles[assetProfileIdentifier] ??
          (await this.getAssetProfileOfUser({ dataSource, symbol, userId }));

        if (!assetProfileOfUser) {
          throw new ImportValidationError(
            `${activityPath}.dataSource ("${maskedDataSource}") requires Ghostfolio Premium`
          );
        }

        assetProfiles[assetProfileIdentifier] = assetProfileOfUser;

        continue;
      }

      if (!assetProfiles[assetProfileIdentifier]) {
        const assetProfileInImport = assetProfilesWithMarketDataDto?.find(
          (assetProfileWithMarketData) => {
            return (
              assetProfileWithMarketData.dataSource === dataSource &&
              assetProfileWithMarketData.symbol === symbol
            );
          }
        );

        // A custom asset profile of the import is created after the
        // validation, thus the data provider cannot resolve it yet
        if (
          (dataSource === DataSource.MANUAL && type === 'BUY') ||
          assetProfileInImport?.dataSource === DataSource.MANUAL ||
          NON_INVESTMENT_ACTIVITY_TYPES.includes(type)
        ) {
          assetProfiles[assetProfileIdentifier] = {
            ...omit(assetProfileInImport ?? {}, [
              'dataSource',
              'marketData',
              'symbol'
            ]),
            dataSource,
            symbol,
            currency: assetProfileInImport?.currency ?? currency,
            name: assetProfileInImport?.name ?? symbol
          };

          continue;
        }

        let assetProfile: Partial<SymbolProfile> = { currency };

        try {
          assetProfile = (
            await this.getAssetProfiles([
              {
                dataSource,
                symbol
              }
            ])
          )?.[assetProfileIdentifier];
        } catch {}

        if (!assetProfile?.name) {
          throw new ImportValidationError(
            `${activityPath}.symbol ("${symbol}") cannot be resolved by the data source ("${maskedDataSource}")`
          );
        }

        assetProfiles[assetProfileIdentifier] = assetProfile;
      }
    }

    return assetProfiles;
  }

  public async getDividends({
    dataSource,
    from,
    granularity = 'day',
    symbol,
    to
  }: {
    from: Date;
    granularity: Granularity;
    to: Date;
  } & AssetProfileIdentifier): Promise<{
    [date: string]: DataProviderHistoricalResponse;
  }> {
    if (!isValid(from) || !isValid(to)) {
      return {};
    }

    return this.getDataProvider(DataSource[dataSource]).getDividends({
      from,
      granularity,
      symbol,
      to,
      requestTimeout: ms('30 seconds')
    });
  }

  public async getHistorical({
    assetProfileIdentifiers,
    from,
    to
  }: {
    assetProfileIdentifiers: AssetProfileIdentifier[];
    from: Date;
    to: Date;
  }): Promise<{
    [assetProfileIdentifier: string]: {
      [date: string]: DataProviderHistoricalResponse;
    };
  }> {
    let response: {
      [assetProfileIdentifier: string]: {
        [date: string]: DataProviderHistoricalResponse;
      };
    } = {};

    if (isEmpty(assetProfileIdentifiers) || !isValid(from) || !isValid(to)) {
      return response;
    }

    const assetProfileIdentifierTuples = assetProfileIdentifiers.map(
      ({ dataSource, symbol }) => {
        return Prisma.sql`(${dataSource}::"DataSource", ${symbol})`;
      }
    );

    try {
      const marketDataItems: Pick<
        MarketData,
        'dataSource' | 'date' | 'marketPrice' | 'symbol'
      >[] = await this.prismaService.$queryRaw`
          SELECT "dataSource", "date", "marketPrice", "symbol"
          FROM "MarketData"
          WHERE ("dataSource", "symbol") IN (${Prisma.join(assetProfileIdentifierTuples)})
            AND date >= ${format(from, DATE_FORMAT, { in: utc })}::timestamp
            AND date <= ${format(to, DATE_FORMAT, { in: utc })}::timestamp
          ORDER BY date;`;

      response = marketDataItems.reduce((r, marketData) => {
        const { dataSource, date, marketPrice, symbol } = marketData;

        const assetProfileIdentifier = getAssetProfileIdentifier({
          dataSource,
          symbol
        });

        if (!r[assetProfileIdentifier]) {
          r[assetProfileIdentifier] = {};
        }

        r[assetProfileIdentifier][
          format(new Date(date), DATE_FORMAT, { in: utc })
        ] = {
          marketPrice
        };

        return r;
      }, {});
    } catch (error) {
      this.logger.error(error.message);
    } finally {
      return response;
    }
  }

  public async getHistoricalRaw({
    assetProfileIdentifiers,
    from,
    to
  }: {
    assetProfileIdentifiers: AssetProfileIdentifier[];
    from: Date;
    to: Date;
  }): Promise<{
    [assetProfileIdentifier: string]: {
      [date: string]: DataProviderHistoricalResponse;
    };
  }> {
    for (const { currency, rootCurrency } of DERIVED_CURRENCIES) {
      if (
        this.hasCurrency({
          assetProfileIdentifiers,
          currency: `${DEFAULT_CURRENCY}${currency}`
        })
      ) {
        // Skip derived currency
        assetProfileIdentifiers = assetProfileIdentifiers.filter(
          ({ symbol }) => {
            return symbol !== `${DEFAULT_CURRENCY}${currency}`;
          }
        );
        // Add root currency
        assetProfileIdentifiers.push({
          dataSource: this.getDataSourceForExchangeRates(),
          symbol: `${DEFAULT_CURRENCY}${rootCurrency}`
        });
      }
    }

    assetProfileIdentifiers = uniqWith(
      assetProfileIdentifiers,
      (obj1, obj2) => {
        return (
          obj1.dataSource === obj2.dataSource && obj1.symbol === obj2.symbol
        );
      }
    );

    const result: {
      [assetProfileIdentifier: string]: {
        [date: string]: DataProviderHistoricalResponse;
      };
    } = {};

    const promises: Promise<{
      data: { [date: string]: DataProviderHistoricalResponse };
      dataSource: DataSource;
      symbol: string;
    }>[] = [];
    for (const { dataSource, symbol } of assetProfileIdentifiers) {
      const dataProvider = this.getDataProvider(dataSource);
      if (dataProvider.canHandle(symbol)) {
        if (symbol === `${DEFAULT_CURRENCY}USX`) {
          const data: {
            [date: string]: DataProviderHistoricalResponse;
          } = {};

          for (const date of eachDayOfInterval(
            { end: to, start: from },
            { in: utc }
          )) {
            data[format(date, DATE_FORMAT, { in: utc })] = { marketPrice: 100 };
          }

          promises.push(
            Promise.resolve({
              data,
              dataSource,
              symbol
            })
          );
        } else {
          promises.push(
            dataProvider
              .getHistorical({
                from,
                symbol,
                to,
                requestTimeout: ms('30 seconds')
              })
              .then((data) => {
                return { data, dataSource, symbol };
              })
          );
        }
      }
    }

    try {
      const allData = await Promise.all(promises);

      for (const { data, dataSource, symbol } of allData) {
        const currency = DERIVED_CURRENCIES.find(({ rootCurrency }) => {
          return `${DEFAULT_CURRENCY}${rootCurrency}` === symbol;
        });

        if (currency) {
          // Add derived currency
          result[
            getAssetProfileIdentifier({
              dataSource,
              symbol: `${DEFAULT_CURRENCY}${currency.currency}`
            })
          ] = this.transformHistoricalData({
            allData,
            currency: `${DEFAULT_CURRENCY}${currency.rootCurrency}`,
            factor: currency.factor
          });
        }

        result[getAssetProfileIdentifier({ dataSource, symbol })] = data;
      }
    } catch (error) {
      this.logger.error(error.message);

      throw error;
    }

    return result;
  }

  public async getMarketDataOfMarkets({
    includeHistoricalData
  }: {
    includeHistoricalData: number;
  }): Promise<MarketDataOfMarketsResponse> {
    const dataProvider = this.getDataProvider(DataSource.GHOSTFOLIO);

    if (!dataProvider.getMarketDataOfMarkets) {
      throw new Error(
        `The data provider (${DataSource.GHOSTFOLIO}) does not support the market data of markets`
      );
    }

    return dataProvider.getMarketDataOfMarkets({ includeHistoricalData });
  }

  public async getQuotes({
    items,
    requestTimeout,
    subscriptionType,
    useCache = true
  }: {
    items: AssetProfileIdentifier[];
    requestTimeout?: number;
    subscriptionType?: SubscriptionType;
    useCache?: boolean;
  }): Promise<{
    [assetProfileIdentifier: string]: DataProviderResponse;
  }> {
    const response: {
      [assetProfileIdentifier: string]: DataProviderResponse & {
        symbol: string;
      };
    } = {};
    const startTimeTotal = performance.now();

    if (
      items.some(({ symbol }) => {
        return symbol === `${DEFAULT_CURRENCY}USX`;
      })
    ) {
      response[
        getAssetProfileIdentifier({
          dataSource: this.getDataSourceForExchangeRates(),
          symbol: `${DEFAULT_CURRENCY}USX`
        })
      ] = {
        currency: 'USX',
        dataSource: this.getDataSourceForExchangeRates(),
        marketPrice: 100,
        marketState: 'open',
        symbol: `${DEFAULT_CURRENCY}USX`
      };
    }

    // Get items from cache
    const itemsToFetch: AssetProfileIdentifier[] = [];

    for (const { dataSource, symbol } of items) {
      if (useCache) {
        const quoteString = await this.redisCacheService.get(
          this.redisCacheService.getQuoteKey({ dataSource, symbol })
        );

        if (quoteString) {
          try {
            const cachedDataProviderResponse = JSON.parse(
              quoteString
            ) as DataProviderResponse;
            response[getAssetProfileIdentifier({ dataSource, symbol })] = {
              ...cachedDataProviderResponse,
              symbol
            };
            continue;
          } catch {}
        }
      }

      itemsToFetch.push({ dataSource, symbol });
    }

    const numberOfItemsInCache = Object.keys(response)?.length;

    if (numberOfItemsInCache) {
      this.logger.debug(
        `Fetched ${numberOfItemsInCache} quote${
          numberOfItemsInCache > 1 ? 's' : ''
        } from cache in ${((performance.now() - startTimeTotal) / 1000).toFixed(
          3
        )} seconds`
      );
    }

    const itemsGroupedByDataSource = groupBy(itemsToFetch, ({ dataSource }) => {
      return dataSource;
    });

    const promises: Promise<any>[] = [];

    for (const [dataSource, assetProfileIdentifiers] of Object.entries(
      itemsGroupedByDataSource
    )) {
      const dataProvider = this.getDataProvider(DataSource[dataSource]);

      const symbols = assetProfileIdentifiers
        .filter(({ symbol }) => {
          if (isCurrency(getCurrencyFromSymbol(symbol))) {
            // Keep non-derived currencies
            return !isDerivedCurrency(getCurrencyFromSymbol(symbol));
          } else if (
            dataProvider.getDataProviderInfo().isPremium &&
            this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION') &&
            subscriptionType === SubscriptionType.Basic
          ) {
            // Skip symbols of Premium data providers for users without subscription
            return false;
          }

          return true;
        })
        .map(({ symbol }) => {
          return symbol;
        });

      const maximumNumberOfSymbolsPerRequest =
        dataProvider.getMaxNumberOfSymbolsPerRequest?.() ??
        Number.MAX_SAFE_INTEGER;

      for (
        let i = 0;
        i < symbols.length;
        i += maximumNumberOfSymbolsPerRequest
      ) {
        const startTimeDataSource = performance.now();

        const symbolsChunk = symbols.slice(
          i,
          i + maximumNumberOfSymbolsPerRequest
        );

        const promise = Promise.resolve(
          dataProvider.getQuotes({
            requestTimeout,
            useCache,
            symbols: symbolsChunk
          })
        );

        promises.push(
          promise.then(async (result) => {
            const fetchedQuotes: (DataProviderResponse & {
              symbol: string;
            })[] = [];

            for (const [symbol, dataProviderResponse] of Object.entries(
              result
            )) {
              if (
                [
                  ...DERIVED_CURRENCIES.map(({ currency }) => {
                    return `${DEFAULT_CURRENCY}${currency}`;
                  }),
                  `${DEFAULT_CURRENCY}USX`
                ].includes(symbol)
              ) {
                continue;
              }

              const quote = { ...dataProviderResponse, symbol };

              fetchedQuotes.push(quote);

              response[
                getAssetProfileIdentifier({
                  symbol,
                  dataSource: DataSource[dataSource]
                })
              ] = quote;

              this.redisCacheService.set(
                this.redisCacheService.getQuoteKey({
                  symbol,
                  dataSource: DataSource[dataSource]
                }),
                JSON.stringify(dataProviderResponse),
                this.configurationService.get('CACHE_QUOTES_TTL')
              );

              for (const {
                currency,
                factor,
                rootCurrency
              } of DERIVED_CURRENCIES) {
                if (symbol === `${DEFAULT_CURRENCY}${rootCurrency}`) {
                  const derivedDataProviderResponse: DataProviderResponse = {
                    ...dataProviderResponse,
                    currency,
                    marketPrice: new Big(
                      result[`${DEFAULT_CURRENCY}${rootCurrency}`].marketPrice
                    )
                      .mul(factor)
                      .toNumber(),
                    marketState: 'open'
                  };

                  const derivedQuote = {
                    ...derivedDataProviderResponse,
                    symbol: `${DEFAULT_CURRENCY}${currency}`
                  };

                  fetchedQuotes.push(derivedQuote);

                  response[
                    getAssetProfileIdentifier({
                      dataSource: DataSource[dataSource],
                      symbol: `${DEFAULT_CURRENCY}${currency}`
                    })
                  ] = derivedQuote;

                  this.redisCacheService.set(
                    this.redisCacheService.getQuoteKey({
                      dataSource: DataSource[dataSource],
                      symbol: `${DEFAULT_CURRENCY}${currency}`
                    }),
                    JSON.stringify(derivedDataProviderResponse),
                    this.configurationService.get('CACHE_QUOTES_TTL')
                  );
                }
              }
            }

            this.logger.debug(
              `Fetched ${symbolsChunk.length} quote${
                symbolsChunk.length > 1 ? 's' : ''
              } from ${dataSource} in ${(
                (performance.now() - startTimeDataSource) /
                1000
              ).toFixed(3)} seconds`
            );

            try {
              await this.marketDataService.updateMany({
                data: fetchedQuotes
                  .filter(({ marketPrice, marketState }) => {
                    return (
                      isNumber(marketPrice) &&
                      marketPrice > 0 &&
                      marketState === 'open'
                    );
                  })
                  .map((dataProviderResponse) => {
                    return {
                      dataSource: dataProviderResponse.dataSource,
                      date: getStartOfUtcDate(new Date()),
                      marketPrice: dataProviderResponse.marketPrice,
                      state: 'INTRADAY',
                      symbol: dataProviderResponse.symbol
                    };
                  })
              });
            } catch {}
          })
        );
      }
    }

    await Promise.all(promises);

    this.logger.debug(
      '--------------------------------------------------------'
    );
    this.logger.debug(
      `Fetched ${items.length} quote${items.length > 1 ? 's' : ''} in ${(
        (performance.now() - startTimeTotal) /
        1000
      ).toFixed(3)} seconds`
    );
    this.logger.debug(
      '========================================================'
    );

    return response;
  }

  public async isDataProviderGhostfolioConfigured(): Promise<boolean> {
    return !!(await this.propertyService.getByKey<string>(
      PROPERTY_API_KEY_GHOSTFOLIO
    ));
  }

  /**
   * Checks if a data source requires Ghostfolio Premium for a subscription,
   * which is the case for a premium data provider and the Basic subscription
   * if the subscription is enabled
   */
  public isPremiumRequired({
    dataSource,
    subscription
  }: {
    dataSource: DataSource;
    subscription: UserWithSettings['subscription'];
  }) {
    return (
      this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION') &&
      subscription?.type === SubscriptionType.Basic &&
      this.getDataProvider(DataSource[dataSource]).getDataProviderInfo()
        .isPremium
    );
  }

  public async search({
    includeIndices = false,
    query,
    user
  }: {
    includeIndices?: boolean;
    query: string;
    user: UserWithSettings;
  }): Promise<LookupResponse> {
    let lookupItems: LookupItem[] = [];
    const promises: Promise<LookupResponse>[] = [];

    query = query?.trim();

    if (!isValidSearchQuery(query)) {
      return { items: lookupItems };
    }

    const dataSources = await this.getDataSources();

    const dataProviderServices = dataSources.map((dataSource) => {
      return this.getDataProvider(DataSource[dataSource]);
    });

    for (const dataProviderService of dataProviderServices) {
      promises.push(
        dataProviderService.search({
          includeIndices,
          query,
          userId: user.id
        })
      );
    }

    const searchResults = await Promise.all(promises);

    for (const { items } of searchResults) {
      if (items?.length > 0) {
        lookupItems = lookupItems.concat(items);
      }
    }

    const filteredItems = lookupItems
      .filter(({ currency }) => {
        if (includeIndices) {
          return true;
        }

        return currency ? isCurrency(currency) : false;
      })
      .map((lookupItem) => {
        if (this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) {
          if (user.subscription?.type === SubscriptionType.Premium) {
            lookupItem.dataProviderInfo.isPremium = false;
          }

          lookupItem.dataProviderInfo.dataSource = undefined;
          lookupItem.dataProviderInfo.name = undefined;
          lookupItem.dataProviderInfo.url = undefined;
        } else {
          lookupItem.dataProviderInfo.isPremium = false;
        }

        lookupItem.name = formatAssetProfileName(lookupItem);

        return lookupItem;
      })
      .sort(({ name: name1 }, { name: name2 }) => {
        return name1?.toLowerCase().localeCompare(name2?.toLowerCase());
      });

    return {
      items: filteredItems
    };
  }

  /**
   * Gives the stored asset profile if the user has an activity with it, with
   * the fields which a data provider gives
   */
  private async getAssetProfileOfUser({
    dataSource,
    symbol,
    userId
  }: AssetProfileIdentifier & { userId: string }): Promise<
    Partial<SymbolProfile>
  > {
    return this.prismaService.symbolProfile.findFirst({
      select: {
        assetClass: true,
        assetSubClass: true,
        currency: true,
        dataSource: true,
        isin: true,
        name: true,
        symbol: true
      },
      where: {
        dataSource,
        symbol,
        activities: { some: { userId } }
      }
    });
  }

  private hasCurrency({
    assetProfileIdentifiers,
    currency
  }: {
    assetProfileIdentifiers: AssetProfileIdentifier[];
    currency: string;
  }) {
    return assetProfileIdentifiers.some(({ dataSource, symbol }) => {
      return (
        dataSource === this.getDataSourceForExchangeRates() &&
        symbol === currency
      );
    });
  }

  private transformHistoricalData({
    allData,
    currency,
    factor
  }: {
    allData: {
      data: {
        [date: string]: DataProviderHistoricalResponse;
      };
      symbol: string;
    }[];
    currency: string;
    factor: number;
  }) {
    const rootData = allData.find(({ symbol }) => {
      return symbol === currency;
    })?.data;

    const data: {
      [date: string]: DataProviderHistoricalResponse;
    } = {};

    for (const date in rootData) {
      if (isNumber(rootData[date].marketPrice)) {
        data[date] = {
          marketPrice: new Big(factor)
            .mul(rootData[date].marketPrice)
            .toNumber()
        };
      }
    }

    return data;
  }
}