Browse Source

Task/improve type safety in data provider and enhancers (#8428)

Improve type safety
pull/8430/merge
Kenrick Tandrian 2 weeks ago
committed by GitHub
parent
commit
aea75a89a3
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 8
      apps/api/src/services/data-provider/data-enhancer/data-enhancer.service.ts
  2. 3
      apps/api/src/services/data-provider/data-enhancer/openfigi/openfigi.service.ts
  3. 3
      apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts
  4. 67
      apps/api/src/services/data-provider/data-enhancer/yahoo-finance/yahoo-finance.service.ts
  5. 23
      apps/api/src/services/data-provider/data-provider.service.ts
  6. 2
      apps/api/src/services/data-provider/interfaces/data-enhancer.interface.ts
  7. 4
      apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts
  8. 4
      libs/common/src/lib/helper.ts

8
apps/api/src/services/data-provider/data-enhancer/data-enhancer.service.ts

@ -25,13 +25,19 @@ export class DataEnhancerService {
}
try {
const testSymbol = dataEnhancer.getTestSymbol();
if (!testSymbol) {
return false;
}
const assetProfile = await dataEnhancer.enhance({
requestTimeout: ms('30 seconds'),
response: {
assetClass: 'EQUITY',
assetSubClass: 'ETF'
},
symbol: dataEnhancer.getTestSymbol()
symbol: testSymbol
});
if (

3
apps/api/src/services/data-provider/data-enhancer/openfigi/openfigi.service.ts

@ -28,7 +28,8 @@ export class OpenFigiDataEnhancerService implements DataEnhancerInterface {
!(
response.assetClass === 'EQUITY' &&
(response.assetSubClass === 'ETF' || response.assetSubClass === 'STOCK')
)
) ||
!response.dataSource
) {
return response;
}

3
apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts

@ -66,7 +66,8 @@ export class TrackinsightDataEnhancerService implements DataEnhancerInterface {
}): Promise<Partial<SymbolProfile>> {
if (!(
response.assetClass === 'EQUITY' &&
['ETF', 'MUTUALFUND'].includes(response.assetSubClass)
(response.assetSubClass === 'ETF' ||
response.assetSubClass === 'MUTUALFUND')
)) {
return response;
}

67
apps/api/src/services/data-provider/data-enhancer/yahoo-finance/yahoo-finance.service.ts

@ -107,33 +107,46 @@ export class YahooFinanceDataEnhancerService implements DataEnhancerInterface {
response: Partial<SymbolProfile>;
symbol: string;
}): Promise<Partial<SymbolProfile>> {
if (response.dataSource !== 'YAHOO' && !response.isin) {
const { isin } = response;
if (response.dataSource !== 'YAHOO' && !isin) {
return response;
}
try {
let yahooSymbol: string;
let yahooSymbol: string | undefined;
if (response.dataSource === 'YAHOO') {
yahooSymbol = symbol;
} else {
const { quotes } = await this.yahooFinance.search(response.isin);
yahooSymbol = quotes[0].symbol as string;
}
} else if (isin) {
const { quotes } = await this.yahooFinance.search(isin);
const firstQuote = quotes?.[0];
const { countries, sectors, url } =
await this.getAssetProfile(yahooSymbol);
if ((countries as unknown as Prisma.JsonArray)?.length > 0) {
response.countries = countries;
if (firstQuote?.isYahooFinance) {
yahooSymbol = firstQuote.symbol;
}
}
if ((sectors as unknown as Prisma.JsonArray)?.length > 0) {
response.sectors = sectors;
if (!yahooSymbol) {
return response;
}
if (url) {
response.url = url;
const assetProfile = await this.getAssetProfile(yahooSymbol);
if (assetProfile) {
const { countries, sectors, url } = assetProfile;
if ((countries as unknown as Prisma.JsonArray)?.length > 0) {
response.countries = countries;
}
if ((sectors as unknown as Prisma.JsonArray)?.length > 0) {
response.sectors = sectors;
}
if (url) {
response.url = url;
}
}
} catch (error) {
this.logger.error(error.message);
@ -175,8 +188,8 @@ export class YahooFinanceDataEnhancerService implements DataEnhancerInterface {
public async getAssetProfile(
aSymbol: string
): Promise<Partial<SymbolProfile>> {
let response: Partial<SymbolProfile> = {};
): Promise<Partial<SymbolProfile> | undefined> {
let response: Partial<SymbolProfile> | undefined = {};
try {
let symbol = aSymbol;
@ -199,6 +212,10 @@ export class YahooFinanceDataEnhancerService implements DataEnhancerInterface {
modules: ['price', 'summaryProfile', 'topHoldings']
});
if (!assetProfile?.price?.symbol) {
throw new Error(`No price data found for ${symbol}`);
}
const { assetClass, assetSubClass } = this.parseAssetClass({
quoteType: assetProfile.price.quoteType,
shortName: assetProfile.price.shortName
@ -206,19 +223,19 @@ export class YahooFinanceDataEnhancerService implements DataEnhancerInterface {
response.assetClass = assetClass;
response.assetSubClass = assetSubClass;
response.currency = assetProfile.price?.currency;
response.currency = assetProfile.price.currency;
response.dataSource = this.getName();
response.name = this.formatName({
longName: assetProfile.price?.longName,
quoteType: assetProfile.price?.quoteType,
shortName: assetProfile.price?.shortName,
symbol: assetProfile.price?.symbol
longName: assetProfile.price.longName,
quoteType: assetProfile.price.quoteType,
shortName: assetProfile.price.shortName,
symbol: assetProfile.price.symbol
});
response.symbol = this.convertFromYahooFinanceSymbol(
assetProfile.price.symbol
);
if (['ETF', 'MUTUALFUND'].includes(assetSubClass)) {
if (assetSubClass === 'ETF' || assetSubClass === 'MUTUALFUND') {
response.holdings =
assetProfile.topHoldings?.holdings
?.filter(({ holdingName }) => {
@ -301,8 +318,8 @@ export class YahooFinanceDataEnhancerService implements DataEnhancerInterface {
quoteType,
shortName
}: {
quoteType: string;
shortName: string;
quoteType?: string;
shortName?: string | null;
}): {
assetClass: AssetClass;
assetSubClass: AssetSubClass;

23
apps/api/src/services/data-provider/data-provider.service.ts

@ -123,7 +123,7 @@ export class DataProviderService implements OnModuleInit {
promises.push(
promise.then((assetProfile) => {
if (isCurrency(assetProfile?.currency)) {
if (assetProfile?.currency && isCurrency(assetProfile.currency)) {
response[
getAssetProfileIdentifier({
symbol,
@ -255,14 +255,21 @@ export class DataProviderService implements OnModuleInit {
] of activitiesDto.entries()) {
const activityPath =
maxActivitiesToImport === 1 ? 'activity' : `activities.${index}`;
if (!dataSource || !dataSources.includes(dataSource)) {
throw new ImportValidationError(
`${activityPath}.dataSource ("${dataSource}") is not valid`
);
}
const maskedDataSource = getMaskedGhostfolioDataSource({
dataSource,
ghostfolioDataSources
});
if (!dataSources.includes(dataSource)) {
if (!symbol) {
throw new ImportValidationError(
`${activityPath}.dataSource ("${dataSource}") is not valid`
`${activityPath}.symbol ("${symbol}") is not valid`
);
}
@ -315,7 +322,7 @@ export class DataProviderService implements OnModuleInit {
if (
(dataSource === DataSource.MANUAL && type === 'BUY') ||
assetProfileInImport?.dataSource === DataSource.MANUAL ||
NON_INVESTMENT_ACTIVITY_TYPES.includes(type)
(type && NON_INVESTMENT_ACTIVITY_TYPES.includes(type))
) {
assetProfiles[assetProfileIdentifier] = {
...omit(assetProfileInImport ?? {}, [
@ -951,7 +958,7 @@ export class DataProviderService implements OnModuleInit {
lookupItem.dataProviderInfo.isPremium = false;
}
lookupItem.name = formatAssetProfileName(lookupItem);
lookupItem.name = formatAssetProfileName(lookupItem) ?? lookupItem.name;
return lookupItem;
})
@ -972,9 +979,9 @@ export class DataProviderService implements OnModuleInit {
dataSource,
symbol,
userId
}: AssetProfileIdentifier & { userId: string }): Promise<
Partial<SymbolProfile>
> {
}: AssetProfileIdentifier & {
userId: string;
}): Promise<Partial<SymbolProfile> | null> {
return this.prismaService.symbolProfile.findFirst({
select: {
assetClass: true,

2
apps/api/src/services/data-provider/interfaces/data-enhancer.interface.ts

@ -13,5 +13,5 @@ export interface DataEnhancerInterface {
getName(): string;
getTestSymbol(): string;
getTestSymbol(): string | undefined;
}

4
apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts

@ -64,7 +64,9 @@ export class YahooFinanceService implements DataProviderInterface {
public async getAssetProfile({
symbol
}: GetAssetProfileParams): Promise<Partial<SymbolProfile>> {
return this.yahooFinanceDataEnhancerService.getAssetProfile(symbol);
return (
(await this.yahooFinanceDataEnhancerService.getAssetProfile(symbol)) ?? {}
);
}
public getDataProviderInfo(): DataProviderInfo {

4
libs/common/src/lib/helper.ts

@ -334,8 +334,8 @@ export function formatAssetProfileName({
assetSubClass,
name
}: {
assetSubClass?: AssetSubClass;
name?: string;
assetSubClass?: AssetSubClass | null;
name?: string | null;
}) {
if (assetSubClass === 'CRYPTOCURRENCY') {
// Remove DEFAULT_CURRENCY at the end of cryptocurrency names

Loading…
Cancel
Save