From 27bfd3b0a73f1e99b83c6947b4a4045ee97700d7 Mon Sep 17 00:00:00 2001 From: KenTandrian Date: Thu, 5 Nov 2026 03:43:22 +0000 Subject: [PATCH] fix(api): improve type safety in symbol, symbol profile, and benchmark services --- .../services/benchmark/benchmark.service.ts | 29 ++++++++------- .../symbol-profile/symbol-profile.service.ts | 36 +++++++++---------- .../api/src/services/symbol/symbol.service.ts | 10 +++--- 3 files changed, 38 insertions(+), 37 deletions(-) diff --git a/apps/api/src/services/benchmark/benchmark.service.ts b/apps/api/src/services/benchmark/benchmark.service.ts index e0120bf127..6b9b1e6bcc 100644 --- a/apps/api/src/services/benchmark/benchmark.service.ts +++ b/apps/api/src/services/benchmark/benchmark.service.ts @@ -16,7 +16,8 @@ import { AssetProfileIdentifier, Benchmark, BenchmarkProperty, - BenchmarkResponse + BenchmarkResponse, + EnhancedAssetProfile } from '@ghostfolio/common/interfaces'; import { Injectable, Logger } from '@nestjs/common'; @@ -88,18 +89,20 @@ export class BenchmarkService { this.CACHE_KEY_BENCHMARKS ); - const { benchmarks, expiration }: BenchmarkValue = - JSON.parse(cachedBenchmarkValue); + if (cachedBenchmarkValue) { + const { benchmarks, expiration }: BenchmarkValue = + JSON.parse(cachedBenchmarkValue); - this.logger.debug('Fetched benchmarks from cache'); + this.logger.debug('Fetched benchmarks from cache'); - if (isPast(new Date(expiration))) { - this.calculateAndCacheBenchmarks({ - enableSharing - }); - } + if (isPast(new Date(expiration))) { + this.calculateAndCacheBenchmarks({ + enableSharing + }); + } - return benchmarks; + return benchmarks; + } } catch {} } @@ -108,7 +111,9 @@ export class BenchmarkService { public async getBenchmarkAssetProfiles({ enableSharing = false - } = {}): Promise[]> { + } = {}): Promise< + Pick[] + > { const symbolProfileIds: string[] = ( (await this.propertyService.getByKey( PROPERTY_BENCHMARKS @@ -293,7 +298,7 @@ export class BenchmarkService { name: benchmarkAssetProfiles[index].name, performances: { allTimeHigh: { - date: allTimeHigh?.date, + date: allTimeHigh?.date as Date, performancePercent: this.getPerformancePercentFromAllTimeHigh( performancePercentFromAllTimeHigh ) diff --git a/apps/api/src/services/symbol-profile/symbol-profile.service.ts b/apps/api/src/services/symbol-profile/symbol-profile.service.ts index 98a1ce1cba..91388d3fb0 100644 --- a/apps/api/src/services/symbol-profile/symbol-profile.service.ts +++ b/apps/api/src/services/symbol-profile/symbol-profile.service.ts @@ -21,6 +21,7 @@ import { SymbolProfile } from '@prisma/client'; import { continents, countries } from 'countries-list'; +import { omit } from 'lodash-es'; @Injectable() export class SymbolProfileService { @@ -284,7 +285,7 @@ export class SymbolProfileService { activities?: { date: Date; }[]; - assetProfileOverrides: AssetProfileOverrides; + assetProfileOverrides: AssetProfileOverrides | null; })[] ): EnhancedAssetProfile[] { return symbolProfiles.map((symbolProfile) => { @@ -293,13 +294,19 @@ export class SymbolProfileService { symbolProfile.assetProfileOverrides ); - const item = { - ...symbolProfileWithOverrides, - activitiesCount: 0, + const symbolProfileWithoutExtras = omit(symbolProfileWithOverrides, [ + '_count', + 'activities', + 'assetProfileOverrides' + ]); + + return { + ...symbolProfileWithoutExtras, + activitiesCount: symbolProfile._count.activities, countries: this.getCountries( symbolProfileWithOverrides?.countries as unknown as Prisma.JsonArray ), - dateOfFirstActivity: undefined as Date, + dateOfFirstActivity: symbolProfile.activities?.[0]?.date, holdings: this.getHoldings( symbolProfileWithOverrides?.holdings as unknown as Prisma.JsonArray ), @@ -310,19 +317,8 @@ export class SymbolProfileService { symbolProfileWithOverrides?.sectors as unknown as Prisma.JsonArray ), symbolMapping: this.getSymbolMapping(symbolProfileWithOverrides), - watchedByCount: 0 - }; - - item.activitiesCount = symbolProfile._count.activities; - item.watchedByCount = symbolProfile._count.watchedBy ?? 0; - delete item._count; - - item.dateOfFirstActivity = symbolProfile.activities?.[0]?.date; - delete item.activities; - - delete item.assetProfileOverrides; - - return item; + watchedByCount: symbolProfile._count.watchedBy ?? 0 + } as EnhancedAssetProfile; }); } @@ -361,7 +357,7 @@ export class SymbolProfileService { private getScraperConfiguration( symbolProfile: SymbolProfile - ): ScraperConfiguration { + ): ScraperConfiguration | undefined { const scraperConfiguration = symbolProfile.scraperConfiguration as Prisma.JsonObject; @@ -378,7 +374,7 @@ export class SymbolProfileService { }; } - return null; + return undefined; } private getSectors(aSectors: Prisma.JsonArray = []): Sector[] { diff --git a/apps/api/src/services/symbol/symbol.service.ts b/apps/api/src/services/symbol/symbol.service.ts index 856ed4bf01..1452bb62e7 100644 --- a/apps/api/src/services/symbol/symbol.service.ts +++ b/apps/api/src/services/symbol/symbol.service.ts @@ -40,8 +40,8 @@ export class SymbolService { dataGatheringItem: DataGatheringItem; includeHistoricalData?: number; useIntradayData?: boolean; - }): Promise { - let currency: string; + }): Promise { + let currency = ''; let marketPrice: number; if (useIntradayData) { @@ -63,7 +63,7 @@ export class SymbolService { if (dataGatheringItem.dataSource && marketPrice >= 0) { let historicalData: HistoricalDataItem[] = []; - if (includeHistoricalData > 0) { + if (includeHistoricalData && includeHistoricalData > 0) { const days = includeHistoricalData; const marketData = await this.marketDataService.getRange({ @@ -168,10 +168,10 @@ export class SymbolService { fearAndGreedIndex: { CRYPTOCURRENCIES: { ...marketDataFearAndGreedIndexCryptocurrencies - }, + } as SymbolItem, STOCKS: { ...marketDataFearAndGreedIndexStocks - } + } as SymbolItem } }; }