Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="withdrawalDto"></param>
        /// <returns></returns>
        public async Task <bool> WithdrawFromWallet(WithdrawalDto withdrawalDto)
        {
            var wallet = GetWalletById(withdrawalDto.WalletId);

            if (wallet.CurrencyId == withdrawalDto.CurrencyId)
            {
                if (!CanWithdrawFromWallet(wallet.Balance, withdrawalDto.Amount))
                {
                    return(false);
                }
                wallet.Balance -= withdrawalDto.Amount;

                _transactionRepository.CreateTransaction(TransactionType.Debit, withdrawalDto.Amount, withdrawalDto.WalletId,
                                                         withdrawalDto.CurrencyId);
            }
            else
            {
                var targetCode = _currencyRepository.GetCurrencyCode(wallet.CurrencyId);
                var sourceCode = _currencyRepository.GetCurrencyCode(withdrawalDto.CurrencyId);

                var newAmount = await CurrencyRate.ConvertCurrency(sourceCode, targetCode, withdrawalDto.Amount);

                if (!CanWithdrawFromWallet(wallet.Balance, newAmount))
                {
                    return(false);
                }

                wallet.Balance -= newAmount ?? 0;

                _transactionRepository.CreateTransaction(TransactionType.Debit, newAmount ?? 0, withdrawalDto.WalletId, withdrawalDto.CurrencyId);
            }

            return(await UpdateWallet(wallet));
        }
Exemple #2
0
        public async Task <ICommandResult> Put(Guid id, [FromBody] WithdrawalDto withdrawalDto)
        {
            withdrawalDto.AccountId = id;
            WithdrawalAccountCommand cmd = withdrawalDto;
            var commandResult            = await _commandBus.Submit(cmd);

            return(commandResult);
        }
Exemple #3
0
        public async Task <IActionResult> WithdrawFromWallet(WithdrawalDto withdrawalDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ResponseMessage.Message("Invalid Model", ModelState, withdrawalDto)));
            }

            var currencyExist = _currencyRepository.CurrencyExist(withdrawalDto.CurrencyId);

            if (!currencyExist)
            {
                return(NotFound(ResponseMessage.Message("Currency Not found", "currency id provided is invalid", withdrawalDto)));
            }

            var walletExist = _walletRepository.CheckWallet(withdrawalDto.WalletId);

            if (!walletExist)
            {
                return(NotFound(ResponseMessage.Message("Wallet Not found", "wallet id provided is invalid", withdrawalDto)));
            }

            var loggedInUserId = _walletRepository.GetUserId();
            var userWallets    = _walletRepository.GetWalletsByUserId(loggedInUserId);

            if (userWallets.All(w => w.Id != withdrawalDto.WalletId))
            {
                return(BadRequest(ResponseMessage.Message("Unable to withdraw from this wallet", "This wallet is not owned by you", withdrawalDto)));
            }

            var walletDebited = await _walletRepository.WithdrawFromWallet(withdrawalDto);

            if (!walletDebited)
            {
                return(BadRequest(ResponseMessage.Message("Unable to withdraw from wallet", "An error was encountered while trying to withdraw from the wallet", withdrawalDto)));
            }

            return(Ok(ResponseMessage.Message("You have successfully debited the walled", null, withdrawalDto)));
        }