Browse Source

Add support for custom cryptocurrencies defined in database

pull/6344/head
Thomas Kaul 1 month ago
parent
commit
61dc1adb83
  1. 7
      apps/api/src/services/cryptocurrency/cryptocurrency.module.ts
  2. 38
      apps/api/src/services/cryptocurrency/cryptocurrency.service.ts
  3. 1
      libs/common/src/lib/config.ts

7
apps/api/src/services/cryptocurrency/cryptocurrency.module.ts

@ -1,9 +1,12 @@
import { PropertyModule } from '@ghostfolio/api/services/property/property.module';
import { Module } from '@nestjs/common';
import { CryptocurrencyService } from './cryptocurrency.service';
@Module({
providers: [CryptocurrencyService],
exports: [CryptocurrencyService]
exports: [CryptocurrencyService],
imports: [PropertyModule],
providers: [CryptocurrencyService]
})
export class CryptocurrencyModule {}

38
apps/api/src/services/cryptocurrency/cryptocurrency.service.ts

@ -1,31 +1,39 @@
import { DEFAULT_CURRENCY } from '@ghostfolio/common/config';
import { PropertyService } from '@ghostfolio/api/services/property/property.service';
import {
DEFAULT_CURRENCY,
PROPERTY_CUSTOM_CRYPTOCURRENCIES
} from '@ghostfolio/common/config';
import { Injectable } from '@nestjs/common';
import { Injectable, OnModuleInit } from '@nestjs/common';
const cryptocurrencies = require('../../assets/cryptocurrencies/cryptocurrencies.json');
const customCryptocurrencies = require('../../assets/cryptocurrencies/custom.json');
@Injectable()
export class CryptocurrencyService {
export class CryptocurrencyService implements OnModuleInit {
private combinedCryptocurrencies: string[];
public constructor(private readonly propertyService: PropertyService) {}
public async onModuleInit() {
const customCryptocurrenciesFromDatabase =
await this.propertyService.getByKey<Record<string, string>>(
PROPERTY_CUSTOM_CRYPTOCURRENCIES
);
this.combinedCryptocurrencies = [
...Object.keys(cryptocurrencies),
...Object.keys(customCryptocurrencies),
...Object.keys(customCryptocurrenciesFromDatabase ?? {})
];
}
public isCryptocurrency(aSymbol = '') {
const cryptocurrencySymbol = aSymbol.substring(0, aSymbol.length - 3);
return (
aSymbol.endsWith(DEFAULT_CURRENCY) &&
this.getCryptocurrencies().includes(cryptocurrencySymbol)
this.combinedCryptocurrencies.includes(cryptocurrencySymbol)
);
}
private getCryptocurrencies() {
if (!this.combinedCryptocurrencies) {
this.combinedCryptocurrencies = [
...Object.keys(cryptocurrencies),
...Object.keys(customCryptocurrencies)
];
}
return this.combinedCryptocurrencies;
}
}

1
libs/common/src/lib/config.ts

@ -199,6 +199,7 @@ export const PROPERTY_BETTER_UPTIME_MONITOR_ID = 'BETTER_UPTIME_MONITOR_ID';
export const PROPERTY_COUNTRIES_OF_SUBSCRIBERS = 'COUNTRIES_OF_SUBSCRIBERS';
export const PROPERTY_COUPONS = 'COUPONS';
export const PROPERTY_CURRENCIES = 'CURRENCIES';
export const PROPERTY_CUSTOM_CRYPTOCURRENCIES = 'CUSTOM_CRYPTOCURRENCIES';
export const PROPERTY_DATA_SOURCE_MAPPING = 'DATA_SOURCE_MAPPING';
export const PROPERTY_DATA_SOURCES_GHOSTFOLIO_DATA_PROVIDER_MAX_REQUESTS =
'DATA_SOURCES_GHOSTFOLIO_DATA_PROVIDER_MAX_REQUESTS';

Loading…
Cancel
Save