diff --git a/.vscode/launch.json b/.vscode/launch.json index c1f19e7f0..6d36314d2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -18,12 +18,20 @@ "autoAttachChildProcesses": true, "console": "integratedTerminal", "cwd": "${workspaceFolder}/apps/api", - "envFile": "${workspaceFolder}/.env", + "env": { + "GHOSTFOLIO_ENV_FILE": "${workspaceFolder}/.env" + }, "name": "Debug API", "outFiles": ["${workspaceFolder}/dist/apps/api/**/*.js"], "program": "${workspaceFolder}/apps/api/src/main.ts", "request": "launch", - "runtimeArgs": ["--nolazy", "-r", "ts-node/register"], + "runtimeArgs": [ + "--nolazy", + "-r", + "ts-node/register", + "-r", + "${workspaceFolder}/tools/load-env.ts" + ], "skipFiles": [ "${workspaceFolder}/node_modules/**/*.js", "/**/*.js" diff --git a/CHANGELOG.md b/CHANGELOG.md index 553d54e6b..5bb91ff6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,36 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 3.7.0 - 2026-06-02 + +### Added + +- Added support for routing selected requests through the _OpenRouter_ `web_fetch` tool in the `FetchService` + +### Changed + +- Extended the countries mapping in the data enhancer for asset profile data via _Trackinsight_ +- Removed the deprecated attributes (`assetClass`, `assetClassLabel`, `assetSubClass`, `assetSubClassLabel`, `countries`, `currency`, `dataSource`, `holdings`, `name`, `sectors`, `symbol` and `url`) from the holdings of the portfolio details endpoint response +- Upgraded `Nx` from version `22.7.2` to `22.7.5` + +### Fixed + +- Resolved an issue in the impersonation mode where the values did not match the owner’s currency +- Fixed the environment variable expansion in the `.env` file when debugging via _Visual Studio Code_ + +## 3.6.0 - 2026-05-28 + +### Added + +- Added `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY` environment variable support to outbound HTTP requests +- Added the `FetchService` to centralize outbound HTTP requests + +### Changed + +- Extracted the floating action buttons (FAB) to a reusable component +- Upgraded `nestjs` from version `11.1.19` to `11.1.21` +- Upgraded `yahoo-finance2` from version `3.14.0` to `3.14.2` + ## 3.5.0 - 2026-05-24 ### Added diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 6ea0b5e40..5b1b36afb 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -84,6 +84,12 @@ https://ghostfol.io/development/storybook 1. Run `npx npm-check-updates --upgrade --target "minor" --filter "/@angular.*/"` +### NestJS + +#### Upgrade (minor versions) + +1. Run `npx npm-check-updates --upgrade --target "minor" --filter "/@nestjs.*/"` + ### Nx #### Upgrade diff --git a/apps/api/src/app/account/account.controller.ts b/apps/api/src/app/account/account.controller.ts index 052720176..d44b716c0 100644 --- a/apps/api/src/app/account/account.controller.ts +++ b/apps/api/src/app/account/account.controller.ts @@ -1,5 +1,6 @@ import { AccountBalanceService } from '@ghostfolio/api/app/account-balance/account-balance.service'; import { PortfolioService } from '@ghostfolio/api/app/portfolio/portfolio.service'; +import { UserService } from '@ghostfolio/api/app/user/user.service'; import { HasPermission } from '@ghostfolio/api/decorators/has-permission.decorator'; import { HasPermissionGuard } from '@ghostfolio/api/guards/has-permission.guard'; import { RedactValuesInResponseInterceptor } from '@ghostfolio/api/interceptors/redact-values-in-response/redact-values-in-response.interceptor'; @@ -50,7 +51,8 @@ export class AccountController { private readonly apiService: ApiService, private readonly impersonationService: ImpersonationService, private readonly portfolioService: PortfolioService, - @Inject(REQUEST) private readonly request: RequestWithUser + @Inject(REQUEST) private readonly request: RequestWithUser, + private readonly userService: UserService ) {} @Delete(':id') @@ -137,11 +139,14 @@ export class AccountController { ): Promise { const impersonationUserId = await this.impersonationService.validateImpersonationId(impersonationId); + const userId = impersonationUserId || this.request.user.id; + + const { settings } = await this.userService.user({ id: userId }); return this.accountBalanceService.getAccountBalances({ + userId, filters: [{ id, type: 'ACCOUNT' }], - userCurrency: this.request.user.settings.settings.baseCurrency, - userId: impersonationUserId || this.request.user.id + userCurrency: settings.settings.baseCurrency }); } diff --git a/apps/api/src/app/account/account.module.ts b/apps/api/src/app/account/account.module.ts index fb89bb2b6..253c7fb1d 100644 --- a/apps/api/src/app/account/account.module.ts +++ b/apps/api/src/app/account/account.module.ts @@ -1,5 +1,6 @@ import { AccountBalanceModule } from '@ghostfolio/api/app/account-balance/account-balance.module'; import { PortfolioModule } from '@ghostfolio/api/app/portfolio/portfolio.module'; +import { UserModule } from '@ghostfolio/api/app/user/user.module'; import { RedactValuesInResponseModule } from '@ghostfolio/api/interceptors/redact-values-in-response/redact-values-in-response.module'; import { ApiModule } from '@ghostfolio/api/services/api/api.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; @@ -23,7 +24,8 @@ import { AccountService } from './account.service'; ImpersonationModule, PortfolioModule, PrismaModule, - RedactValuesInResponseModule + RedactValuesInResponseModule, + UserModule ], providers: [AccountService] }) diff --git a/apps/api/src/app/admin/admin.service.ts b/apps/api/src/app/admin/admin.service.ts index ba338b5b9..0bf5c3925 100644 --- a/apps/api/src/app/admin/admin.service.ts +++ b/apps/api/src/app/admin/admin.service.ts @@ -576,8 +576,8 @@ export class AdminService { } try { - Promise.all([ - await this.symbolProfileService.updateAssetProfileIdentifier( + await Promise.all([ + this.symbolProfileService.updateAssetProfileIdentifier( { dataSource, symbol @@ -587,7 +587,7 @@ export class AdminService { symbol: newSymbol as string } ), - await this.marketDataService.updateAssetProfileIdentifier( + this.marketDataService.updateAssetProfileIdentifier( { dataSource, symbol @@ -599,12 +599,15 @@ export class AdminService { ) ]); - return this.symbolProfileService.getSymbolProfiles([ - { - dataSource: DataSource[newDataSource.toString()], - symbol: newSymbol as string - } - ])?.[0]; + const [updatedAssetProfile] = + await this.symbolProfileService.getSymbolProfiles([ + { + dataSource: DataSource[newDataSource.toString()], + symbol: newSymbol as string + } + ]); + + return updatedAssetProfile; } catch { throw new HttpException( getReasonPhrase(StatusCodes.BAD_REQUEST), @@ -650,12 +653,15 @@ export class AdminService { updatedSymbolProfile ); - return this.symbolProfileService.getSymbolProfiles([ - { - dataSource: dataSource as DataSource, - symbol: symbol as string - } - ])?.[0]; + const [updatedAssetProfile] = + await this.symbolProfileService.getSymbolProfiles([ + { + dataSource: dataSource as DataSource, + symbol: symbol as string + } + ]); + + return updatedAssetProfile; } } diff --git a/apps/api/src/app/auth/auth.module.ts b/apps/api/src/app/auth/auth.module.ts index 40fcda4af..5b0603a92 100644 --- a/apps/api/src/app/auth/auth.module.ts +++ b/apps/api/src/app/auth/auth.module.ts @@ -5,6 +5,8 @@ import { UserModule } from '@ghostfolio/api/app/user/user.module'; import { ApiKeyService } from '@ghostfolio/api/services/api-key/api-key.service'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; +import { FetchModule } from '@ghostfolio/api/services/fetch/fetch.module'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module'; import { PropertyModule } from '@ghostfolio/api/services/property/property.module'; @@ -24,6 +26,7 @@ import { OidcStrategy } from './oidc.strategy'; controllers: [AuthController], imports: [ ConfigurationModule, + FetchModule, JwtModule.register({ secret: process.env.JWT_SECRET_KEY, signOptions: { expiresIn: '180 days' } @@ -42,13 +45,20 @@ import { OidcStrategy } from './oidc.strategy'; JwtStrategy, OidcStateStore, { - inject: [AuthService, JwtService, OidcStateStore, ConfigurationService], + inject: [ + AuthService, + ConfigurationService, + FetchService, + JwtService, + OidcStateStore + ], provide: OidcStrategy, useFactory: async ( authService: AuthService, + configurationService: ConfigurationService, + fetchService: FetchService, jwtService: JwtService, - stateStore: OidcStateStore, - configurationService: ConfigurationService + stateStore: OidcStateStore ) => { const isOidcEnabled = configurationService.get( 'ENABLE_FEATURE_AUTH_OIDC' @@ -83,7 +93,7 @@ import { OidcStrategy } from './oidc.strategy'; userInfoURL = manualUserInfoUrl; } else { try { - const response = await fetch( + const response = await fetchService.fetch( `${issuer}/.well-known/openid-configuration` ); diff --git a/apps/api/src/app/endpoints/data-providers/ghostfolio/ghostfolio.module.ts b/apps/api/src/app/endpoints/data-providers/ghostfolio/ghostfolio.module.ts index 01691bcf4..484f30ee3 100644 --- a/apps/api/src/app/endpoints/data-providers/ghostfolio/ghostfolio.module.ts +++ b/apps/api/src/app/endpoints/data-providers/ghostfolio/ghostfolio.module.ts @@ -12,6 +12,7 @@ import { GoogleSheetsService } from '@ghostfolio/api/services/data-provider/goog import { ManualService } from '@ghostfolio/api/services/data-provider/manual/manual.service'; import { RapidApiService } from '@ghostfolio/api/services/data-provider/rapid-api/rapid-api.service'; import { YahooFinanceService } from '@ghostfolio/api/services/data-provider/yahoo-finance/yahoo-finance.service'; +import { FetchModule } from '@ghostfolio/api/services/fetch/fetch.module'; import { MarketDataModule } from '@ghostfolio/api/services/market-data/market-data.module'; import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module'; import { PropertyModule } from '@ghostfolio/api/services/property/property.module'; @@ -27,6 +28,7 @@ import { GhostfolioService } from './ghostfolio.service'; imports: [ CryptocurrencyModule, DataProviderModule, + FetchModule, MarketDataModule, PrismaModule, PropertyModule, diff --git a/apps/api/src/app/endpoints/data-providers/ghostfolio/ghostfolio.service.ts b/apps/api/src/app/endpoints/data-providers/ghostfolio/ghostfolio.service.ts index d088bf3ac..3f91dbecc 100644 --- a/apps/api/src/app/endpoints/data-providers/ghostfolio/ghostfolio.service.ts +++ b/apps/api/src/app/endpoints/data-providers/ghostfolio/ghostfolio.service.ts @@ -8,6 +8,7 @@ import { GetQuotesParams, GetSearchParams } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; import { PropertyService } from '@ghostfolio/api/services/property/property.service'; import { @@ -36,6 +37,7 @@ export class GhostfolioService { public constructor( private readonly configurationService: ConfigurationService, private readonly dataProviderService: DataProviderService, + private readonly fetchService: FetchService, private readonly prismaService: PrismaService, private readonly propertyService: PropertyService ) {} @@ -355,6 +357,7 @@ export class GhostfolioService { private getDataProviderInfo(): DataProviderInfo { const ghostfolioDataProviderService = new GhostfolioDataProviderService( this.configurationService, + this.fetchService, this.propertyService ); diff --git a/apps/api/src/app/endpoints/market-data/market-data.controller.ts b/apps/api/src/app/endpoints/market-data/market-data.controller.ts index 0dae82d2c..f6857283b 100644 --- a/apps/api/src/app/endpoints/market-data/market-data.controller.ts +++ b/apps/api/src/app/endpoints/market-data/market-data.controller.ts @@ -120,10 +120,10 @@ export class MarketDataController { if (!canReadAllAssetProfiles && !canReadOwnAssetProfile) { throw new HttpException( - assetProfile.userId + assetProfile?.userId ? getReasonPhrase(StatusCodes.NOT_FOUND) : getReasonPhrase(StatusCodes.FORBIDDEN), - assetProfile.userId ? StatusCodes.NOT_FOUND : StatusCodes.FORBIDDEN + assetProfile?.userId ? StatusCodes.NOT_FOUND : StatusCodes.FORBIDDEN ); } diff --git a/apps/api/src/app/logo/logo.module.ts b/apps/api/src/app/logo/logo.module.ts index 1f59df1c8..8eede126a 100644 --- a/apps/api/src/app/logo/logo.module.ts +++ b/apps/api/src/app/logo/logo.module.ts @@ -1,5 +1,6 @@ import { TransformDataSourceInRequestModule } from '@ghostfolio/api/interceptors/transform-data-source-in-request/transform-data-source-in-request.module'; import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; +import { FetchModule } from '@ghostfolio/api/services/fetch/fetch.module'; import { SymbolProfileModule } from '@ghostfolio/api/services/symbol-profile/symbol-profile.module'; import { Module } from '@nestjs/common'; @@ -11,6 +12,7 @@ import { LogoService } from './logo.service'; controllers: [LogoController], imports: [ ConfigurationModule, + FetchModule, SymbolProfileModule, TransformDataSourceInRequestModule ], diff --git a/apps/api/src/app/logo/logo.service.ts b/apps/api/src/app/logo/logo.service.ts index ba1acdd29..551d62438 100644 --- a/apps/api/src/app/logo/logo.service.ts +++ b/apps/api/src/app/logo/logo.service.ts @@ -1,4 +1,5 @@ import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; import { AssetProfileIdentifier } from '@ghostfolio/common/interfaces'; @@ -10,6 +11,7 @@ import { StatusCodes, getReasonPhrase } from 'http-status-codes'; export class LogoService { public constructor( private readonly configurationService: ConfigurationService, + private readonly fetchService: FetchService, private readonly symbolProfileService: SymbolProfileService ) {} @@ -43,15 +45,17 @@ export class LogoService { } private async getBuffer(aUrl: string) { - const blob = await fetch( - `https://t0.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=${aUrl}&size=64`, - { - headers: { 'User-Agent': 'request' }, - signal: AbortSignal.timeout( - this.configurationService.get('REQUEST_TIMEOUT') - ) - } - ).then((res) => res.blob()); + const blob = await this.fetchService + .fetch( + `https://t0.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=${aUrl}&size=64`, + { + headers: { 'User-Agent': 'request' }, + signal: AbortSignal.timeout( + this.configurationService.get('REQUEST_TIMEOUT') + ) + } + ) + .then((res) => res.blob()); return { buffer: await blob.arrayBuffer().then((arrayBuffer) => { diff --git a/apps/api/src/app/portfolio/portfolio.controller.ts b/apps/api/src/app/portfolio/portfolio.controller.ts index 8aa94ee92..ca94605f9 100644 --- a/apps/api/src/app/portfolio/portfolio.controller.ts +++ b/apps/api/src/app/portfolio/portfolio.controller.ts @@ -1,4 +1,5 @@ import { ActivitiesService } from '@ghostfolio/api/app/activities/activities.service'; +import { UserService } from '@ghostfolio/api/app/user/user.service'; import { HasPermission } from '@ghostfolio/api/decorators/has-permission.decorator'; import { HasPermissionGuard } from '@ghostfolio/api/guards/has-permission.guard'; import { @@ -70,7 +71,8 @@ export class PortfolioController { private readonly configurationService: ConfigurationService, private readonly impersonationService: ImpersonationService, private readonly portfolioService: PortfolioService, - @Inject(REQUEST) private readonly request: RequestWithUser + @Inject(REQUEST) private readonly request: RequestWithUser, + private readonly userService: UserService ) {} @Get('details') @@ -144,10 +146,10 @@ export class PortfolioController { .reduce((a, b) => a + b, 0); const totalValue = Object.values(holdings) - .filter(({ assetClass, assetSubClass }) => { + .filter(({ assetProfile }) => { return ( - assetClass !== AssetClass.LIQUIDITY && - assetSubClass !== AssetSubClass.CASH + assetProfile.assetClass !== AssetClass.LIQUIDITY && + assetProfile.assetSubClass !== AssetSubClass.CASH ); }) .map(({ valueInBaseCurrency }) => { @@ -217,37 +219,41 @@ export class PortfolioController { for (const [symbol, portfolioPosition] of Object.entries(holdings)) { holdings[symbol] = { ...portfolioPosition, - assetClass: - hasDetails || portfolioPosition.assetClass === AssetClass.LIQUIDITY - ? portfolioPosition.assetClass - : undefined, assetProfile: { ...portfolioPosition.assetProfile, + assetClass: + hasDetails || + portfolioPosition.assetProfile.assetClass === AssetClass.LIQUIDITY + ? portfolioPosition.assetProfile.assetClass + : undefined, + assetClassLabel: + hasDetails || + portfolioPosition.assetProfile.assetClass === AssetClass.LIQUIDITY + ? portfolioPosition.assetProfile.assetClassLabel + : undefined, + assetSubClass: + hasDetails || + portfolioPosition.assetProfile.assetSubClass === AssetSubClass.CASH + ? portfolioPosition.assetProfile.assetSubClass + : undefined, + assetSubClassLabel: + hasDetails || + portfolioPosition.assetProfile.assetSubClass === AssetSubClass.CASH + ? portfolioPosition.assetProfile.assetSubClassLabel + : undefined, ...(hasDetails ? {} : { - assetClass: undefined, - assetClassLabel: undefined, - assetSubClass: undefined, - assetSubClassLabel: undefined, countries: [], currency: undefined, holdings: [], sectors: [] }) }, - assetSubClass: - hasDetails || portfolioPosition.assetSubClass === AssetSubClass.CASH - ? portfolioPosition.assetSubClass - : undefined, - countries: hasDetails ? portfolioPosition.countries : [], - currency: hasDetails ? portfolioPosition.currency : undefined, - holdings: hasDetails ? portfolioPosition.holdings : [], markets: hasDetails ? portfolioPosition.markets : undefined, marketsAdvanced: hasDetails ? portfolioPosition.marketsAdvanced - : undefined, - sectors: hasDetails ? portfolioPosition.sectors : [] + : undefined }; } @@ -336,7 +342,10 @@ export class PortfolioController { const impersonationUserId = await this.impersonationService.validateImpersonationId(impersonationId); - const userCurrency = this.request.user.settings.settings.baseCurrency; + const userId = impersonationUserId || this.request.user.id; + + const { settings } = await this.userService.user({ id: userId }); + const userCurrency = settings.settings.baseCurrency; const { endDate, startDate } = getIntervalFromDateRange({ dateRange }); @@ -345,7 +354,7 @@ export class PortfolioController { filters, startDate, userCurrency, - userId: impersonationUserId || this.request.user.id, + userId, types: ['DIVIDEND'] }); diff --git a/apps/api/src/app/portfolio/portfolio.service.spec.ts b/apps/api/src/app/portfolio/portfolio.service.spec.ts new file mode 100644 index 000000000..da846c45d --- /dev/null +++ b/apps/api/src/app/portfolio/portfolio.service.spec.ts @@ -0,0 +1,272 @@ +import { AccountService } from '@ghostfolio/api/app/account/account.service'; +import { CashDetails } from '@ghostfolio/api/app/account/interfaces/cash-details.interface'; +import { ActivitiesService } from '@ghostfolio/api/app/activities/activities.service'; +import { userDummyData } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils'; +import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory'; +import { UserService } from '@ghostfolio/api/app/user/user.service'; +import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; +import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service'; +import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service'; +import { ImpersonationService } from '@ghostfolio/api/services/impersonation/impersonation.service'; +import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; +import { parseDate } from '@ghostfolio/common/helper'; + +import { Account, DataSource } from '@prisma/client'; +import { Big } from 'big.js'; +import { randomUUID } from 'node:crypto'; + +import { PortfolioService } from './portfolio.service'; + +describe('PortfolioService', () => { + let accountService: AccountService; + let activitiesService: ActivitiesService; + let configurationService: ConfigurationService; + let dataProviderService: DataProviderService; + let exchangeRateDataService: ExchangeRateDataService; + let impersonationService: ImpersonationService; + let portfolioCalculatorFactory: PortfolioCalculatorFactory; + let portfolioService: PortfolioService; + let symbolProfileService: SymbolProfileService; + let userService: UserService; + + beforeEach(() => { + configurationService = new ConfigurationService(); + + dataProviderService = new DataProviderService( + configurationService, + null, + null, + null, + null, + null + ); + + exchangeRateDataService = new ExchangeRateDataService( + null, + null, + null, + null + ); + + accountService = new AccountService( + null, + null, + exchangeRateDataService, + null + ); + + activitiesService = new ActivitiesService( + null, + accountService, + null, + dataProviderService, + null, + exchangeRateDataService, + null, + null + ); + + impersonationService = new ImpersonationService(null, null); + + portfolioCalculatorFactory = new PortfolioCalculatorFactory( + configurationService, + null, + exchangeRateDataService, + null, + null + ); + + symbolProfileService = new SymbolProfileService(null); + + userService = new UserService( + null, + null, + null, + null, + null, + null, + null, + null + ); + + portfolioService = new PortfolioService( + null, + accountService, + activitiesService, + null, + portfolioCalculatorFactory, + dataProviderService, + exchangeRateDataService, + null, + impersonationService, + null, + null, + symbolProfileService, + userService + ); + }); + + describe('getCashSymbolProfiles', () => { + it('should use the exchange-rate data source so the symbol-profile join in getDetails matches the calculator positions', () => { + jest + .spyOn(dataProviderService, 'getDataSourceForExchangeRates') + .mockReturnValue(DataSource.YAHOO); + + const cashDetails: CashDetails = { + accounts: [ + { + balance: 2000, + comment: null, + createdAt: parseDate('2024-01-01'), + currency: 'USD', + id: randomUUID(), + isExcluded: false, + name: 'USD', + platformId: null, + updatedAt: parseDate('2024-01-01'), + userId: userDummyData.id + } + ], + balanceInBaseCurrency: 1820 + }; + + const assetProfiles = ( + portfolioService as unknown as { + getCashSymbolProfiles: ( + aCashDetails: CashDetails + ) => { dataSource: DataSource; symbol: string }[]; + } + ).getCashSymbolProfiles(cashDetails); + + expect(assetProfiles).toHaveLength(1); + expect(assetProfiles[0].dataSource).toBe(DataSource.YAHOO); + expect(assetProfiles[0].symbol).toBe('USD'); + }); + }); + + describe('getDetails', () => { + it('should return cash holdings when the calculator emits cash positions with the exchange-rate data source', async () => { + const accountId = randomUUID(); + + const cashAccount: Account = { + balance: 2000, + comment: null, + createdAt: parseDate('2024-01-01'), + currency: 'USD', + id: accountId, + isExcluded: false, + name: 'USD', + platformId: null, + updatedAt: parseDate('2024-01-01'), + userId: userDummyData.id + }; + + jest.spyOn(accountService, 'getCashDetails').mockResolvedValue({ + accounts: [cashAccount], + balanceInBaseCurrency: 1820 + }); + + jest + .spyOn(activitiesService, 'getActivitiesForPortfolioCalculator') + .mockResolvedValue({ activities: [], count: 0 }); + + jest + .spyOn(dataProviderService, 'getDataSourceForExchangeRates') + .mockReturnValue(DataSource.YAHOO); + + jest + .spyOn(impersonationService, 'validateImpersonationId') + .mockResolvedValue(null); + + jest + .spyOn(symbolProfileService, 'getSymbolProfiles') + .mockResolvedValue([]); + + jest.spyOn(userService, 'user').mockResolvedValue({ + accessesGet: [], + accounts: [], + activityCount: 0, + dataProviderGhostfolioDailyRequests: 0, + id: userDummyData.id, + settings: { + settings: { + baseCurrency: 'CHF' + } + } + } as unknown as Awaited>); + + const usdPosition = { + activitiesCount: 1, + averagePrice: new Big(1), + currency: 'USD', + dataSource: DataSource.YAHOO, + dateOfFirstActivity: '2024-01-01', + dividend: new Big(0), + dividendInBaseCurrency: new Big(0), + fee: new Big(0), + feeInBaseCurrency: new Big(0), + grossPerformance: new Big(0), + grossPerformancePercentage: new Big(0), + grossPerformancePercentageWithCurrencyEffect: new Big(0), + grossPerformanceWithCurrencyEffect: new Big(0), + investment: new Big(1820), + investmentWithCurrencyEffect: new Big(1820), + marketPrice: 1, + marketPriceInBaseCurrency: 0.91, + netPerformance: new Big(0), + netPerformancePercentage: new Big(0), + netPerformancePercentageWithCurrencyEffectMap: {}, + netPerformanceWithCurrencyEffectMap: {}, + quantity: new Big(2000), + symbol: 'USD', + tags: [], + timeWeightedInvestment: new Big(0), + timeWeightedInvestmentWithCurrencyEffect: new Big(0), + valueInBaseCurrency: new Big(1820) + }; + + jest + .spyOn(portfolioCalculatorFactory, 'createCalculator') + .mockReturnValue({ + getSnapshot: jest.fn().mockResolvedValue({ + activitiesCount: 1, + createdAt: parseDate('2024-01-01'), + currentValueInBaseCurrency: new Big(1820), + errors: [], + hasErrors: false, + historicalData: [], + positions: [usdPosition], + totalFeesWithCurrencyEffect: new Big(0), + totalInterestWithCurrencyEffect: new Big(0), + totalInvestment: new Big(1820), + totalInvestmentWithCurrencyEffect: new Big(1820), + totalLiabilitiesWithCurrencyEffect: new Big(0) + }) + } as unknown as ReturnType< + typeof portfolioCalculatorFactory.createCalculator + >); + + jest + .spyOn( + portfolioService as unknown as { + getValueOfAccountsAndPlatforms: () => Promise<{ + accounts: object; + platforms: object; + }>; + }, + 'getValueOfAccountsAndPlatforms' + ) + .mockResolvedValue({ accounts: {}, platforms: {} }); + + const { holdings } = await portfolioService.getDetails({ + filters: [], + impersonationId: userDummyData.id, + userId: userDummyData.id + }); + + expect(holdings['USD']).toBeDefined(); + expect(holdings['USD'].assetProfile.dataSource).toBe(DataSource.YAHOO); + expect(holdings['USD'].assetProfile.symbol).toBe('USD'); + }); + }); +}); diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index cee36ec27..37d76bcfa 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -164,7 +164,7 @@ export class PortfolioService { }; } - const [accounts, details] = await Promise.all([ + const [accounts, details, user] = await Promise.all([ this.accountService.accounts({ where, include: { @@ -178,10 +178,11 @@ export class PortfolioService { withExcludedAccounts, impersonationId: userId, userId: this.request.user.id - }) + }), + this.userService.user({ id: userId }) ]); - const userCurrency = this.request.user.settings.settings.baseCurrency; + const userCurrency = this.getUserCurrency(user); return Promise.all( accounts.map(async (account) => { @@ -584,7 +585,6 @@ export class PortfolioService { for (const { activitiesCount, - currency, dataSource, dateOfFirstActivity, dividend, @@ -638,16 +638,13 @@ export class PortfolioService { holdings[symbol] = { activitiesCount, - currency, markets, marketsAdvanced, marketPrice, - symbol, tags, allocationInPercentage: filteredValueInBaseCurrency.eq(0) ? 0 : valueInBaseCurrency.div(filteredValueInBaseCurrency).toNumber(), - assetClass: assetProfile.assetClass, assetProfile: { assetClass: assetProfile.assetClass, assetSubClass: assetProfile.assetSubClass, @@ -670,9 +667,6 @@ export class PortfolioService { symbol: assetProfile.symbol, url: assetProfile.url }, - assetSubClass: assetProfile.assetSubClass, - countries: assetProfile.countries, - dataSource: assetProfile.dataSource, dateOfFirstActivity: parseDate(dateOfFirstActivity), dividend: dividend?.toNumber() ?? 0, grossPerformance: grossPerformance?.toNumber() ?? 0, @@ -681,19 +675,7 @@ export class PortfolioService { grossPerformancePercentageWithCurrencyEffect?.toNumber() ?? 0, grossPerformanceWithCurrencyEffect: grossPerformanceWithCurrencyEffect?.toNumber() ?? 0, - holdings: assetProfile.holdings.map( - ({ allocationInPercentage, name }) => { - return { - allocationInPercentage, - name, - valueInBaseCurrency: valueInBaseCurrency - .mul(allocationInPercentage) - .toNumber() - }; - } - ), investment: investment.toNumber(), - name: assetProfile.name, netPerformance: netPerformance?.toNumber() ?? 0, netPerformancePercent: netPerformancePercentage?.toNumber() ?? 0, netPerformancePercentWithCurrencyEffect: @@ -703,8 +685,6 @@ export class PortfolioService { netPerformanceWithCurrencyEffect: netPerformanceWithCurrencyEffectMap?.[dateRange]?.toNumber() ?? 0, quantity: quantity.toNumber(), - sectors: assetProfile.sectors, - url: assetProfile.url, valueInBaseCurrency: valueInBaseCurrency.toNumber() }; } @@ -1472,8 +1452,8 @@ export class PortfolioService { for (const [, position] of Object.entries(holdings)) { const value = position.valueInBaseCurrency; - if (position.assetClass !== AssetClass.LIQUIDITY) { - if (position.countries.length > 0) { + if (position.assetProfile.assetClass !== AssetClass.LIQUIDITY) { + if (position.assetProfile.countries.length > 0) { markets.developedMarkets.valueInBaseCurrency += position.markets.developedMarkets * value; markets.emergingMarkets.valueInBaseCurrency += @@ -1719,11 +1699,8 @@ export class PortfolioService { currency: string; }): PortfolioPosition { return { - currency, activitiesCount: 0, allocationInPercentage: 0, - assetClass: AssetClass.LIQUIDITY, - assetSubClass: AssetSubClass.CASH, assetProfile: { currency, assetClass: AssetClass.LIQUIDITY, @@ -1735,25 +1712,19 @@ export class PortfolioService { sectors: [], symbol: currency }, - countries: [], - dataSource: undefined, dateOfFirstActivity: undefined, dividend: 0, grossPerformance: 0, grossPerformancePercent: 0, grossPerformancePercentWithCurrencyEffect: 0, grossPerformanceWithCurrencyEffect: 0, - holdings: [], investment: balance, marketPrice: 0, - name: currency, netPerformance: 0, netPerformancePercent: 0, netPerformancePercentWithCurrencyEffect: 0, netPerformanceWithCurrencyEffect: 0, quantity: 0, - sectors: [], - symbol: currency, tags: [], valueInBaseCurrency: balance }; diff --git a/apps/api/src/app/user/user.service.ts b/apps/api/src/app/user/user.service.ts index 278566691..29abe5bfb 100644 --- a/apps/api/src/app/user/user.service.ts +++ b/apps/api/src/app/user/user.service.ts @@ -49,7 +49,7 @@ import { PerformanceCalculationType } from '@ghostfolio/common/types/performance import { Injectable } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; -import { Prisma, Role, User } from '@prisma/client'; +import { Prisma, Role, Settings, User } from '@prisma/client'; import { differenceInDays, subDays } from 'date-fns'; import { without } from 'lodash'; import { createHmac } from 'node:crypto'; @@ -116,7 +116,14 @@ export class UserService { thirdPartyId } = user; - const userData = await Promise.all([ + const [ + access, + accounts, + activitiesCount, + firstActivity, + impersonationUserSettings, + tagsForUser + ] = await Promise.all([ this.prismaService.access.findMany({ include: { user: true @@ -141,16 +148,17 @@ export class UserService { }, where: { userId: impersonationUserId || user.id } }), + impersonationUserId + ? this.prismaService.settings.findUnique({ + where: { userId: impersonationUserId } + }) + : Promise.resolve(null), this.tagService.getTagsForUser(impersonationUserId || user.id) ]); - const access = userData[0]; - const accounts = userData[1]; - const activitiesCount = userData[2]; - const firstActivity = userData[3]; - let tags = userData[4].filter((tag) => { - return tag.id !== TAG_ID_EXCLUDE_FROM_ANALYSIS; - }); + const baseCurrency = + (impersonationUserSettings?.settings as UserSettings)?.baseCurrency ?? + (settings.settings as UserSettings)?.baseCurrency; let systemMessage: SystemMessage; @@ -163,6 +171,10 @@ export class UserService { systemMessage = systemMessageProperty; } + let tags = tagsForUser.filter((tag) => { + return tag.id !== TAG_ID_EXCLUDE_FROM_ANALYSIS; + }); + if ( this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION') && subscription.type === SubscriptionType.Basic @@ -192,6 +204,7 @@ export class UserService { dateOfFirstActivity: firstActivity?.date ?? new Date(), settings: { ...(settings.settings as UserSettings), + baseCurrency, locale: (settings.settings as UserSettings)?.locale ?? locale } }; diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index f08a09a83..94e389f6a 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -18,11 +18,15 @@ import type { NestExpressApplication } from '@nestjs/platform-express'; import cookieParser from 'cookie-parser'; import { NextFunction, Request, Response } from 'express'; import helmet from 'helmet'; +import { EnvHttpProxyAgent, setGlobalDispatcher } from 'undici'; import { AppModule } from './app/app.module'; import { environment } from './environments/environment'; async function bootstrap() { + // Respect HTTP_PROXY / HTTPS_PROXY / NO_PROXY for outbound HTTP requests + setGlobalDispatcher(new EnvHttpProxyAgent()); + const configApp = await NestFactory.create(AppModule); const configService = configApp.get(ConfigService); let customLogLevels: LogLevel[]; diff --git a/apps/api/src/services/data-provider/coingecko/coingecko.service.ts b/apps/api/src/services/data-provider/coingecko/coingecko.service.ts index d5ed69d06..b01ba177b 100644 --- a/apps/api/src/services/data-provider/coingecko/coingecko.service.ts +++ b/apps/api/src/services/data-provider/coingecko/coingecko.service.ts @@ -7,6 +7,7 @@ import { GetQuotesParams, GetSearchParams } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; import { DEFAULT_CURRENCY } from '@ghostfolio/common/config'; import { DATE_FORMAT } from '@ghostfolio/common/helper'; import { @@ -32,7 +33,8 @@ export class CoinGeckoService implements DataProviderInterface, OnModuleInit { private headers: HeadersInit = {}; public constructor( - private readonly configurationService: ConfigurationService + private readonly configurationService: ConfigurationService, + private readonly fetchService: FetchService ) {} public onModuleInit() { @@ -67,12 +69,14 @@ export class CoinGeckoService implements DataProviderInterface, OnModuleInit { }; try { - const { name } = await fetch(`${this.apiUrl}/coins/${symbol}`, { - headers: this.headers, - signal: AbortSignal.timeout( - this.configurationService.get('REQUEST_TIMEOUT') - ) - }).then((res) => res.json()); + const { name } = await this.fetchService + .fetch(`${this.apiUrl}/coins/${symbol}`, { + headers: this.headers, + signal: AbortSignal.timeout( + this.configurationService.get('REQUEST_TIMEOUT') + ) + }) + .then((res) => res.json()); response.name = name; } catch (error) { @@ -118,13 +122,15 @@ export class CoinGeckoService implements DataProviderInterface, OnModuleInit { vs_currency: DEFAULT_CURRENCY.toLowerCase() }); - const { error, prices, status } = await fetch( - `${this.apiUrl}/coins/${symbol}/market_chart/range?${queryParams.toString()}`, - { - headers: this.headers, - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + const { error, prices, status } = await this.fetchService + .fetch( + `${this.apiUrl}/coins/${symbol}/market_chart/range?${queryParams.toString()}`, + { + headers: this.headers, + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()); if (error?.status) { throw new Error(error.status.error_message); @@ -181,13 +187,12 @@ export class CoinGeckoService implements DataProviderInterface, OnModuleInit { vs_currencies: DEFAULT_CURRENCY.toLowerCase() }); - const quotes = await fetch( - `${this.apiUrl}/simple/price?${queryParams.toString()}`, - { + const quotes = await this.fetchService + .fetch(`${this.apiUrl}/simple/price?${queryParams.toString()}`, { headers: this.headers, signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + }) + .then((res) => res.json()); for (const symbol in quotes) { response[symbol] = { @@ -230,13 +235,12 @@ export class CoinGeckoService implements DataProviderInterface, OnModuleInit { query }); - const { coins } = await fetch( - `${this.apiUrl}/search?${queryParams.toString()}`, - { + const { coins } = await this.fetchService + .fetch(`${this.apiUrl}/search?${queryParams.toString()}`, { headers: this.headers, signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + }) + .then((res) => res.json()); items = coins.map(({ id: symbol, name }) => { return { diff --git a/apps/api/src/services/data-provider/data-enhancer/data-enhancer.module.ts b/apps/api/src/services/data-provider/data-enhancer/data-enhancer.module.ts index cadf8cf1d..ecad9a673 100644 --- a/apps/api/src/services/data-provider/data-enhancer/data-enhancer.module.ts +++ b/apps/api/src/services/data-provider/data-enhancer/data-enhancer.module.ts @@ -3,6 +3,7 @@ import { CryptocurrencyModule } from '@ghostfolio/api/services/cryptocurrency/cr import { OpenFigiDataEnhancerService } from '@ghostfolio/api/services/data-provider/data-enhancer/openfigi/openfigi.service'; import { TrackinsightDataEnhancerService } from '@ghostfolio/api/services/data-provider/data-enhancer/trackinsight/trackinsight.service'; import { YahooFinanceDataEnhancerService } from '@ghostfolio/api/services/data-provider/data-enhancer/yahoo-finance/yahoo-finance.service'; +import { FetchModule } from '@ghostfolio/api/services/fetch/fetch.module'; import { Module } from '@nestjs/common'; @@ -16,7 +17,7 @@ import { DataEnhancerService } from './data-enhancer.service'; YahooFinanceDataEnhancerService, 'DataEnhancers' ], - imports: [ConfigurationModule, CryptocurrencyModule], + imports: [ConfigurationModule, CryptocurrencyModule, FetchModule], providers: [ DataEnhancerService, OpenFigiDataEnhancerService, diff --git a/apps/api/src/services/data-provider/data-enhancer/openfigi/openfigi.service.ts b/apps/api/src/services/data-provider/data-enhancer/openfigi/openfigi.service.ts index bb9d0606c..1f5bb74b4 100644 --- a/apps/api/src/services/data-provider/data-enhancer/openfigi/openfigi.service.ts +++ b/apps/api/src/services/data-provider/data-enhancer/openfigi/openfigi.service.ts @@ -1,5 +1,6 @@ import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { DataEnhancerInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-enhancer.interface'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; import { parseSymbol } from '@ghostfolio/common/helper'; import { Injectable } from '@nestjs/common'; @@ -10,7 +11,8 @@ export class OpenFigiDataEnhancerService implements DataEnhancerInterface { private static baseUrl = 'https://api.openfigi.com'; public constructor( - private readonly configurationService: ConfigurationService + private readonly configurationService: ConfigurationService, + private readonly fetchService: FetchService ) {} public async enhance({ @@ -42,9 +44,8 @@ export class OpenFigiDataEnhancerService implements DataEnhancerInterface { this.configurationService.get('API_KEY_OPEN_FIGI'); } - const mappings = (await fetch( - `${OpenFigiDataEnhancerService.baseUrl}/v3/mapping`, - { + const mappings = (await this.fetchService + .fetch(`${OpenFigiDataEnhancerService.baseUrl}/v3/mapping`, { body: JSON.stringify([ { exchCode: exchange, idType: 'TICKER', idValue: ticker } ]), @@ -54,8 +55,8 @@ export class OpenFigiDataEnhancerService implements DataEnhancerInterface { }, method: 'POST', signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json())) as any[]; + }) + .then((res) => res.json())) as any[]; if (mappings?.length === 1 && mappings[0].data?.length === 1) { const { compositeFIGI, figi, shareClassFIGI } = mappings[0].data[0]; diff --git a/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts b/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts index 1e297b93b..eeccf725e 100644 --- a/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts +++ b/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts @@ -1,5 +1,6 @@ import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { DataEnhancerInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-enhancer.interface'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; import { Holding } from '@ghostfolio/common/interfaces'; import { Country } from '@ghostfolio/common/interfaces/country.interface'; import { Sector } from '@ghostfolio/common/interfaces/sector.interface'; @@ -12,7 +13,8 @@ import { countries } from 'countries-list'; export class TrackinsightDataEnhancerService implements DataEnhancerInterface { private static baseUrl = 'https://www.trackinsight.com/data-api'; private static countriesMapping = { - 'Russian Federation': 'Russia' + 'Russian Federation': 'Russia', + USA: 'United States' }; private static holdingsWeightTreshold = 0.85; private static sectorsMapping = { @@ -23,7 +25,8 @@ export class TrackinsightDataEnhancerService implements DataEnhancerInterface { }; public constructor( - private readonly configurationService: ConfigurationService + private readonly configurationService: ConfigurationService, + private readonly fetchService: FetchService ) {} public async enhance({ @@ -60,12 +63,13 @@ export class TrackinsightDataEnhancerService implements DataEnhancerInterface { return response; } - const profile = await fetch( - `${TrackinsightDataEnhancerService.baseUrl}/funds/${trackinsightSymbol}.json`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ) + const profile = await this.fetchService + .fetch( + `${TrackinsightDataEnhancerService.baseUrl}/funds/${trackinsightSymbol}.json`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) .then((res) => res.json()) .catch(() => { return {}; @@ -83,12 +87,13 @@ export class TrackinsightDataEnhancerService implements DataEnhancerInterface { response.isin = isin; } - const holdings = await fetch( - `${TrackinsightDataEnhancerService.baseUrl}/holdings/${trackinsightSymbol}.json`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ) + const holdings = await this.fetchService + .fetch( + `${TrackinsightDataEnhancerService.baseUrl}/holdings/${trackinsightSymbol}.json`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) .then((res) => res.json()) .catch(() => { return {}; @@ -182,12 +187,13 @@ export class TrackinsightDataEnhancerService implements DataEnhancerInterface { requestTimeout: number; symbol: string; }) { - return fetch( - `https://www.trackinsight.com/search-api/search_v2/${symbol}/_/ticker/default/0/3`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ) + return this.fetchService + .fetch( + `https://www.trackinsight.com/search-api/search_v2/${symbol}/_/ticker/default/0/3`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) .then((res) => res.json()) .then((jsonRes) => { if ( diff --git a/apps/api/src/services/data-provider/data-provider.module.ts b/apps/api/src/services/data-provider/data-provider.module.ts index 71b54f01e..2c6e9fce1 100644 --- a/apps/api/src/services/data-provider/data-provider.module.ts +++ b/apps/api/src/services/data-provider/data-provider.module.ts @@ -10,6 +10,7 @@ import { GoogleSheetsService } from '@ghostfolio/api/services/data-provider/goog import { ManualService } from '@ghostfolio/api/services/data-provider/manual/manual.service'; import { RapidApiService } from '@ghostfolio/api/services/data-provider/rapid-api/rapid-api.service'; import { YahooFinanceService } from '@ghostfolio/api/services/data-provider/yahoo-finance/yahoo-finance.service'; +import { FetchModule } from '@ghostfolio/api/services/fetch/fetch.module'; import { MarketDataModule } from '@ghostfolio/api/services/market-data/market-data.module'; import { PrismaModule } from '@ghostfolio/api/services/prisma/prisma.module'; import { PropertyModule } from '@ghostfolio/api/services/property/property.module'; @@ -26,6 +27,7 @@ import { DataProviderService } from './data-provider.service'; ConfigurationModule, CryptocurrencyModule, DataEnhancerModule, + FetchModule, MarketDataModule, PrismaModule, PropertyModule, diff --git a/apps/api/src/services/data-provider/eod-historical-data/eod-historical-data.service.ts b/apps/api/src/services/data-provider/eod-historical-data/eod-historical-data.service.ts index 8c718108c..3fa38842b 100644 --- a/apps/api/src/services/data-provider/eod-historical-data/eod-historical-data.service.ts +++ b/apps/api/src/services/data-provider/eod-historical-data/eod-historical-data.service.ts @@ -7,6 +7,7 @@ import { GetQuotesParams, GetSearchParams } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; import { DEFAULT_CURRENCY, @@ -41,6 +42,7 @@ export class EodHistoricalDataService public constructor( private readonly configurationService: ConfigurationService, + private readonly fetchService: FetchService, private readonly symbolProfileService: SymbolProfileService ) {} @@ -111,12 +113,11 @@ export class EodHistoricalDataService [date: string]: DataProviderHistoricalResponse; } = {}; - const historicalResult = await fetch( - `${this.URL}/div/${symbol}?${queryParams.toString()}`, - { + const historicalResult = await this.fetchService + .fetch(`${this.URL}/div/${symbol}?${queryParams.toString()}`, { signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + }) + .then((res) => res.json()); for (const { date, value } of historicalResult) { response[date] = { @@ -158,12 +159,11 @@ export class EodHistoricalDataService to: format(to, DATE_FORMAT) }); - const response = await fetch( - `${this.URL}/eod/${symbol}?${queryParams.toString()}`, - { + const response = await this.fetchService + .fetch(`${this.URL}/eod/${symbol}?${queryParams.toString()}`, { signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + }) + .then((res) => res.json()); return response.reduce( (result, { adjusted_close, date }) => { @@ -223,12 +223,14 @@ export class EodHistoricalDataService s: eodHistoricalDataSymbols.join(',') }); - const realTimeResponse = await fetch( - `${this.URL}/real-time/${eodHistoricalDataSymbols[0]}?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + const realTimeResponse = await this.fetchService + .fetch( + `${this.URL}/real-time/${eodHistoricalDataSymbols[0]}?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()); const quotes: { close: number; @@ -430,12 +432,11 @@ export class EodHistoricalDataService api_token: this.apiKey }); - const response = await fetch( - `${this.URL}/search/${query}?${queryParams.toString()}`, - { + const response = await this.fetchService + .fetch(`${this.URL}/search/${query}?${queryParams.toString()}`, { signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + }) + .then((res) => res.json()); searchResult = response.map( ({ Code, Currency, Exchange, ISIN: isin, Name: name, Type }) => { diff --git a/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts b/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts index d9a43fc50..fa36a0d17 100644 --- a/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts +++ b/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts @@ -9,6 +9,7 @@ import { GetQuotesParams, GetSearchParams } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; import { DEFAULT_CURRENCY, @@ -59,6 +60,7 @@ export class FinancialModelingPrepService public constructor( private readonly configurationService: ConfigurationService, private readonly cryptocurrencyService: CryptocurrencyService, + private readonly fetchService: FetchService, private readonly prismaService: PrismaService ) {} @@ -96,12 +98,14 @@ export class FinancialModelingPrepService apikey: this.apiKey }); - const [quote] = await fetch( - `${this.getUrl({ version: 'stable' })}/quote?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + const [quote] = await this.fetchService + .fetch( + `${this.getUrl({ version: 'stable' })}/quote?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()); response.assetClass = AssetClass.LIQUIDITY; response.assetSubClass = AssetSubClass.CRYPTOCURRENCY; @@ -115,12 +119,14 @@ export class FinancialModelingPrepService apikey: this.apiKey }); - const [assetProfile] = await fetch( - `${this.getUrl({ version: 'stable' })}/profile?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + const [assetProfile] = await this.fetchService + .fetch( + `${this.getUrl({ version: 'stable' })}/profile?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()); if (!assetProfile) { throw new AssetProfileDelistedError( @@ -143,12 +149,14 @@ export class FinancialModelingPrepService apikey: this.apiKey }); - const etfCountryWeightings = await fetch( - `${this.getUrl({ version: 'stable' })}/etf/country-weightings?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + const etfCountryWeightings = await this.fetchService + .fetch( + `${this.getUrl({ version: 'stable' })}/etf/country-weightings?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()); response.countries = etfCountryWeightings .filter(({ country: countryName }) => { @@ -174,12 +182,14 @@ export class FinancialModelingPrepService }; }); - const etfHoldings = await fetch( - `${this.getUrl({ version: 'stable' })}/etf/holdings?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + const etfHoldings = await this.fetchService + .fetch( + `${this.getUrl({ version: 'stable' })}/etf/holdings?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()); const sortedTopHoldings = etfHoldings .sort((a, b) => { @@ -193,23 +203,27 @@ export class FinancialModelingPrepService } ); - const [etfInformation] = await fetch( - `${this.getUrl({ version: 'stable' })}/etf/info?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + const [etfInformation] = await this.fetchService + .fetch( + `${this.getUrl({ version: 'stable' })}/etf/info?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()); if (etfInformation?.website) { response.url = etfInformation.website; } - const etfSectorWeightings = await fetch( - `${this.getUrl({ version: 'stable' })}/etf/sector-weightings?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + const etfSectorWeightings = await this.fetchService + .fetch( + `${this.getUrl({ version: 'stable' })}/etf/sector-weightings?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()); response.sectors = etfSectorWeightings.map( ({ sector, weightPercentage }) => { @@ -286,12 +300,14 @@ export class FinancialModelingPrepService [date: string]: DataProviderHistoricalResponse; } = {}; - const dividends = await fetch( - `${this.getUrl({ version: 'stable' })}/dividends?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + const dividends = await this.fetchService + .fetch( + `${this.getUrl({ version: 'stable' })}/dividends?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()); dividends .filter(({ date }) => { @@ -354,12 +370,14 @@ export class FinancialModelingPrepService to: format(currentTo, DATE_FORMAT) }); - const historical = await fetch( - `${this.getUrl({ version: 'stable' })}/historical-price-eod/full?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + const historical = await this.fetchService + .fetch( + `${this.getUrl({ version: 'stable' })}/historical-price-eod/full?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()); for (const { close, date } of historical) { if ( @@ -422,14 +440,17 @@ export class FinancialModelingPrepService symbolTarget: { in: symbols } } }), - fetch( - `${this.getUrl({ version: 'stable' })}/batch-quote-short?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then( - (res) => res.json() as unknown as { price: number; symbol: string }[] - ) + this.fetchService + .fetch( + `${this.getUrl({ version: 'stable' })}/batch-quote-short?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then( + (res) => + res.json() as unknown as { price: number; symbol: string }[] + ) ]); for (const { currency, symbolTarget } of assetProfileResolutions) { @@ -525,12 +546,14 @@ export class FinancialModelingPrepService isin: query.toUpperCase() }); - const result = await fetch( - `${this.getUrl({ version: 'stable' })}/search-isin?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + const result = await this.fetchService + .fetch( + `${this.getUrl({ version: 'stable' })}/search-isin?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()); await Promise.all( result.map(({ symbol }) => { @@ -558,18 +581,22 @@ export class FinancialModelingPrepService }); const [nameResults, symbolResults] = await Promise.all([ - fetch( - `${this.getUrl({ version: 'stable' })}/search-name?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()), - fetch( - `${this.getUrl({ version: 'stable' })}/search-symbol?${queryParams.toString()}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()) + this.fetchService + .fetch( + `${this.getUrl({ version: 'stable' })}/search-name?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()), + this.fetchService + .fetch( + `${this.getUrl({ version: 'stable' })}/search-symbol?${queryParams.toString()}`, + { + signal: AbortSignal.timeout(requestTimeout) + } + ) + .then((res) => res.json()) ]); const result = uniqBy( diff --git a/apps/api/src/services/data-provider/ghostfolio/ghostfolio.service.ts b/apps/api/src/services/data-provider/ghostfolio/ghostfolio.service.ts index 2b49e89c2..2f2601d5d 100644 --- a/apps/api/src/services/data-provider/ghostfolio/ghostfolio.service.ts +++ b/apps/api/src/services/data-provider/ghostfolio/ghostfolio.service.ts @@ -8,6 +8,7 @@ import { GetQuotesParams, GetSearchParams } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; import { PropertyService } from '@ghostfolio/api/services/property/property.service'; import { HEADER_KEY_TOKEN, @@ -38,6 +39,7 @@ export class GhostfolioService implements DataProviderInterface { public constructor( private readonly configurationService: ConfigurationService, + private readonly fetchService: FetchService, private readonly propertyService: PropertyService ) {} @@ -52,7 +54,7 @@ export class GhostfolioService implements DataProviderInterface { let assetProfile: DataProviderGhostfolioAssetProfileResponse; try { - const response = await fetch( + const response = await this.fetchService.fetch( `${this.URL}/v1/data-providers/ghostfolio/asset-profile/${symbol}`, { headers: await this.getRequestHeaders(), @@ -122,7 +124,7 @@ export class GhostfolioService implements DataProviderInterface { to: format(to, DATE_FORMAT) }); - const response = await fetch( + const response = await this.fetchService.fetch( `${this.URL}/v2/data-providers/ghostfolio/dividends/${symbol}?${queryParams.toString()}`, { headers: await this.getRequestHeaders(), @@ -174,7 +176,7 @@ export class GhostfolioService implements DataProviderInterface { to: format(to, DATE_FORMAT) }); - const response = await fetch( + const response = await this.fetchService.fetch( `${this.URL}/v2/data-providers/ghostfolio/historical/${symbol}?${queryParams.toString()}`, { headers: await this.getRequestHeaders(), @@ -245,7 +247,7 @@ export class GhostfolioService implements DataProviderInterface { symbols: symbols.join(',') }); - const response = await fetch( + const response = await this.fetchService.fetch( `${this.URL}/v2/data-providers/ghostfolio/quotes?${queryParams.toString()}`, { headers: await this.getRequestHeaders(), @@ -302,7 +304,7 @@ export class GhostfolioService implements DataProviderInterface { query }); - const response = await fetch( + const response = await this.fetchService.fetch( `${this.URL}/v2/data-providers/ghostfolio/lookup?${queryParams.toString()}`, { headers: await this.getRequestHeaders(), diff --git a/apps/api/src/services/data-provider/manual/manual.service.ts b/apps/api/src/services/data-provider/manual/manual.service.ts index 51e65e631..11e0aae6a 100644 --- a/apps/api/src/services/data-provider/manual/manual.service.ts +++ b/apps/api/src/services/data-provider/manual/manual.service.ts @@ -8,6 +8,7 @@ import { GetQuotesParams, GetSearchParams } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service'; import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service'; import { @@ -32,6 +33,7 @@ import { addDays, format, isBefore } from 'date-fns'; export class ManualService implements DataProviderInterface { public constructor( private readonly configurationService: ConfigurationService, + private readonly fetchService: FetchService, private readonly prismaService: PrismaService, private readonly symbolProfileService: SymbolProfileService ) {} @@ -292,7 +294,7 @@ export class ManualService implements DataProviderInterface { }): Promise { let locale = scraperConfiguration.locale; - const response = await fetch(scraperConfiguration.url, { + const response = await this.fetchService.fetch(scraperConfiguration.url, { headers: scraperConfiguration.headers as HeadersInit, signal: AbortSignal.timeout( this.configurationService.get('REQUEST_TIMEOUT') diff --git a/apps/api/src/services/data-provider/rapid-api/rapid-api.service.ts b/apps/api/src/services/data-provider/rapid-api/rapid-api.service.ts index d6bc8d0e4..22896cccc 100644 --- a/apps/api/src/services/data-provider/rapid-api/rapid-api.service.ts +++ b/apps/api/src/services/data-provider/rapid-api/rapid-api.service.ts @@ -7,6 +7,7 @@ import { GetQuotesParams, GetSearchParams } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; import { ghostfolioFearAndGreedIndexSymbol, ghostfolioFearAndGreedIndexSymbolStocks @@ -26,7 +27,8 @@ import { format } from 'date-fns'; @Injectable() export class RapidApiService implements DataProviderInterface { public constructor( - private readonly configurationService: ConfigurationService + private readonly configurationService: ConfigurationService, + private readonly fetchService: FetchService ) {} public canHandle() { @@ -142,9 +144,8 @@ export class RapidApiService implements DataProviderInterface { oneYearAgo: { value: number; valueText: string }; }> { try { - const { fgi } = await fetch( - `https://fear-and-greed-index.p.rapidapi.com/v1/fgi`, - { + const { fgi } = await this.fetchService + .fetch(`https://fear-and-greed-index.p.rapidapi.com/v1/fgi`, { headers: { useQueryString: 'true', 'x-rapidapi-host': 'fear-and-greed-index.p.rapidapi.com', @@ -153,8 +154,8 @@ export class RapidApiService implements DataProviderInterface { signal: AbortSignal.timeout( this.configurationService.get('REQUEST_TIMEOUT') ) - } - ).then((res) => res.json()); + }) + .then((res) => res.json()); return fgi; } catch (error) { diff --git a/apps/api/src/services/fetch/fetch.module.ts b/apps/api/src/services/fetch/fetch.module.ts new file mode 100644 index 000000000..16e6f5f5d --- /dev/null +++ b/apps/api/src/services/fetch/fetch.module.ts @@ -0,0 +1,11 @@ +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; +import { PropertyModule } from '@ghostfolio/api/services/property/property.module'; + +import { Module } from '@nestjs/common'; + +@Module({ + exports: [FetchService], + imports: [PropertyModule], + providers: [FetchService] +}) +export class FetchModule {} diff --git a/apps/api/src/services/fetch/fetch.service.ts b/apps/api/src/services/fetch/fetch.service.ts new file mode 100644 index 000000000..f32e56a1c --- /dev/null +++ b/apps/api/src/services/fetch/fetch.service.ts @@ -0,0 +1,205 @@ +import { redactPaths } from '@ghostfolio/api/helper/object.helper'; +import { PropertyService } from '@ghostfolio/api/services/property/property.service'; +import { + PROPERTY_API_KEY_OPENROUTER, + PROPERTY_OPENROUTER_MODEL, + PROPERTY_WEB_FETCH_ROUTES +} from '@ghostfolio/common/config'; + +import { Injectable, Logger, OnModuleInit } from '@nestjs/common'; +import { createOpenRouter } from '@openrouter/ai-sdk-provider'; +import { generateText, jsonSchema, tool } from 'ai'; +import ms from 'ms'; + +import { WebFetchRoute } from './interfaces/web-fetch-route.interface'; + +@Injectable() +export class FetchService implements OnModuleInit { + private static readonly REDACTED_QUERY_PARAM_NAMES = ['apikey', 'api_token']; + private static readonly WEB_FETCH_TIMEOUT = ms('30 seconds'); + + private webFetchRoutes: WebFetchRoute[] = []; + + public constructor(private readonly propertyService: PropertyService) {} + + public async onModuleInit() { + this.webFetchRoutes = + (await this.propertyService.getByKey( + PROPERTY_WEB_FETCH_ROUTES + )) ?? []; + } + + public async fetch(input: RequestInfo | URL, init?: RequestInit) { + const method = ( + init?.method ?? + (input instanceof Request ? input.method : undefined) ?? + 'GET' + ).toUpperCase(); + + const url = input instanceof Request ? input.url : input.toString(); + const urlRedacted = this.redactUrl(url); + + Logger.debug(`${method} ${urlRedacted}`, 'FetchService'); + + if (method === 'GET') { + const webFetchRoute = this.getMatchingWebFetchRoute(url); + + if (webFetchRoute) { + const response = await this.fetchViaWebFetchTool({ + url, + webFetchRoute + }); + + if (response) { + return response; + } + } + } + + try { + return await globalThis.fetch(input, init); + } catch (error) { + if (error instanceof Error) { + Logger.error( + `${method} ${urlRedacted} failed: [${error.name}] ${error.message}`, + 'FetchService' + ); + } else { + Logger.error( + `${method} ${urlRedacted} failed: ${String(error)}`, + 'FetchService' + ); + } + + throw error; + } + } + + private async fetchViaWebFetchTool({ + url, + webFetchRoute + }: { + url: string; + webFetchRoute: WebFetchRoute; + }) { + const [openRouterApiKey, openRouterModel] = await Promise.all([ + this.propertyService.getByKey(PROPERTY_API_KEY_OPENROUTER), + this.propertyService.getByKey(PROPERTY_OPENROUTER_MODEL) + ]); + + if (!openRouterApiKey || !openRouterModel) { + return undefined; + } + + try { + const openRouterService = createOpenRouter({ apiKey: openRouterApiKey }); + + const { sources, text } = await generateText({ + model: openRouterService.chat(openRouterModel), + prompt: [ + 'You have access to a web_fetch tool. You MUST call it to retrieve the URL below, do not answer from prior knowledge.', + 'Return the fetched response body exactly as received: raw body only, no commentary, no Markdown, and no code fences.', + `URL: ${url}` + ].join('\n'), + timeout: FetchService.WEB_FETCH_TIMEOUT, + tools: { + // Provider-defined tool: lets OpenRouter perform the actual web + // request server-side via its `web_fetch` engine. `id` and `args` + // are the OpenRouter-specific identifiers; the input schema is left + // open as the arguments are supplied by the model. + web_fetch: tool({ + args: { engine: 'openrouter' }, + id: 'openrouter.web_fetch', + inputSchema: jsonSchema({ + additionalProperties: true, + type: 'object' + }), + type: 'provider' + }) + } + }); + + const candidates = [ + ...(sources ?? []).map((source) => { + return source.providerMetadata?.openrouter?.content; + }), + text + ]; + + for (const candidate of candidates) { + if (typeof candidate !== 'string') { + continue; + } + + const body = candidate.trim(); + + if (!body) { + continue; + } + + if (webFetchRoute.responseContentType?.includes('application/json')) { + try { + JSON.parse(body); + } catch { + continue; + } + } + + Logger.debug( + `Routed ${this.redactUrl(url)} via web fetch tool`, + 'FetchService' + ); + + return new Response(body, { + headers: webFetchRoute.responseContentType + ? { 'content-type': webFetchRoute.responseContentType } + : undefined + }); + } + + return undefined; + } catch (error) { + Logger.error( + `Web fetch tool failed for ${this.redactUrl(url)}: ${ + error instanceof Error ? error.message : String(error) + }`, + 'FetchService' + ); + + return undefined; + } + } + + private getMatchingWebFetchRoute(url: string) { + try { + const { hostname } = new URL(url); + + return this.webFetchRoutes.find(({ domain }) => { + return hostname === domain || hostname.endsWith(`.${domain}`); + }); + } catch { + return undefined; + } + } + + private redactUrl(rawUrl: string): string { + try { + const url = new URL(rawUrl); + + const redacted = redactPaths({ + object: Object.fromEntries(url.searchParams), + paths: FetchService.REDACTED_QUERY_PARAM_NAMES + }); + + for (const [key, value] of Object.entries(redacted)) { + if (value === null) { + url.searchParams.set(key, '*******'); + } + } + + return url.toString(); + } catch { + return rawUrl; + } + } +} diff --git a/apps/api/src/services/fetch/interfaces/web-fetch-route.interface.ts b/apps/api/src/services/fetch/interfaces/web-fetch-route.interface.ts new file mode 100644 index 000000000..efff09398 --- /dev/null +++ b/apps/api/src/services/fetch/interfaces/web-fetch-route.interface.ts @@ -0,0 +1,19 @@ +/** + * Routes outgoing GET requests for a given domain through the OpenRouter + * `web_fetch` tool instead of a direct network request. + * + * Configured via the `WEB_FETCH_ROUTES` property as a JSON array, e.g. + * + * [ + * { + * "domain": "example.com", + * "responseContentType": "application/json" + * } + * ] + * + * Matches the domain itself and its subdomains (e.g. `api.example.com`). + */ +export interface WebFetchRoute { + domain: string; + responseContentType?: string; +} diff --git a/apps/api/src/services/queues/statistics-gathering/statistics-gathering.module.ts b/apps/api/src/services/queues/statistics-gathering/statistics-gathering.module.ts index 60b963c69..d6f6d5ccd 100644 --- a/apps/api/src/services/queues/statistics-gathering/statistics-gathering.module.ts +++ b/apps/api/src/services/queues/statistics-gathering/statistics-gathering.module.ts @@ -1,4 +1,5 @@ import { ConfigurationModule } from '@ghostfolio/api/services/configuration/configuration.module'; +import { FetchModule } from '@ghostfolio/api/services/fetch/fetch.module'; import { PropertyModule } from '@ghostfolio/api/services/property/property.module'; import { STATISTICS_GATHERING_QUEUE } from '@ghostfolio/common/config'; @@ -29,6 +30,7 @@ import { StatisticsGatheringService } from './statistics-gathering.service'; name: STATISTICS_GATHERING_QUEUE }), ConfigurationModule, + FetchModule, PropertyModule ], providers: [StatisticsGatheringProcessor, StatisticsGatheringService] diff --git a/apps/api/src/services/queues/statistics-gathering/statistics-gathering.processor.ts b/apps/api/src/services/queues/statistics-gathering/statistics-gathering.processor.ts index 1312d49ea..a523ef4f2 100644 --- a/apps/api/src/services/queues/statistics-gathering/statistics-gathering.processor.ts +++ b/apps/api/src/services/queues/statistics-gathering/statistics-gathering.processor.ts @@ -1,4 +1,5 @@ import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; +import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; import { PropertyService } from '@ghostfolio/api/services/property/property.service'; import { GATHER_STATISTICS_DOCKER_HUB_PULLS_PROCESS_JOB_NAME, @@ -28,6 +29,7 @@ import { format, subDays } from 'date-fns'; export class StatisticsGatheringProcessor { public constructor( private readonly configurationService: ConfigurationService, + private readonly fetchService: FetchService, private readonly propertyService: PropertyService ) {} @@ -126,15 +128,14 @@ export class StatisticsGatheringProcessor { private async countDockerHubPulls(): Promise { try { - const { pull_count } = (await fetch( - 'https://hub.docker.com/v2/repositories/ghostfolio/ghostfolio', - { + const { pull_count } = (await this.fetchService + .fetch('https://hub.docker.com/v2/repositories/ghostfolio/ghostfolio', { headers: { 'User-Agent': 'request' }, signal: AbortSignal.timeout( this.configurationService.get('REQUEST_TIMEOUT') ) - } - ).then((res) => res.json())) as { pull_count: number }; + }) + .then((res) => res.json())) as { pull_count: number }; return pull_count; } catch (error) { @@ -146,11 +147,13 @@ export class StatisticsGatheringProcessor { private async countGitHubContributors(): Promise { try { - const body = await fetch('https://github.com/ghostfolio/ghostfolio', { - signal: AbortSignal.timeout( - this.configurationService.get('REQUEST_TIMEOUT') - ) - }).then((res) => res.text()); + const body = await this.fetchService + .fetch('https://github.com/ghostfolio/ghostfolio', { + signal: AbortSignal.timeout( + this.configurationService.get('REQUEST_TIMEOUT') + ) + }) + .then((res) => res.text()); const $ = cheerio.load(body); @@ -174,15 +177,14 @@ export class StatisticsGatheringProcessor { private async countGitHubStargazers(): Promise { try { - const { stargazers_count } = (await fetch( - 'https://api.github.com/repos/ghostfolio/ghostfolio', - { + const { stargazers_count } = (await this.fetchService + .fetch('https://api.github.com/repos/ghostfolio/ghostfolio', { headers: { 'User-Agent': 'request' }, signal: AbortSignal.timeout( this.configurationService.get('REQUEST_TIMEOUT') ) - } - ).then((res) => res.json())) as { stargazers_count: number }; + }) + .then((res) => res.json())) as { stargazers_count: number }; return stargazers_count; } catch (error) { @@ -194,22 +196,24 @@ export class StatisticsGatheringProcessor { private async getUptime(monitorId: string): Promise { try { - const { data } = await fetch( - `https://uptime.betterstack.com/api/v2/monitors/${monitorId}/sla?from=${format( - subDays(new Date(), 90), - DATE_FORMAT - )}&to${format(new Date(), DATE_FORMAT)}`, - { - headers: { - [HEADER_KEY_TOKEN]: `Bearer ${this.configurationService.get( - 'API_KEY_BETTER_UPTIME' - )}` - }, - signal: AbortSignal.timeout( - this.configurationService.get('REQUEST_TIMEOUT') - ) - } - ).then((res) => res.json()); + const { data } = await this.fetchService + .fetch( + `https://uptime.betterstack.com/api/v2/monitors/${monitorId}/sla?from=${format( + subDays(new Date(), 90), + DATE_FORMAT + )}&to${format(new Date(), DATE_FORMAT)}`, + { + headers: { + [HEADER_KEY_TOKEN]: `Bearer ${this.configurationService.get( + 'API_KEY_BETTER_UPTIME' + )}` + }, + signal: AbortSignal.timeout( + this.configurationService.get('REQUEST_TIMEOUT') + ) + } + ) + .then((res) => res.json()); return data.attributes.availability / 100; } catch (error) { diff --git a/apps/client/src/app/components/admin-market-data/admin-market-data.component.ts b/apps/client/src/app/components/admin-market-data/admin-market-data.component.ts index 72a3c337a..805adf89d 100644 --- a/apps/client/src/app/components/admin-market-data/admin-market-data.component.ts +++ b/apps/client/src/app/components/admin-market-data/admin-market-data.component.ts @@ -4,7 +4,10 @@ import { DEFAULT_PAGE_SIZE, locale } from '@ghostfolio/common/config'; -import { getDateFormatString } from '@ghostfolio/common/helper'; +import { + canDeleteAssetProfile, + getDateFormatString +} from '@ghostfolio/common/helper'; import { AssetProfileIdentifier, Filter, @@ -15,6 +18,7 @@ import { AdminMarketDataItem } from '@ghostfolio/common/interfaces/admin-market- import { hasPermission, permissions } from '@ghostfolio/common/permissions'; import { GfSymbolPipe } from '@ghostfolio/common/pipes'; import { GfActivitiesFilterComponent } from '@ghostfolio/ui/activities-filter'; +import { GfFabComponent } from '@ghostfolio/ui/fab'; import { translate } from '@ghostfolio/ui/i18n'; import { GfPremiumIndicatorComponent } from '@ghostfolio/ui/premium-indicator'; import { AdminService, DataService } from '@ghostfolio/ui/services'; @@ -77,10 +81,10 @@ import { CreateAssetProfileDialogParams } from './create-asset-profile-dialog/in @Component({ changeDetection: ChangeDetectionStrategy.OnPush, - host: { class: 'has-fab' }, imports: [ CommonModule, GfActivitiesFilterComponent, + GfFabComponent, GfPremiumIndicatorComponent, GfSymbolPipe, GfValueComponent, @@ -101,6 +105,7 @@ import { CreateAssetProfileDialogParams } from './create-asset-profile-dialog/in }) export class GfAdminMarketDataComponent implements AfterViewInit, OnInit { protected readonly adminMarketDataService = inject(AdminMarketDataService); + protected readonly allFilters: Filter[] = [ ...Object.keys(AssetSubClass) .filter((assetSubClass) => { @@ -146,6 +151,7 @@ export class GfAdminMarketDataComponent implements AfterViewInit, OnInit { type: 'PRESET_ID' as Filter['type'] } ]; + protected readonly canDeleteAssetProfile = canDeleteAssetProfile; protected dataSource = new MatTableDataSource(); protected defaultDateFormat: string; protected readonly displayedColumns: string[] = []; diff --git a/apps/client/src/app/components/admin-market-data/admin-market-data.html b/apps/client/src/app/components/admin-market-data/admin-market-data.html index e2c6d1a87..63d425513 100644 --- a/apps/client/src/app/components/admin-market-data/admin-market-data.html +++ b/apps/client/src/app/components/admin-market-data/admin-market-data.html @@ -52,7 +52,7 @@ @if ( - adminMarketDataService.hasPermissionToDeleteAssetProfile({ + canDeleteAssetProfile({ activitiesCount: element.activitiesCount, isBenchmark: element.isBenchmark, symbol: element.symbol, @@ -271,7 +271,7 @@