import { DataGatheringService } from '@ghostfolio/api/services/data-gathering.service'; import { PrismaService } from '@ghostfolio/api/services/prisma.service'; import { OrderWithAccount } from '@ghostfolio/common/types'; import { Injectable } from '@nestjs/common'; import { DataSource, Order, Prisma } from '@prisma/client'; import { endOfToday, isAfter } from 'date-fns'; import { CacheService } from '../cache/cache.service'; import { RedisCacheService } from '../redis-cache/redis-cache.service'; @Injectable() export class OrderService { public constructor( private readonly cacheService: CacheService, private readonly dataGatheringService: DataGatheringService, private readonly redisCacheService: RedisCacheService, private prisma: PrismaService ) {} public async order( orderWhereUniqueInput: Prisma.OrderWhereUniqueInput ): Promise { return this.prisma.order.findUnique({ where: orderWhereUniqueInput }); } public async orders(params: { include?: Prisma.OrderInclude; skip?: number; take?: number; cursor?: Prisma.OrderWhereUniqueInput; where?: Prisma.OrderWhereInput; orderBy?: Prisma.OrderOrderByInput; }): Promise { const { include, skip, take, cursor, where, orderBy } = params; return this.prisma.order.findMany({ cursor, include, orderBy, skip, take, where }); } public async createOrder( data: Prisma.OrderCreateInput, aUserId: string ): Promise { this.redisCacheService.remove(`${aUserId}.portfolio`); if (!isAfter(data.date as Date, endOfToday())) { // Gather symbol data of order in the background, if not draft this.dataGatheringService.gatherSymbols([ { dataSource: data.dataSource, date: data.date, symbol: data.symbol } ]); } this.dataGatheringService.gatherProfileData([data.symbol]); await this.cacheService.flush(aUserId); return this.prisma.order.create({ data }); } public async deleteOrder( where: Prisma.OrderWhereUniqueInput, aUserId: string ): Promise { this.redisCacheService.remove(`${aUserId}.portfolio`); return this.prisma.order.delete({ where }); } public async updateOrder( params: { where: Prisma.OrderWhereUniqueInput; data: Prisma.OrderUpdateInput; }, aUserId: string ): Promise { const { data, where } = params; this.redisCacheService.remove(`${aUserId}.portfolio`); // Gather symbol data of order in the background this.dataGatheringService.gatherSymbols([ { dataSource: data.dataSource, date: data.date, symbol: data.symbol } ]); await this.cacheService.flush(aUserId); return this.prisma.order.update({ data, where }); } }