Browse Source

Task/refactor country code logic in data provider services (#6993)

Refactor country code logic
pull/6902/head
Thomas Kaul 3 days ago
committed by GitHub
parent
commit
bf7409ec20
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 17
      apps/api/src/helper/country.helper.ts
  2. 24
      apps/api/src/services/data-provider/data-enhancer/trackinsight/trackinsight.service.ts
  3. 24
      apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts

17
apps/api/src/helper/country.helper.ts

@ -0,0 +1,17 @@
import { countries } from 'countries-list';
export function getCountryCodeByName({
aliases = {},
name
}: {
aliases?: Record<string, string>;
name: string;
}): string {
for (const [code, country] of Object.entries(countries)) {
if (country.name === name || country.name === aliases[name]) {
return code;
}
}
return undefined;
}

24
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<any>(
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
});
}

24
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
};
});

Loading…
Cancel
Save