Browse Source

Refactor reduce() with getSum() (#3121)

pull/3124/head
Thomas Kaul 11 months ago
committed by GitHub
parent
commit
d8bfb23f20
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 85
      apps/api/src/app/portfolio/portfolio.service.ts

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

@ -1575,29 +1575,26 @@ export class PortfolioService {
private getFees({ private getFees({
activities, activities,
date = new Date(0),
userCurrency userCurrency
}: { }: {
activities: OrderWithAccount[]; activities: OrderWithAccount[];
date?: Date;
userCurrency: string; userCurrency: string;
}) { }) {
return activities return getSum(
.filter((activity) => { activities
// Filter out all activities before given date (drafts) .filter(({ isDraft }) => {
return isBefore(date, new Date(activity.date)); return isDraft === false;
}) })
.map(({ fee, SymbolProfile }) => { .map(({ fee, SymbolProfile }) => {
return this.exchangeRateDataService.toCurrency( return new Big(
fee, this.exchangeRateDataService.toCurrency(
SymbolProfile.currency, fee,
userCurrency SymbolProfile.currency,
); userCurrency
}) )
.reduce( );
(previous, current) => new Big(previous).plus(current), })
new Big(0) );
);
} }
private getInitialCashPosition({ private getInitialCashPosition({
@ -1745,15 +1742,16 @@ export class PortfolioService {
} }
} }
const dividendInBaseCurrency = ( const dividendInBaseCurrency = getSum(
await this.getDividends({ (
activities: activities.filter(({ type }) => { await this.getDividends({
return type === 'DIVIDEND'; 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( const emergencyFund = new Big(
@ -1919,34 +1917,27 @@ export class PortfolioService {
private getSumOfActivityType({ private getSumOfActivityType({
activities, activities,
activityType, activityType,
date = new Date(0),
userCurrency userCurrency
}: { }: {
activities: OrderWithAccount[]; activities: OrderWithAccount[];
activityType: ActivityType; activityType: ActivityType;
date?: Date;
userCurrency: string; userCurrency: string;
}) { }) {
return activities return getSum(
.filter((activity) => { activities
// Filter out all activities before given date (drafts) and .filter(({ isDraft, type }) => {
// activity type return isDraft === false && type === activityType;
return ( })
isBefore(date, new Date(activity.date)) && .map(({ quantity, SymbolProfile, unitPrice }) => {
activity.type === activityType return new Big(
); this.exchangeRateDataService.toCurrency(
}) new Big(quantity).mul(unitPrice).toNumber(),
.map(({ quantity, SymbolProfile, unitPrice }) => { SymbolProfile.currency,
return this.exchangeRateDataService.toCurrency( userCurrency
new Big(quantity).mul(unitPrice).toNumber(), )
SymbolProfile.currency, );
userCurrency })
); );
})
.reduce(
(previous, current) => new Big(previous).plus(current),
new Big(0)
);
} }
private async getTransactionPoints({ private async getTransactionPoints({

Loading…
Cancel
Save