ghostfolio/apps/api/src/app/symbol/symbol.service.ts

76 lines
2.1 KiB
TypeScript
Raw Normal View History

import { DataProviderService } from '@ghostfolio/api/services/data-provider/data-provider.service';
import { IDataGatheringItem } from '@ghostfolio/api/services/interfaces/interfaces';
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
2021-04-13 21:53:58 +02:00
import { Injectable } from '@nestjs/common';
import { DataSource } from '@prisma/client';
2021-04-13 21:53:58 +02:00
import { LookupItem } from './interfaces/lookup-item.interface';
import { SymbolItem } from './interfaces/symbol-item.interface';
@Injectable()
export class SymbolService {
public constructor(
private readonly dataProviderService: DataProviderService,
private readonly prismaService: PrismaService
2021-04-13 21:53:58 +02:00
) {}
public async get(dataGatheringItem: IDataGatheringItem): Promise<SymbolItem> {
const response = await this.dataProviderService.get([dataGatheringItem]);
const { currency, marketPrice } = response[dataGatheringItem.symbol] ?? {};
2021-04-13 21:53:58 +02:00
if (dataGatheringItem.dataSource && marketPrice) {
return {
currency,
marketPrice,
dataSource: dataGatheringItem.dataSource
};
}
return undefined;
2021-04-13 21:53:58 +02:00
}
public async lookup(aQuery: string): Promise<{ items: LookupItem[] }> {
const results: { items: LookupItem[] } = { items: [] };
if (!aQuery) {
return results;
}
2021-04-13 21:53:58 +02:00
try {
const { items } = await this.dataProviderService.search(aQuery);
results.items = items;
// Add custom symbols
const ghostfolioSymbolProfiles =
await this.prismaService.symbolProfile.findMany({
select: {
currency: true,
dataSource: true,
name: true,
symbol: true
},
where: {
AND: [
{
dataSource: DataSource.GHOSTFOLIO,
name: {
startsWith: aQuery
}
}
]
}
});
for (const ghostfolioSymbolProfile of ghostfolioSymbolProfiles) {
results.items.push(ghostfolioSymbolProfile);
}
2021-04-13 21:53:58 +02:00
return results;
2021-04-13 21:53:58 +02:00
} catch (error) {
console.error(error);
throw error;
}
}
}