diff --git a/CHANGELOG.md b/CHANGELOG.md index ec59301fa..c3ff6246f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Changed + +- Improved the usability and validation in the cash balance transfer from one to another account + ## 2.15.0 - 2023-10-26 ### Added diff --git a/apps/api/src/app/account/account.controller.ts b/apps/api/src/app/account/account.controller.ts index 4666e5084..e141dc11f 100644 --- a/apps/api/src/app/account/account.controller.ts +++ b/apps/api/src/app/account/account.controller.ts @@ -190,36 +190,46 @@ export class AccountController { this.request.user.id ); - const currentAccountIds = accountsOfUser.map(({ id }) => { - return id; + const accountFrom = accountsOfUser.find(({ id }) => { + return id === accountIdFrom; }); - if ( - ![accountIdFrom, accountIdTo].every((accountId) => { - return currentAccountIds.includes(accountId); - }) - ) { + const accountTo = accountsOfUser.find(({ id }) => { + return id === accountIdTo; + }); + + if (!accountFrom || !accountTo) { throw new HttpException( getReasonPhrase(StatusCodes.NOT_FOUND), StatusCodes.NOT_FOUND ); } - const { currency } = accountsOfUser.find(({ id }) => { - return id === accountIdFrom; - }); + if (accountFrom.id === accountTo.id) { + throw new HttpException( + getReasonPhrase(StatusCodes.BAD_REQUEST), + StatusCodes.BAD_REQUEST + ); + } + + if (accountFrom.balance < balance) { + throw new HttpException( + getReasonPhrase(StatusCodes.BAD_REQUEST), + StatusCodes.BAD_REQUEST + ); + } await this.accountService.updateAccountBalance({ - currency, - accountId: accountIdFrom, + accountId: accountFrom.id, amount: -balance, + currency: accountFrom.currency, userId: this.request.user.id }); await this.accountService.updateAccountBalance({ - currency, - accountId: accountIdTo, + accountId: accountTo.id, amount: balance, + currency: accountFrom.currency, userId: this.request.user.id }); } 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 bfe5a667a..991ab7454 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 @@ -2,6 +2,7 @@