Skip to content

Commit

Permalink
Update create notify cheque
Browse files Browse the repository at this point in the history
  • Loading branch information
anhnmt committed Aug 27, 2021
1 parent 3bb1968 commit 1d657ac
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 153 deletions.
57 changes: 34 additions & 23 deletions Backend/Controllers/BankAccountsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
{
Expand All @@ -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
{
Expand All @@ -95,7 +103,10 @@ public ActionResult GetData(int Account)
// GET: Admin/BankAccounts/Details/5
public ActionResult GetStatus()
{
var data = Enum.GetValues(typeof(BankAccountStatus)).Cast<BankAccountStatus>().Select(v => v.ToString())
var data = Enum
.GetValues(typeof(BankAccountStatus))
.Cast<BankAccountStatus>()
.Select(v => v.ToString())
.ToArray();

return Json(data, JsonRequestBehavior.AllowGet);
Expand Down
249 changes: 153 additions & 96 deletions Backend/Controllers/ChequesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -103,104 +104,133 @@ public ActionResult FindId(int chequeId)
[HttpPost]
public ActionResult PostData(Cheques chequeInformation)
{
var errors = new Dictionary<string, string>();
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, string>();
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.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);
}

// cheques.Add(chequeInformation);
_context.Cheques.Add(chequeInformation);
_context.SaveChanges();

fromBankAccount.Balance -= chequeInformation.Amount;
// bankAccounts.Update(fromBankAccount);
_context.SaveChanges();

var message = "Your account balance -" + chequeInformation.Amount +
", available balance: " + fromBankAccount.Balance;

var newNotifications = CreateNotification(chequeInformation, message);

transaction.Commit();

fromBankAccount.Balance -= chequeInformation.Amount;
bankAccounts.Update(fromBankAccount);
ChatHub.Instance().SendNotifications(newNotifications);
}
catch (Exception ex)
{
transaction.Rollback();
return Json(new
{
data = ex,
message = "error",
statuscode = 404
}, JsonRequestBehavior.AllowGet);
throw;
}
}
}

return Json(new
{
Expand Down Expand Up @@ -343,40 +373,67 @@ public ActionResult DeleteData(int chequeId)

var fromBankAccount =
_context.BankAccounts.FirstOrDefault(x => x.BankAccountId == cheque.FromBankAccountId);

List<Notifications> 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<Notifications> CreateNotification(Cheques cheque, string message)
{
var lstNotification = new List<Notifications>()
{
new Notifications
{
AccountId = cheque.FromBankAccount.AccountId,
Content = message,
Status = (int) NotificationStatus.Unread,
PkType = (int) NotificationType.Cheque,
PkId = cheque.ChequeBookId,
}
};

_context.Set<Notifications>().AddRange(lstNotification);
_context.SaveChanges();
return lstNotification;
}
}
}
Loading

0 comments on commit 1d657ac

Please sign in to comment.