Browse Source

Add missing accounts to portfolio details

pull/308/head
Thomas 4 years ago
parent
commit
d850595005
  1. 47
      apps/api/src/app/portfolio/portfolio.service.ts

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

@ -209,7 +209,6 @@ export class PortfolioService {
for (const position of currentPositions.positions) { for (const position of currentPositions.positions) {
portfolioItemsNow[position.symbol] = position; portfolioItemsNow[position.symbol] = position;
} }
const accounts = this.getAccounts(orders, portfolioItemsNow, userCurrency);
for (const item of currentPositions.positions) { for (const item of currentPositions.positions) {
const value = item.quantity.mul(item.marketPrice); const value = item.quantity.mul(item.marketPrice);
@ -244,6 +243,13 @@ export class PortfolioService {
value: totalValue value: totalValue
}); });
const accounts = await this.getAccounts(
orders,
portfolioItemsNow,
userCurrency,
userId
);
return { accounts, holdings, hasErrors: currentPositions.hasErrors }; return { accounts, holdings, hasErrors: currentPositions.hasErrors };
} }
@ -601,7 +607,12 @@ export class PortfolioService {
for (const position of currentPositions.positions) { for (const position of currentPositions.positions) {
portfolioItemsNow[position.symbol] = position; portfolioItemsNow[position.symbol] = position;
} }
const accounts = this.getAccounts(orders, portfolioItemsNow, baseCurrency); const accounts = await this.getAccounts(
orders,
portfolioItemsNow,
baseCurrency,
userId
);
return { return {
rules: { rules: {
accountClusterRisk: await this.rulesService.evaluate( accountClusterRisk: await this.rulesService.evaluate(
@ -785,13 +796,37 @@ export class PortfolioService {
}; };
} }
private getAccounts( private async getAccounts(
orders: OrderWithAccount[], orders: OrderWithAccount[],
portfolioItemsNow: { [p: string]: TimelinePosition }, portfolioItemsNow: { [p: string]: TimelinePosition },
userCurrency userCurrency: Currency,
userId: string
) { ) {
const accounts: PortfolioDetails['accounts'] = {}; const accounts: PortfolioDetails['accounts'] = {};
for (const order of orders) {
const currentAccounts = await this.accountService.getAccounts(userId);
for (const account of currentAccounts) {
const ordersByAccount = orders.filter(({ accountId }) => {
return accountId === account.id;
});
if (ordersByAccount.length <= 0) {
// Add account without orders
const balance = this.exchangeRateDataService.toCurrency(
account.balance,
account.currency,
userCurrency
);
accounts[account.name] = {
current: balance,
original: balance
};
continue;
}
for (const order of ordersByAccount) {
let currentValueOfSymbol = this.exchangeRateDataService.toCurrency( let currentValueOfSymbol = this.exchangeRateDataService.toCurrency(
order.quantity * portfolioItemsNow[order.symbol].marketPrice, order.quantity * portfolioItemsNow[order.symbol].marketPrice,
order.currency, order.currency,
@ -820,6 +855,8 @@ export class PortfolioService {
}; };
} }
} }
}
return accounts; return accounts;
} }

Loading…
Cancel
Save