From 20cefaba19bd56021754494f891b3eb7d851fad5 Mon Sep 17 00:00:00 2001 From: Ajay <97356092+iajaymk@users.noreply.github.com> Date: Sat, 28 Oct 2023 18:39:01 +0530 Subject: [PATCH] Improve usability and validation in cash balance transfer (#2552) * Improve usability and validation in cash balance transfer * Update changelog --------- Co-authored-by: Thomas <4159106+dtslvr@users.noreply.github.com> --- CHANGELOG.md | 6 +++ .../api/src/app/account/account.controller.ts | 38 ++++++++++++------- .../accounts-table.component.html | 1 + .../pages/accounts/accounts-page.component.ts | 14 +++++-- .../transfer-balance-dialog.component.ts | 32 +++++++++++++--- 5 files changed, 67 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec59301f..c3ff6246 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 4666e508..e141dc11 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 bfe5a667..991ab745 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 @@