|
@ -107,15 +107,19 @@ export class PortfolioService { |
|
|
this.baseCurrency = this.configurationService.get('BASE_CURRENCY'); |
|
|
this.baseCurrency = this.configurationService.get('BASE_CURRENCY'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async getAccounts( |
|
|
public async getAccounts({ |
|
|
aUserId: string, |
|
|
filters, |
|
|
aFilters?: Filter[], |
|
|
userId, |
|
|
withExcludedAccounts = false |
|
|
withExcludedAccounts = false |
|
|
): Promise<AccountWithValue[]> { |
|
|
}: { |
|
|
const where: Prisma.AccountWhereInput = { userId: aUserId }; |
|
|
filters?: Filter[]; |
|
|
|
|
|
userId: string; |
|
|
|
|
|
withExcludedAccounts?: boolean; |
|
|
|
|
|
}): Promise<AccountWithValue[]> { |
|
|
|
|
|
const where: Prisma.AccountWhereInput = { userId: userId }; |
|
|
|
|
|
|
|
|
if (aFilters?.[0].id && aFilters?.[0].type === 'ACCOUNT') { |
|
|
if (filters?.[0].id && filters?.[0].type === 'ACCOUNT') { |
|
|
where.id = aFilters[0].id; |
|
|
where.id = filters[0].id; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const [accounts, details] = await Promise.all([ |
|
|
const [accounts, details] = await Promise.all([ |
|
@ -124,13 +128,12 @@ export class PortfolioService { |
|
|
include: { Order: true, Platform: true }, |
|
|
include: { Order: true, Platform: true }, |
|
|
orderBy: { name: 'asc' } |
|
|
orderBy: { name: 'asc' } |
|
|
}), |
|
|
}), |
|
|
this.getDetails( |
|
|
this.getDetails({ |
|
|
aUserId, |
|
|
filters, |
|
|
aUserId, |
|
|
userId, |
|
|
undefined, |
|
|
withExcludedAccounts, |
|
|
aFilters, |
|
|
impersonationId: userId |
|
|
withExcludedAccounts |
|
|
}) |
|
|
) |
|
|
|
|
|
]); |
|
|
]); |
|
|
|
|
|
|
|
|
const userCurrency = this.request.user.Settings.settings.baseCurrency; |
|
|
const userCurrency = this.request.user.Settings.settings.baseCurrency; |
|
@ -168,16 +171,20 @@ export class PortfolioService { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async getAccountsWithAggregations( |
|
|
public async getAccountsWithAggregations({ |
|
|
aUserId: string, |
|
|
filters, |
|
|
aFilters?: Filter[], |
|
|
userId, |
|
|
withExcludedAccounts = false |
|
|
withExcludedAccounts = false |
|
|
): Promise<Accounts> { |
|
|
}: { |
|
|
const accounts = await this.getAccounts( |
|
|
filters?: Filter[]; |
|
|
aUserId, |
|
|
userId: string; |
|
|
aFilters, |
|
|
withExcludedAccounts?: boolean; |
|
|
|
|
|
}): Promise<Accounts> { |
|
|
|
|
|
const accounts = await this.getAccounts({ |
|
|
|
|
|
filters, |
|
|
|
|
|
userId, |
|
|
withExcludedAccounts |
|
|
withExcludedAccounts |
|
|
); |
|
|
}); |
|
|
let totalBalanceInBaseCurrency = new Big(0); |
|
|
let totalBalanceInBaseCurrency = new Big(0); |
|
|
let totalValueInBaseCurrency = new Big(0); |
|
|
let totalValueInBaseCurrency = new Big(0); |
|
|
let transactionCount = 0; |
|
|
let transactionCount = 0; |
|
@ -421,14 +428,21 @@ export class PortfolioService { |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async getDetails( |
|
|
public async getDetails({ |
|
|
aImpersonationId: string, |
|
|
impersonationId, |
|
|
aUserId: string, |
|
|
userId, |
|
|
aDateRange: DateRange = 'max', |
|
|
dateRange = 'max', |
|
|
aFilters?: Filter[], |
|
|
filters, |
|
|
withExcludedAccounts = false |
|
|
withExcludedAccounts = false |
|
|
): Promise<PortfolioDetails & { hasErrors: boolean }> { |
|
|
}: { |
|
|
const userId = await this.getUserId(aImpersonationId, aUserId); |
|
|
impersonationId: string; |
|
|
|
|
|
userId: string; |
|
|
|
|
|
dateRange?: DateRange; |
|
|
|
|
|
filters?: Filter[]; |
|
|
|
|
|
withExcludedAccounts?: boolean; |
|
|
|
|
|
}): Promise<PortfolioDetails & { hasErrors: boolean }> { |
|
|
|
|
|
// TODO:
|
|
|
|
|
|
userId = await this.getUserId(impersonationId, userId); |
|
|
const user = await this.userService.user({ id: userId }); |
|
|
const user = await this.userService.user({ id: userId }); |
|
|
|
|
|
|
|
|
const emergencyFund = new Big( |
|
|
const emergencyFund = new Big( |
|
@ -441,9 +455,9 @@ export class PortfolioService { |
|
|
|
|
|
|
|
|
const { orders, portfolioOrders, transactionPoints } = |
|
|
const { orders, portfolioOrders, transactionPoints } = |
|
|
await this.getTransactionPoints({ |
|
|
await this.getTransactionPoints({ |
|
|
|
|
|
filters, |
|
|
userId, |
|
|
userId, |
|
|
withExcludedAccounts, |
|
|
withExcludedAccounts |
|
|
filters: aFilters |
|
|
|
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
const portfolioCalculator = new PortfolioCalculator({ |
|
|
const portfolioCalculator = new PortfolioCalculator({ |
|
@ -457,15 +471,15 @@ export class PortfolioService { |
|
|
const portfolioStart = parseDate( |
|
|
const portfolioStart = parseDate( |
|
|
transactionPoints[0]?.date ?? format(new Date(), DATE_FORMAT) |
|
|
transactionPoints[0]?.date ?? format(new Date(), DATE_FORMAT) |
|
|
); |
|
|
); |
|
|
const startDate = this.getStartDate(aDateRange, portfolioStart); |
|
|
const startDate = this.getStartDate(dateRange, portfolioStart); |
|
|
const currentPositions = await portfolioCalculator.getCurrentPositions( |
|
|
const currentPositions = await portfolioCalculator.getCurrentPositions( |
|
|
startDate |
|
|
startDate |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
|
const cashDetails = await this.accountService.getCashDetails({ |
|
|
const cashDetails = await this.accountService.getCashDetails({ |
|
|
|
|
|
filters, |
|
|
userId, |
|
|
userId, |
|
|
currency: userCurrency, |
|
|
currency: userCurrency |
|
|
filters: aFilters |
|
|
|
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
const holdings: PortfolioDetails['holdings'] = {}; |
|
|
const holdings: PortfolioDetails['holdings'] = {}; |
|
@ -475,10 +489,10 @@ export class PortfolioService { |
|
|
let filteredValueInBaseCurrency = currentPositions.currentValue; |
|
|
let filteredValueInBaseCurrency = currentPositions.currentValue; |
|
|
|
|
|
|
|
|
if ( |
|
|
if ( |
|
|
aFilters?.length === 0 || |
|
|
filters?.length === 0 || |
|
|
(aFilters?.length === 1 && |
|
|
(filters?.length === 1 && |
|
|
aFilters[0].type === 'ASSET_CLASS' && |
|
|
filters[0].type === 'ASSET_CLASS' && |
|
|
aFilters[0].id === 'CASH') |
|
|
filters[0].id === 'CASH') |
|
|
) { |
|
|
) { |
|
|
filteredValueInBaseCurrency = filteredValueInBaseCurrency.plus( |
|
|
filteredValueInBaseCurrency = filteredValueInBaseCurrency.plus( |
|
|
cashDetails.balanceInBaseCurrency |
|
|
cashDetails.balanceInBaseCurrency |
|
@ -574,10 +588,10 @@ export class PortfolioService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ( |
|
|
if ( |
|
|
aFilters?.length === 0 || |
|
|
filters?.length === 0 || |
|
|
(aFilters?.length === 1 && |
|
|
(filters?.length === 1 && |
|
|
aFilters[0].type === 'ASSET_CLASS' && |
|
|
filters[0].type === 'ASSET_CLASS' && |
|
|
aFilters[0].id === 'CASH') |
|
|
filters[0].id === 'CASH') |
|
|
) { |
|
|
) { |
|
|
const cashPositions = await this.getCashPositions({ |
|
|
const cashPositions = await this.getCashPositions({ |
|
|
cashDetails, |
|
|
cashDetails, |
|
@ -593,15 +607,15 @@ export class PortfolioService { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const accounts = await this.getValueOfAccounts({ |
|
|
const accounts = await this.getValueOfAccounts({ |
|
|
|
|
|
filters, |
|
|
orders, |
|
|
orders, |
|
|
portfolioItemsNow, |
|
|
portfolioItemsNow, |
|
|
userCurrency, |
|
|
userCurrency, |
|
|
userId, |
|
|
userId, |
|
|
withExcludedAccounts, |
|
|
withExcludedAccounts |
|
|
filters: aFilters |
|
|
|
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
const summary = await this.getSummary(aImpersonationId); |
|
|
const summary = await this.getSummary(impersonationId); |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
accounts, |
|
|
accounts, |
|
|