2021-04-21 20:27:39 +02:00
|
|
|
import { DataProviderService } from '@ghostfolio/api/services/data-provider.service';
|
2021-08-09 21:33:58 +02:00
|
|
|
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
|
2021-04-13 21:53:58 +02:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2021-05-20 20:36:08 +02:00
|
|
|
import { Currency, 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(
|
2021-04-27 21:03:22 +02:00
|
|
|
private readonly dataProviderService: DataProviderService,
|
2021-08-09 21:33:58 +02:00
|
|
|
private readonly prismaService: PrismaService
|
2021-04-13 21:53:58 +02:00
|
|
|
) {}
|
|
|
|
|
|
|
|
public async get(aSymbol: string): Promise<SymbolItem> {
|
|
|
|
const response = await this.dataProviderService.get([aSymbol]);
|
2021-04-28 21:30:49 +02:00
|
|
|
const { currency, dataSource, marketPrice } = response[aSymbol];
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
return {
|
2021-04-28 21:30:49 +02:00
|
|
|
dataSource,
|
2021-04-13 21:53:58 +02:00
|
|
|
marketPrice,
|
|
|
|
currency: <Currency>(<unknown>currency)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:36:08 +02:00
|
|
|
public async lookup(aQuery: string): Promise<{ items: LookupItem[] }> {
|
|
|
|
const results: { items: LookupItem[] } = { items: [] };
|
2021-04-27 21:03:22 +02:00
|
|
|
|
2021-05-20 20:36:08 +02:00
|
|
|
if (!aQuery) {
|
2021-04-27 21:03:22 +02:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:53:58 +02:00
|
|
|
try {
|
2021-05-20 20:36:08 +02:00
|
|
|
const { items } = await this.dataProviderService.search(aQuery);
|
|
|
|
results.items = items;
|
|
|
|
|
|
|
|
// Add custom symbols
|
2021-08-09 21:33:58 +02:00
|
|
|
const ghostfolioSymbolProfiles =
|
|
|
|
await this.prismaService.symbolProfile.findMany({
|
|
|
|
select: {
|
2021-08-14 11:06:21 +02:00
|
|
|
currency: true,
|
2021-08-09 21:33:58 +02:00
|
|
|
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
|
|
|
|
2021-05-20 20:36:08 +02:00
|
|
|
return results;
|
2021-04-13 21:53:58 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|