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

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

@ -161,7 +161,7 @@ export class PortfolioService {
this.accountService.accounts({
where,
include: {
activities: true,
activities: { include: { SymbolProfile: true } },
platform: true
},
orderBy: { name: 'asc' }
@ -177,9 +177,34 @@ export class PortfolioService {
const userCurrency = this.request.user.settings.settings.baseCurrency;
return accounts.map((account) => {
let dividendInBaseCurrency = 0;
let interestInBaseCurrency = 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) {
transactionCount += 1;
}
@ -190,6 +215,8 @@ export class PortfolioService {
const result = {
...account,
dividendInBaseCurrency,
interestInBaseCurrency,
transactionCount,
valueInBaseCurrency,
allocationInPercentage: null, // TODO
@ -242,6 +269,8 @@ export class PortfolioService {
}
let totalBalanceInBaseCurrency = new Big(0);
let totalDividendInBaseCurrency = new Big(0);
let totalInterestInBaseCurrency = new Big(0);
let totalValueInBaseCurrency = new Big(0);
let transactionCount = 0;
@ -249,6 +278,12 @@ export class PortfolioService {
totalBalanceInBaseCurrency = totalBalanceInBaseCurrency.plus(
account.balanceInBaseCurrency
);
totalDividendInBaseCurrency = totalDividendInBaseCurrency.plus(
account.dividendInBaseCurrency
);
totalInterestInBaseCurrency = totalInterestInBaseCurrency.plus(
account.interestInBaseCurrency
);
totalValueInBaseCurrency = totalValueInBaseCurrency.plus(
account.valueInBaseCurrency
);
@ -259,6 +294,8 @@ export class PortfolioService {
accounts,
transactionCount,
totalBalanceInBaseCurrency: totalBalanceInBaseCurrency.toNumber(),
totalDividendInBaseCurrency: totalDividendInBaseCurrency.toNumber(),
totalInterestInBaseCurrency: totalInterestInBaseCurrency.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 {
accounts: AccountWithValue[];
totalBalanceInBaseCurrency: number;
totalDividendInBaseCurrency: number;
totalInterestInBaseCurrency: number;
totalValueInBaseCurrency: 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 & {
allocationInPercentage: number;
balanceInBaseCurrency: number;
dividendInBaseCurrency: number;
interestInBaseCurrency: number;
platform?: Platform;
transactionCount: number;
value: number;

Loading…
Cancel
Save