|
|
@ -1486,52 +1486,6 @@ export class PortfolioService { |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
private getItems(activities: OrderWithAccount[], date = new Date(0)) { |
|
|
|
return activities |
|
|
|
.filter((activity) => { |
|
|
|
// Filter out all activities before given date (drafts) and type item
|
|
|
|
return ( |
|
|
|
isBefore(date, new Date(activity.date)) && |
|
|
|
activity.type === TypeOfOrder.ITEM |
|
|
|
); |
|
|
|
}) |
|
|
|
.map(({ quantity, SymbolProfile, unitPrice }) => { |
|
|
|
return this.exchangeRateDataService.toCurrency( |
|
|
|
new Big(quantity).mul(unitPrice).toNumber(), |
|
|
|
SymbolProfile.currency, |
|
|
|
this.request.user.Settings.settings.baseCurrency |
|
|
|
); |
|
|
|
}) |
|
|
|
.reduce( |
|
|
|
(previous, current) => new Big(previous).plus(current), |
|
|
|
new Big(0) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
private getLiabilities({ |
|
|
|
activities, |
|
|
|
userCurrency |
|
|
|
}: { |
|
|
|
activities: OrderWithAccount[]; |
|
|
|
userCurrency: string; |
|
|
|
}) { |
|
|
|
return activities |
|
|
|
.filter(({ type }) => { |
|
|
|
return type === TypeOfOrder.LIABILITY; |
|
|
|
}) |
|
|
|
.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) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
private getStartDate(aDateRange: DateRange, portfolioStart: Date) { |
|
|
|
switch (aDateRange) { |
|
|
|
case '1d': |
|
|
@ -1562,39 +1516,6 @@ export class PortfolioService { |
|
|
|
return portfolioStart; |
|
|
|
} |
|
|
|
|
|
|
|
private getSumOfActivityType({ |
|
|
|
activities, |
|
|
|
activityType, |
|
|
|
date = new Date(0), |
|
|
|
userCurrency |
|
|
|
}: { |
|
|
|
activities: OrderWithAccount[]; |
|
|
|
activityType: TypeOfOrder; |
|
|
|
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) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
private getStreaks({ |
|
|
|
investments, |
|
|
|
savingsRate |
|
|
@ -1671,23 +1592,44 @@ export class PortfolioService { |
|
|
|
userCurrency, |
|
|
|
activityType: TypeOfOrder.INTEREST |
|
|
|
}).toNumber(); |
|
|
|
const items = this.getItems(activities).toNumber(); |
|
|
|
const liabilities = this.getLiabilities({ |
|
|
|
const items = this.getSumOfActivityType({ |
|
|
|
activities, |
|
|
|
userCurrency |
|
|
|
userCurrency, |
|
|
|
activityType: 'ITEM' |
|
|
|
}).toNumber(); |
|
|
|
const liabilities = this.getSumOfActivityType({ |
|
|
|
activities, |
|
|
|
userCurrency, |
|
|
|
activityType: 'LIABILITY' |
|
|
|
}).toNumber(); |
|
|
|
|
|
|
|
const totalBuy = this.getTotalByType(activities, userCurrency, 'BUY'); |
|
|
|
const totalSell = this.getTotalByType(activities, userCurrency, 'SELL'); |
|
|
|
const totalBuy = this.getSumOfActivityType({ |
|
|
|
activities, |
|
|
|
userCurrency, |
|
|
|
activityType: 'BUY' |
|
|
|
}).toNumber(); |
|
|
|
const totalSell = this.getSumOfActivityType({ |
|
|
|
activities, |
|
|
|
userCurrency, |
|
|
|
activityType: 'SELL' |
|
|
|
}).toNumber(); |
|
|
|
|
|
|
|
const cash = new Big(balanceInBaseCurrency) |
|
|
|
.minus(emergencyFund) |
|
|
|
.plus(emergencyFundPositionsValueInBaseCurrency) |
|
|
|
.toNumber(); |
|
|
|
const committedFunds = new Big(totalBuy).minus(totalSell); |
|
|
|
const totalOfExcludedActivities = new Big( |
|
|
|
this.getTotalByType(excludedActivities, userCurrency, 'BUY') |
|
|
|
).minus(this.getTotalByType(excludedActivities, userCurrency, 'SELL')); |
|
|
|
const totalOfExcludedActivities = this.getSumOfActivityType({ |
|
|
|
userCurrency, |
|
|
|
activities: excludedActivities, |
|
|
|
activityType: 'BUY' |
|
|
|
}).minus( |
|
|
|
this.getSumOfActivityType({ |
|
|
|
userCurrency, |
|
|
|
activities: excludedActivities, |
|
|
|
activityType: 'SELL' |
|
|
|
}) |
|
|
|
); |
|
|
|
|
|
|
|
const cashDetailsWithExcludedAccounts = |
|
|
|
await this.accountService.getCashDetails({ |
|
|
@ -1757,6 +1699,39 @@ export class PortfolioService { |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
private getSumOfActivityType({ |
|
|
|
activities, |
|
|
|
activityType, |
|
|
|
date = new Date(0), |
|
|
|
userCurrency |
|
|
|
}: { |
|
|
|
activities: OrderWithAccount[]; |
|
|
|
activityType: TypeOfOrder; |
|
|
|
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) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
private async getTransactionPoints({ |
|
|
|
filters, |
|
|
|
includeDrafts = false, |
|
|
@ -1828,6 +1803,21 @@ export class PortfolioService { |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
private getUserCurrency(aUser: UserWithSettings) { |
|
|
|
return ( |
|
|
|
aUser.Settings?.settings.baseCurrency ?? |
|
|
|
this.request.user?.Settings?.settings.baseCurrency ?? |
|
|
|
DEFAULT_CURRENCY |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
private async getUserId(aImpersonationId: string, aUserId: string) { |
|
|
|
const impersonationUserId = |
|
|
|
await this.impersonationService.validateImpersonationId(aImpersonationId); |
|
|
|
|
|
|
|
return impersonationUserId || aUserId; |
|
|
|
} |
|
|
|
|
|
|
|
private async getValueOfAccountsAndPlatforms({ |
|
|
|
filters = [], |
|
|
|
orders, |
|
|
@ -1971,38 +1961,4 @@ export class PortfolioService { |
|
|
|
|
|
|
|
return { accounts, platforms }; |
|
|
|
} |
|
|
|
|
|
|
|
private getTotalByType( |
|
|
|
orders: OrderWithAccount[], |
|
|
|
currency: string, |
|
|
|
type: TypeOfOrder |
|
|
|
) { |
|
|
|
return orders |
|
|
|
.filter( |
|
|
|
(order) => !isAfter(order.date, endOfToday()) && order.type === type |
|
|
|
) |
|
|
|
.map((order) => { |
|
|
|
return this.exchangeRateDataService.toCurrency( |
|
|
|
order.quantity * order.unitPrice, |
|
|
|
order.SymbolProfile.currency, |
|
|
|
currency |
|
|
|
); |
|
|
|
}) |
|
|
|
.reduce((previous, current) => previous + current, 0); |
|
|
|
} |
|
|
|
|
|
|
|
private getUserCurrency(aUser: UserWithSettings) { |
|
|
|
return ( |
|
|
|
aUser.Settings?.settings.baseCurrency ?? |
|
|
|
this.request.user?.Settings?.settings.baseCurrency ?? |
|
|
|
DEFAULT_CURRENCY |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
private async getUserId(aImpersonationId: string, aUserId: string) { |
|
|
|
const impersonationUserId = |
|
|
|
await this.impersonationService.validateImpersonationId(aImpersonationId); |
|
|
|
|
|
|
|
return impersonationUserId || aUserId; |
|
|
|
} |
|
|
|
} |
|
|
|