@ -207,11 +207,16 @@ export class PortfolioService {
} ;
}
public async getInvestments (
aImpersonationId : string ,
groupBy? : GroupBy
) : Promise < InvestmentItem [ ] > {
const userId = await this . getUserId ( aImpersonationId , this . request . user . id ) ;
public async getInvestments ( {
dateRange ,
impersonationId ,
groupBy
} : {
dateRange : DateRange ;
impersonationId : string ;
groupBy? : GroupBy ;
} ) : Promise < InvestmentItem [ ] > {
const userId = await this . getUserId ( impersonationId , this . request . user . id ) ;
const { portfolioOrders , transactionPoints } =
await this . getTransactionPoints ( {
@ -283,98 +288,18 @@ export class PortfolioService {
}
}
return sortBy ( investments , ( investment ) = > {
investments = sortBy ( investments , ( investment ) = > {
return investment . date ;
} ) ;
}
public async getChart (
aImpersonationId : string ,
aDateRange : DateRange = 'max'
) : Promise < HistoricalDataContainer > {
const userId = await this . getUserId ( aImpersonationId , this . request . user . id ) ;
const { portfolioOrders , transactionPoints } =
await this . getTransactionPoints ( {
userId
} ) ;
const portfolioCalculator = new PortfolioCalculator ( {
currency : this.request.user.Settings.settings.baseCurrency ,
currentRateService : this.currentRateService ,
orders : portfolioOrders
} ) ;
portfolioCalculator . setTransactionPoints ( transactionPoints ) ;
if ( transactionPoints . length === 0 ) {
return {
isAllTimeHigh : false ,
isAllTimeLow : false ,
items : [ ]
} ;
}
let portfolioStart = parse (
transactionPoints [ 0 ] . date ,
DATE_FORMAT ,
new Date ( )
) ;
// Get start date for the full portfolio because of because of the
// min and max calculation
portfolioStart = this . getStartDate ( 'max' , portfolioStart ) ;
const timelineSpecification : TimelineSpecification [ ] = [
{
start : format ( portfolioStart , DATE_FORMAT ) ,
accuracy : 'day'
}
] ;
const timelineInfo = await portfolioCalculator . calculateTimeline (
timelineSpecification ,
format ( new Date ( ) , DATE_FORMAT )
) ;
const timeline = timelineInfo . timelinePeriods ;
const items = timeline
. filter ( ( timelineItem ) = > timelineItem !== null )
. map ( ( timelineItem ) = > ( {
date : timelineItem.date ,
value : timelineItem.netPerformance.toNumber ( )
} ) ) ;
let lastItem = null ;
if ( timeline . length > 0 ) {
lastItem = timeline [ timeline . length - 1 ] ;
}
let isAllTimeHigh = timelineInfo . maxNetPerformance ? . eq (
lastItem ? . netPerformance ? ? 0
) ;
let isAllTimeLow = timelineInfo . minNetPerformance ? . eq (
lastItem ? . netPerformance ? ? 0
) ;
if ( isAllTimeHigh && isAllTimeLow ) {
isAllTimeHigh = false ;
isAllTimeLow = false ;
}
portfolioStart = startOfDay (
this . getStartDate (
aDateRange ,
parse ( transactionPoints [ 0 ] . date , DATE_FORMAT , new Date ( ) )
)
const startDate = this . getStartDate (
dateRange ,
parseDate ( investments [ 0 ] ? . date )
) ;
return {
isAllTimeHigh ,
isAllTimeLow ,
items : items.filter ( ( item ) = > {
// Filter items of date range
return ! isAfter ( portfolioStart , parseDate ( item . date ) ) ;
} )
} ;
return investments . filter ( ( { date } ) = > {
return ! isBefore ( parseDate ( date ) , startDate ) ;
} ) ;
}
public async getChartV2 ( {
@ -441,7 +366,7 @@ export class PortfolioService {
filters? : Filter [ ] ;
withExcludedAccounts? : boolean ;
} ) : Promise < PortfolioDetails & { hasErrors : boolean } > {
// TODO:
// TODO
userId = await this . getUserId ( impersonationId , userId ) ;
const user = await this . userService . user ( { id : userId } ) ;
@ -979,13 +904,15 @@ export class PortfolioService {
if ( transactionPoints ? . length <= 0 ) {
return {
chart : [ ] ,
firstOrderDate : undefined ,
hasErrors : false ,
performance : {
currentGrossPerformance : 0 ,
currentGrossPerformancePercent : 0 ,
currentNetPerformance : 0 ,
currentNetPerformancePercent : 0 ,
currentValue : 0
currentValue : 0 ,
totalInvestment : 0
}
} ;
}
@ -1006,6 +933,7 @@ export class PortfolioService {
let currentNetPerformance = currentPositions . netPerformance ;
let currentNetPerformancePercent =
currentPositions . netPerformancePercentage ;
const totalInvestment = currentPositions . totalInvestment ;
// if (currentGrossPerformance.mul(currentGrossPerformancePercent).lt(0)) {
// // If algebraic sign is different, harmonize it
@ -1035,14 +963,24 @@ export class PortfolioService {
return {
chart : historicalDataContainer.items.map (
( { date , netPerformanceInPercentage } ) = > {
( {
date ,
netPerformance ,
netPerformanceInPercentage ,
totalInvestment ,
value
} ) = > {
return {
date ,
value : netPerformanceInPercentage
netPerformance ,
netPerformanceInPercentage ,
totalInvestment ,
value
} ;
}
) ,
errors : currentPositions.errors ,
firstOrderDate : parseDate ( historicalDataContainer . items [ 0 ] ? . date ) ,
hasErrors : currentPositions.hasErrors || hasErrors ,
performance : {
currentValue ,
@ -1050,7 +988,8 @@ export class PortfolioService {
currentGrossPerformancePercent :
currentGrossPerformancePercent . toNumber ( ) ,
currentNetPerformance : currentNetPerformance.toNumber ( ) ,
currentNetPerformancePercent : currentNetPerformancePercent.toNumber ( )
currentNetPerformancePercent : currentNetPerformancePercent.toNumber ( ) ,
totalInvestment : totalInvestment.toNumber ( )
}
} ;
}