Browse Source

fix(api): improve type safety in symbol, symbol profile, and benchmark services

pull/8404/head
KenTandrian 3 weeks ago
parent
commit
27bfd3b0a7
  1. 29
      apps/api/src/services/benchmark/benchmark.service.ts
  2. 36
      apps/api/src/services/symbol-profile/symbol-profile.service.ts
  3. 10
      apps/api/src/services/symbol/symbol.service.ts

29
apps/api/src/services/benchmark/benchmark.service.ts

@ -16,7 +16,8 @@ import {
AssetProfileIdentifier, AssetProfileIdentifier,
Benchmark, Benchmark,
BenchmarkProperty, BenchmarkProperty,
BenchmarkResponse BenchmarkResponse,
EnhancedAssetProfile
} from '@ghostfolio/common/interfaces'; } from '@ghostfolio/common/interfaces';
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
@ -88,18 +89,20 @@ export class BenchmarkService {
this.CACHE_KEY_BENCHMARKS this.CACHE_KEY_BENCHMARKS
); );
const { benchmarks, expiration }: BenchmarkValue = if (cachedBenchmarkValue) {
JSON.parse(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))) { if (isPast(new Date(expiration))) {
this.calculateAndCacheBenchmarks({ this.calculateAndCacheBenchmarks({
enableSharing enableSharing
}); });
} }
return benchmarks; return benchmarks;
}
} catch {} } catch {}
} }
@ -108,7 +111,9 @@ export class BenchmarkService {
public async getBenchmarkAssetProfiles({ public async getBenchmarkAssetProfiles({
enableSharing = false enableSharing = false
} = {}): Promise<Partial<SymbolProfile>[]> { } = {}): Promise<
Pick<EnhancedAssetProfile, 'dataSource' | 'id' | 'name' | 'symbol'>[]
> {
const symbolProfileIds: string[] = ( const symbolProfileIds: string[] = (
(await this.propertyService.getByKey<BenchmarkProperty[]>( (await this.propertyService.getByKey<BenchmarkProperty[]>(
PROPERTY_BENCHMARKS PROPERTY_BENCHMARKS
@ -293,7 +298,7 @@ export class BenchmarkService {
name: benchmarkAssetProfiles[index].name, name: benchmarkAssetProfiles[index].name,
performances: { performances: {
allTimeHigh: { allTimeHigh: {
date: allTimeHigh?.date, date: allTimeHigh?.date as Date,
performancePercent: this.getPerformancePercentFromAllTimeHigh( performancePercent: this.getPerformancePercentFromAllTimeHigh(
performancePercentFromAllTimeHigh performancePercentFromAllTimeHigh
) )

36
apps/api/src/services/symbol-profile/symbol-profile.service.ts

@ -21,6 +21,7 @@ import {
SymbolProfile SymbolProfile
} from '@prisma/client'; } from '@prisma/client';
import { continents, countries } from 'countries-list'; import { continents, countries } from 'countries-list';
import { omit } from 'lodash-es';
@Injectable() @Injectable()
export class SymbolProfileService { export class SymbolProfileService {
@ -284,7 +285,7 @@ export class SymbolProfileService {
activities?: { activities?: {
date: Date; date: Date;
}[]; }[];
assetProfileOverrides: AssetProfileOverrides; assetProfileOverrides: AssetProfileOverrides | null;
})[] })[]
): EnhancedAssetProfile[] { ): EnhancedAssetProfile[] {
return symbolProfiles.map((symbolProfile) => { return symbolProfiles.map((symbolProfile) => {
@ -293,13 +294,19 @@ export class SymbolProfileService {
symbolProfile.assetProfileOverrides symbolProfile.assetProfileOverrides
); );
const item = { const symbolProfileWithoutExtras = omit(symbolProfileWithOverrides, [
...symbolProfileWithOverrides, '_count',
activitiesCount: 0, 'activities',
'assetProfileOverrides'
]);
return {
...symbolProfileWithoutExtras,
activitiesCount: symbolProfile._count.activities,
countries: this.getCountries( countries: this.getCountries(
symbolProfileWithOverrides?.countries as unknown as Prisma.JsonArray symbolProfileWithOverrides?.countries as unknown as Prisma.JsonArray
), ),
dateOfFirstActivity: undefined as Date, dateOfFirstActivity: symbolProfile.activities?.[0]?.date,
holdings: this.getHoldings( holdings: this.getHoldings(
symbolProfileWithOverrides?.holdings as unknown as Prisma.JsonArray symbolProfileWithOverrides?.holdings as unknown as Prisma.JsonArray
), ),
@ -310,19 +317,8 @@ export class SymbolProfileService {
symbolProfileWithOverrides?.sectors as unknown as Prisma.JsonArray symbolProfileWithOverrides?.sectors as unknown as Prisma.JsonArray
), ),
symbolMapping: this.getSymbolMapping(symbolProfileWithOverrides), symbolMapping: this.getSymbolMapping(symbolProfileWithOverrides),
watchedByCount: 0 watchedByCount: symbolProfile._count.watchedBy ?? 0
}; } as EnhancedAssetProfile;
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;
}); });
} }
@ -361,7 +357,7 @@ export class SymbolProfileService {
private getScraperConfiguration( private getScraperConfiguration(
symbolProfile: SymbolProfile symbolProfile: SymbolProfile
): ScraperConfiguration { ): ScraperConfiguration | undefined {
const scraperConfiguration = const scraperConfiguration =
symbolProfile.scraperConfiguration as Prisma.JsonObject; symbolProfile.scraperConfiguration as Prisma.JsonObject;
@ -378,7 +374,7 @@ export class SymbolProfileService {
}; };
} }
return null; return undefined;
} }
private getSectors(aSectors: Prisma.JsonArray = []): Sector[] { private getSectors(aSectors: Prisma.JsonArray = []): Sector[] {

10
apps/api/src/services/symbol/symbol.service.ts

@ -40,8 +40,8 @@ export class SymbolService {
dataGatheringItem: DataGatheringItem; dataGatheringItem: DataGatheringItem;
includeHistoricalData?: number; includeHistoricalData?: number;
useIntradayData?: boolean; useIntradayData?: boolean;
}): Promise<SymbolItem> { }): Promise<SymbolItem | undefined> {
let currency: string; let currency = '';
let marketPrice: number; let marketPrice: number;
if (useIntradayData) { if (useIntradayData) {
@ -63,7 +63,7 @@ export class SymbolService {
if (dataGatheringItem.dataSource && marketPrice >= 0) { if (dataGatheringItem.dataSource && marketPrice >= 0) {
let historicalData: HistoricalDataItem[] = []; let historicalData: HistoricalDataItem[] = [];
if (includeHistoricalData > 0) { if (includeHistoricalData && includeHistoricalData > 0) {
const days = includeHistoricalData; const days = includeHistoricalData;
const marketData = await this.marketDataService.getRange({ const marketData = await this.marketDataService.getRange({
@ -168,10 +168,10 @@ export class SymbolService {
fearAndGreedIndex: { fearAndGreedIndex: {
CRYPTOCURRENCIES: { CRYPTOCURRENCIES: {
...marketDataFearAndGreedIndexCryptocurrencies ...marketDataFearAndGreedIndexCryptocurrencies
}, } as SymbolItem,
STOCKS: { STOCKS: {
...marketDataFearAndGreedIndexStocks ...marketDataFearAndGreedIndexStocks
} } as SymbolItem
} }
}; };
} }

Loading…
Cancel
Save