|
|
@ -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 |
|
|
|
}); |
|
|
|
} |
|
|
|