Browse Source

Refactoring

pull/5481/head
Thomas Kaul 1 month ago
parent
commit
99d360ef32
  1. 11
      apps/api/src/app/endpoints/activities/activities.controller.ts
  2. 20
      apps/api/src/services/activities/activities.service.spec.ts

11
apps/api/src/app/endpoints/activities/activities.controller.ts

@ -18,6 +18,7 @@ import {
} from '@ghostfolio/common/config'; } from '@ghostfolio/common/config';
import { CreateOrderDto, UpdateOrderDto } from '@ghostfolio/common/dtos'; import { CreateOrderDto, UpdateOrderDto } from '@ghostfolio/common/dtos';
import { SubscriptionType } from '@ghostfolio/common/enums'; import { SubscriptionType } from '@ghostfolio/common/enums';
import { getAssetProfileIdentifier } from '@ghostfolio/common/helper';
import { import {
ActivitiesResponse, ActivitiesResponse,
ActivityResponse, ActivityResponse,
@ -229,17 +230,21 @@ export class ActivitiesController {
userSubscription userSubscription
}: ImpersonationContext }: ImpersonationContext
): Promise<CreateActivityResponse> { ): Promise<CreateActivityResponse> {
await this.validateActivity({ const assetProfiles = await this.validateActivity({
authenticatedUserSubscription, authenticatedUserSubscription,
userId, userId,
userSubscription, userSubscription,
activityDto: data activityDto: data
}); });
const currency = data.currency;
const customCurrency = data.customCurrency; const customCurrency = data.customCurrency;
const dataSource = data.dataSource; const dataSource = data.dataSource;
const currency =
assetProfiles[
getAssetProfileIdentifier({ dataSource, symbol: data.symbol })
]?.currency ?? data.currency;
if (customCurrency) { if (customCurrency) {
data.currency = customCurrency; data.currency = customCurrency;
@ -449,7 +454,7 @@ export class ActivitiesController {
: authenticatedUserSubscription; : authenticatedUserSubscription;
try { try {
await this.dataProviderService.validateActivities({ return await this.dataProviderService.validateActivities({
subscription, subscription,
userId, userId,
activitiesDto: [{ currency, dataSource, symbol, type }], activitiesDto: [{ currency, dataSource, symbol, type }],

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

@ -779,15 +779,15 @@ describe('ActivitiesService', () => {
}); });
it.each([ it.each([
{ dataSource: DataSource.MANUAL, symbol: 'GF_GOLD' }, { currency: 'USD', dataSource: DataSource.MANUAL, symbol: 'GF_GOLD' },
{ dataSource: DataSource.YAHOO, symbol: 'AAPL' } { currency: 'EUR', dataSource: DataSource.YAHOO, symbol: 'AAPL' }
])( ])(
'removes the currency if it is the currency of the asset profile ($dataSource)', 'removes the currency if it is the currency of the asset profile ($dataSource)',
async ({ dataSource, symbol }) => { async ({ currency, dataSource, symbol }) => {
const data = await getUpdatedActivityData({ const data = await getUpdatedActivityData({
currency,
dataSource, dataSource,
symbol, symbol,
currency: 'USD',
userId: 'user-id' userId: 'user-id'
}); });
@ -796,19 +796,19 @@ describe('ActivitiesService', () => {
); );
it.each([ it.each([
{ dataSource: DataSource.MANUAL, symbol: 'GF_GOLD' }, { currency: 'EUR', dataSource: DataSource.MANUAL, symbol: 'GF_GOLD' },
{ dataSource: DataSource.YAHOO, symbol: 'AAPL' } { currency: 'USD', dataSource: DataSource.YAHOO, symbol: 'AAPL' }
])( ])(
'keeps the currency if it is not the currency of the asset profile ($dataSource)', 'keeps the currency if it is not the currency of the asset profile ($dataSource)',
async ({ dataSource, symbol }) => { async ({ currency, dataSource, symbol }) => {
const data = await getUpdatedActivityData({ const data = await getUpdatedActivityData({
currency,
dataSource, dataSource,
symbol, symbol,
currency: 'EUR',
userId: 'user-id' userId: 'user-id'
}); });
expect(data.currency).toBe('EUR'); expect(data.currency).toBe(currency);
} }
); );
@ -845,7 +845,7 @@ describe('ActivitiesService', () => {
}) })
}, },
symbolProfile: { symbolProfile: {
findUnique: jest.fn().mockResolvedValue({ currency: 'USD' }) findUnique: jest.fn().mockResolvedValue({ currency: 'EUR' })
} }
} as unknown as PrismaService, } as unknown as PrismaService,
null, null,

Loading…
Cancel
Save