From 6fc1aca7dac6472bd917f7c2d518fe6bb29094ee Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Sat, 28 Oct 2023 11:12:56 +0200 Subject: [PATCH] Refactoring --- .../api/src/app/account/account.controller.ts | 40 ++++++++----------- .../accounts-table.component.html | 1 + .../pages/accounts/accounts-page.component.ts | 13 ++++-- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/apps/api/src/app/account/account.controller.ts b/apps/api/src/app/account/account.controller.ts index 04d688d38..e141dc11f 100644 --- a/apps/api/src/app/account/account.controller.ts +++ b/apps/api/src/app/account/account.controller.ts @@ -190,54 +190,46 @@ export class AccountController { this.request.user.id ); - const currentAccountIds = accountsOfUser.map(({ id }) => { - return id; + const accountFrom = accountsOfUser.find(({ id }) => { + return id === accountIdFrom; }); - const fromAccountBalance = accountsOfUser.find( - (account) => account.id === accountIdFrom - ).balance; + const accountTo = accountsOfUser.find(({ id }) => { + return id === accountIdTo; + }); - if (fromAccountBalance < balance) { + if (!accountFrom || !accountTo) { throw new HttpException( - getReasonPhrase(StatusCodes.BAD_REQUEST), - StatusCodes.BAD_REQUEST + getReasonPhrase(StatusCodes.NOT_FOUND), + StatusCodes.NOT_FOUND ); } - if (accountIdFrom === accountIdTo) { + if (accountFrom.id === accountTo.id) { throw new HttpException( getReasonPhrase(StatusCodes.BAD_REQUEST), StatusCodes.BAD_REQUEST ); } - if ( - ![accountIdFrom, accountIdTo].every((accountId) => { - return currentAccountIds.includes(accountId); - }) - ) { + if (accountFrom.balance < balance) { throw new HttpException( - getReasonPhrase(StatusCodes.NOT_FOUND), - StatusCodes.NOT_FOUND + getReasonPhrase(StatusCodes.BAD_REQUEST), + StatusCodes.BAD_REQUEST ); } - const { currency } = accountsOfUser.find(({ id }) => { - return id === accountIdFrom; - }); - 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 @@