2021-08-14 16:55:40 +02:00
|
|
|
import { CacheService } from '@ghostfolio/api/app/cache/cache.service';
|
2021-04-21 20:27:39 +02:00
|
|
|
import { DataGatheringService } from '@ghostfolio/api/services/data-gathering.service';
|
|
|
|
import { PrismaService } from '@ghostfolio/api/services/prisma.service';
|
2021-05-16 22:11:14 +02:00
|
|
|
import { OrderWithAccount } from '@ghostfolio/common/types';
|
2021-04-13 21:53:58 +02:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2021-05-27 20:50:10 +02:00
|
|
|
import { DataSource, Order, Prisma } from '@prisma/client';
|
2021-07-03 11:32:03 +02:00
|
|
|
import { endOfToday, isAfter } from 'date-fns';
|
2021-04-13 21:53:58 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class OrderService {
|
|
|
|
public constructor(
|
|
|
|
private readonly cacheService: CacheService,
|
|
|
|
private readonly dataGatheringService: DataGatheringService,
|
2021-08-07 22:38:07 +02:00
|
|
|
private readonly prismaService: PrismaService
|
2021-04-13 21:53:58 +02:00
|
|
|
) {}
|
|
|
|
|
|
|
|
public async order(
|
|
|
|
orderWhereUniqueInput: Prisma.OrderWhereUniqueInput
|
|
|
|
): Promise<Order | null> {
|
2021-08-07 22:38:07 +02:00
|
|
|
return this.prismaService.order.findUnique({
|
2021-04-13 21:53:58 +02:00
|
|
|
where: orderWhereUniqueInput
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async orders(params: {
|
|
|
|
include?: Prisma.OrderInclude;
|
|
|
|
skip?: number;
|
|
|
|
take?: number;
|
|
|
|
cursor?: Prisma.OrderWhereUniqueInput;
|
|
|
|
where?: Prisma.OrderWhereInput;
|
|
|
|
orderBy?: Prisma.OrderOrderByInput;
|
2021-05-02 21:18:52 +02:00
|
|
|
}): Promise<OrderWithAccount[]> {
|
2021-04-13 21:53:58 +02:00
|
|
|
const { include, skip, take, cursor, where, orderBy } = params;
|
|
|
|
|
2021-08-07 22:38:07 +02:00
|
|
|
return this.prismaService.order.findMany({
|
2021-04-13 21:53:58 +02:00
|
|
|
cursor,
|
|
|
|
include,
|
|
|
|
orderBy,
|
|
|
|
skip,
|
|
|
|
take,
|
|
|
|
where
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:52:55 +02:00
|
|
|
public async createOrder(data: Prisma.OrderCreateInput): Promise<Order> {
|
|
|
|
const isDraft = isAfter(data.date as Date, endOfToday());
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2021-08-07 20:52:55 +02:00
|
|
|
if (!isDraft) {
|
2021-07-03 11:32:03 +02:00
|
|
|
// Gather symbol data of order in the background, if not draft
|
|
|
|
this.dataGatheringService.gatherSymbols([
|
|
|
|
{
|
|
|
|
dataSource: data.dataSource,
|
|
|
|
date: <Date>data.date,
|
|
|
|
symbol: data.symbol
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2021-07-24 21:13:48 +02:00
|
|
|
this.dataGatheringService.gatherProfileData([data.symbol]);
|
|
|
|
|
2021-08-07 20:52:55 +02:00
|
|
|
await this.cacheService.flush();
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2021-08-07 22:38:07 +02:00
|
|
|
return this.prismaService.order.create({
|
2021-08-07 20:52:55 +02:00
|
|
|
data: {
|
|
|
|
...data,
|
|
|
|
isDraft
|
|
|
|
}
|
2021-04-13 21:53:58 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async deleteOrder(
|
2021-08-07 20:52:55 +02:00
|
|
|
where: Prisma.OrderWhereUniqueInput
|
2021-04-13 21:53:58 +02:00
|
|
|
): Promise<Order> {
|
2021-08-07 22:38:07 +02:00
|
|
|
return this.prismaService.order.delete({
|
2021-04-13 21:53:58 +02:00
|
|
|
where
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:52:55 +02:00
|
|
|
public getOrders({
|
|
|
|
includeDrafts = false,
|
|
|
|
userId
|
|
|
|
}: {
|
|
|
|
includeDrafts?: boolean;
|
|
|
|
userId: string;
|
|
|
|
}) {
|
|
|
|
const where: Prisma.OrderWhereInput = { userId };
|
|
|
|
|
|
|
|
if (includeDrafts === false) {
|
|
|
|
where.isDraft = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.orders({
|
|
|
|
where,
|
|
|
|
include: {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
Account: true,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
SymbolProfile: true
|
|
|
|
},
|
|
|
|
orderBy: { date: 'asc' }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async updateOrder(params: {
|
|
|
|
where: Prisma.OrderWhereUniqueInput;
|
|
|
|
data: Prisma.OrderUpdateInput;
|
|
|
|
}): Promise<Order> {
|
2021-04-13 21:53:58 +02:00
|
|
|
const { data, where } = params;
|
|
|
|
|
2021-08-07 20:52:55 +02:00
|
|
|
const isDraft = isAfter(data.date as Date, endOfToday());
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2021-08-07 20:52:55 +02:00
|
|
|
if (!isDraft) {
|
|
|
|
// Gather symbol data of order in the background, if not draft
|
|
|
|
this.dataGatheringService.gatherSymbols([
|
|
|
|
{
|
|
|
|
dataSource: <DataSource>data.dataSource,
|
|
|
|
date: <Date>data.date,
|
|
|
|
symbol: <string>data.symbol
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2021-08-07 20:52:55 +02:00
|
|
|
await this.cacheService.flush();
|
2021-04-13 21:53:58 +02:00
|
|
|
|
2021-08-07 22:38:07 +02:00
|
|
|
return this.prismaService.order.update({
|
2021-08-07 20:52:55 +02:00
|
|
|
data: {
|
|
|
|
...data,
|
|
|
|
isDraft
|
|
|
|
},
|
2021-04-13 21:53:58 +02:00
|
|
|
where
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|