From 67ae86763e137ec1ca131066b1b05ee7540e4467 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sun, 18 Feb 2024 14:37:42 +0100 Subject: [PATCH] Handle premium data provider in getQuotes() (#3020) * Handle premium data provider in getQuotes() --- .../src/app/portfolio/current-rate.service.spec.ts | 3 ++- apps/api/src/app/portfolio/current-rate.service.ts | 10 +++++++--- .../portfolio-calculator-baln-buy-and-sell.spec.ts | 2 +- .../portfolio/portfolio-calculator-baln-buy.spec.ts | 2 +- ...-calculator-btcusd-buy-and-sell-partially.spec.ts | 2 +- .../portfolio/portfolio-calculator-googl-buy.spec.ts | 2 +- .../portfolio/portfolio-calculator-no-orders.spec.ts | 2 +- ...io-calculator-novn-buy-and-sell-partially.spec.ts | 2 +- .../portfolio-calculator-novn-buy-and-sell.spec.ts | 2 +- .../src/app/portfolio/portfolio-calculator.spec.ts | 2 +- apps/api/src/app/portfolio/portfolio.service.ts | 6 ++++-- .../services/data-provider/data-provider.service.ts | 12 +++++++++++- 12 files changed, 32 insertions(+), 15 deletions(-) diff --git a/apps/api/src/app/portfolio/current-rate.service.spec.ts b/apps/api/src/app/portfolio/current-rate.service.spec.ts index 2fc931071..d87ee7ccc 100644 --- a/apps/api/src/app/portfolio/current-rate.service.spec.ts +++ b/apps/api/src/app/portfolio/current-rate.service.spec.ts @@ -107,7 +107,8 @@ describe('CurrentRateService', () => { currentRateService = new CurrentRateService( dataProviderService, - marketDataService + marketDataService, + null ); }); diff --git a/apps/api/src/app/portfolio/current-rate.service.ts b/apps/api/src/app/portfolio/current-rate.service.ts index ecc00eb9d..f32bc18c5 100644 --- a/apps/api/src/app/portfolio/current-rate.service.ts +++ b/apps/api/src/app/portfolio/current-rate.service.ts @@ -6,8 +6,10 @@ import { ResponseError, UniqueAsset } from '@ghostfolio/common/interfaces'; +import { RequestWithUser } from '@ghostfolio/common/types'; -import { Injectable } from '@nestjs/common'; +import { Inject, Injectable } from '@nestjs/common'; +import { REQUEST } from '@nestjs/core'; import { isBefore, isToday } from 'date-fns'; import { flatten, isEmpty, uniqBy } from 'lodash'; @@ -19,9 +21,11 @@ import { GetValuesParams } from './interfaces/get-values-params.interface'; export class CurrentRateService { public constructor( private readonly dataProviderService: DataProviderService, - private readonly marketDataService: MarketDataService + private readonly marketDataService: MarketDataService, + @Inject(REQUEST) private readonly request: RequestWithUser ) {} + // TODO: Pass user instead of using this.request.user public async getValues({ dataGatheringItems, dateQuery @@ -40,7 +44,7 @@ export class CurrentRateService { if (includeToday) { promises.push( this.dataProviderService - .getQuotes({ items: dataGatheringItems }) + .getQuotes({ items: dataGatheringItems, user: this.request?.user }) .then((dataResultProvider) => { const result: GetValueObject[] = []; diff --git a/apps/api/src/app/portfolio/portfolio-calculator-baln-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/portfolio-calculator-baln-buy-and-sell.spec.ts index 07b53b4de..60956a9d6 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-baln-buy-and-sell.spec.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-baln-buy-and-sell.spec.ts @@ -21,7 +21,7 @@ describe('PortfolioCalculator', () => { let exchangeRateDataService: ExchangeRateDataService; beforeEach(() => { - currentRateService = new CurrentRateService(null, null); + currentRateService = new CurrentRateService(null, null, null); exchangeRateDataService = new ExchangeRateDataService( null, diff --git a/apps/api/src/app/portfolio/portfolio-calculator-baln-buy.spec.ts b/apps/api/src/app/portfolio/portfolio-calculator-baln-buy.spec.ts index c60f99990..ecbe2851c 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-baln-buy.spec.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-baln-buy.spec.ts @@ -21,7 +21,7 @@ describe('PortfolioCalculator', () => { let exchangeRateDataService: ExchangeRateDataService; beforeEach(() => { - currentRateService = new CurrentRateService(null, null); + currentRateService = new CurrentRateService(null, null, null); exchangeRateDataService = new ExchangeRateDataService( null, diff --git a/apps/api/src/app/portfolio/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts b/apps/api/src/app/portfolio/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts index b6ee34622..c1e3a71c7 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts @@ -34,7 +34,7 @@ describe('PortfolioCalculator', () => { let exchangeRateDataService: ExchangeRateDataService; beforeEach(() => { - currentRateService = new CurrentRateService(null, null); + currentRateService = new CurrentRateService(null, null, null); exchangeRateDataService = new ExchangeRateDataService( null, diff --git a/apps/api/src/app/portfolio/portfolio-calculator-googl-buy.spec.ts b/apps/api/src/app/portfolio/portfolio-calculator-googl-buy.spec.ts index 9a12bdc83..2a6eeaedb 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-googl-buy.spec.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-googl-buy.spec.ts @@ -34,7 +34,7 @@ describe('PortfolioCalculator', () => { let exchangeRateDataService: ExchangeRateDataService; beforeEach(() => { - currentRateService = new CurrentRateService(null, null); + currentRateService = new CurrentRateService(null, null, null); exchangeRateDataService = new ExchangeRateDataService( null, diff --git a/apps/api/src/app/portfolio/portfolio-calculator-no-orders.spec.ts b/apps/api/src/app/portfolio/portfolio-calculator-no-orders.spec.ts index 4b6d243f3..bc860b2aa 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-no-orders.spec.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-no-orders.spec.ts @@ -21,7 +21,7 @@ describe('PortfolioCalculator', () => { let exchangeRateDataService: ExchangeRateDataService; beforeEach(() => { - currentRateService = new CurrentRateService(null, null); + currentRateService = new CurrentRateService(null, null, null); exchangeRateDataService = new ExchangeRateDataService( null, diff --git a/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell-partially.spec.ts b/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell-partially.spec.ts index 2f93096d9..d73db8c2c 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell-partially.spec.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell-partially.spec.ts @@ -21,7 +21,7 @@ describe('PortfolioCalculator', () => { let exchangeRateDataService: ExchangeRateDataService; beforeEach(() => { - currentRateService = new CurrentRateService(null, null); + currentRateService = new CurrentRateService(null, null, null); exchangeRateDataService = new ExchangeRateDataService( null, diff --git a/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts index ada9acc16..253b47346 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator-novn-buy-and-sell.spec.ts @@ -21,7 +21,7 @@ describe('PortfolioCalculator', () => { let exchangeRateDataService: ExchangeRateDataService; beforeEach(() => { - currentRateService = new CurrentRateService(null, null); + currentRateService = new CurrentRateService(null, null, null); exchangeRateDataService = new ExchangeRateDataService( null, diff --git a/apps/api/src/app/portfolio/portfolio-calculator.spec.ts b/apps/api/src/app/portfolio/portfolio-calculator.spec.ts index 1537fb8fe..dfe3f3466 100644 --- a/apps/api/src/app/portfolio/portfolio-calculator.spec.ts +++ b/apps/api/src/app/portfolio/portfolio-calculator.spec.ts @@ -10,7 +10,7 @@ describe('PortfolioCalculator', () => { let exchangeRateDataService: ExchangeRateDataService; beforeEach(() => { - currentRateService = new CurrentRateService(null, null); + currentRateService = new CurrentRateService(null, null, null); exchangeRateDataService = new ExchangeRateDataService( null, diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 68fce7977..9c70c404b 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -420,7 +420,7 @@ export class PortfolioService { ); const [dataProviderResponses, symbolProfiles] = await Promise.all([ - this.dataProviderService.getQuotes({ items: dataGatheringItems }), + this.dataProviderService.getQuotes({ user, items: dataGatheringItems }), this.symbolProfileService.getSymbolProfiles(dataGatheringItems) ]); @@ -861,6 +861,7 @@ export class PortfolioService { }; } else { const currentData = await this.dataProviderService.getQuotes({ + user, items: [{ dataSource: DataSource.YAHOO, symbol: aSymbol }] }); const marketPrice = currentData[aSymbol]?.marketPrice; @@ -939,6 +940,7 @@ export class PortfolioService { return type === 'SEARCH_QUERY'; })?.id; const userId = await this.getUserId(impersonationId, this.request.user.id); + const user = await this.userService.user({ id: userId }); const { portfolioOrders, transactionPoints } = await this.getTransactionPoints({ @@ -979,7 +981,7 @@ export class PortfolioService { }); const [dataProviderResponses, symbolProfiles] = await Promise.all([ - this.dataProviderService.getQuotes({ items: dataGatheringItems }), + this.dataProviderService.getQuotes({ user, items: dataGatheringItems }), this.symbolProfileService.getSymbolProfiles( positions.map(({ dataSource, symbol }) => { return { dataSource, symbol }; diff --git a/apps/api/src/services/data-provider/data-provider.service.ts b/apps/api/src/services/data-provider/data-provider.service.ts index e16c40a54..127af3f0d 100644 --- a/apps/api/src/services/data-provider/data-provider.service.ts +++ b/apps/api/src/services/data-provider/data-provider.service.ts @@ -335,11 +335,13 @@ export class DataProviderService { public async getQuotes({ items, requestTimeout, - useCache = true + useCache = true, + user }: { items: UniqueAsset[]; requestTimeout?: number; useCache?: boolean; + user?: UserWithSettings; }): Promise<{ [symbol: string]: IDataProviderResponse; }> { @@ -405,6 +407,14 @@ export class DataProviderService { )) { const dataProvider = this.getDataProvider(DataSource[dataSource]); + if ( + dataProvider.getDataProviderInfo().isPremium && + this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION') && + user?.subscription.type === 'Basic' + ) { + continue; + } + const symbols = dataGatheringItems.map((dataGatheringItem) => { return dataGatheringItem.symbol; });