diff --git a/Backend/Areas/Admin/Controllers/ChequesController.cs b/Backend/Areas/Admin/Controllers/ChequesController.cs index 81e5d85..2b180c5 100644 --- a/Backend/Areas/Admin/Controllers/ChequesController.cs +++ b/Backend/Areas/Admin/Controllers/ChequesController.cs @@ -114,7 +114,7 @@ public ActionResult PostData(Cheques chequeInformation) var fromBankAccount = _context.BankAccounts.FirstOrDefault(x => x.BankAccountId == chequeInformation.FromBankAccountId); - if (fromBankAccount.Status != (int) BankAccountStatus.Actived) + if (fromBankAccount != null && fromBankAccount.Status != (int) BankAccountStatus.Actived) { errors.Add("FromBankAccountId", "This bank account is not actived"); return Json(new @@ -127,7 +127,7 @@ public ActionResult PostData(Cheques chequeInformation) var chequeBook = _context.ChequeBooks.FirstOrDefault(x => x.ChequeBookId == chequeInformation.ChequeBookId); - if (chequeBook.Status != (int) ChequeBookStatus.Opened) + if (chequeBook != null && chequeBook.Status != (int) ChequeBookStatus.Opened) { return Json(new { @@ -145,7 +145,7 @@ public ActionResult PostData(Cheques chequeInformation) chequeInformation.Code = code; chequeInformation.Status = (int) ChequeStatus.Actived; - if (fromBankAccount.Balance < chequeInformation.Amount) + if (fromBankAccount != null && fromBankAccount.Balance < chequeInformation.Amount) { errors.Add("Amount", "Your balance is not enough"); return Json(new @@ -171,18 +171,23 @@ public ActionResult PostData(Cheques chequeInformation) _context.Cheques.Add(chequeInformation); _context.SaveChanges(); - fromBankAccount.Balance -= chequeInformation.Amount; - // bankAccounts.Update(fromBankAccount); - // _context.Entry(fromBankAccount).State = EntityState.Modified; - _context.SaveChanges(); + List newNotifications = null; + if (fromBankAccount != null) + { + fromBankAccount.Balance -= chequeInformation.Amount; + // bankAccounts.Update(fromBankAccount); + // _context.Entry(fromBankAccount).State = EntityState.Modified; + _context.SaveChanges(); + + var message = "Your account balance -" + chequeInformation.Amount + + ", available balance: " + fromBankAccount.Balance; + + newNotifications = CreateNotification(chequeInformation, message); + } transaction.Commit(); - return Json(new - { - message = "Success", - statusCode = 200, - }, JsonRequestBehavior.AllowGet); + ChatHub.Instance().SendNotifications(newNotifications); } catch (Exception ex) { @@ -193,8 +198,13 @@ public ActionResult PostData(Cheques chequeInformation) message = "error", statuscode = 404 }, JsonRequestBehavior.AllowGet); - throw; } + + return Json(new + { + message = "Success", + statusCode = 200, + }, JsonRequestBehavior.AllowGet); } } } @@ -435,7 +445,7 @@ public ActionResult ChequeExec(ChequesExecViewModel chequeExec) var newNotifications = CreateNotifications(newTransaction); transaction.Commit(); - + ChatHub.Instance().SendNotifications(newNotifications); return Json(new @@ -573,6 +583,8 @@ public ActionResult DeleteData(int chequeId) var fromBankAccount = _context.BankAccounts.FirstOrDefault(x => x.BankAccountId == cheque.FromBankAccountId); + List newNotifications = null; + if (fromBankAccount != null) { fromBankAccount.Balance += cheque.Amount; @@ -580,33 +592,58 @@ public ActionResult DeleteData(int chequeId) // cheques.Delete(cheque); // bankAccounts.Edit(fromBankAccount); - _context.Cheques.Remove(cheque); - _context.SaveChanges(); + + var message = "Your account balance +" + cheque.Amount + + ", available balance: " + cheque.FromBankAccount.Balance; + + newNotifications = CreateNotification(cheque, message); } + _context.Cheques.Remove(cheque); _context.SaveChanges(); transaction.Commit(); - return Json(new - { - message = "Success", - data = "Delete Successfully", - statusCode = 200, - }, JsonRequestBehavior.AllowGet); + ChatHub.Instance().SendNotifications(newNotifications); } catch (Exception ex) { transaction.Rollback(); + return Json(new { data = ex, message = "error", statuscode = 404 }, JsonRequestBehavior.AllowGet); - throw; } + + return Json(new + { + message = "Success", + data = "Delete Successfully", + statusCode = 200, + }, JsonRequestBehavior.AllowGet); } } } + + private List CreateNotification(Cheques cheque, string message) + { + var lstNotification = new List() + { + new Notifications + { + AccountId = cheque.FromBankAccount.AccountId, + Content = message, + Status = (int) NotificationStatus.Unread, + PkType = (int) NotificationType.Cheque, + PkId = cheque.ChequeBookId, + } + }; + + _context.Set().AddRange(lstNotification); + _context.SaveChanges(); + return lstNotification; + } } } \ No newline at end of file diff --git a/Backend/Controllers/BankAccountsController.cs b/Backend/Controllers/BankAccountsController.cs index 5173c2e..46f9de2 100644 --- a/Backend/Controllers/BankAccountsController.cs +++ b/Backend/Controllers/BankAccountsController.cs @@ -28,12 +28,14 @@ public ActionResult Index() [HttpPost] public ActionResult GetBalance(int bankId) { - var data = bankAccounts.Get().Where(x => x.BankAccountId == bankId).Select(x => new BalanceViewModels - { - Balance = x.Balance, - BankId = x.BankAccountId, - Currency = x.Currency.Name - }); + var data = bankAccounts.Get() + .Where(x => x.BankAccountId == bankId) + .Select(x => new BalanceViewModels + { + Balance = x.Balance, + BankId = x.BankAccountId, + Currency = x.Currency.Name + }); return Json(new { @@ -45,11 +47,14 @@ public ActionResult GetBalance(int bankId) public ActionResult GetInfoBankAccount(string name) { - var data = bankAccounts.Get().Where(x => x.Name == name).Select(x => new GetInfoBankAccountViewModels - { - Name = x.Account.Name, - Id = x.BankAccountId - }); + var data = bankAccounts + .Get() + .Where(x => x.Name == name) + .Select(x => new GetInfoBankAccountViewModels + { + Name = x.Account.Name, + Id = x.BankAccountId + }); if (data.FirstOrDefault() == null) { @@ -72,17 +77,20 @@ public ActionResult GetInfoBankAccount(string name) public ActionResult GetData(int Account) { ViewBag.Accounts = "active"; - var data = bankAccounts.Get().Where(a => a.AccountId == Account).Select(x => new BankAccountsViewModels - { - AccountId = x.AccountId, - BankAccountId = x.BankAccountId, - CurrencyId = x.CurrencyId, - CurrencyName = x.Currency.Name, - Name = x.Name, - Balance = x.Balance, - Status = x.Status, - StatusName = ((BankAccountStatus) x.Status).ToString() - }); + var data = bankAccounts + .Get() + .Where(a => a.AccountId == Account) + .Select(x => new BankAccountsViewModels + { + AccountId = x.AccountId, + BankAccountId = x.BankAccountId, + CurrencyId = x.CurrencyId, + CurrencyName = x.Currency.Name, + Name = x.Name, + Balance = x.Balance, + Status = x.Status, + StatusName = ((BankAccountStatus) x.Status).ToString() + }); return Json(new { @@ -95,7 +103,10 @@ public ActionResult GetData(int Account) // GET: Admin/BankAccounts/Details/5 public ActionResult GetStatus() { - var data = Enum.GetValues(typeof(BankAccountStatus)).Cast().Select(v => v.ToString()) + var data = Enum + .GetValues(typeof(BankAccountStatus)) + .Cast() + .Select(v => v.ToString()) .ToArray(); return Json(data, JsonRequestBehavior.AllowGet); diff --git a/Backend/Controllers/ChequesController.cs b/Backend/Controllers/ChequesController.cs index 1bdfec9..f74e9f5 100644 --- a/Backend/Controllers/ChequesController.cs +++ b/Backend/Controllers/ChequesController.cs @@ -5,6 +5,7 @@ using System.Web; using System.Web.Mvc; using Backend.Areas.Admin.Data; +using Backend.Hubs; using OnlineBanking.BLL.Repositories; using OnlineBanking.DAL; @@ -103,104 +104,137 @@ public ActionResult FindId(int chequeId) [HttpPost] public ActionResult PostData(Cheques chequeInformation) { - var errors = new Dictionary(); - string code; - - - if (!ModelState.IsValid) + using (_context = new ApplicationDbContext()) { - foreach (var k in ModelState.Keys) - foreach (var err in ModelState[k].Errors) + using (var transaction = _context.Database.BeginTransaction()) { - var key = Regex.Replace(k, @"(\w+)\.(\w+)", @"$2"); - if (!errors.ContainsKey(key)) - errors.Add(key, err.ErrorMessage); - } + try + { + var errors = new Dictionary(); + string code; - return Json(new - { - message = "Error", - data = errors, - statusCode = 400, - }, JsonRequestBehavior.AllowGet); - } - var user = (Accounts) Session["user"]; - var account = accounts.Get(user.AccountId); - if (!chequebooks.CheckDuplicate(x => - x.ChequeBookId == chequeInformation.ChequeBookId && x.AccountId == account.AccountId)) - { - return Json(new - { - message = "Error", - statusCode = 404, - }, JsonRequestBehavior.AllowGet); - } + if (!ModelState.IsValid) + { + foreach (var k in ModelState.Keys) + foreach (var err in ModelState[k].Errors) + { + var key = Regex.Replace(k, @"(\w+)\.(\w+)", @"$2"); + if (!errors.ContainsKey(key)) + errors.Add(key, err.ErrorMessage); + } - var fromBankAccount = bankAccounts.Get(chequeInformation.FromBankAccountId); + return Json(new + { + message = "Error", + data = errors, + statusCode = 400, + }, JsonRequestBehavior.AllowGet); + } - if (fromBankAccount.Status != (int) BankAccountStatus.Actived) - { - errors.Add("FromBankAccountId", "This bank account is not actived"); - return Json(new - { - message = "Error", - data = errors, - statusCode = 400 - }, JsonRequestBehavior.AllowGet); - } + var user = (Accounts) Session["user"]; + var account = _context.Accounts.FirstOrDefault(x => x.AccountId == user.AccountId); + if (!_context.ChequeBooks.AsNoTracking().Any(x => + x.ChequeBookId == chequeInformation.ChequeBookId && x.AccountId == account.AccountId)) + { + return Json(new + { + message = "Error", + statusCode = 404, + }, JsonRequestBehavior.AllowGet); + } - var chequeBook = chequebooks.Get(chequeInformation.ChequeBookId); - if (chequeBook.Status != (int) ChequeBookStatus.Opened) - { - return Json(new - { - message = "Error", - data = "This cheque book is not opened", - statusCode = 400 - }, JsonRequestBehavior.AllowGet); - } + var fromBankAccount = _context.BankAccounts.FirstOrDefault(x => + x.BankAccountId == chequeInformation.FromBankAccountId); - do - { - code = Utils.RandomString(16); - } while (cheques.CheckDuplicate(x => x.Code == code)); + if (fromBankAccount != null && fromBankAccount.Status != (int) BankAccountStatus.Actived) + { + errors.Add("FromBankAccountId", "This bank account is not actived"); + return Json(new + { + message = "Error", + data = errors, + statusCode = 400 + }, JsonRequestBehavior.AllowGet); + } - chequeInformation.Code = code; - chequeInformation.Status = (int) ChequeStatus.Actived; + var chequeBook = + _context.ChequeBooks.FirstOrDefault(x => x.ChequeBookId == chequeInformation.ChequeBookId); - if (fromBankAccount.Balance < chequeInformation.Amount) - { - errors.Add("Amount", "Your balance is not enough"); - return Json(new - { - message = "Error", - data = errors, - statusCode = 400 - }, JsonRequestBehavior.AllowGet); - } + if (chequeBook != null && chequeBook.Status != (int) ChequeBookStatus.Opened) + { + return Json(new + { + message = "Error", + data = "This cheque book is not opened", + statusCode = 400 + }, JsonRequestBehavior.AllowGet); + } - if (0 > chequeInformation.Amount) - { - errors.Add("Amount", "Please enter a positive number"); - return Json(new - { - message = "Error", - data = errors, - statusCode = 400 - }, JsonRequestBehavior.AllowGet); - } + do + { + code = Utils.RandomString(16); + } while (_context.Cheques.AsNoTracking().Any(x => x.Code == code)); - if (!cheques.Add(chequeInformation)) - return Json(new - { - message = "Error", - statusCode = 400, - data = ModelState - }, JsonRequestBehavior.AllowGet); + chequeInformation.Code = code; + chequeInformation.Status = (int) ChequeStatus.Actived; + + if (fromBankAccount != null && fromBankAccount.Balance < chequeInformation.Amount) + { + errors.Add("Amount", "Your balance is not enough"); + return Json(new + { + message = "Error", + data = errors, + statusCode = 400 + }, JsonRequestBehavior.AllowGet); + } + + if (0 > chequeInformation.Amount) + { + errors.Add("Amount", "Please enter a positive number"); + return Json(new + { + message = "Error", + data = errors, + statusCode = 400 + }, JsonRequestBehavior.AllowGet); + } - fromBankAccount.Balance -= chequeInformation.Amount; - bankAccounts.Update(fromBankAccount); + // cheques.Add(chequeInformation); + _context.Cheques.Add(chequeInformation); + _context.SaveChanges(); + + List newNotifications = null; + if (fromBankAccount != null) + { + fromBankAccount.Balance -= chequeInformation.Amount; + // bankAccounts.Update(fromBankAccount); + _context.SaveChanges(); + + var message = "Your account balance -" + chequeInformation.Amount + + ", available balance: " + fromBankAccount.Balance; + + newNotifications = CreateNotification(chequeInformation, message); + } + + transaction.Commit(); + + ChatHub.Instance().SendNotifications(newNotifications); + } + catch (Exception ex) + { + transaction.Rollback(); + return Json(new + { + data = ex, + message = "error", + statuscode = 404 + }, JsonRequestBehavior.AllowGet); + throw; + } + } + } return Json(new { @@ -343,40 +377,69 @@ public ActionResult DeleteData(int chequeId) var fromBankAccount = _context.BankAccounts.FirstOrDefault(x => x.BankAccountId == cheque.FromBankAccountId); - + + List newNotifications = null; + if (fromBankAccount != null) { fromBankAccount.Balance += cheque.Amount; _context.SaveChanges(); + var message = "Your account balance +" + cheque.Amount + + ", available balance: " + cheque.FromBankAccount.Balance; + + newNotifications = CreateNotification(cheque, message); + // cheques.Delete(cheque); // bankAccounts.Edit(fromBankAccount); - _context.Cheques.Remove(cheque); - _context.SaveChanges(); } - + + _context.Cheques.Remove(cheque); + _context.SaveChanges(); + transaction.Commit(); - return Json(new - { - message = "Success", - data = "Delete Successfully", - statusCode = 200, - }, JsonRequestBehavior.AllowGet); + ChatHub.Instance().SendNotifications(newNotifications); } catch (Exception ex) { transaction.Rollback(); + return Json(new { data = ex, message = "error", statuscode = 404 }, JsonRequestBehavior.AllowGet); - throw; } + + return Json(new + { + message = "Success", + data = "Delete Successfully", + statusCode = 200, + }, JsonRequestBehavior.AllowGet); } } } + + private List CreateNotification(Cheques cheque, string message) + { + var lstNotification = new List() + { + new Notifications + { + AccountId = cheque.FromBankAccount.AccountId, + Content = message, + Status = (int) NotificationStatus.Unread, + PkType = (int) NotificationType.Cheque, + PkId = cheque.ChequeBookId, + } + }; + + _context.Set().AddRange(lstNotification); + _context.SaveChanges(); + return lstNotification; + } } } \ No newline at end of file diff --git a/Backend/Controllers/NotificationsController.cs b/Backend/Controllers/NotificationsController.cs index 764df5e..bccefc1 100644 --- a/Backend/Controllers/NotificationsController.cs +++ b/Backend/Controllers/NotificationsController.cs @@ -37,13 +37,7 @@ public ActionResult GetData() var data = notifications.Get() .Where(x => x.AccountId == currentUser.AccountId) .OrderByDescending(x => x.CreatedAt) - .Select(x => - { - var pkObject = transactionDetails - .Get().FirstOrDefault(y => y.TransactionDetailId == x.PkId); - - return new NotificationViewModel(x, pkObject); - }); + .Select(x => new NotificationViewModel(x)); return Json(new { diff --git a/Backend/Hubs/ChatHub.cs b/Backend/Hubs/ChatHub.cs index b87424f..af8ab5e 100644 --- a/Backend/Hubs/ChatHub.cs +++ b/Backend/Hubs/ChatHub.cs @@ -146,13 +146,7 @@ public IEnumerable GetNotificationsHistory(int accountId) .OrderByDescending(m => m.CreatedAt) .Take(5) .AsEnumerable() - .Select(x => - { - var pkObject = transactionDetailRepo - .Get().FirstOrDefault(y => y.TransactionDetailId == x.PkId); - - return new NotificationViewModel(x, pkObject); - }); + .Select(x => new NotificationViewModel(x)); } public string ReadNotification(int notificationId) @@ -173,9 +167,13 @@ public string ReadNotification(int notificationId) _context.SaveChanges(); } - if (notification.PkType == (int) NotificationType.Transaction) + switch (notification.PkType) { - return "/Transactions/TransactionsDetails/" + notification.PkId; + case (int) NotificationType.Transaction: + return "/Transactions/TransactionsDetails/" + notification.PkId; + + case (int) NotificationType.Cheque: + return "/Cheques/?ChequeBookId=" + notification.PkId; } } catch (Exception ex) @@ -195,11 +193,8 @@ public void SendNotifications(List notifications) notifications.ForEach(x => { - var pkObject = transactionDetailRepo.Get() - .FirstOrDefault(y => y.TransactionDetailId == x.PkId); - context.Clients.Group("user-" + x.AccountId) - .newNotification(new NotificationViewModel(x, pkObject)); + .newNotification(new NotificationViewModel(x)); // await context.Clients.Group("user-" + x.AccountId) // .historyNotifications(GetNotificationsHistory(GetIntegerAccountId())); }); diff --git a/Backend/Views/Home/Index.cshtml b/Backend/Views/Home/Index.cshtml index 5611886..4cc26df 100644 --- a/Backend/Views/Home/Index.cshtml +++ b/Backend/Views/Home/Index.cshtml @@ -28,9 +28,9 @@ - - + + diff --git a/Backend/Views/Transactions/TransactionsDetails.cshtml b/Backend/Views/Transactions/TransactionsDetails.cshtml index 252c4d4..ad30696 100644 --- a/Backend/Views/Transactions/TransactionsDetails.cshtml +++ b/Backend/Views/Transactions/TransactionsDetails.cshtml @@ -4,7 +4,6 @@ @{ ViewBag.Title = "title"; Layout = "~/Views/Shared/_Layout.cshtml"; - var user = (Accounts) Session["user"]; }
@@ -29,10 +28,8 @@
  • Amount - - @Model.Currency - @(Model.Type == 1 ? "-" : "+") @Model.Amount - + @* @Model.Currency *@ +
  • Beneficiary @Model.ToName @@ -48,4 +45,13 @@
  • - \ No newline at end of file + + +@section scripts{ + +} \ No newline at end of file diff --git a/OnlineBanking.DAL/Common/Status.cs b/OnlineBanking.DAL/Common/Status.cs index 4154cb8..3c0ed46 100644 --- a/OnlineBanking.DAL/Common/Status.cs +++ b/OnlineBanking.DAL/Common/Status.cs @@ -65,7 +65,7 @@ public enum NotificationStatus public enum NotificationType { Transaction = 0, - Message = 1, + Cheque = 1, } public enum TransactionType { diff --git a/OnlineBanking.DAL/ViewModel/NotificationViewModel.cs b/OnlineBanking.DAL/ViewModel/NotificationViewModel.cs index be32842..465b610 100644 --- a/OnlineBanking.DAL/ViewModel/NotificationViewModel.cs +++ b/OnlineBanking.DAL/ViewModel/NotificationViewModel.cs @@ -12,7 +12,7 @@ public NotificationViewModel() { } - public NotificationViewModel(Notifications notification, TransactionDetails transactionDetails) + public NotificationViewModel(Notifications notification) { NotificationId = notification.NotificationId; AccountId = notification.AccountId; @@ -24,10 +24,6 @@ public NotificationViewModel(Notifications notification, TransactionDetails tran PkType = notification.PkType; PkTypeName = ((NotificationType) notification.PkType).ToString(); - var obj = new TransactionsViewModels(transactionDetails, transactionDetails.Transaction); - PkId = obj.TransactionDetailId; - PkObject = obj; - CreatedAt = notification.CreatedAt?.ToString("dd-MM-yyyy HH:mm:ss"); UpdatedAt = notification.UpdatedAt?.ToString("dd-MM-yyyy HH:mm:ss"); }
    Account NumberCurrencyId BalanceStatus CurrencyStatus