Browse Source

Filter order types in portfolio service

pull/547/head
Thomas 4 years ago
parent
commit
454b578c7f
  1. 14
      apps/api/src/app/order/order.service.ts
  2. 17
      apps/api/src/app/portfolio/portfolio.service.ts

14
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 { PrismaService } from '@ghostfolio/api/services/prisma.service';
import { OrderWithAccount } from '@ghostfolio/common/types'; import { OrderWithAccount } from '@ghostfolio/common/types';
import { Injectable } from '@nestjs/common'; 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 Big from 'big.js';
import { endOfToday, isAfter } from 'date-fns'; import { endOfToday, isAfter } from 'date-fns';
@ -85,9 +85,11 @@ export class OrderService {
public async getOrders({ public async getOrders({
includeDrafts = false, includeDrafts = false,
types,
userId userId
}: { }: {
includeDrafts?: boolean; includeDrafts?: boolean;
types?: TypeOfOrder[];
userId: string; userId: string;
}) { }) {
const where: Prisma.OrderWhereInput = { userId }; const where: Prisma.OrderWhereInput = { userId };
@ -96,6 +98,16 @@ export class OrderService {
where.isDraft = false; where.isDraft = false;
} }
if (types) {
where.OR = types.map((type) => {
return {
type: {
equals: type
}
};
});
}
return ( return (
await this.orders({ await this.orders({
where, where,

17
apps/api/src/app/portfolio/portfolio.service.ts

@ -369,9 +369,9 @@ export class PortfolioService {
): Promise<PortfolioPositionDetail> { ): Promise<PortfolioPositionDetail> {
const userId = await this.getUserId(aImpersonationId, this.request.user.id); const userId = await this.getUserId(aImpersonationId, this.request.user.id);
const orders = (await this.orderService.getOrders({ userId })).filter( const orders = (
(order) => order.symbol === aSymbol await this.orderService.getOrders({ userId, types: ['BUY', 'SELL'] })
); ).filter((order) => order.symbol === aSymbol);
if (orders.length <= 0) { if (orders.length <= 0) {
return { return {
@ -828,7 +828,10 @@ export class PortfolioService {
userId, userId,
currency 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 dividend = this.getDividend(orders).toNumber();
const fees = this.getFees(orders).toNumber(); const fees = this.getFees(orders).toNumber();
const firstOrderDate = orders[0]?.date; const firstOrderDate = orders[0]?.date;
@ -994,7 +997,11 @@ export class PortfolioService {
transactionPoints: TransactionPoint[]; transactionPoints: TransactionPoint[];
orders: OrderWithAccount[]; 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) { if (orders.length <= 0) {
return { transactionPoints: [], orders: [] }; return { transactionPoints: [], orders: [] };

Loading…
Cancel
Save