Browse Source

Merge 49600d534c into 618e6c9d27

pull/8417/merge
Junaid Khan 17 hours ago
committed by GitHub
parent
commit
b47bd33294
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      CHANGELOG.md
  2. 103
      apps/api/src/app/portfolio/calculator/portfolio-calculator-missing-fx.spec.ts
  3. 10
      apps/api/src/app/portfolio/calculator/portfolio-calculator.ts
  4. 42
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.spec.ts
  5. 5
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

4
CHANGELOG.md

@ -48,6 +48,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Improved the language localization for Spanish (`es`)
- Upgraded `ngx-skeleton-loader` from version `13.0.0` to `14.0.0`
### Fixed
- Fixed the portfolio crash (`[big.js] Invalid number`) when historical exchange rates are missing
## 3.98.0 - 2026-11-04
### Changed

103
apps/api/src/app/portfolio/calculator/portfolio-calculator-missing-fx.spec.ts

@ -0,0 +1,103 @@
import {
activityDummyData,
assetProfileDummyData,
userDummyData
} from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator-test-utils';
import { PortfolioCalculatorFactory } from '@ghostfolio/api/app/portfolio/calculator/portfolio-calculator.factory';
import { CurrentRateService } from '@ghostfolio/api/app/portfolio/current-rate.service';
import { CurrentRateServiceMock } from '@ghostfolio/api/app/portfolio/current-rate.service.mock';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service';
import { PortfolioSnapshotServiceMock } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock';
import { RedisCacheService } from '@ghostfolio/api/services/redis-cache/redis-cache.service';
import { RedisCacheServiceMock } from '@ghostfolio/api/services/redis-cache/redis-cache.service.mock';
import { parseDate } from '@ghostfolio/common/helper';
import { Activity } from '@ghostfolio/common/interfaces';
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type';
jest.mock('@ghostfolio/api/app/portfolio/current-rate.service', () => {
return {
CurrentRateService: jest.fn().mockImplementation(() => {
return CurrentRateServiceMock;
})
};
});
jest.mock(
'@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service',
() => {
return {
PortfolioSnapshotService: jest.fn().mockImplementation(() => {
return PortfolioSnapshotServiceMock;
})
};
}
);
jest.mock('@ghostfolio/api/services/redis-cache/redis-cache.service', () => {
return {
RedisCacheService: jest.fn().mockImplementation(() => {
return RedisCacheServiceMock;
})
};
});
describe('PortfolioCalculator missing FX conversions (#6482)', () => {
let portfolioCalculatorFactory: PortfolioCalculatorFactory;
beforeEach(() => {
PortfolioSnapshotServiceMock.reset();
RedisCacheServiceMock.reset();
process.env.ACCESS_TOKEN_SALT = process.env.ACCESS_TOKEN_SALT || 'test-salt';
process.env.JWT_SECRET_KEY = process.env.JWT_SECRET_KEY || 'test-jwt';
process.env.DATABASE_URL =
process.env.DATABASE_URL ||
'postgresql://user:pass@localhost:5432/ghostfolio-db';
portfolioCalculatorFactory = new PortfolioCalculatorFactory(
new ConfigurationService(),
new CurrentRateService(null, null, null),
new ExchangeRateDataService(null, null, null, null),
new PortfolioSnapshotService(null, null),
new RedisCacheService(null, null)
);
});
it('does not throw when activity FX fields are undefined', () => {
const activities: Activity[] = [
{
...activityDummyData,
assetProfile: {
...assetProfileDummyData,
currency: 'USD',
dataSource: 'YAHOO',
name: 'Test',
symbol: 'TEST'
},
currency: 'USD',
date: parseDate('2021-04-15'),
fee: 1,
// Simulates toCurrencyAtDate() returning undefined before the fix
feeInAssetProfileCurrency: undefined,
feeInBaseCurrency: undefined,
quantity: 1,
type: 'BUY',
unitPrice: 100,
unitPriceInAssetProfileCurrency: undefined,
value: 100,
valueInBaseCurrency: undefined
}
];
expect(() => {
portfolioCalculatorFactory.createCalculator({
activities,
calculationType: PerformanceCalculationType.ROI,
currency: 'EUR',
userId: userDummyData.id
});
}).not.toThrow();
});
});

10
apps/api/src/app/portfolio/calculator/portfolio-calculator.ts

@ -179,10 +179,12 @@ export abstract class PortfolioCalculator {
tags,
type,
date: format(date, DATE_FORMAT),
fee: new Big(feeInAssetProfileCurrency),
feeInBaseCurrency: new Big(feeInBaseCurrency),
quantity: new Big(quantity),
unitPrice: new Big(unitPriceInAssetProfileCurrency)
// Missing FX conversion can leave these undefined (#6482);
// Big.js throws "[big.js] Invalid number" on undefined/null.
fee: new Big(feeInAssetProfileCurrency ?? 0),
feeInBaseCurrency: new Big(feeInBaseCurrency ?? 0),
quantity: new Big(quantity ?? 0),
unitPrice: new Big(unitPriceInAssetProfileCurrency ?? 0)
};
}
)

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

@ -0,0 +1,42 @@
import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';
import { Logger } from '@nestjs/common';
describe('ExchangeRateDataService.toCurrencyAtDate (#6482)', () => {
let exchangeRateDataService: ExchangeRateDataService;
beforeEach(() => {
exchangeRateDataService = new ExchangeRateDataService(
null,
null,
null,
null
);
// Avoid currency-pair shortcut paths; force the historical lookup branch.
(exchangeRateDataService as any).derivedCurrencyFactors = {};
(exchangeRateDataService as any).dataProviderService = {
getDataSourceForExchangeRates: () => 'YAHOO'
};
(exchangeRateDataService as any).marketDataService = {
get: jest.fn().mockResolvedValue(null)
};
jest.spyOn(Logger.prototype, 'error').mockImplementation(() => undefined);
});
afterEach(() => {
jest.restoreAllMocks();
});
it('returns 0 instead of undefined when no historical exchange rate exists', async () => {
const result = await exchangeRateDataService.toCurrencyAtDate(
100,
'USD',
'EUR',
new Date('2021-04-15')
);
expect(result).toBe(0);
});
});

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

@ -371,6 +371,9 @@ export class ExchangeRateDataService {
return factor * aValue;
}
// Missing historical FX must not propagate undefined into Big.js
// (PortfolioCalculator / overview crash — #6482). Treat as 0, same
// pattern as missing market prices in #6397; keep the error log.
this.logger.error(
`No exchange rate has been found for ${aFromCurrency}${aToCurrency} at ${format(
aDate,
@ -378,7 +381,7 @@ export class ExchangeRateDataService {
)}`
);
return undefined;
return 0;
}
private async getExchangeRates({

Loading…
Cancel
Save