Browse Source

Compare with currency of stored asset profile

pull/5481/head
Thomas Kaul 1 month ago
parent
commit
9b4cddfe5b
  1. 140
      apps/api/src/services/activities/activities.service.spec.ts
  2. 31
      apps/api/src/services/activities/activities.service.ts
  3. 0
      prisma/migrations/20261029000000_removed_redundant_currency_from_order/migration.sql

140
apps/api/src/services/activities/activities.service.spec.ts

@ -109,7 +109,8 @@ describe('ActivitiesService', () => {
}, },
userId: 'user-id' userId: 'user-id'
}) })
} },
symbolProfile: { findUnique: jest.fn().mockResolvedValue(null) }
} as unknown as PrismaService, } as unknown as PrismaService,
null, null,
{ validateTagIds: jest.fn() } as unknown as TagService { 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({ async function getCreatedAssetProfile({
existingAssetProfile, existingAssetProfile,
symbol symbol
@ -688,12 +778,48 @@ describe('ActivitiesService', () => {
expect(data).not.toHaveProperty('SymbolProfile'); 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({ async function getUpdatedActivityData({
currency,
dataSource = DataSource.MANUAL, dataSource = DataSource.MANUAL,
symbol = 'GF_GOLD', symbol = 'GF_GOLD',
type = ActivityType.BUY, type = ActivityType.BUY,
userId userId
}: { }: {
currency?: string;
dataSource?: DataSource; dataSource?: DataSource;
symbol?: string; symbol?: string;
type?: ActivityType; type?: ActivityType;
@ -706,7 +832,7 @@ describe('ActivitiesService', () => {
null, null,
null, null,
null, null,
null, { gatherSymbols: jest.fn() } as unknown as DataGatheringService,
null, null,
{ emit: jest.fn() } as unknown as EventEmitter2, { emit: jest.fn() } as unknown as EventEmitter2,
null, null,
@ -714,9 +840,12 @@ describe('ActivitiesService', () => {
{ {
order: { order: {
update, update,
findUnique: jest findUnique: jest.fn().mockResolvedValue({
.fn() SymbolProfile: { userId, currency: 'USD' }
.mockResolvedValue({ SymbolProfile: { userId } }) })
},
symbolProfile: {
findUnique: jest.fn().mockResolvedValue({ currency: 'USD' })
} }
} as unknown as PrismaService, } as unknown as PrismaService,
null, null,
@ -725,6 +854,7 @@ describe('ActivitiesService', () => {
await service.updateActivity({ await service.updateActivity({
data: { data: {
currency,
type, type,
assetClass: AssetClass.COMMODITY, assetClass: AssetClass.COMMODITY,
assetSubClass: AssetSubClass.PRECIOUS_METAL, assetSubClass: AssetSubClass.PRECIOUS_METAL,

31
apps/api/src/services/activities/activities.service.ts

@ -285,7 +285,18 @@ export class ActivitiesService {
delete data.comment; 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; delete data.currency;
} }
@ -910,7 +921,6 @@ export class ActivitiesService {
data: Prisma.OrderUpdateInput & { data: Prisma.OrderUpdateInput & {
assetClass?: AssetClass; assetClass?: AssetClass;
assetSubClass?: AssetSubClass; assetSubClass?: AssetSubClass;
currency?: string;
symbol?: string; symbol?: string;
tags?: { id: string }[]; tags?: { id: string }[];
type?: ActivityType; type?: ActivityType;
@ -933,6 +943,8 @@ export class ActivitiesService {
data.comment = null; data.comment = null;
} }
let assetProfileCurrency: string;
if ( if (
NON_INVESTMENT_ACTIVITY_TYPES.includes(data.type) || NON_INVESTMENT_ACTIVITY_TYPES.includes(data.type) ||
(data.SymbolProfile.connect.dataSource_symbol.dataSource === 'MANUAL' && (data.SymbolProfile.connect.dataSource_symbol.dataSource === 'MANUAL' &&
@ -941,10 +953,12 @@ export class ActivitiesService {
delete data.SymbolProfile.connect; delete data.SymbolProfile.connect;
const activityToUpdate = await this.prismaService.order.findUnique({ const activityToUpdate = await this.prismaService.order.findUnique({
select: { SymbolProfile: { select: { userId: true } } }, select: { SymbolProfile: { select: { currency: true, userId: true } } },
where where
}); });
assetProfileCurrency = activityToUpdate?.SymbolProfile.currency;
if (activityToUpdate?.SymbolProfile.userId === userId) { if (activityToUpdate?.SymbolProfile.userId === userId) {
data.SymbolProfile.update = { data.SymbolProfile.update = {
data: { data: {
@ -959,6 +973,13 @@ export class ActivitiesService {
} else { } else {
delete data.SymbolProfile.update; 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 })) { if (!isActivityInFuture({ date: data.date as Date })) {
// Gather symbol data of order in the background, if the date is not in // Gather symbol data of order in the background, if the date is not in
// the future // the future
@ -979,7 +1000,9 @@ export class ActivitiesService {
delete data.assetClass; delete data.assetClass;
delete data.assetSubClass; delete data.assetSubClass;
// TODO: Remove currency? if (data.currency === assetProfileCurrency) {
data.currency = null;
}
delete data.symbol; delete data.symbol;
delete data.tags; delete data.tags;

0
prisma/migrations/20250907093545_removed_redundant_currency_from_order/migration.sql → prisma/migrations/20261029000000_removed_redundant_currency_from_order/migration.sql

Loading…
Cancel
Save