Browse Source

Refactoring

pull/4204/head
Thomas Kaul 7 months ago
parent
commit
565240d93a
  1. 40
      apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts

40
apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts

@ -20,8 +20,8 @@ import {
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { DataSource, SymbolProfile } from '@prisma/client'; import { DataSource, SymbolProfile } from '@prisma/client';
import { isISIN } from 'class-validator';
import { format, isAfter, isBefore, isSameDay } from 'date-fns'; import { format, isAfter, isBefore, isSameDay } from 'date-fns';
import { uniqBy } from 'lodash';
@Injectable() @Injectable()
export class FinancialModelingPrepService implements DataProviderInterface { export class FinancialModelingPrepService implements DataProviderInterface {
@ -162,35 +162,49 @@ export class FinancialModelingPrepService implements DataProviderInterface {
let items: LookupItem[] = []; let items: LookupItem[] = [];
try { try {
const [resultSearch, resultSearchIsin] = await Promise.all([ if (isISIN(query)) {
fetch(`${this.URL}/search?query=${query}&apikey=${this.apiKey}`, { const result = await fetch(
`${this.getUrl({ version: 4 })}/search/isin?isin=${query}&apikey=${this.apiKey}`,
{
signal: AbortSignal.timeout( signal: AbortSignal.timeout(
this.configurationService.get('REQUEST_TIMEOUT') this.configurationService.get('REQUEST_TIMEOUT')
) )
}).then((res) => res.json()), }
fetch( ).then((res) => res.json());
`${this.getUrl({ version: 4 })}/search/isin?isin=${query}&apikey=${this.apiKey}`,
items = result.map(({ companyName, currency, symbol }) => {
return {
currency,
symbol,
assetClass: undefined, // TODO
assetSubClass: undefined, // TODO
dataProviderInfo: this.getDataProviderInfo(),
dataSource: this.getName(),
name: companyName
};
});
} else {
const result = await fetch(
`${this.URL}/search?query=${query}&apikey=${this.apiKey}`,
{ {
signal: AbortSignal.timeout( signal: AbortSignal.timeout(
this.configurationService.get('REQUEST_TIMEOUT') this.configurationService.get('REQUEST_TIMEOUT')
) )
} }
).then((res) => res.json()) ).then((res) => res.json());
]);
const result = uniqBy([...resultSearch, ...resultSearchIsin], 'symbol');
items = result.map(({ companyName, currency, name, symbol }) => { items = result.map(({ currency, name, symbol }) => {
return { return {
currency, currency,
name,
symbol, symbol,
assetClass: undefined, // TODO assetClass: undefined, // TODO
assetSubClass: undefined, // TODO assetSubClass: undefined, // TODO
dataProviderInfo: this.getDataProviderInfo(), dataProviderInfo: this.getDataProviderInfo(),
dataSource: this.getName(), dataSource: this.getName()
name: name ?? companyName
}; };
}); });
}
} catch (error) { } catch (error) {
let message = error; let message = error;

Loading…
Cancel
Save