Feature/add support for custom cryptocurrencies defined in database (#6344)
* Add support for custom cryptocurrencies defined in database * Update changelogpull/6337/head
parent
d1345b8147
commit
8c88ce918f
@ -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 {}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue