From 803ad2cc947d94b631c2b4be939a627db4fd1137 Mon Sep 17 00:00:00 2001 From: Attila Cseh Date: Fri, 8 Aug 2025 13:37:06 +0200 Subject: [PATCH] getAccountById endpoint response extended by dividendInBaseCurrency and interestInBaseCurrency --- apps/api/src/app/account/account.service.ts | 5 ++- .../src/app/portfolio/portfolio.service.ts | 41 ++++++++++++++++++- .../responses/accounts-response.interface.ts | 2 + .../src/lib/types/account-with-value.type.ts | 2 + 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/apps/api/src/app/account/account.service.ts b/apps/api/src/app/account/account.service.ts index 1c8adbd16..398a89bb9 100644 --- a/apps/api/src/app/account/account.service.ts +++ b/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; })[] diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 784661e20..3853182b8 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/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() }; } diff --git a/libs/common/src/lib/interfaces/responses/accounts-response.interface.ts b/libs/common/src/lib/interfaces/responses/accounts-response.interface.ts index 5e03ea34a..0a6af978f 100644 --- a/libs/common/src/lib/interfaces/responses/accounts-response.interface.ts +++ b/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; } diff --git a/libs/common/src/lib/types/account-with-value.type.ts b/libs/common/src/lib/types/account-with-value.type.ts index d86a7ca1f..08af86454 100644 --- a/libs/common/src/lib/types/account-with-value.type.ts +++ b/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;