From 454b578c7f11fcc01398fe11e44d4eacf1a5dd0a Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sun, 19 Dec 2021 16:26:28 +0100 Subject: [PATCH] Filter order types in portfolio service --- apps/api/src/app/order/order.service.ts | 14 +++++++++++++- apps/api/src/app/portfolio/portfolio.service.ts | 17 ++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/apps/api/src/app/order/order.service.ts b/apps/api/src/app/order/order.service.ts index eb58b889e..1654380ae 100644 --- a/apps/api/src/app/order/order.service.ts +++ b/apps/api/src/app/order/order.service.ts @@ -3,7 +3,7 @@ import { DataGatheringService } from '@ghostfolio/api/services/data-gathering.se 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 { DataSource, Order, Prisma, Type as TypeOfOrder } from '@prisma/client'; import Big from 'big.js'; import { endOfToday, isAfter } from 'date-fns'; @@ -85,9 +85,11 @@ export class OrderService { public async getOrders({ includeDrafts = false, + types, userId }: { includeDrafts?: boolean; + types?: TypeOfOrder[]; userId: string; }) { const where: Prisma.OrderWhereInput = { userId }; @@ -96,6 +98,16 @@ export class OrderService { where.isDraft = false; } + if (types) { + where.OR = types.map((type) => { + return { + type: { + equals: type + } + }; + }); + } + return ( await this.orders({ where, diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index c6bb6051e..eb0d60ed5 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -369,9 +369,9 @@ export class PortfolioService { ): Promise { const userId = await this.getUserId(aImpersonationId, this.request.user.id); - const orders = (await this.orderService.getOrders({ userId })).filter( - (order) => order.symbol === aSymbol - ); + const orders = ( + await this.orderService.getOrders({ userId, types: ['BUY', 'SELL'] }) + ).filter((order) => order.symbol === aSymbol); if (orders.length <= 0) { return { @@ -828,7 +828,10 @@ export class PortfolioService { userId, currency ); - const orders = await this.orderService.getOrders({ userId }); + const orders = await this.orderService.getOrders({ + userId, + types: ['BUY', 'SELL'] + }); const dividend = this.getDividend(orders).toNumber(); const fees = this.getFees(orders).toNumber(); const firstOrderDate = orders[0]?.date; @@ -994,7 +997,11 @@ export class PortfolioService { transactionPoints: TransactionPoint[]; orders: OrderWithAccount[]; }> { - const orders = await this.orderService.getOrders({ includeDrafts, userId }); + const orders = await this.orderService.getOrders({ + includeDrafts, + userId, + types: ['BUY', 'SELL'] + }); if (orders.length <= 0) { return { transactionPoints: [], orders: [] };