Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍
TypeScript angularetffinancefintechghostfoliohacktoberfestinvestingnestjsosspersonal-financeportfolioprismasoftwarestocktrackertradingtypescriptwealth-managementweb
Bugfix/repeated asset profile gathering on activity creation (#8055)
Thomas Kaula864545f94a4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7fa0d1c870..af2b34499b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Improved the accessibility of the _Buy me a coffee_ button on the about page- Improved the performance of the historical market data table of the admin control panel by loading only the latest market price per asset profile+- Introduced a cooldown of 12 hours for the asset profile and exchange rate gathering on activity creation by retaining the completed jobs## 3.88.0 - 2026-10-17diff --git a/apps/api/src/app/activities/activities.service.spec.ts b/apps/api/src/app/activities/activities.service.spec.tsindex 7fa771ea2c..b32d209ad0 100644--- a/apps/api/src/app/activities/activities.service.spec.ts+++ b/apps/api/src/app/activities/activities.service.spec.ts@@ -7,10 +7,13 @@ import { WHERE_ACTIVITY_NOT_DRAFT } from '@ghostfolio/api/helper/activity.helperimport { AssetProfileSplitService } from '@ghostfolio/api/services/asset-profile-split/asset-profile-split.service';import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';import { PrismaService } from '@ghostfolio/api/services/prisma/prisma.service';+import { DataGatheringService } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.service';import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/symbol-profile.service';import { TagService } from '@ghostfolio/api/services/tag/tag.service';import { getIntervalFromDateRange } from '@ghostfolio/common/calculation-helper';import {+ GATHER_ASSET_PROFILE_COOLDOWN_IN_MS,+ GATHER_ASSET_PROFILE_PROCESS_JOB_NAME,INVESTMENT_ACTIVITY_TYPES,NON_INVESTMENT_ACTIVITY_TYPES} from '@ghostfolio/common/config';@@ -83,6 +86,73 @@ describe('ActivitiesService', () => {expect(assetProfile.symbol).toBe(symbol);});+ it('retains the completed asset profile job for the duration of the cooldown', async () => {+ const addJobToQueue = jest.fn();++ const service = new ActivitiesService(+ null,+ null,+ null,+ null,+ { addJobToQueue } as unknown as DataGatheringService,+ null,+ { emit: jest.fn() } as unknown as EventEmitter2,+ null,+ null,+ {+ order: {+ create: jest.fn().mockResolvedValue({+ SymbolProfile: {+ currency: 'USD',+ dataSource: DataSource.YAHOO,+ symbol: 'VT'+ },+ userId: 'user-id'+ })+ }+ } as unknown as PrismaService,+ null,+ { validateTagIds: jest.fn() } as unknown as TagService+ );++ await service.createActivity({+ currency: 'USD',+ date: parseDate('2026-10-01'),+ fee: 0,+ quantity: 1,+ SymbolProfile: {+ connectOrCreate: {+ create: {+ currency: 'USD',+ dataSource: DataSource.YAHOO,+ symbol: 'VT'+ },+ where: {+ dataSource_symbol: {+ dataSource: DataSource.YAHOO,+ symbol: 'VT'+ }+ }+ }+ },+ type: 'BUY',+ unitPrice: 100,+ user: { connect: { id: 'user-id' } },+ userId: 'user-id'+ });++ expect(addJobToQueue).toHaveBeenCalledWith(+ expect.objectContaining({+ name: GATHER_ASSET_PROFILE_PROCESS_JOB_NAME,+ opts: expect.objectContaining({+ removeOnComplete: {+ age: GATHER_ASSET_PROFILE_COOLDOWN_IN_MS / 1000+ }+ })+ })+ );+ });+async function getCreatedAssetProfile({existingAssetProfile,symboldiff --git a/apps/api/src/app/activities/activities.service.ts b/apps/api/src/app/activities/activities.service.tsindex c0051aefb2..4a20658f97 100644--- a/apps/api/src/app/activities/activities.service.ts+++ b/apps/api/src/app/activities/activities.service.ts@@ -23,6 +23,7 @@ import { SymbolProfileService } from '@ghostfolio/api/services/symbol-profile/syimport { TagService } from '@ghostfolio/api/services/tag/tag.service';import {DATA_GATHERING_QUEUE_PRIORITY_HIGH,+ GATHER_ASSET_PROFILE_COOLDOWN_IN_MS,GATHER_ASSET_PROFILE_PROCESS_JOB_NAME,GATHER_ASSET_PROFILE_PROCESS_JOB_OPTIONS,NON_INVESTMENT_ACTIVITY_TYPES,@@ -269,7 +270,10 @@ export class ActivitiesService {dataSource: data.SymbolProfile.connectOrCreate.create.dataSource,symbol: data.SymbolProfile.connectOrCreate.create.symbol}),- priority: DATA_GATHERING_QUEUE_PRIORITY_HIGH+ priority: DATA_GATHERING_QUEUE_PRIORITY_HIGH,+ removeOnComplete: {+ age: GATHER_ASSET_PROFILE_COOLDOWN_IN_MS / 1000+ }}});}diff --git a/apps/api/src/events/asset-profile-changed.listener.spec.ts b/apps/api/src/events/asset-profile-changed.listener.spec.tsnew file mode 100644index 0000000000..e1e24c6f79--- /dev/null+++ b/apps/api/src/events/asset-profile-changed.listener.spec.ts@@ -0,0 +1,54 @@+import { ActivitiesService } from '@ghostfolio/api/app/activities/activities.service';+import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';+import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service';+import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';+import { DataGatheringService } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.service';+import { GATHER_HISTORICAL_MARKET_DATA_COOLDOWN_IN_MS } from '@ghostfolio/common/config';+import { parseDate } from '@ghostfolio/common/helper';++import { DataSource } from '@prisma/client';++import { AssetProfileChangedListener } from './asset-profile-changed.listener';++describe('AssetProfileChangedListener', () => {+ describe('processAssetProfileChanged', () => {+ it('retains the completed exchange rate job for the duration of the cooldown', async () => {+ const gatherSymbol = jest.fn();++ const listener = new AssetProfileChangedListener(+ {+ getStatisticsByCurrency: jest.fn().mockResolvedValue({+ dateOfFirstActivity: parseDate('2026-01-01')+ })+ } as unknown as ActivitiesService,+ {+ get: jest.fn().mockReturnValue(true)+ } as unknown as ConfigurationService,+ { gatherSymbol } as unknown as DataGatheringService,+ {+ getDataSourceForExchangeRates: jest+ .fn()+ .mockReturnValue(DataSource.YAHOO)+ } as unknown as DataProviderService,+ {+ getCurrencies: jest.fn().mockReturnValue(['EUR', 'USD'])+ } as unknown as ExchangeRateDataService+ );++ await listener['processAssetProfileChanged']({+ currency: 'EUR',+ dataSource: DataSource.YAHOO,+ symbol: 'PPFB.DE'+ });++ expect(gatherSymbol).toHaveBeenCalledWith({+ dataSource: DataSource.YAHOO,+ date: parseDate('2026-01-01'),+ removeOnComplete: {+ age: GATHER_HISTORICAL_MARKET_DATA_COOLDOWN_IN_MS / 1000+ },+ symbol: 'USDEUR'+ });+ });+ });+});diff --git a/apps/api/src/events/asset-profile-changed.listener.ts b/apps/api/src/events/asset-profile-changed.listener.tsindex 15c19ff0ac..ffc4014c68 100644--- a/apps/api/src/events/asset-profile-changed.listener.ts+++ b/apps/api/src/events/asset-profile-changed.listener.ts@@ -3,7 +3,10 @@ import { ConfigurationService } from '@ghostfolio/api/services/configuration/conimport { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service';import { ExchangeRateDataService } from '@ghostfolio/api/services/exchange-rate-data/exchange-rate-data.service';import { DataGatheringService } from '@ghostfolio/api/services/queues/data-gathering/data-gathering.service';-import { DEFAULT_CURRENCY } from '@ghostfolio/common/config';+import {+ DEFAULT_CURRENCY,+ GATHER_HISTORICAL_MARKET_DATA_COOLDOWN_IN_MS+} from '@ghostfolio/common/config';import { getAssetProfileIdentifier } from '@ghostfolio/common/helper';import { AssetProfileIdentifier } from '@ghostfolio/common/interfaces';@@ -91,6 +94,9 @@ export class AssetProfileChangedListener {await this.dataGatheringService.gatherSymbol({dataSource: this.dataProviderService.getDataSourceForExchangeRates(),date: dateOfFirstActivity,+ removeOnComplete: {+ age: GATHER_HISTORICAL_MARKET_DATA_COOLDOWN_IN_MS / 1000+ },symbol: `${DEFAULT_CURRENCY}${currency}`});}diff --git a/apps/api/src/services/queues/data-gathering/data-gathering.service.spec.ts b/apps/api/src/services/queues/data-gathering/data-gathering.service.spec.tsindex 4fc80d3d64..95ca49efcc 100644--- a/apps/api/src/services/queues/data-gathering/data-gathering.service.spec.ts+++ b/apps/api/src/services/queues/data-gathering/data-gathering.service.spec.ts@@ -186,6 +186,47 @@ describe('DataGatheringService', () => {});});+ describe('gatherSymbol', () => {+ beforeEach(() => {+ jest+ .spyOn(dataGatheringService as any, 'getSymbolsMax')+ .mockResolvedValue([+ {+ dataSource: 'YAHOO',+ date: parseDate('2026-08-01'),+ symbol: 'USDEUR'+ }+ ]);+ });++ it('does not apply the cooldown by default', async () => {+ await dataGatheringService.gatherSymbol({+ dataSource: 'YAHOO',+ symbol: 'USDEUR'+ });++ const [jobs] = dataGatheringQueue.addBulk.mock.calls[0];++ expect(jobs[0].opts.removeOnComplete).toBe(true);+ });++ it('retains its completed jobs for the duration of a given cooldown', async () => {+ await dataGatheringService.gatherSymbol({+ dataSource: 'YAHOO',+ removeOnComplete: {+ age: GATHER_HISTORICAL_MARKET_DATA_COOLDOWN_IN_MS / 1000+ },+ symbol: 'USDEUR'+ });++ const [jobs] = dataGatheringQueue.addBulk.mock.calls[0];++ expect(jobs[0].opts.removeOnComplete).toEqual({+ age: GATHER_HISTORICAL_MARKET_DATA_COOLDOWN_IN_MS / 1000+ });+ });+ });+describe('gatherSymbols', () => {it('does not apply the cooldown to a manually triggered gathering', async () => {await dataGatheringService.gatherSymbols({diff --git a/apps/api/src/services/queues/data-gathering/data-gathering.service.ts b/apps/api/src/services/queues/data-gathering/data-gathering.service.tsindex 2897855b01..5547d4cca6 100644--- a/apps/api/src/services/queues/data-gathering/data-gathering.service.ts+++ b/apps/api/src/services/queues/data-gathering/data-gathering.service.ts@@ -297,8 +297,11 @@ export class DataGatheringService {dataSource,date,force = true,+ removeOnComplete,symbol- }: DataGatheringItem) {+ }: DataGatheringItem & {+ removeOnComplete?: JobOptions['removeOnComplete'];+ }) {const dataGatheringItems = (await this.getSymbolsMax()).filter((dataGatheringItem) => {return (@@ -314,6 +317,7 @@ export class DataGatheringService {return this.gatherSymbols({dataGatheringItems,force,+ removeOnComplete,priority: DATA_GATHERING_QUEUE_PRIORITY_HIGH});}diff --git a/libs/common/src/lib/config.ts b/libs/common/src/lib/config.tsindex 615ed09214..61008f1fc7 100644--- a/libs/common/src/lib/config.ts+++ b/libs/common/src/lib/config.ts@@ -208,6 +208,12 @@ export const DERIVED_CURRENCIES = [export const E_MAIL_LINE_BREAK = '%0D%0A';+/**+ * Keep equal to GATHER_HISTORICAL_MARKET_DATA_COOLDOWN_IN_MS: Bull removes the+ * completed jobs of all job names by age, so the lower value applies to both.+ */+export const GATHER_ASSET_PROFILE_COOLDOWN_IN_MS = ms('12 hours');+export const GATHER_ASSET_PROFILE_PROCESS_JOB_NAME = 'GATHER_ASSET_PROFILE';export const GATHER_ASSET_PROFILE_PROCESS_JOB_OPTIONS: JobOptions = {attempts: 6, // Retries after 1, 3, 7, 15 and 31 minutes (57 minutes in total)@@ -220,7 +226,12 @@ export const GATHER_ASSET_PROFILE_PROCESS_JOB_OPTIONS: JobOptions = {timeout: ms('5 minutes')};+/**+ * Keep equal to GATHER_ASSET_PROFILE_COOLDOWN_IN_MS: Bull removes the+ * completed jobs of all job names by age, so the lower value applies to both.+ */export const GATHER_HISTORICAL_MARKET_DATA_COOLDOWN_IN_MS = ms('12 hours');+export const GATHER_HISTORICAL_MARKET_DATA_PROCESS_JOB_NAME ='GATHER_HISTORICAL_MARKET_DATA';export const GATHER_HISTORICAL_MARKET_DATA_PROCESS_JOB_OPTIONS: JobOptions = {