Joseph Bao 1 day ago
committed by GitHub
parent
commit
77f734cb49
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 68
      apps/api/src/app/portfolio/portfolio.service.ts
  2. 37
      apps/client/src/app/components/accounts-table/accounts-table.component.html
  3. 1
      apps/client/src/app/components/accounts-table/accounts-table.component.ts
  4. 1
      libs/common/src/lib/types/account-with-value.type.ts

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

@ -72,6 +72,7 @@ import {
Order,
Platform,
Prisma,
SymbolProfile,
Tag
} from '@prisma/client';
import { Big } from 'big.js';
@ -160,7 +161,11 @@ export class PortfolioService {
this.accountService.accounts({
where,
include: {
activities: true,
activities: {
include: {
SymbolProfile: true
}
},
platform: true
},
orderBy: { name: 'asc' }
@ -175,38 +180,71 @@ export class PortfolioService {
const userCurrency = this.request.user.Settings.settings.baseCurrency;
return accounts.map((account) => {
const accountsWithValue = accounts.map((account) => {
let transactionCount = 0;
let valueInBaseCurrency =
details.accounts[account.id]?.valueInBaseCurrency ?? 0;
for (const { isDraft } of account.activities) {
if (!isDraft) {
transactionCount += 1;
if (filterByDataSource && filterBySymbol) {
const holding = details.holdings[filterBySymbol];
const activities = (
account.activities as (Order & {
SymbolProfile: SymbolProfile;
})[]
).filter((activity) => {
return (
activity.SymbolProfile.dataSource === filterByDataSource &&
activity.SymbolProfile.symbol === filterBySymbol
);
});
let quantity = new Big(0);
for (const activity of activities) {
quantity = quantity.plus(
new Big(getFactor(activity.type)).mul(activity.quantity)
);
}
}
const valueInBaseCurrency =
details.accounts[account.id]?.valueInBaseCurrency ?? 0;
valueInBaseCurrency = quantity.mul(holding.marketPrice ?? 0).toNumber();
transactionCount = activities.length;
} else {
for (const { isDraft } of account.activities) {
if (!isDraft) {
transactionCount += 1;
}
}
}
const result = {
...account,
transactionCount,
valueInBaseCurrency,
allocationInPercentage: 0,
balanceInBaseCurrency: this.exchangeRateDataService.toCurrency(
account.balance,
account.currency,
userCurrency
),
transactionCount,
value: this.exchangeRateDataService.toCurrency(
valueInBaseCurrency,
userCurrency,
account.currency
)
),
valueInBaseCurrency
};
delete result.activities;
return result;
});
if (filterByDataSource && filterBySymbol) {
return accountsWithValue.filter((account) => {
return account.transactionCount > 0;
});
}
return accountsWithValue;
}
public async getAccountsWithAggregations({
@ -237,6 +275,14 @@ export class PortfolioService {
transactionCount += account.transactionCount;
}
for (const account of accounts) {
account.allocationInPercentage = totalValueInBaseCurrency.gt(0)
? new Big(account.valueInBaseCurrency)
.div(totalValueInBaseCurrency)
.toNumber()
: 0;
}
return {
accounts,
transactionCount,

37
apps/client/src/app/components/accounts-table/accounts-table.component.html

@ -231,6 +231,43 @@
</td>
</ng-container>
<ng-container matColumnDef="allocation">
<th
*matHeaderCellDef
class="d-none d-lg-table-cell justify-content-end px-1"
mat-header-cell
mat-sort-header
>
<ng-container i18n>Allocation</ng-container>
</th>
<td
*matCellDef="let element"
class="d-none d-lg-table-cell px-1 text-right"
mat-cell
>
<gf-value
class="d-inline-block justify-content-end"
[isPercent]="true"
[locale]="locale"
[precision]="2"
[value]="element.allocationInPercentage"
/>
</td>
<td
*matFooterCellDef
class="d-none d-lg-table-cell px-1 text-right"
mat-footer-cell
>
<gf-value
class="d-inline-block justify-content-end"
[isPercent]="true"
[locale]="locale"
[precision]="2"
[value]="1"
/>
</td>
</ng-container>
<ng-container matColumnDef="comment">
<th
*matHeaderCellDef

1
apps/client/src/app/components/accounts-table/accounts-table.component.ts

@ -81,6 +81,7 @@ export class AccountsTableComponent implements OnChanges, OnDestroy {
this.displayedColumns.push('valueInBaseCurrency');
}
this.displayedColumns.push('allocation');
this.displayedColumns.push('comment');
if (this.showActions) {

1
libs/common/src/lib/types/account-with-value.type.ts

@ -1,6 +1,7 @@
import { Account as AccountModel, Platform } from '@prisma/client';
export type AccountWithValue = AccountModel & {
allocationInPercentage: number;
balanceInBaseCurrency: number;
platform?: Platform;
transactionCount: number;

Loading…
Cancel
Save