Browse Source

Task/refactor transform data source in request interceptor (#8020)

Refactoring
pull/8028/head
Thomas Kaul 5 days ago
committed by GitHub
parent
commit
8e86a4d2b1
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 22
      apps/api/src/helper/data-source.helper.spec.ts
  2. 28
      apps/api/src/helper/data-source.helper.ts
  3. 50
      apps/api/src/interceptors/transform-data-source-in-request/transform-data-source-in-request.interceptor.ts

22
apps/api/src/helper/data-source.helper.spec.ts

@ -83,6 +83,28 @@ describe('DataSourceHelper', () => {
}) })
).toBeUndefined(); ).toBeUndefined();
}); });
it('Decodes an encoded data source if the subscription is not enabled', () => {
expect(
transformDataSourceInRequest({
configurationService: createConfigurationService({
isSubscriptionEnabled: false
}),
dataSource: encodeDataSource(DataSource.COINGECKO)
})
).toBe(DataSource.COINGECKO);
});
it('Keeps the data source GHOSTFOLIO if the subscription is not enabled', () => {
expect(
transformDataSourceInRequest({
configurationService: createConfigurationService({
isSubscriptionEnabled: false
}),
dataSource: DataSource.GHOSTFOLIO
})
).toBe(DataSource.GHOSTFOLIO);
});
}); });
describe('transformDataSourceInResponse', () => { describe('transformDataSourceInResponse', () => {

28
apps/api/src/helper/data-source.helper.ts

@ -48,6 +48,18 @@ function getGhostfolioDataSources({
: []; : [];
} }
function getUnmaskedGhostfolioDataSource({
dataSource,
ghostfolioDataSources
}: {
dataSource?: DataSource;
ghostfolioDataSources: string[];
}) {
return dataSource === DataSource.GHOSTFOLIO && ghostfolioDataSources?.[0]
? (ghostfolioDataSources[0] as DataSource)
: dataSource;
}
function hashDataSource(dataSource: DataSource) { function hashDataSource(dataSource: DataSource) {
return createHash('sha256').update(dataSource).digest('hex').slice(0, 8); return createHash('sha256').update(dataSource).digest('hex').slice(0, 8);
} }
@ -84,18 +96,6 @@ export function getMaskedGhostfolioDataSource({
: dataSource; : dataSource;
} }
export function getUnmaskedGhostfolioDataSource({
dataSource,
ghostfolioDataSources
}: {
dataSource?: DataSource;
ghostfolioDataSources: string[];
}) {
return dataSource === DataSource.GHOSTFOLIO && ghostfolioDataSources?.[0]
? (ghostfolioDataSources[0] as DataSource)
: dataSource;
}
export function isDataGatheringSupported({ export function isDataGatheringSupported({
dataSource, dataSource,
scraperConfiguration scraperConfiguration
@ -118,8 +118,8 @@ export function isValidEncodedDataSource(encodedDataSource: string) {
/** /**
* Gives the data source of a request without a transformation: an encoded data * Gives the data source of a request without a transformation: an encoded data
* source is decoded, and the mask of the Ghostfolio data provider is resolved * source is decoded, and the mask of the Ghostfolio data provider is resolved if
* (see the TransformDataSourceInRequestInterceptor) * the subscription is enabled (see the TransformDataSourceInRequestInterceptor)
*/ */
export function transformDataSourceInRequest({ export function transformDataSourceInRequest({
configurationService, configurationService,

50
apps/api/src/interceptors/transform-data-source-in-request/transform-data-source-in-request.interceptor.ts

@ -1,6 +1,6 @@
import { import {
decodeDataSource, decodeDataSource,
getUnmaskedGhostfolioDataSource transformDataSourceInRequest
} from '@ghostfolio/api/helper/data-source.helper'; } from '@ghostfolio/api/helper/data-source.helper';
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service'; import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';
@ -28,30 +28,19 @@ export class TransformDataSourceInRequestInterceptor<
const http = context.switchToHttp(); const http = context.switchToHttp();
const request = http.getRequest(); const request = http.getRequest();
if (this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) { if (request.body?.activities) {
if (request.body?.activities) { request.body.activities = request.body.activities.map((activity) => {
const ghostfolioDataSources = this.configurationService.get( return {
'DATA_SOURCES_GHOSTFOLIO_DATA_PROVIDER' ...activity,
); dataSource: transformDataSourceInRequest({
configurationService: this.configurationService,
request.body.activities = request.body.activities.map((activity) => { dataSource: activity.dataSource
if (DataSource[activity.dataSource]) { })
return { };
...activity, });
dataSource: getUnmaskedGhostfolioDataSource({ }
ghostfolioDataSources,
dataSource: activity.dataSource
})
};
} else {
return {
...activity,
dataSource: decodeDataSource(activity.dataSource)
};
}
});
}
if (this.configurationService.get('ENABLE_FEATURE_SUBSCRIPTION')) {
for (const type of ['body', 'params', 'query']) { for (const type of ['body', 'params', 'query']) {
const dataSourceValue = request[type]?.dataSource; const dataSourceValue = request[type]?.dataSource;
@ -68,19 +57,6 @@ export class TransformDataSourceInRequestInterceptor<
}); });
} }
} }
} else {
if (request.body?.activities) {
request.body.activities = request.body.activities.map((activity) => {
if (DataSource[activity.dataSource]) {
return activity;
} else {
return {
...activity,
dataSource: decodeDataSource(activity.dataSource)
};
}
});
}
} }
return next.handle(); return next.handle();

Loading…
Cancel
Save