ghostfolio/apps/api/src/services/market-data.service.ts

69 lines
1.5 KiB
TypeScript
Raw Normal View History

import { DateQuery } from '@ghostfolio/api/app/portfolio/interfaces/date-query.interface';
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, Prisma } from '@prisma/client';
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
}
}
});
}
public async marketDataItems(params: {
skip?: number;
take?: number;
cursor?: Prisma.MarketDataWhereUniqueInput;
where?: Prisma.MarketDataWhereInput;
orderBy?: Prisma.MarketDataOrderByInput;
}): Promise<MarketData[]> {
const { skip, take, cursor, where, orderBy } = params;
return this.prismaService.marketData.findMany({
cursor,
orderBy,
skip,
take,
where
});
}
2021-07-03 16:37:33 +02:00
}