public async Task <IActionResult> Deposit(DepositType type, [FromBody] DepositDTO model)
 {
     if (type == DepositType.Cash)
     {
         try
         {
             await _accountApplicationService.DepositCashAsync(AccountId.With(model.AccountId), model.Amount);
         }
         catch (Exception e)
         {
             return(BadRequest(new { Error = e.Message }));
         }
         return(Ok(new { isSuccess = true }));
     }
     if (type == DepositType.Check)
     {
         try
         {
             await _accountApplicationService.DepositCheckAsync(AccountId.With(model.AccountId), model.Amount);
         }
         catch (Exception e)
         {
             return(BadRequest(new { Error = e.Message }));
         }
         return(Ok(new { isSuccess = true }));
     }
     return(BadRequest());
 }
 public async Task <IActionResult> SetOverDraftLimit([FromBody] SetOverDraftLimitDTO model)
 {
     try
     {
         await _accountApplicationService.SetOverDraftLimitAsync(AccountId.With(model.AccountId), model.OverDraftLimit);
     }
     catch (Exception e)
     {
         return(BadRequest(new { Error = e.Message }));
     }
     return(Ok(new { isSuccess = true }));
 }
 public async Task <IActionResult> WithdrawCash([FromBody] WithdrawCashDTO model)
 {
     try
     {
         await _accountApplicationService.WithdrawCashAsync(AccountId.With(model.AccountId), model.Amount);
     }
     catch (Exception e)
     {
         return(BadRequest(new { Error = e.Message }));
     }
     return(Ok(new { isSuccess = true }));
 }
        public async Task <Account> GetAccountById(GetAccountByIdQuery query, CancellationToken cancellationToken)
        {
            var conn = await Connect();

            var     streamName   = query.AccountId.Value;
            Account _account     = new Account(AccountId.With(streamName));
            var     streamEvents = conn.ReadStreamEventsForwardAsync(streamName, 0, 100, true, _adminCredentials).Result;

            foreach (var evt in streamEvents.Events)
            {
                var json    = Encoding.UTF8.GetString(evt.Event.Data);
                var account = JsonConvert.DeserializeObject <AccountReadModel>(json);
                if (!string.IsNullOrEmpty(account.HolderName))
                {
                    _account.HolderName = account.HolderName;
                    _account.Balance    = account.Balance;
                }

                if (!string.IsNullOrEmpty(account.AccountState))
                {
                    _account.AccountState = account.AccountState;
                }


                if (!account.Amount.Equals(default(float)))
                {
                    if (evt.Event.EventType.ToLower().Contains("withdraw"))
                    {
                        _account.Balance -= account.Amount;
                    }

                    if (evt.Event.EventType.ToLower().Contains("deposit"))
                    {
                        if (evt.Event.EventType.ToLower().Contains("cash"))
                        {
                            _account.Balance += account.Amount;
                        }

                        if (evt.Event.EventType.ToLower().Contains("check"))
                        {
                            var date        = evt.Event.Created;
                            var depositDate = date.AddDays(1);
                            while (!(depositDate.DayOfWeek >= DayOfWeek.Monday && depositDate.DayOfWeek <= DayOfWeek.Friday))
                            {
                                depositDate = depositDate.AddDays(1);
                            }
                            if (DateTime.UtcNow.Date >= depositDate.Date)
                            {
                                _account.Balance += account.Amount;
                            }
                        }
                    }
                }

                if (!account.DailyWireTransferLimit.Equals(default(float)))
                {
                    _account.DailyWireTransferLimit = account.DailyWireTransferLimit;
                }

                if (!account.OverDraftLimit.Equals(default(float)))
                {
                    _account.OverDraftLimit = account.OverDraftLimit;
                }
            }
            conn.Close();
            return(_account);
        }
        public async Task <List <Account> > GetAllAccounts(GetAllAccountsQuery query, CancellationToken cancellationToken)
        {
            var conn = await Connect();

            List <Account>     _accounts = new List <Account>();
            ProjectionsManager pm        = new ProjectionsManager(new ConsoleLogger(), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2113), TimeSpan.FromMilliseconds(5000));

            CreateProjection(pm);

            var streams = await pm.GetStateAsync("getAllStreams", _adminCredentials);

            var streamNames = JsonConvert.DeserializeObject <Streams>(streams);

            foreach (var streamName in streamNames.StreamNames)
            {
                Account _account     = new Account(AccountId.With(streamName));
                var     streamEvents = conn.ReadStreamEventsForwardAsync(streamName, 0, 100, true, _adminCredentials).Result;
                foreach (var evt in streamEvents.Events)
                {
                    var json    = Encoding.UTF8.GetString(evt.Event.Data);
                    var account = JsonConvert.DeserializeObject <AccountReadModel>(json);
                    if (!string.IsNullOrEmpty(account.HolderName))
                    {
                        _account.HolderName = account.HolderName;
                        _account.Balance    = account.Balance;
                    }

                    if (!string.IsNullOrEmpty(account.AccountState))
                    {
                        _account.AccountState = account.AccountState;
                    }

                    if (!account.Amount.Equals(default(float)))
                    {
                        if (evt.Event.EventType.ToLower().Contains("withdraw"))
                        {
                            _account.Balance -= account.Amount;
                        }

                        if (evt.Event.EventType.ToLower().Contains("deposit"))
                        {
                            if (evt.Event.EventType.ToLower().Contains("cash"))
                            {
                                _account.Balance += account.Amount;
                            }

                            if (evt.Event.EventType.ToLower().Contains("check"))
                            {
                                var date        = evt.Event.Created;
                                var depositDate = date.AddDays(1);
                                while (!(depositDate.DayOfWeek >= DayOfWeek.Monday && depositDate.DayOfWeek <= DayOfWeek.Friday))
                                {
                                    depositDate = depositDate.AddDays(1);
                                }
                                if (DateTime.UtcNow.Date >= depositDate.Date)
                                {
                                    _account.Balance += account.Amount;
                                }
                            }
                        }
                    }

                    if (!account.DailyWireTransferLimit.Equals(default(float)))
                    {
                        _account.DailyWireTransferLimit = account.DailyWireTransferLimit;
                    }

                    if (!account.OverDraftLimit.Equals(default(float)))
                    {
                        _account.OverDraftLimit = account.OverDraftLimit;
                    }
                }
                _accounts.Add(_account);
            }
            conn.Close();
            return(_accounts);
        }
        public async Task <IActionResult> GetAccountById(string accountId)
        {
            var account = await _accountQueryService.GetAccountByIdAsync(AccountId.With(accountId));

            return(Ok(account));
        }