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 @@