diff --git a/apps/api/src/services/activities/activities.service.spec.ts b/apps/api/src/services/activities/activities.service.spec.ts index a91ca439da..e7f9fab013 100644 --- a/apps/api/src/services/activities/activities.service.spec.ts +++ b/apps/api/src/services/activities/activities.service.spec.ts @@ -109,7 +109,8 @@ describe('ActivitiesService', () => { }, userId: 'user-id' }) - } + }, + symbolProfile: { findUnique: jest.fn().mockResolvedValue(null) } } as unknown as PrismaService, null, { validateTagIds: jest.fn() } as unknown as TagService @@ -153,6 +154,95 @@ describe('ActivitiesService', () => { ); }); + it('does not store the currency if it is the currency of the existing asset profile', async () => { + const data = await getCreatedActivityData({ + assetProfile: { currency: 'USD' }, + currency: 'USD' + }); + + expect(data).not.toHaveProperty('currency'); + }); + + it('stores the currency if it is not the currency of the existing asset profile', async () => { + const data = await getCreatedActivityData({ + assetProfile: { currency: 'USD' }, + currency: 'EUR' + }); + + expect(data.currency).toBe('EUR'); + }); + + it('does not store the currency if it is the currency of a new custom asset profile', async () => { + const data = await getCreatedActivityData({ + currency: 'USD', + dataSource: DataSource.MANUAL + }); + + expect(data).not.toHaveProperty('currency'); + }); + + it('stores the currency of an activity with a new asset profile of a data provider', async () => { + const data = await getCreatedActivityData({ currency: 'USD' }); + + expect(data.currency).toBe('USD'); + }); + + async function getCreatedActivityData({ + assetProfile, + currency, + dataSource = DataSource.YAHOO + }: { + assetProfile?: { currency: string }; + currency: string; + dataSource?: DataSource; + }) { + const create = jest.fn(({ data }: Prisma.OrderCreateArgs) => { + return { + ...data, + SymbolProfile: data.SymbolProfile.connectOrCreate.create + }; + }); + + const service = new ActivitiesService( + null, + null, + null, + null, + { addJobToQueue: jest.fn() } as unknown as DataGatheringService, + null, + { emit: jest.fn() } as unknown as EventEmitter2, + null, + null, + { + order: { create }, + symbolProfile: { + findUnique: jest.fn().mockResolvedValue(assetProfile ?? null) + } + } as unknown as PrismaService, + null, + { validateTagIds: jest.fn() } as unknown as TagService + ); + + await service.createActivity({ + currency, + date: parseDate('2026-10-01'), + fee: 0, + quantity: 1, + SymbolProfile: { + connectOrCreate: { + create: { dataSource, currency: 'USD', symbol: 'VT' }, + where: { dataSource_symbol: { dataSource, symbol: 'VT' } } + } + }, + type: 'BUY', + unitPrice: 100, + user: { connect: { id: 'user-id' } }, + userId: 'user-id' + }); + + return create.mock.calls[0][0].data; + } + async function getCreatedAssetProfile({ existingAssetProfile, symbol @@ -688,12 +778,48 @@ describe('ActivitiesService', () => { expect(data).not.toHaveProperty('SymbolProfile'); }); + it.each([ + { dataSource: DataSource.MANUAL, symbol: 'GF_GOLD' }, + { dataSource: DataSource.YAHOO, symbol: 'AAPL' } + ])( + 'removes the currency if it is the currency of the asset profile ($dataSource)', + async ({ dataSource, symbol }) => { + const data = await getUpdatedActivityData({ + dataSource, + symbol, + currency: 'USD', + userId: 'user-id' + }); + + expect(data.currency).toBeNull(); + } + ); + + it.each([ + { dataSource: DataSource.MANUAL, symbol: 'GF_GOLD' }, + { dataSource: DataSource.YAHOO, symbol: 'AAPL' } + ])( + 'keeps the currency if it is not the currency of the asset profile ($dataSource)', + async ({ dataSource, symbol }) => { + const data = await getUpdatedActivityData({ + dataSource, + symbol, + currency: 'EUR', + userId: 'user-id' + }); + + expect(data.currency).toBe('EUR'); + } + ); + async function getUpdatedActivityData({ + currency, dataSource = DataSource.MANUAL, symbol = 'GF_GOLD', type = ActivityType.BUY, userId }: { + currency?: string; dataSource?: DataSource; symbol?: string; type?: ActivityType; @@ -706,7 +832,7 @@ describe('ActivitiesService', () => { null, null, null, - null, + { gatherSymbols: jest.fn() } as unknown as DataGatheringService, null, { emit: jest.fn() } as unknown as EventEmitter2, null, @@ -714,9 +840,12 @@ describe('ActivitiesService', () => { { order: { update, - findUnique: jest - .fn() - .mockResolvedValue({ SymbolProfile: { userId } }) + findUnique: jest.fn().mockResolvedValue({ + SymbolProfile: { userId, currency: 'USD' } + }) + }, + symbolProfile: { + findUnique: jest.fn().mockResolvedValue({ currency: 'USD' }) } } as unknown as PrismaService, null, @@ -725,6 +854,7 @@ describe('ActivitiesService', () => { await service.updateActivity({ data: { + currency, type, assetClass: AssetClass.COMMODITY, assetSubClass: AssetSubClass.PRECIOUS_METAL, diff --git a/apps/api/src/services/activities/activities.service.ts b/apps/api/src/services/activities/activities.service.ts index d26a21372d..0b77a31aed 100644 --- a/apps/api/src/services/activities/activities.service.ts +++ b/apps/api/src/services/activities/activities.service.ts @@ -285,7 +285,18 @@ export class ActivitiesService { delete data.comment; } - if (data.currency === data.SymbolProfile.connectOrCreate.create.currency) { + const assetProfile = await this.prismaService.symbolProfile.findUnique({ + select: { currency: true }, + where: data.SymbolProfile.connectOrCreate.where + }); + + const assetProfileCurrency = + assetProfile?.currency ?? + (data.SymbolProfile.connectOrCreate.create.dataSource === 'MANUAL' + ? data.SymbolProfile.connectOrCreate.create.currency + : undefined); + + if (data.currency === assetProfileCurrency) { delete data.currency; } @@ -910,7 +921,6 @@ export class ActivitiesService { data: Prisma.OrderUpdateInput & { assetClass?: AssetClass; assetSubClass?: AssetSubClass; - currency?: string; symbol?: string; tags?: { id: string }[]; type?: ActivityType; @@ -933,6 +943,8 @@ export class ActivitiesService { data.comment = null; } + let assetProfileCurrency: string; + if ( NON_INVESTMENT_ACTIVITY_TYPES.includes(data.type) || (data.SymbolProfile.connect.dataSource_symbol.dataSource === 'MANUAL' && @@ -941,10 +953,12 @@ export class ActivitiesService { delete data.SymbolProfile.connect; const activityToUpdate = await this.prismaService.order.findUnique({ - select: { SymbolProfile: { select: { userId: true } } }, + select: { SymbolProfile: { select: { currency: true, userId: true } } }, where }); + assetProfileCurrency = activityToUpdate?.SymbolProfile.currency; + if (activityToUpdate?.SymbolProfile.userId === userId) { data.SymbolProfile.update = { data: { @@ -959,6 +973,13 @@ export class ActivitiesService { } else { delete data.SymbolProfile.update; + const assetProfile = await this.prismaService.symbolProfile.findUnique({ + select: { currency: true }, + where: data.SymbolProfile.connect + }); + + assetProfileCurrency = assetProfile?.currency; + if (!isActivityInFuture({ date: data.date as Date })) { // Gather symbol data of order in the background, if the date is not in // the future @@ -979,7 +1000,9 @@ export class ActivitiesService { delete data.assetClass; delete data.assetSubClass; - // TODO: Remove currency? + if (data.currency === assetProfileCurrency) { + data.currency = null; + } delete data.symbol; delete data.tags; diff --git a/prisma/migrations/20250907093545_removed_redundant_currency_from_order/migration.sql b/prisma/migrations/20261029000000_removed_redundant_currency_from_order/migration.sql similarity index 100% rename from prisma/migrations/20250907093545_removed_redundant_currency_from_order/migration.sql rename to prisma/migrations/20261029000000_removed_redundant_currency_from_order/migration.sql