Browse Source

Add missing accounts to portfolio details

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

93
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,41 +796,67 @@ 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) {
let currentValueOfSymbol = this.exchangeRateDataService.toCurrency(
order.quantity * portfolioItemsNow[order.symbol].marketPrice,
order.currency,
userCurrency
);
let originalValueOfSymbol = this.exchangeRateDataService.toCurrency(
order.quantity * order.unitPrice,
order.currency,
userCurrency
);
if (order.type === 'SELL') { const currentAccounts = await this.accountService.getAccounts(userId);
currentValueOfSymbol *= -1;
originalValueOfSymbol *= -1; for (const account of currentAccounts) {
} const ordersByAccount = orders.filter(({ accountId }) => {
return accountId === account.id;
});
if (accounts[order.Account?.name || UNKNOWN_KEY]?.current) { if (ordersByAccount.length <= 0) {
accounts[order.Account?.name || UNKNOWN_KEY].current += // Add account without orders
currentValueOfSymbol; const balance = this.exchangeRateDataService.toCurrency(
accounts[order.Account?.name || UNKNOWN_KEY].original += account.balance,
originalValueOfSymbol; account.currency,
} else { userCurrency
accounts[order.Account?.name || UNKNOWN_KEY] = { );
current: currentValueOfSymbol, accounts[account.name] = {
original: originalValueOfSymbol current: balance,
original: balance
}; };
continue;
}
for (const order of ordersByAccount) {
let currentValueOfSymbol = this.exchangeRateDataService.toCurrency(
order.quantity * portfolioItemsNow[order.symbol].marketPrice,
order.currency,
userCurrency
);
let originalValueOfSymbol = this.exchangeRateDataService.toCurrency(
order.quantity * order.unitPrice,
order.currency,
userCurrency
);
if (order.type === 'SELL') {
currentValueOfSymbol *= -1;
originalValueOfSymbol *= -1;
}
if (accounts[order.Account?.name || UNKNOWN_KEY]?.current) {
accounts[order.Account?.name || UNKNOWN_KEY].current +=
currentValueOfSymbol;
accounts[order.Account?.name || UNKNOWN_KEY].original +=
originalValueOfSymbol;
} else {
accounts[order.Account?.name || UNKNOWN_KEY] = {
current: currentValueOfSymbol,
original: originalValueOfSymbol
};
}
} }
} }
return accounts; return accounts;
} }

Loading…
Cancel
Save