ghostfolio/apps/api/src/app/portfolio/market-data.service.ts

52 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-07-03 16:37:33 +02:00
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
2021-07-09 20:25:42 +02:00
import { resetHours } from '@ghostfolio/common/helper';
2021-07-03 16:37:33 +02:00
import { Injectable } from '@nestjs/common';
import { MarketData } from '@prisma/client';
2021-07-28 15:45:11 +02:00
2021-07-28 15:41:38 +02:00
import { DateQuery } from './interfaces/date-query.interface';
2021-07-03 16:37:33 +02:00
@Injectable()
export class MarketDataService {
2021-08-07 22:38:07 +02:00
public constructor(private readonly prismaService: PrismaService) {}
2021-07-03 16:37:33 +02:00
public async get({
date,
symbol
}: {
date: Date;
symbol: string;
}): Promise<MarketData> {
2021-08-07 22:38:07 +02:00
return await this.prismaService.marketData.findFirst({
where: {
2021-07-28 15:32:36 +02:00
symbol,
date: resetHours(date)
}
2021-07-03 16:37:33 +02:00
});
}
public async getRange({
dateQuery,
2021-07-20 20:42:56 +02:00
symbols
}: {
dateQuery: DateQuery;
2021-07-20 20:42:56 +02:00
symbols: string[];
}): Promise<MarketData[]> {
2021-08-07 22:38:07 +02:00
return await this.prismaService.marketData.findMany({
2021-07-20 21:07:17 +02:00
orderBy: [
{
date: 'asc'
},
{
symbol: 'asc'
}
],
where: {
date: dateQuery,
2021-07-20 20:42:56 +02:00
symbol: {
in: symbols
}
}
});
}
2021-07-03 16:37:33 +02:00
}