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,
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<Partial<SymbolProfile>[]> {
} = {}): Promise<
Pick<EnhancedAssetProfile, 'dataSource' | 'id' | 'name' | 'symbol'>[]
> {
const symbolProfileIds: string[] = (
(await this.propertyService.getByKey<BenchmarkProperty[]>(
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
)

36
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[] {

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

@ -40,8 +40,8 @@ export class SymbolService {
dataGatheringItem: DataGatheringItem;
includeHistoricalData?: number;
useIntradayData?: boolean;
}): Promise<SymbolItem> {
let currency: string;
}): Promise<SymbolItem | undefined> {
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
}
};
}

Loading…
Cancel
Save