Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

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

Thomas Kaulbf7409ec2018
diff --git a/apps/api/src/helper/country.helper.ts b/apps/api/src/helper/country.helper.ts
new file mode 100644
index 0000000000..9d14d87785
--- /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<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;
+}
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 a74aaeb46e..3d42de4437 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<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
});
}
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 80eeadeb0a..1572852780 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
};
});