public static async Task <bool> SendNotiSells(ApplicationDbContext db, List <OrderDetails> orderDetails, DateTime?date = null) { if (date == null) { date = DateTime.Now; } Dictionary <string, List <OrderDetails> > sells_by_user = new Dictionary <string, List <OrderDetails> >(); foreach (var od in orderDetails) { var seller = od.ProductSale.Seller; var id = seller.Id; List <OrderDetails> sell_list; if (sells_by_user.TryGetValue(id, out sell_list)) { sell_list.Add(od); } else { sell_list = new List <OrderDetails>(); sell_list.Add(od); sells_by_user.Add(id, sell_list); } } foreach (var(sellerID, sell_list) in sells_by_user) { var message = "You have sold " + sell_list[0].Count + " Unit(s) of " + sell_list[0].Name; if (sell_list.Count > 1) { message = "You have sold multiple products to a user, check the details for more information"; } var noti_sell = new NotiSell() { SendToUser = sellerID, NotiDate = (DateTime)date, Message = message, Seen = false, OrderDetails = sell_list, }; await db.NotiSell.AddAsync(noti_sell); await db.SaveChangesAsync(); } return(true); }
public async Task <IActionResult> Index(string noti_type) { var claimsIdentity = (ClaimsIdentity)this.User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); bool is_admin = this.User.IsInRole(SD.ManagerUser); NotificationViewModel nvm = new NotificationViewModel(); if (!this.User.Identity.IsAuthenticated) { return(NotFound()); } if (noti_type == "NotiRole" || noti_type == null) { var notifications = await _db.NotiRole .Where(n => n.SendToUser == claim.Value || (n.SendToUser == "All_A" && is_admin) ) .Include(nr => nr.User) .ToListAsync(); foreach (var n in notifications) { NotiRole nr = new NotiRole() { Id = n.Id, Message = n.Message, User = n.User, UserID = n.UserID, NotiDate = n.NotiDate, Seen = n.Seen, SendToUser = n.SendToUser, }; nvm.NotiRole.Add(nr); n.Seen = true; } _db.UpdateRange(notifications); } if (noti_type == "NotiBuy" || noti_type == null) { var notifications = await _db.NotiBuy .Where(n => n.SendToUser == claim.Value ) .Include(n => n.OrderHeader) .Include(n => n.OrderHeader.User) .ToListAsync(); foreach (var n in notifications) { n.OrderDetails = await _db.OrderDetails.Where(o => o.OrderId == n.OrderHeaderID).ToListAsync(); NotiBuy nr = new NotiBuy() { Id = n.Id, Message = n.Message, OrderHeaderID = n.OrderHeaderID, OrderHeader = n.OrderHeader, OrderDetails = n.OrderDetails, NotiDate = n.NotiDate, Seen = n.Seen, SendToUser = n.SendToUser, }; nvm.NotiBuy.Add(nr); n.Seen = true; } _db.UpdateRange(notifications); } if (noti_type == "NotiSell" || noti_type == null) { var notifications = await _db.NotiSell .Where(n => n.SendToUser == claim.Value ) .Include(n => n.OrderDetails) .ToListAsync(); foreach (var n in notifications) { NotiSell ns = new NotiSell() { Id = n.Id, Message = n.Message, NotiDate = n.NotiDate, Seen = n.Seen, SendToUser = n.SendToUser, OrderDetails = n.OrderDetails, }; nvm.NotiSell.Add(ns); n.Seen = true; } _db.UpdateRange(notifications); } if (noti_type == "NotiGeneral" || noti_type == null) { var notifications = await _db.Notification.OfType <Notification>().Where(n => (n.SendToUser == claim.Value)).ToListAsync(); foreach (var n in notifications) { if (n.GetType() == typeof(Notification)) { Notification not = new Notification() { Id = n.Id, Message = n.Message, NotiDate = n.NotiDate, Seen = n.Seen, SendToUser = n.SendToUser }; nvm.NotiGeneral.Add(not); n.Seen = true; } } _db.UpdateRange(notifications); } if (noti_type == "NotiAuction" || noti_type == null) { var notifications = await _db.NotiAuction .Where(n => n.SendToUser == claim.Value ) .Include(n => n.AuctionHeader) .ToListAsync(); foreach (var n in notifications) { var products = await _db.AuctionProduct.Where(a => a.AuctionId == n.AuctionHeaderID).Include(a => a.Product).ToListAsync(); NotiAuction na = new NotiAuction() { Id = n.Id, Message = n.Message, NotiDate = n.NotiDate, Seen = n.Seen, SendToUser = n.SendToUser, AuctionHeader = n.AuctionHeader, AuctionHeaderID = n.AuctionHeaderID, AuctionProduct = products, }; var auction = await _db.AuctionHeader.FirstOrDefaultAsync(a => a.Id == n.AuctionHeaderID); string status = SD.ActiveStatus; if (DateTime.Compare(DateTime.Now, auction.EndDate) >= 0) { status = SD.PastStatus; } na.Status = status; nvm.NotiAuction.Add(na); n.Seen = true; } _db.UpdateRange(notifications); } await _db.SaveChangesAsync(); nvm.Type = noti_type; return(View(nvm)); }