Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Transfer cash balance between accounts (#2455)

Aldrin0ac97bd112f3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e8f1c342aa..8bff6b3002 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
+- Added support to transfer a part of the cash balance from one to another account
- Extended the markets overview by benchmarks (date of last all time high)
### Changed
diff --git a/apps/api/src/app/account/account.controller.ts b/apps/api/src/app/account/account.controller.ts
index bf15d998f3..05d0c11def 100644
--- a/apps/api/src/app/account/account.controller.ts
+++ b/apps/api/src/app/account/account.controller.ts
@@ -29,6 +29,7 @@ import { StatusCodes, getReasonPhrase } from 'http-status-codes';
import { AccountService } from './account.service';
import { CreateAccountDto } from './create-account.dto';
+import { TransferBalanceDto } from './transfer-balance.dto';
import { UpdateAccountDto } from './update-account.dto';
@Controller('account')
@@ -154,6 +155,58 @@ export class AccountController {
}
}
+ @Post('transfer-balance')
+ @UseGuards(AuthGuard('jwt'))
+ public async transferAccountBalance(
+ @Body() { accountIdFrom, accountIdTo, balance }: TransferBalanceDto
+ ) {
+ if (
+ !hasPermission(this.request.user.permissions, permissions.updateAccount)
+ ) {
+ throw new HttpException(
+ getReasonPhrase(StatusCodes.FORBIDDEN),
+ StatusCodes.FORBIDDEN
+ );
+ }
+
+ const accountsOfUser = await this.accountService.getAccounts(
+ this.request.user.id
+ );
+
+ const currentAccountIds = accountsOfUser.map(({ id }) => {
+ return id;
+ });
+
+ if (
+ ![accountIdFrom, accountIdTo].every((accountId) => {
+ return currentAccountIds.includes(accountId);
+ })
+ ) {
+ throw new HttpException(
+ getReasonPhrase(StatusCodes.NOT_FOUND),
+ StatusCodes.NOT_FOUND
+ );
+ }
+
+ const { currency } = accountsOfUser.find(({ id }) => {
+ return id === accountIdFrom;
+ });
+
+ await this.accountService.updateAccountBalance({
+ currency,
+ accountId: accountIdFrom,
+ amount: -balance,
+ userId: this.request.user.id
+ });
+
+ await this.accountService.updateAccountBalance({
+ currency,
+ accountId: accountIdTo,
+ amount: balance,
+ userId: this.request.user.id
+ });
+ }
+
@Put(':id')
@UseGuards(AuthGuard('jwt'))
public async update(@Param('id') id: string, @Body() data: UpdateAccountDto) {
diff --git a/apps/api/src/app/account/account.service.ts b/apps/api/src/app/account/account.service.ts
index dc049108c1..bc6abcc7ab 100644
--- a/apps/api/src/app/account/account.service.ts
+++ b/apps/api/src/app/account/account.service.ts
@@ -109,7 +109,7 @@ export class AccountService {
});
}
- public async getAccounts(aUserId: string) {
+ public async getAccounts(aUserId: string): Promise<Account[]> {
const accounts = await this.accounts({
include: { Order: true, Platform: true },
orderBy: { name: 'asc' },
@@ -218,13 +218,13 @@ export class AccountService {
accountId,
amount,
currency,
- date,
+ date = new Date(),
userId
}: {
accountId: string;
amount: number;
currency: string;
- date: Date;
+ date?: Date;
userId: string;
}) {
const { balance, currency: currencyOfAccount } = await this.account({
diff --git a/apps/client/src/app/components/accounts-table/accounts-table.component.html b/apps/client/src/app/components/accounts-table/accounts-table.component.html
index 664694735a..a4548c1b9b 100644
--- a/apps/client/src/app/components/accounts-table/accounts-table.component.html
+++ b/apps/client/src/app/components/accounts-table/accounts-table.component.html
@@ -1,4 +1,4 @@
-<div *ngIf="false" class="d-flex justify-content-end">
+<div class="d-flex justify-content-end">
<button
class="align-items-center d-flex"
mat-stroked-button
diff --git a/apps/client/src/app/pages/accounts/accounts-page.component.ts b/apps/client/src/app/pages/accounts/accounts-page.component.ts
index 18d5d1e2a9..882f9b3f6f 100644
--- a/apps/client/src/app/pages/accounts/accounts-page.component.ts
+++ b/apps/client/src/app/pages/accounts/accounts-page.component.ts
@@ -295,9 +295,16 @@ export class AccountsPageComponent implements OnDestroy, OnInit {
const { accountIdFrom, accountIdTo, balance }: TransferBalanceDto =
data?.account;
- console.log(
- `Transfer cash balance of ${balance} from account ${accountIdFrom} to account ${accountIdTo}`
- );
+ this.dataService
+ .transferAccountBalance({
+ accountIdFrom,
+ accountIdTo,
+ balance
+ })
+ .pipe(takeUntil(this.unsubscribeSubject))
+ .subscribe(() => {
+ this.fetchAccounts();
+ });
}
this.router.navigate(['.'], { relativeTo: this.route });
diff --git a/apps/client/src/app/services/data.service.ts b/apps/client/src/app/services/data.service.ts
index d5dce61597..f5cafb57a7 100644
--- a/apps/client/src/app/services/data.service.ts
+++ b/apps/client/src/app/services/data.service.ts
@@ -2,6 +2,7 @@ import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { CreateAccessDto } from '@ghostfolio/api/app/access/create-access.dto';
import { CreateAccountDto } from '@ghostfolio/api/app/account/create-account.dto';
+import { TransferBalanceDto } from '@ghostfolio/api/app/account/transfer-balance.dto';
import { UpdateAccountDto } from '@ghostfolio/api/app/account/update-account.dto';
import { CreateOrderDto } from '@ghostfolio/api/app/order/create-order.dto';
import { Activities } from '@ghostfolio/api/app/order/interfaces/activities.interface';
@@ -505,6 +506,18 @@ export class DataService {
});
}
+ public transferAccountBalance({
+ accountIdFrom,
+ accountIdTo,
+ balance
+ }: TransferBalanceDto) {
+ return this.http.post('/api/v1/account/transfer-balance', {
+ accountIdFrom,
+ accountIdTo,
+ balance
+ });
+ }
+
public updateInfo() {
this.http.get<InfoItem>('/api/v1/info').subscribe((info) => {
const utmSource = <'ios' | 'trusted-web-activity'>(