From d8bfb23f208867a0d5483f97046e761465c36c4a Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 9 Mar 2024 16:53:59 +0100 Subject: [PATCH] Refactor reduce() with getSum() (#3121) --- .../src/app/portfolio/portfolio.service.ts | 85 +++++++++---------- 1 file changed, 38 insertions(+), 47 deletions(-) diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 78a803e9e..5848a78c5 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -1575,29 +1575,26 @@ export class PortfolioService { private getFees({ activities, - date = new Date(0), userCurrency }: { activities: OrderWithAccount[]; - date?: Date; userCurrency: string; }) { - return activities - .filter((activity) => { - // Filter out all activities before given date (drafts) - return isBefore(date, new Date(activity.date)); - }) - .map(({ fee, SymbolProfile }) => { - return this.exchangeRateDataService.toCurrency( - fee, - SymbolProfile.currency, - userCurrency - ); - }) - .reduce( - (previous, current) => new Big(previous).plus(current), - new Big(0) - ); + return getSum( + activities + .filter(({ isDraft }) => { + return isDraft === false; + }) + .map(({ fee, SymbolProfile }) => { + return new Big( + this.exchangeRateDataService.toCurrency( + fee, + SymbolProfile.currency, + userCurrency + ) + ); + }) + ); } private getInitialCashPosition({ @@ -1745,15 +1742,16 @@ export class PortfolioService { } } - const dividendInBaseCurrency = ( - await this.getDividends({ - activities: activities.filter(({ type }) => { - return type === 'DIVIDEND'; + const dividendInBaseCurrency = getSum( + ( + await this.getDividends({ + activities: activities.filter(({ type }) => { + return type === 'DIVIDEND'; + }) }) + ).map(({ investment }) => { + return new Big(investment); }) - ).reduce( - (previous, current) => new Big(previous).plus(current.investment), - new Big(0) ); const emergencyFund = new Big( @@ -1919,34 +1917,27 @@ export class PortfolioService { private getSumOfActivityType({ activities, activityType, - date = new Date(0), userCurrency }: { activities: OrderWithAccount[]; activityType: ActivityType; - date?: Date; userCurrency: string; }) { - return activities - .filter((activity) => { - // Filter out all activities before given date (drafts) and - // activity type - return ( - isBefore(date, new Date(activity.date)) && - activity.type === activityType - ); - }) - .map(({ quantity, SymbolProfile, unitPrice }) => { - return this.exchangeRateDataService.toCurrency( - new Big(quantity).mul(unitPrice).toNumber(), - SymbolProfile.currency, - userCurrency - ); - }) - .reduce( - (previous, current) => new Big(previous).plus(current), - new Big(0) - ); + return getSum( + activities + .filter(({ isDraft, type }) => { + return isDraft === false && type === activityType; + }) + .map(({ quantity, SymbolProfile, unitPrice }) => { + return new Big( + this.exchangeRateDataService.toCurrency( + new Big(quantity).mul(unitPrice).toNumber(), + SymbolProfile.currency, + userCurrency + ) + ); + }) + ); } private async getTransactionPoints({