diff --git a/apps/api/src/helper/country.helper.ts b/apps/api/src/helper/country.helper.ts new file mode 100644 index 000000000..9d14d8778 --- /dev/null +++ b/apps/api/src/helper/country.helper.ts @@ -0,0 +1,17 @@ +import { countries } from 'countries-list'; + +export function getCountryCodeByName({ + aliases = {}, + name +}: { + aliases?: Record; + name: string; +}): string { + for (const [code, country] of Object.entries(countries)) { + if (country.name === name || country.name === aliases[name]) { + return code; + } + } + + return undefined; +} diff --git a/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts b/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts index a74aaeb46..3d42de443 100644 --- a/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts +++ b/apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts @@ -1,3 +1,4 @@ +import { getCountryCodeByName } from '@ghostfolio/api/helper/country.helper'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { DataEnhancerInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-enhancer.interface'; import { FetchService } from '@ghostfolio/api/services/fetch/fetch.service'; @@ -7,12 +8,9 @@ import { Sector } from '@ghostfolio/common/interfaces/sector.interface'; import { Injectable, Logger } from '@nestjs/common'; import { SymbolProfile } from '@prisma/client'; -import { countries } from 'countries-list'; @Injectable() export class TrackinsightDataEnhancerService implements DataEnhancerInterface { - private readonly logger = new Logger(TrackinsightDataEnhancerService.name); - private static baseUrl = 'https://www.trackinsight.com/data-api'; private static countriesMapping = { 'Russian Federation': 'Russia', @@ -26,6 +24,8 @@ export class TrackinsightDataEnhancerService implements DataEnhancerInterface { 'Information Technology': 'Technology' }; + private readonly logger = new Logger(TrackinsightDataEnhancerService.name); + public constructor( private readonly configurationService: ConfigurationService, private readonly fetchService: FetchService @@ -117,21 +117,11 @@ export class TrackinsightDataEnhancerService implements DataEnhancerInterface { for (const [name, value] of Object.entries( holdings?.countries ?? {} )) { - let countryCode: string; - - for (const [code, country] of Object.entries(countries)) { - if ( - country.name === name || - country.name === - TrackinsightDataEnhancerService.countriesMapping[name] - ) { - countryCode = code; - break; - } - } - response.countries.push({ - code: countryCode, + code: getCountryCodeByName({ + name, + aliases: TrackinsightDataEnhancerService.countriesMapping + }), weight: value.weight }); } diff --git a/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts b/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts index 80eeadeb0..157285278 100644 --- a/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts +++ b/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts @@ -1,3 +1,4 @@ +import { getCountryCodeByName } from '@ghostfolio/api/helper/country.helper'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { CryptocurrencyService } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.service'; import { AssetProfileDelistedError } from '@ghostfolio/api/services/data-provider/errors/asset-profile-delisted.error'; @@ -33,7 +34,6 @@ import { SymbolProfile } from '@prisma/client'; import { isISIN } from 'class-validator'; -import { countries } from 'countries-list'; import { addDays, addYears, @@ -49,14 +49,14 @@ import { uniqBy } from 'lodash'; export class FinancialModelingPrepService implements DataProviderInterface, OnModuleInit { - private readonly logger = new Logger(FinancialModelingPrepService.name); - private static countriesMapping = { 'Korea (the Republic of)': 'South Korea', 'Russian Federation': 'Russia', 'Taiwan (Province of China)': 'Taiwan' }; + private readonly logger = new Logger(FinancialModelingPrepService.name); + private apiKey: string; public constructor( @@ -165,21 +165,11 @@ export class FinancialModelingPrepService return countryName.toLowerCase() !== 'other'; }) .map(({ country: countryName, weightPercentage }) => { - let countryCode: string; - - for (const [code, country] of Object.entries(countries)) { - if ( - country.name === countryName || - country.name === - FinancialModelingPrepService.countriesMapping[countryName] - ) { - countryCode = code; - break; - } - } - return { - code: countryCode, + code: getCountryCodeByName({ + aliases: FinancialModelingPrepService.countriesMapping, + name: countryName + }), weight: parseFloat(weightPercentage.slice(0, -1)) / 100 }; });