diff --git a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts index b1f430ec0e..fd73bc73d2 100644 --- a/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts +++ b/apps/api/src/app/portfolio/calculator/portfolio-calculator.ts @@ -176,6 +176,11 @@ export abstract class PortfolioCalculator { this.computeTransactionPoints(); this.snapshotPromise = this.initialize(); + + // Mark the rejection as handled to prevent an unhandled promise rejection + // in case the snapshot promise is never awaited. Consumers awaiting it + // still receive the error. + this.snapshotPromise.catch(() => undefined); } protected abstract calculateOverallPerformance( @@ -1185,6 +1190,12 @@ export abstract class PortfolioCalculator { }); } } else { + if (attempt >= PortfolioCalculator.MAX_INITIALIZATION_ATTEMPTS) { + throw new Error( + `Portfolio snapshot of user '${this.userId}' could not be computed after ${attempt} attempts` + ); + } + // Wait for computation await this.portfolioSnapshotService.addJobToQueue({ data: { @@ -1207,12 +1218,6 @@ export abstract class PortfolioCalculator { await job.finished(); } - if (attempt >= PortfolioCalculator.MAX_INITIALIZATION_ATTEMPTS) { - throw new Error( - `Portfolio snapshot of user '${this.userId}' could not be computed after ${attempt} attempts` - ); - } - await this.initialize(attempt + 1); } } diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-buy.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-buy.spec.ts index fe23af04bb..a6bedc55d1 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-buy.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-buy.spec.ts @@ -54,6 +54,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts index 061aaf8170..dc22cdbab5 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-sell-in-two-activities.spec.ts @@ -54,6 +54,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-sell.spec.ts index c8e0af46de..9d55a79ddb 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-sell.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy-and-sell.spec.ts @@ -54,6 +54,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy.spec.ts index 4922382a9a..a2d576361d 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-baln-buy.spec.ts @@ -54,6 +54,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btceur-in-base-currency-eur.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btceur-in-base-currency-eur.spec.ts index 21a8d2056a..1143e3bd27 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btceur-in-base-currency-eur.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btceur-in-base-currency-eur.spec.ts @@ -76,6 +76,9 @@ describe('PortfolioCalculator', () => { }); beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btceur.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btceur.spec.ts index 4f5da58b85..e5b0d69d64 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btceur.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btceur.spec.ts @@ -64,6 +64,9 @@ describe('PortfolioCalculator', () => { }); beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts index f20506b061..ea1df42032 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd-buy-and-sell-partially.spec.ts @@ -66,6 +66,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd-short.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd-short.spec.ts index 79c6979ef3..93d91c500e 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd-short.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd-short.spec.ts @@ -64,6 +64,9 @@ describe('PortfolioCalculator', () => { }); beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd.spec.ts index eb5571feb1..1fa2d12641 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-btcusd.spec.ts @@ -64,6 +64,9 @@ describe('PortfolioCalculator', () => { }); beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-cash.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-cash.spec.ts index aaa2f63cfa..3b09bfd260 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-cash.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-cash.spec.ts @@ -77,6 +77,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); exchangeRateDataService = new ExchangeRateDataService( diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-fee.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-fee.spec.ts index 4e413c0c52..000cc59357 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-fee.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-fee.spec.ts @@ -54,6 +54,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-googl-buy.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-googl-buy.spec.ts index 984dc11548..451973a9ff 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-googl-buy.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-googl-buy.spec.ts @@ -66,6 +66,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-jnug-buy-and-sell-and-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-jnug-buy-and-sell-and-buy-and-sell.spec.ts index 962cfe2d88..2cc87934ae 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-jnug-buy-and-sell-and-buy-and-sell.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-jnug-buy-and-sell-and-buy-and-sell.spec.ts @@ -67,6 +67,9 @@ describe('PortfolioCalculator', () => { }); beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-liability.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-liability.spec.ts index 94654af610..68572c63e1 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-liability.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-liability.spec.ts @@ -54,6 +54,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-and-sell.spec.ts index b1f030aae4..53236f007f 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-and-sell.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-and-sell.spec.ts @@ -52,6 +52,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); exchangeRateDataService = new ExchangeRateDataService( diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-with-dividend.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-with-dividend.spec.ts index 91c095623b..f6598f22b8 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-with-dividend.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-msft-buy-with-dividend.spec.ts @@ -54,6 +54,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-no-activities.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-no-activities.spec.ts index ff5dc93f9d..fb7a434771 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-no-activities.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-no-activities.spec.ts @@ -49,6 +49,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-novn-buy-and-sell-partially.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-novn-buy-and-sell-partially.spec.ts index 8ce62db598..8c3858dcd6 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-novn-buy-and-sell-partially.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-novn-buy-and-sell-partially.spec.ts @@ -67,6 +67,9 @@ describe('PortfolioCalculator', () => { }); beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-novn-buy-and-sell.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-novn-buy-and-sell.spec.ts index 10cc2da01f..364d173e14 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-novn-buy-and-sell.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-novn-buy-and-sell.spec.ts @@ -67,6 +67,9 @@ describe('PortfolioCalculator', () => { }); beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-valuable.spec.ts b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-valuable.spec.ts index 226eaa3d87..ce5f90f5c3 100644 --- a/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-valuable.spec.ts +++ b/apps/api/src/app/portfolio/calculator/roai/portfolio-calculator-valuable.spec.ts @@ -54,6 +54,9 @@ describe('PortfolioCalculator', () => { let redisCacheService: RedisCacheService; beforeEach(() => { + PortfolioSnapshotServiceMock.reset(); + RedisCacheServiceMock.reset(); + configurationService = new ConfigurationService(); currentRateService = new CurrentRateService(null, null, null, null); diff --git a/apps/api/src/app/redis-cache/redis-cache.service.mock.ts b/apps/api/src/app/redis-cache/redis-cache.service.mock.ts index feb669ab0b..2a3c1cc7af 100644 --- a/apps/api/src/app/redis-cache/redis-cache.service.mock.ts +++ b/apps/api/src/app/redis-cache/redis-cache.service.mock.ts @@ -18,6 +18,9 @@ export const RedisCacheServiceMock = { return `portfolio-snapshot-${userId}${filtersHash > 0 ? `-${filtersHash}` : ''}`; }, + reset: () => { + RedisCacheServiceMock.cache.clear(); + }, set: (key: string, value: string): Promise => { RedisCacheServiceMock.cache.set(key, value); diff --git a/apps/api/src/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock.ts b/apps/api/src/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock.ts index 6bfad95422..855721159f 100644 --- a/apps/api/src/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock.ts +++ b/apps/api/src/services/queues/portfolio-snapshot/portfolio-snapshot.service.mock.ts @@ -40,5 +40,8 @@ export const PortfolioSnapshotServiceMock = { return Promise.resolve(job as Job); }, - jobsStore: new Map>() + jobsStore: new Map>(), + reset() { + this.jobsStore.clear(); + } };