From cc64342cadf1d276c47d49d47039d334bb250960 Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Mon, 18 Sep 2023 20:45:37 +0200 Subject: [PATCH] Add support for interest --- apps/api/src/app/export/export.service.ts | 5 +- apps/api/src/app/order/order.service.ts | 8 +- .../src/app/portfolio/portfolio.service.ts | 74 ++++---- .../portfolio-summary.component.html | 12 ++ ...ate-or-update-activity-dialog.component.ts | 17 +- .../create-or-update-activity-dialog.html | 15 +- apps/client/src/locales/messages.de.xlf | 175 +++++++++++------- apps/client/src/locales/messages.es.xlf | 170 ++++++++++------- apps/client/src/locales/messages.fr.xlf | 170 ++++++++++------- apps/client/src/locales/messages.it.xlf | 170 ++++++++++------- apps/client/src/locales/messages.nl.xlf | 170 ++++++++++------- apps/client/src/locales/messages.pt.xlf | 170 ++++++++++------- apps/client/src/locales/messages.xlf | 168 ++++++++++------- .../interfaces/portfolio-summary.interface.ts | 1 + .../activities-table.component.html | 8 +- .../activities-table.component.scss | 4 + .../activities-table.component.ts | 9 +- libs/ui/src/lib/i18n.ts | 1 + .../migration.sql | 2 + prisma/schema.prisma | 1 + 20 files changed, 811 insertions(+), 539 deletions(-) create mode 100644 prisma/migrations/20230918204124_added_interest_to_order_type/migration.sql diff --git a/apps/api/src/app/export/export.service.ts b/apps/api/src/app/export/export.service.ts index f717a3ead..2134a6520 100644 --- a/apps/api/src/app/export/export.service.ts +++ b/apps/api/src/app/export/export.service.ts @@ -78,7 +78,10 @@ export class ExportService { dataSource: SymbolProfile.dataSource, date: date.toISOString(), symbol: - type === 'FEE' || type === 'ITEM' || type === 'LIABILITY' + type === 'FEE' || + type === 'INTEREST' || + type === 'ITEM' || + type === 'LIABILITY' ? SymbolProfile.name : SymbolProfile.symbol }; diff --git a/apps/api/src/app/order/order.service.ts b/apps/api/src/app/order/order.service.ts index 13709fad1..3c20f9ba0 100644 --- a/apps/api/src/app/order/order.service.ts +++ b/apps/api/src/app/order/order.service.ts @@ -99,6 +99,7 @@ export class OrderService { if ( data.type === 'FEE' || + data.type === 'INTEREST' || data.type === 'ITEM' || data.type === 'LIABILITY' ) { @@ -155,7 +156,10 @@ export class OrderService { const orderData: Prisma.OrderCreateInput = data; const isDraft = - data.type === 'FEE' || data.type === 'ITEM' || data.type === 'LIABILITY' + data.type === 'FEE' || + data.type === 'INTEREST' || + data.type === 'ITEM' || + data.type === 'LIABILITY' ? false : isAfter(data.date as Date, endOfToday()); @@ -203,6 +207,7 @@ export class OrderService { if ( order.type === 'FEE' || + order.type === 'INTEREST' || order.type === 'ITEM' || order.type === 'LIABILITY' ) { @@ -378,6 +383,7 @@ export class OrderService { if ( data.type === 'FEE' || + data.type === 'INTEREST' || data.type === 'ITEM' || data.type === 'LIABILITY' ) { diff --git a/apps/api/src/app/portfolio/portfolio.service.ts b/apps/api/src/app/portfolio/portfolio.service.ts index 6deda49b7..001dd929e 100644 --- a/apps/api/src/app/portfolio/portfolio.service.ts +++ b/apps/api/src/app/portfolio/portfolio.service.ts @@ -1342,36 +1342,6 @@ export class PortfolioService { return cashPositions; } - private getDividend({ - activities, - date = new Date(0), - userCurrency - }: { - activities: OrderWithAccount[]; - date?: Date; - userCurrency: string; - }) { - return activities - .filter((activity) => { - // Filter out all activities before given date (drafts) and type dividend - return ( - isBefore(date, new Date(activity.date)) && - activity.type === TypeOfOrder.DIVIDEND - ); - }) - .map(({ quantity, SymbolProfile, unitPrice }) => { - return this.exchangeRateDataService.toCurrency( - new Big(quantity).mul(unitPrice).toNumber(), - SymbolProfile.currency, - userCurrency - ); - }) - .reduce( - (previous, current) => new Big(previous).plus(current), - new Big(0) - ); - } - private getDividendsByGroup({ dividends, groupBy @@ -1592,6 +1562,39 @@ export class PortfolioService { return portfolioStart; } + private getSumOfActivityType({ + activities, + activityType, + date = new Date(0), + userCurrency + }: { + activities: OrderWithAccount[]; + activityType: TypeOfOrder; + date?: Date; + userCurrency: string; + }) { + return activities + .filter((activity) => { + // Filter out all activities before given date (drafts) and + // activity type + return ( + isBefore(date, new Date(activity.date)) && + activity.type === activityType + ); + }) + .map(({ quantity, SymbolProfile, unitPrice }) => { + return this.exchangeRateDataService.toCurrency( + new Big(quantity).mul(unitPrice).toNumber(), + SymbolProfile.currency, + userCurrency + ); + }) + .reduce( + (previous, current) => new Big(previous).plus(current), + new Big(0) + ); + } + private getStreaks({ investments, savingsRate @@ -1650,9 +1653,10 @@ export class PortfolioService { return account?.isExcluded ?? false; }); - const dividend = this.getDividend({ + const dividend = this.getSumOfActivityType({ activities, - userCurrency + userCurrency, + activityType: TypeOfOrder.DIVIDEND }).toNumber(); const emergencyFund = new Big( Math.max( @@ -1662,6 +1666,11 @@ export class PortfolioService { ); const fees = this.getFees({ activities, userCurrency }).toNumber(); const firstOrderDate = activities[0]?.date; + const interest = this.getSumOfActivityType({ + activities, + userCurrency, + activityType: TypeOfOrder.INTEREST + }).toNumber(); const items = this.getItems(activities).toNumber(); const liabilities = this.getLiabilities({ activities, @@ -1725,6 +1734,7 @@ export class PortfolioService { excludedAccountsAndActivities, fees, firstOrderDate, + interest, items, liabilities, netWorth, diff --git a/apps/client/src/app/components/portfolio-summary/portfolio-summary.component.html b/apps/client/src/app/components/portfolio-summary/portfolio-summary.component.html index a84fbebaf..aaf9bfb73 100644 --- a/apps/client/src/app/components/portfolio-summary/portfolio-summary.component.html +++ b/apps/client/src/app/components/portfolio-summary/portfolio-summary.component.html @@ -276,6 +276,18 @@