Browse Source

getAccountById endpoint response extended by dividendInBaseCurrency and interestInBaseCurrency

pull/5335/head
Attila Cseh 2 weeks ago
parent
commit
803ad2cc94
  1. 5
      apps/api/src/app/account/account.service.ts
  2. 41
      apps/api/src/app/portfolio/portfolio.service.ts
  3. 2
      libs/common/src/lib/interfaces/responses/accounts-response.interface.ts
  4. 2
      libs/common/src/lib/types/account-with-value.type.ts

5
apps/api/src/app/account/account.service.ts

@ -12,7 +12,8 @@ import {
AccountBalance, AccountBalance,
Order, Order,
Platform, Platform,
Prisma Prisma,
SymbolProfile
} from '@prisma/client'; } from '@prisma/client';
import { Big } from 'big.js'; import { Big } from 'big.js';
import { format } from 'date-fns'; import { format } from 'date-fns';
@ -62,7 +63,7 @@ export class AccountService {
orderBy?: Prisma.AccountOrderByWithRelationInput; orderBy?: Prisma.AccountOrderByWithRelationInput;
}): Promise< }): Promise<
(Account & { (Account & {
activities?: Order[]; activities?: (Order & { SymbolProfile?: SymbolProfile })[];
balances?: AccountBalance[]; balances?: AccountBalance[];
platform?: Platform; platform?: Platform;
})[] })[]

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

@ -161,7 +161,7 @@ export class PortfolioService {
this.accountService.accounts({ this.accountService.accounts({
where, where,
include: { include: {
activities: true, activities: { include: { SymbolProfile: true } },
platform: true platform: true
}, },
orderBy: { name: 'asc' } orderBy: { name: 'asc' }
@ -177,9 +177,34 @@ export class PortfolioService {
const userCurrency = this.request.user.settings.settings.baseCurrency; const userCurrency = this.request.user.settings.settings.baseCurrency;
return accounts.map((account) => { return accounts.map((account) => {
let dividendInBaseCurrency = 0;
let interestInBaseCurrency = 0;
let transactionCount = 0; let transactionCount = 0;
for (const { isDraft } of account.activities) { for (const {
isDraft,
currency,
SymbolProfile,
type,
unitPrice
} of account.activities) {
switch (type) {
case ActivityType.DIVIDEND:
dividendInBaseCurrency += this.exchangeRateDataService.toCurrency(
unitPrice,
currency ?? SymbolProfile.currency,
userCurrency
);
break;
case ActivityType.INTEREST:
interestInBaseCurrency += this.exchangeRateDataService.toCurrency(
unitPrice,
currency ?? SymbolProfile.currency,
userCurrency
);
break;
}
if (!isDraft) { if (!isDraft) {
transactionCount += 1; transactionCount += 1;
} }
@ -190,6 +215,8 @@ export class PortfolioService {
const result = { const result = {
...account, ...account,
dividendInBaseCurrency,
interestInBaseCurrency,
transactionCount, transactionCount,
valueInBaseCurrency, valueInBaseCurrency,
allocationInPercentage: null, // TODO allocationInPercentage: null, // TODO
@ -242,6 +269,8 @@ export class PortfolioService {
} }
let totalBalanceInBaseCurrency = new Big(0); let totalBalanceInBaseCurrency = new Big(0);
let totalDividendInBaseCurrency = new Big(0);
let totalInterestInBaseCurrency = new Big(0);
let totalValueInBaseCurrency = new Big(0); let totalValueInBaseCurrency = new Big(0);
let transactionCount = 0; let transactionCount = 0;
@ -249,6 +278,12 @@ export class PortfolioService {
totalBalanceInBaseCurrency = totalBalanceInBaseCurrency.plus( totalBalanceInBaseCurrency = totalBalanceInBaseCurrency.plus(
account.balanceInBaseCurrency account.balanceInBaseCurrency
); );
totalDividendInBaseCurrency = totalDividendInBaseCurrency.plus(
account.dividendInBaseCurrency
);
totalInterestInBaseCurrency = totalInterestInBaseCurrency.plus(
account.interestInBaseCurrency
);
totalValueInBaseCurrency = totalValueInBaseCurrency.plus( totalValueInBaseCurrency = totalValueInBaseCurrency.plus(
account.valueInBaseCurrency account.valueInBaseCurrency
); );
@ -259,6 +294,8 @@ export class PortfolioService {
accounts, accounts,
transactionCount, transactionCount,
totalBalanceInBaseCurrency: totalBalanceInBaseCurrency.toNumber(), totalBalanceInBaseCurrency: totalBalanceInBaseCurrency.toNumber(),
totalDividendInBaseCurrency: totalDividendInBaseCurrency.toNumber(),
totalInterestInBaseCurrency: totalInterestInBaseCurrency.toNumber(),
totalValueInBaseCurrency: totalValueInBaseCurrency.toNumber() totalValueInBaseCurrency: totalValueInBaseCurrency.toNumber()
}; };
} }

2
libs/common/src/lib/interfaces/responses/accounts-response.interface.ts

@ -3,6 +3,8 @@ import { AccountWithValue } from '@ghostfolio/common/types';
export interface AccountsResponse { export interface AccountsResponse {
accounts: AccountWithValue[]; accounts: AccountWithValue[];
totalBalanceInBaseCurrency: number; totalBalanceInBaseCurrency: number;
totalDividendInBaseCurrency: number;
totalInterestInBaseCurrency: number;
totalValueInBaseCurrency: number; totalValueInBaseCurrency: number;
transactionCount: number; transactionCount: number;
} }

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

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

Loading…
Cancel
Save