From fb3c5855b7ca62ec75aa1f18553d358b21038dd9 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Mon, 23 Aug 2021 21:22:44 +0200 Subject: [PATCH] Automate countries for stocks in symbol profile data --- .../src/services/data-gathering.service.ts | 4 +++- .../yahoo-finance/interfaces/interfaces.ts | 1 + .../yahoo-finance/yahoo-finance.service.ts | 20 +++++++++++++++++++ .../api/src/services/interfaces/interfaces.ts | 1 + 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/apps/api/src/services/data-gathering.service.ts b/apps/api/src/services/data-gathering.service.ts index fcf9c4eba..c4a3fd0ec 100644 --- a/apps/api/src/services/data-gathering.service.ts +++ b/apps/api/src/services/data-gathering.service.ts @@ -136,13 +136,14 @@ export class DataGatheringService { for (const [ symbol, - { assetClass, assetSubClass, currency, dataSource, name } + { assetClass, assetSubClass, countries, currency, dataSource, name } ] of Object.entries(currentData)) { try { await this.prismaService.symbolProfile.upsert({ create: { assetClass, assetSubClass, + countries, currency, dataSource, name, @@ -151,6 +152,7 @@ export class DataGatheringService { update: { assetClass, assetSubClass, + countries, currency, name }, diff --git a/apps/api/src/services/data-provider/yahoo-finance/interfaces/interfaces.ts b/apps/api/src/services/data-provider/yahoo-finance/interfaces/interfaces.ts index 3594f7e02..d41a43d39 100644 --- a/apps/api/src/services/data-provider/yahoo-finance/interfaces/interfaces.ts +++ b/apps/api/src/services/data-provider/yahoo-finance/interfaces/interfaces.ts @@ -25,6 +25,7 @@ export interface IYahooFinancePrice { } export interface IYahooFinanceSummaryProfile { + country?: string; industry?: string; sector?: string; website?: string; diff --git a/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts b/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts index 3b59271aa..a3665db1d 100644 --- a/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts +++ b/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts @@ -16,6 +16,7 @@ import { } from '@prisma/client'; import * as bent from 'bent'; import Big from 'big.js'; +import { countries } from 'countries-list'; import { format } from 'date-fns'; import * as yahooFinance from 'yahoo-finance'; @@ -92,6 +93,25 @@ export class YahooFinanceService implements DataProviderInterface { .toNumber(); } + // Add country if stock and available + if ( + assetSubClass === AssetSubClass.STOCK && + value.summaryProfile?.country + ) { + try { + const [code] = Object.entries(countries).find( + ([, currentCountry]) => { + return currentCountry.name === value.summaryProfile?.country; + } + ); + + if (code) { + response[symbol].countries = [{ code, weight: 1 }]; + } + } catch {} + } + + // Add url if available const url = value.summaryProfile?.website; if (url) { response[symbol].url = url; diff --git a/apps/api/src/services/interfaces/interfaces.ts b/apps/api/src/services/interfaces/interfaces.ts index fce1e94d1..eef8d2dc0 100644 --- a/apps/api/src/services/interfaces/interfaces.ts +++ b/apps/api/src/services/interfaces/interfaces.ts @@ -37,6 +37,7 @@ export interface IDataProviderHistoricalResponse { export interface IDataProviderResponse { assetClass?: AssetClass; assetSubClass?: AssetSubClass; + countries?: { code: string; weight: number }[]; currency: Currency; dataSource: DataSource; exchange?: string;