private void UpdateAccountBalance(string accountNumberId, long balance) { AccountPM accountToUpdate = GetAccount(accountNumberId); accountToUpdate.Balance = balance; accountToUpdate.BalanceUpdateTime = DateTime.Now; }
public Account GetAccount() { AccountPM account = accountsRepository.GetAccountAndCardsForUser(GetCurrentUserId()); Account accountDetails = new Account() { AccountNumberId = account.AccountNumberId, AccountState = account.AccountState, CustomerType = account.CustomerType, FirstName = account.FirstName, LastName = account.LastName, BusinessName = account.BusinessName, CompanyRegNumber = account.CompanyRegNumber, TaxNumber = account.TaxNumber, Balance = account.Balance, }; account.Cards.ForEach(x => { Card cd = new Card() { CardSerialNumberId = x.CardSerialNumberId, CardState = x.CardState, DailySpendLimit = x.DailySpendLimit, MonthlySpendLimit = x.MonthlySpendLimit, FreindlyName = x.FreindlyName, }; accountDetails.Cards.Add(cd); }); return(accountDetails); }
public void UpdateAccountState(string accountNumberId, AccountState accountState, string credentialsId) { AccountPM accountLoggedIn = GetAccountForUser(credentialsId); if (accountLoggedIn.AccountNumberId != accountNumberId) { throw new ValidationException("Invalid AccountNumberId"); } accountLoggedIn.AccountState = accountState; _context.SaveChanges(); }
public void CancelCard(string cardSerialNumber, string credentialsId) { CardPM card = GetCard(cardSerialNumber); AccountPM accountLoggedIn = _accountsRepository.GetAccountForUser(credentialsId); if (accountLoggedIn.AccountNumberId != card.AccountNumberIdRef) { throw new ValidationException("Invalid AccountNumberId"); } card.CardState = CardState.Cancelled; _context.SaveChanges(); }
public void UpdateCard(CardPM card, string credentialsId) { CardPM cardToUpdate = GetCard(card.CardSerialNumberId); AccountPM accountLoggedIn = _accountsRepository.GetAccountForUser(credentialsId); if (accountLoggedIn.AccountNumberId != cardToUpdate.AccountNumberIdRef) { throw new ValidationException("Invalid AccountNumberId"); } cardToUpdate.UpdateCard(card); cardToUpdate.AvailablegDailySpendLimit = cardToUpdate.DailySpendLimit; cardToUpdate.AvailableMonthlySpendLimit = cardToUpdate.MonthlySpendLimit; _context.SaveChanges(); }
public void AddAccount(string accountNumberId, string credentialsId) { AccountPM a = new AccountPM() { CustomerType = CustomerType.None, CredentialsId = credentialsId, AccountNumberId = accountNumberId, Balance = 0, BalanceUpdateTime = DateTime.Now, AccountState = AccountState.PendingUpdate, }; _context.Accounts.Add(a); _context.SaveChanges(); }
private int StoreTx(TransactionPM t, AccountPM accountFrom, AccountPM accountTo, CardPM cardFrom, bool useTransaction) { Action action = () => { _context.Transactions.Add(t); UpdateAccountBalance(accountFrom.AccountNumberId, accountFrom.Balance = accountFrom.Balance - t.Amount); UpdateAccountBalance(accountTo.AccountNumberId, accountTo.Balance = accountTo.Balance + t.Amount); if (cardFrom != null) { cardFrom.AvailablegDailySpendLimit = cardFrom.AvailablegDailySpendLimit - t.Amount; cardFrom.AvailableMonthlySpendLimit = cardFrom.AvailableMonthlySpendLimit - t.Amount; } }; if (!useTransaction) { try { action.Invoke(); _context.SaveChanges(); return(t.TransactionId); } catch { throw new TechnicalException("Error Occured during transaction db operation. Transaction Rolled Back"); } } else { using (IDbContextTransaction dbTransaction = _context.Database.BeginTransaction()) { try { action.Invoke(); _context.SaveChanges(); dbTransaction.Commit(); return(t.TransactionId); } catch (Exception ex) { dbTransaction.Rollback(); throw new TechnicalException("Error Occured during transaction db operation. Transaction Rolled Back:" + ex.Message); } } } }
public AccountPM GetAccountAndCardsForUser(string credentialsId) { //this code creates sql queries that query the account table twice... //List<Account> accounts = _context.Accounts.Include(c => c.Cards).Where(w => w.CredentialsId == credentialsId).ToList(); //if (accounts.Count == 0) // throw new ValidationException("No account found"); //if (accounts.Count > 1) // throw new ValidationException("More than 1 account found"); //return accounts[0]; AccountPM account = GetAccountForUser(credentialsId); account.Cards = _context.Cards.Where(w => w.Account.AccountNumberId == account.AccountNumberId).ToList(); return(account); }
public void UpdateAccount(AccountPM account, string credentialsId) { AccountPM accountLoggedIn = GetAccountForUser(credentialsId); if (accountLoggedIn.AccountNumberId != account.AccountNumberId) { throw new ValidationException("Invalid AccountNumberId"); } if (accountLoggedIn.CustomerType != CustomerType.None && accountLoggedIn.CustomerType != account.CustomerType) { throw new ValidationException("Cannot change customer type"); } account.AccountState = AccountState.Enabled; accountLoggedIn.UpdateAccount(account); _context.SaveChanges(); }
public AccountPM GetTransactionsForUser(string credentialsId) { AccountPM account = GetAccountForUser(credentialsId); //List<TransactionPM> txList = _context.Transactions.Where(w => // w.AccountNumberIdFromRef == account.AccountNumberId || // w.AccountNumberIdToRef == account.AccountNumberId //).ToList(); account.TransactionsFrom = _context.Transactions.Where(w => w.AccountNumberIdFromRef == account.AccountNumberId).ToList(); account.TransactionsTo = _context.Transactions.Where(w => w.AccountNumberIdToRef == account.AccountNumberId).ToList(); account.CCTopUpTransactions = _context.TopUpTransactions.Where(w => w.AccountNumberIdToRef == account.AccountNumberId).ToList(); account.POSTransactionsTo = _context.POSTransactions.Where(w => w.AccountNumberIdToRef == account.AccountNumberId || w.AccountNumberIdFromRef == account.AccountNumberId ).ToList(); return(account); }
public int AddQRCodeBasedTransaction(TransactionPM t, string credentialsId, bool useTransaction) { ValidateCommonTxDetail(t); AccountPM accountLoggedIn = _accountsRepository.GetAccountForUser(credentialsId); AccountPM accountFrom = null; AccountPM accountTo = null; if (accountLoggedIn.AccountNumberId != t.AccountNumberIdFromRef) { throw new ValidationException("Invalid AccountNumberId"); } accountTo = GetAccount(t.AccountNumberIdToRef); accountFrom = accountLoggedIn; //_accountsRepository.GetAccount(transaction.AccountNumberIdFrom); if (accountFrom.Balance < t.Amount) { throw new ValidationException("Insufficient funds"); } return(StoreTx(t, accountFrom, accountTo, null, useTransaction)); }
public int AddTopUpTransaction(CCTopUpTransactionPM t, string credentialsId) { //TODO: reject top-ups with our cards if (t.Amount > ConfigSingleton.MaxTopUpTransactionAmount) { throw new ValidationException("Invalid transaction, greater than max top up amount allowed"); } if (String.IsNullOrEmpty(t.EMV_Data)) { throw new ValidationException("Card EMV data not supplied"); } AccountPM accountLoggedIn = _accountsRepository.GetAccountForUser(credentialsId); t.AccountNumberIdToRef = accountLoggedIn.AccountNumberId; t.TransactionDateTime = DateTime.Now; using (IDbContextTransaction dbTransaction = _context.Database.BeginTransaction()) { try { _context.TopUpTransactions.Add(t); UpdateAccountBalance(accountLoggedIn.AccountNumberId, accountLoggedIn.Balance = accountLoggedIn.Balance + t.Amount); _context.SaveChanges(); dbTransaction.Commit(); return(t.TopUpTransactionId); } catch { dbTransaction.Rollback(); throw new TechnicalException("Error Occured during transaction db operation. Transaction Rolled Back"); } } }
public Account GetAccountTransactions() { AccountPM account = accountsRepository.GetTransactionsForUser(GetCurrentUserId()); Account accountDetails = new Account() { AccountNumberId = account.AccountNumberId, AccountState = account.AccountState, CustomerType = account.CustomerType, FirstName = account.FirstName, LastName = account.LastName, BusinessName = account.BusinessName, CompanyRegNumber = account.CompanyRegNumber, TaxNumber = account.TaxNumber, Balance = account.Balance, }; account.TransactionsFrom.ForEach(x => { CardTransferTransaction tx = new CardTransferTransaction() { AccountFrom = x.AccountNumberIdFromRef, //AccountTo = x.AccountNumberIdToRef, Amount = x.Amount, TransactionType = x.TransactionType, DateTime = x.TransactionDateTime, TransactionId = x.TransactionId, }; accountDetails.TransferFromTransactions.Add(tx); }); account.TransactionsTo.ForEach(x => { CardTransferTransaction tx = new CardTransferTransaction() { //AccountFrom = x.AccountNumberIdFromRef, AccountTo = x.AccountNumberIdToRef, Amount = x.Amount, TransactionType = x.TransactionType, DateTime = x.TransactionDateTime, TransactionId = x.TransactionId, }; accountDetails.TransferToTransactions.Add(tx); }); account.POSTransactionsTo.ForEach(x => { POSTransaction tx = new POSTransaction() { AccountNumberId = x.AccountNumberIdToRef, TransactionDateTime = x.TransactionDateTime, TransactionId = x.TransactionIdRef, POSTransactionId = x.POSTransactionId, }; accountDetails.POSTransactions.Add(tx); }); account.CCTopUpTransactions.ForEach(x => { CCTopUpTransaction tx = new CCTopUpTransaction() { AccountNumberId = x.AccountNumberIdToRef, Amount = x.Amount, DateTime = x.TransactionDateTime, }; accountDetails.TopUpTransactions.Add(tx); }); return(accountDetails); }
public int AddCardBasedTransaction(TransactionPM t, string credentialsId, bool useTransaction) { ValidateCommonTxDetail(t); AccountPM accountLoggedIn = _accountsRepository.GetAccountForUser(credentialsId); AccountPM accountFrom = null; AccountPM accountTo = null; CardPM cardTo = null; CardPM cardFrom = null; switch (t.TransactionType) { case TransactionType.SendMoneyFromAppToCard: if (accountLoggedIn.AccountNumberId != t.AccountNumberIdFromRef) { throw new ValidationException("Invalid AccountNumberId"); } cardTo = _cardsRepository.GetCard(t.CardSerialNumberIdTo); if (cardTo.CardState != CardState.Active) { throw new ValidationException("Invalid card"); } t.AccountNumberIdToRef = cardTo.AccountNumberIdRef; accountTo = GetAccount(t.AccountNumberIdToRef); accountFrom = accountLoggedIn; //_accountsRepository.GetAccount(transaction.AccountNumberIdFrom); if (accountFrom.Balance < t.Amount) { throw new ValidationException("Insufficient funds"); } break; case TransactionType.SendMoneyFromCardToApp: if (accountLoggedIn.AccountNumberId != t.AccountNumberIdToRef) { throw new ValidationException("Invalid AccountNumberId"); } cardFrom = _cardsRepository.GetCard(t.CardSerialNumberIdFrom); if (cardFrom.CardState != CardState.Active) { throw new ValidationException("Invalid card"); } if (cardFrom.AvailablegDailySpendLimit < t.Amount) { throw new ValidationException("Daily Spend Limit Exceeded"); } if (cardFrom.MonthlySpendLimit < t.Amount) { throw new ValidationException("Monthly Spend Limit Exceeded"); } if (String.IsNullOrEmpty(t.CardFromEMVData)) { throw new ValidationException("Card EMV data not supplied"); } t.AccountNumberIdFromRef = cardFrom.AccountNumberIdRef; accountFrom = GetAccount(t.AccountNumberIdFromRef); accountTo = accountLoggedIn; //_accountsRepository.GetAccount(transaction.AccountNumberIdTo); if (accountFrom.Balance < t.Amount) { throw new ValidationException("Insufficient funds"); } break; default: throw new ValidationException("Invalid transaction type: " + t.TransactionType); } return(StoreTx(t, accountFrom, accountTo, cardFrom, useTransaction)); }