diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b5c01483..05daaabf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Extended the export functionality by the account balances - Added a _Copy portfolio data to clipboard for AI prompt_ action to the analysis page (experimental) ### Changed diff --git a/apps/api/src/app/account/account.service.ts b/apps/api/src/app/account/account.service.ts index df369859b..aab4c0766 100644 --- a/apps/api/src/app/account/account.service.ts +++ b/apps/api/src/app/account/account.service.ts @@ -7,7 +7,13 @@ import { Filter } from '@ghostfolio/common/interfaces'; import { Injectable } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; -import { Account, Order, Platform, Prisma } from '@prisma/client'; +import { + Account, + AccountBalance, + Order, + Platform, + Prisma +} from '@prisma/client'; import { Big } from 'big.js'; import { format } from 'date-fns'; import { groupBy } from 'lodash'; @@ -56,13 +62,19 @@ export class AccountService { orderBy?: Prisma.AccountOrderByWithRelationInput; }): Promise< (Account & { + balances?: AccountBalance[]; Order?: Order[]; Platform?: Platform; })[] > { const { include = {}, skip, take, cursor, where, orderBy } = params; - include.balances = { orderBy: { date: 'desc' }, take: 1 }; + const isBalancesIncluded = !!include.balances; + + include.balances = { + orderBy: { date: 'desc' }, + ...(isBalancesIncluded ? {} : { take: 1 }) + }; const accounts = await this.prismaService.account.findMany({ cursor, @@ -76,7 +88,9 @@ export class AccountService { return accounts.map((account) => { account = { ...account, balance: account.balances[0]?.value ?? 0 }; - delete account.balances; + if (!isBalancesIncluded) { + delete account.balances; + } return account; }); diff --git a/apps/api/src/app/export/export.service.ts b/apps/api/src/app/export/export.service.ts index c6c24d6af..8b9d2c56c 100644 --- a/apps/api/src/app/export/export.service.ts +++ b/apps/api/src/app/export/export.service.ts @@ -31,6 +31,7 @@ export class ExportService { const accounts = ( await this.accountService.accounts({ include: { + balances: true, Platform: true }, orderBy: { @@ -41,6 +42,7 @@ export class ExportService { ).map( ({ balance, + balances, comment, currency, id, @@ -55,6 +57,9 @@ export class ExportService { return { balance, + balances: balances.map(({ date, value }) => { + return { date: date.toISOString(), value }; + }), comment, currency, id, diff --git a/libs/common/src/lib/interfaces/export.interface.ts b/libs/common/src/lib/interfaces/export.interface.ts index 14a017428..0772159e6 100644 --- a/libs/common/src/lib/interfaces/export.interface.ts +++ b/libs/common/src/lib/interfaces/export.interface.ts @@ -1,7 +1,9 @@ import { Account, Order, Platform, Tag } from '@prisma/client'; export interface Export { - accounts: Omit[]; + accounts: (Omit & { + balances: { date: string; value: number }[]; + })[]; activities: (Omit< Order, | 'accountUserId'