Skip to content

Commit

Permalink
Update notify cheque admin
Browse files Browse the repository at this point in the history
  • Loading branch information
anhnmt committed Aug 27, 2021
1 parent 1d657ac commit b5694ed
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 30 deletions.
83 changes: 60 additions & 23 deletions Backend/Areas/Admin/Controllers/ChequesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
{
Expand All @@ -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
Expand All @@ -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<Notifications> 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)
{
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -435,7 +445,7 @@ public ActionResult ChequeExec(ChequesExecViewModel chequeExec)
var newNotifications = CreateNotifications(newTransaction);

transaction.Commit();

ChatHub.Instance().SendNotifications(newNotifications);

return Json(new
Expand Down Expand Up @@ -573,40 +583,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();

// 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<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;
}
}
}
20 changes: 13 additions & 7 deletions Backend/Controllers/ChequesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,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
Expand All @@ -205,14 +205,18 @@ public ActionResult PostData(Cheques chequeInformation)
_context.Cheques.Add(chequeInformation);
_context.SaveChanges();

fromBankAccount.Balance -= chequeInformation.Amount;
// bankAccounts.Update(fromBankAccount);
_context.SaveChanges();
List<Notifications> 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;
var message = "Your account balance -" + chequeInformation.Amount +
", available balance: " + fromBankAccount.Balance;

var newNotifications = CreateNotification(chequeInformation, message);
newNotifications = CreateNotification(chequeInformation, message);
}

transaction.Commit();

Expand Down Expand Up @@ -373,7 +377,9 @@ 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;
Expand Down

0 comments on commit b5694ed

Please sign in to comment.