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. 47
      apps/api/src/app/portfolio/portfolio.service.ts

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

@ -1575,28 +1575,25 @@ 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(
this.exchangeRateDataService.toCurrency(
fee, fee,
SymbolProfile.currency, SymbolProfile.currency,
userCurrency userCurrency
)
); );
}) })
.reduce(
(previous, current) => new Big(previous).plus(current),
new Big(0)
); );
} }
@ -1745,15 +1742,16 @@ export class PortfolioService {
} }
} }
const dividendInBaseCurrency = ( const dividendInBaseCurrency = getSum(
(
await this.getDividends({ await this.getDividends({
activities: activities.filter(({ type }) => { activities: activities.filter(({ type }) => {
return type === 'DIVIDEND'; return type === 'DIVIDEND';
}) })
}) })
).reduce( ).map(({ investment }) => {
(previous, current) => new Big(previous).plus(current.investment), return new Big(investment);
new Big(0) })
); );
const emergencyFund = new Big( const emergencyFund = new Big(
@ -1919,33 +1917,26 @@ 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)) &&
activity.type === activityType
);
}) })
.map(({ quantity, SymbolProfile, unitPrice }) => { .map(({ quantity, SymbolProfile, unitPrice }) => {
return this.exchangeRateDataService.toCurrency( return new Big(
this.exchangeRateDataService.toCurrency(
new Big(quantity).mul(unitPrice).toNumber(), new Big(quantity).mul(unitPrice).toNumber(),
SymbolProfile.currency, SymbolProfile.currency,
userCurrency userCurrency
)
); );
}) })
.reduce(
(previous, current) => new Big(previous).plus(current),
new Big(0)
); );
} }

Loading…
Cancel
Save