refactor PortfolioService#getOverview

Co-authored-by: Thomas <dotsilver@gmail.com>
pull/239/head
Valentin Zickner 3 years ago committed by Thomas
parent 328d814922
commit 4a0695613e

@ -29,16 +29,18 @@ import {
} from '@ghostfolio/common/types'; } from '@ghostfolio/common/types';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { REQUEST } from '@nestjs/core'; import { REQUEST } from '@nestjs/core';
import { DataSource } from '@prisma/client'; import { DataSource, Currency, Type as TypeOfOrder } from '@prisma/client';
import Big from 'big.js'; import Big from 'big.js';
import { import {
add, add,
addMonths, addMonths,
endOfToday,
format, format,
getDate, getDate,
getMonth, getMonth,
getYear, getYear,
isAfter, isAfter,
isBefore,
isSameDay, isSameDay,
max, max,
parse, parse,
@ -204,30 +206,25 @@ export class PortfolioService {
public async getOverview( public async getOverview(
aImpersonationId: string aImpersonationId: string
): Promise<PortfolioOverview> { ): Promise<PortfolioOverview> {
const impersonationUserId = const userId = await this.getUserId(aImpersonationId);
await this.impersonationService.validateImpersonationId(
aImpersonationId,
this.request.user.id
);
const portfolio = await this.createPortfolio(
impersonationUserId || this.request.user.id
);
const currency = this.request.user.Settings.currency;
const { balance } = await this.accountService.getCashDetails( const { balance } = await this.accountService.getCashDetails(
impersonationUserId || this.request.user.id, userId,
this.request.user.Settings.currency currency
); );
const committedFunds = portfolio.getCommittedFunds(); const orders = await this.getOrders(userId);
const fees = portfolio.getFees(); const fees = this.getFees(orders);
const totalBuy = this.getTotalByType(orders, currency, TypeOfOrder.BUY);
const totalSell = this.getTotalByType(orders, currency, TypeOfOrder.SELL);
return { return {
committedFunds, committedFunds: totalBuy - totalSell,
fees, fees,
cash: balance, cash: balance,
ordersCount: portfolio.getOrders().length, ordersCount: orders.length,
totalBuy: portfolio.getTotalBuy(), totalBuy: totalBuy,
totalSell: portfolio.getTotalSell() totalSell: totalSell
}; };
} }
@ -586,6 +583,22 @@ export class PortfolioService {
}; };
} }
public getFees(orders: OrderWithAccount[], date = new Date(0)) {
return orders
.filter((order) => {
// Filter out all orders before given date
return isBefore(date, new Date(order.date));
})
.map((order) => {
return this.exchangeRateDataService.toCurrency(
order.fee,
order.currency,
this.request.user.Settings.currency
);
})
.reduce((previous, current) => previous + current, 0);
}
private getStartDate(aDateRange: DateRange, portfolioStart: Date) { private getStartDate(aDateRange: DateRange, portfolioStart: Date) {
switch (aDateRange) { switch (aDateRange) {
case '1d': case '1d':
@ -742,4 +755,23 @@ export class PortfolioService {
return impersonationUserId || this.request.user.id; return impersonationUserId || this.request.user.id;
} }
private getTotalByType(
orders: OrderWithAccount[],
currency: Currency,
type: TypeOfOrder
) {
return orders
.filter(
(order) => !isAfter(order.date, endOfToday()) && order.type === type
)
.map((order) => {
return this.exchangeRateDataService.toCurrency(
order.quantity * order.unitPrice,
order.currency,
currency
);
})
.reduce((previous, current) => previous + current, 0);
}
} }

@ -5,13 +5,9 @@ import { Order } from '../order';
export interface PortfolioInterface { export interface PortfolioInterface {
get(aDate?: Date): PortfolioItem[]; get(aDate?: Date): PortfolioItem[];
getCommittedFunds(): number;
getFees(): number; getFees(): number;
getPositions( getPositions(aDate: Date): {
aDate: Date
): {
[symbol: string]: Position; [symbol: string]: Position;
}; };

@ -232,14 +232,6 @@ describe('Portfolio', () => {
} }
]); ]);
expect(portfolio.getCommittedFunds()).toEqual(
exchangeRateDataService.toCurrency(
1 * 49631.24,
Currency.USD,
baseCurrency
)
);
const details = await portfolio.getDetails('1d'); const details = await portfolio.getDetails('1d');
expect(details).toMatchObject({ expect(details).toMatchObject({
BTCUSD: { BTCUSD: {
@ -311,14 +303,6 @@ describe('Portfolio', () => {
} }
]); ]);
expect(portfolio.getCommittedFunds()).toEqual(
exchangeRateDataService.toCurrency(
0.2 * 991.49,
Currency.USD,
baseCurrency
)
);
expect(portfolio.getFees()).toEqual(0); expect(portfolio.getFees()).toEqual(0);
expect(portfolio.getPositions(getYesterday())).toMatchObject({ expect(portfolio.getPositions(getYesterday())).toMatchObject({
@ -378,19 +362,6 @@ describe('Portfolio', () => {
} }
]); ]);
expect(portfolio.getCommittedFunds()).toEqual(
exchangeRateDataService.toCurrency(
0.2 * 991.49,
Currency.USD,
baseCurrency
) +
exchangeRateDataService.toCurrency(
0.3 * 1050,
Currency.USD,
baseCurrency
)
);
expect(portfolio.getFees()).toEqual(0); expect(portfolio.getFees()).toEqual(0);
expect(portfolio.getPositions(getYesterday())).toMatchObject({ expect(portfolio.getPositions(getYesterday())).toMatchObject({
@ -456,19 +427,6 @@ describe('Portfolio', () => {
} }
]); ]);
expect(portfolio.getCommittedFunds()).toEqual(
exchangeRateDataService.toCurrency(
0.05614682 * 3562.089535970158,
Currency.EUR,
baseCurrency
) +
exchangeRateDataService.toCurrency(
0.2 * 991.49,
Currency.USD,
baseCurrency
)
);
expect(portfolio.getFees()).toEqual( expect(portfolio.getFees()).toEqual(
exchangeRateDataService.toCurrency(2.99, Currency.EUR, baseCurrency) + exchangeRateDataService.toCurrency(2.99, Currency.EUR, baseCurrency) +
exchangeRateDataService.toCurrency(2.99, Currency.USD, baseCurrency) exchangeRateDataService.toCurrency(2.99, Currency.USD, baseCurrency)
@ -564,24 +522,6 @@ describe('Portfolio', () => {
} }
]); ]);
expect(portfolio.getCommittedFunds()).toEqual(
exchangeRateDataService.toCurrency(
0.2 * 991.49,
Currency.USD,
baseCurrency
) -
exchangeRateDataService.toCurrency(
0.1 * 1050,
Currency.USD,
baseCurrency
) +
exchangeRateDataService.toCurrency(
0.2 * 1050,
Currency.USD,
baseCurrency
)
);
expect(portfolio.getFees()).toEqual( expect(portfolio.getFees()).toEqual(
exchangeRateDataService.toCurrency(3, Currency.USD, baseCurrency) exchangeRateDataService.toCurrency(3, Currency.USD, baseCurrency)
); );

@ -225,10 +225,6 @@ export class Portfolio implements PortfolioInterface {
return cloneDeep(this.portfolioItems); return cloneDeep(this.portfolioItems);
} }
public getCommittedFunds() {
return this.getTotalBuy() - this.getTotalSell();
}
public async getDetails( public async getDetails(
aDateRange: DateRange = 'max' aDateRange: DateRange = 'max'
): Promise<{ [symbol: string]: PortfolioPosition }> { ): Promise<{ [symbol: string]: PortfolioPosition }> {

Loading…
Cancel
Save