Browse Source

Refactoring

pull/550/head
Thomas 4 years ago
parent
commit
2ffebc03bd
  1. 2
      apps/api/src/services/cryptocurrency/cryptocurrency.service.ts
  2. 2
      apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.spec.ts
  3. 39
      apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts

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

@ -10,7 +10,7 @@ export class CryptocurrencyService {
public constructor() {}
public isCrypto(aSymbol = '') {
public isCryptocurrency(aSymbol = '') {
const cryptocurrencySymbol = aSymbol.substring(0, aSymbol.length - 3);
return this.getCryptocurrencies().includes(cryptocurrencySymbol);
}

2
apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.spec.ts

@ -7,7 +7,7 @@ jest.mock(
return {
CryptocurrencyService: jest.fn().mockImplementation(() => {
return {
isCrypto: (symbol: string) => {
isCryptocurrency: (symbol: string) => {
switch (symbol) {
case 'BTCUSD':
return true;

39
apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts

@ -1,6 +1,6 @@
import { LookupItem } from '@ghostfolio/api/app/symbol/interfaces/lookup-item.interface';
import { CryptocurrencyService } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.service';
import { UNKNOWN_KEY } from '@ghostfolio/common/config';
import { baseCurrency, UNKNOWN_KEY } from '@ghostfolio/common/config';
import { DATE_FORMAT, isCurrency } from '@ghostfolio/common/helper';
import { Granularity } from '@ghostfolio/common/types';
import { Injectable, Logger } from '@nestjs/common';
@ -36,7 +36,10 @@ export class YahooFinanceService implements DataProviderInterface {
}
public convertFromYahooFinanceSymbol(aYahooFinanceSymbol: string) {
const symbol = aYahooFinanceSymbol.replace('-USD', 'USD');
const symbol = aYahooFinanceSymbol.replace(
new RegExp(`-${baseCurrency}$`),
baseCurrency
);
return symbol.replace('=X', '');
}
@ -49,24 +52,24 @@ export class YahooFinanceService implements DataProviderInterface {
* SOL1USD -> SOL1-USD
*/
public convertToYahooFinanceSymbol(aSymbol: string) {
if (
(aSymbol.includes('CHF') ||
aSymbol.includes('EUR') ||
aSymbol.includes('USD')) &&
aSymbol.length >= 6
) {
if (aSymbol.includes(baseCurrency) && aSymbol.length >= 6) {
if (isCurrency(aSymbol.substring(0, aSymbol.length - 3))) {
return `${aSymbol}=X`;
} else if (
this.cryptocurrencyService.isCrypto(
aSymbol.replace(new RegExp('-USD$'), 'USD').replace('1', '')
this.cryptocurrencyService.isCryptocurrency(
aSymbol
.replace(new RegExp(`-${baseCurrency}$`), baseCurrency)
.replace('1', '')
)
) {
// Add a dash before the last three characters
// BTCUSD -> BTC-USD
// DOGEUSD -> DOGE-USD
// SOL1USD -> SOL1-USD
return aSymbol.replace(new RegExp('-?USD$'), '-USD');
return aSymbol.replace(
new RegExp(`-?${baseCurrency}$`),
`-${baseCurrency}`
);
}
}
@ -107,7 +110,7 @@ export class YahooFinanceService implements DataProviderInterface {
exchange: this.parseExchange(value.price?.exchangeName),
marketState:
value.price?.marketState === 'REGULAR' ||
this.cryptocurrencyService.isCrypto(symbol)
this.cryptocurrencyService.isCryptocurrency(symbol)
? MarketState.open
: MarketState.closed,
marketPrice: value.price?.regularMarketPrice || 0,
@ -242,8 +245,10 @@ export class YahooFinanceService implements DataProviderInterface {
.filter(({ quoteType, symbol }) => {
return (
(quoteType === 'CRYPTOCURRENCY' &&
this.cryptocurrencyService.isCrypto(
symbol.replace(new RegExp('-USD$'), 'USD').replace('1', '')
this.cryptocurrencyService.isCryptocurrency(
symbol
.replace(new RegExp(`-${baseCurrency}$`), baseCurrency)
.replace('1', '')
)) ||
quoteType === 'EQUITY' ||
quoteType === 'ETF'
@ -251,9 +256,9 @@ export class YahooFinanceService implements DataProviderInterface {
})
.filter(({ quoteType, symbol }) => {
if (quoteType === 'CRYPTOCURRENCY') {
// Only allow cryptocurrencies in USD to avoid having redundancy in the database.
// Trades need to be converted manually before to USD (or a UI converter needs to be developed)
return symbol.includes('USD');
// Only allow cryptocurrencies in base currency to avoid having redundancy in the database.
// Transactions need to be converted manually to the base currency before
return symbol.includes(baseCurrency);
}
return true;

Loading…
Cancel
Save