Browse Source

fix(api): return value unchanged in toCurrency for null currencies

Prevents 'No exchange rate has been found for nullEUR' error logs when
toCurrency is called with a null or undefined source or target currency
(e.g. an account without a currency set).

Fixes ghostfolio/ghostfolio#6168

Signed-off-by: Rene Turcios <95189389+CloudCorpRecords@users.noreply.github.com>
pull/8219/head
Rene Turcios 6 days ago
parent
commit
aa753d0146
  1. 42
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.spec.ts
  2. 7
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

42
apps/api/src/services/exchange-rate-data/exchange-rate-data.service.spec.ts

@ -0,0 +1,42 @@
import { ExchangeRateDataService } from './exchange-rate-data.service';
describe('ExchangeRateDataService', () => {
let service: ExchangeRateDataService;
beforeEach(() => {
service = new ExchangeRateDataService(null, null, null, null);
});
describe('toCurrency', () => {
it('should return the value unchanged for a null source currency without logging an error', () => {
const loggerSpy = jest.spyOn(service['logger'], 'error');
const result = service.toCurrency(100, null as any, 'EUR');
expect(result).toEqual(100);
expect(loggerSpy).not.toHaveBeenCalled();
});
it('should return the value unchanged for an undefined source currency without logging an error', () => {
const loggerSpy = jest.spyOn(service['logger'], 'error');
const result = service.toCurrency(100, undefined as any, 'EUR');
expect(result).toEqual(100);
expect(loggerSpy).not.toHaveBeenCalled();
});
it('should return the value unchanged for a null target currency without logging an error', () => {
const loggerSpy = jest.spyOn(service['logger'], 'error');
const result = service.toCurrency(100, 'EUR', null as any);
expect(result).toEqual(100);
expect(loggerSpy).not.toHaveBeenCalled();
});
it('should return 0 for a value of 0', () => {
expect(service.toCurrency(0, 'USD', 'EUR')).toEqual(0);
});
});
});

7
apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

@ -257,6 +257,13 @@ export class ExchangeRateDataService {
return 0;
}
if (!aFromCurrency || !aToCurrency) {
// The currency of the value is unknown (e.g. an account without a
// currency), so no conversion can be performed. Return the value
// unchanged instead of logging an error for a "null" currency.
return aValue;
}
let factor: number;
if (aFromCurrency === aToCurrency) {

Loading…
Cancel
Save