<commit_before>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Policy;
using System.Web;
using System.Web.Mvc;
using Cats.Services.Security;
using Cats.Services.Common;
using Cats.Services.EarlyWarning;
namespace Cats.Helpers
{
    public static class  NotificationHelper
    {
       

        public static int GetUnreadNotifications(this HtmlHelper helper)
        {
            try
            {
                var accountService = (IUserAccountService)DependencyResolver.Current.GetService(typeof(IUserAccountService));
                var user = HttpContext.Current.User.Identity.Name;
                var roles = accountService.GetUserPermissions(user).Select(a => a.Roles).ToList();
                var allUserRollsInAllApplications = new List<string>();

                foreach (var app in roles)
                {
                    allUserRollsInAllApplications.AddRange(app.Select(role => role.RoleName));
                }
                var notificationService = (INotificationService)DependencyResolver.Current.GetService(typeof(INotificationService));
                var totallUnread = notificationService.GetAllNotification().Where(n => n.IsRead == false && allUserRollsInAllApplications.Contains(n.RoleName)).OrderByDescending(n=>n.NotificationId).ToList();
                

                
               

                return totallUnread.Count();
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public static int GetUnreadNotifications()
        {
            try
            {
                var accountService = (IUserAccountService)DependencyResolver.Current.GetService(typeof(IUserAccountService));
                var user = HttpContext.Current.User.Identity.Name;
                var roles = accountService.GetUserPermissions(user).Select(a => a.Roles).ToList();
                var allUserRollsInAllApplications = new List<string>();

                foreach (var app in roles)
                {
                    allUserRollsInAllApplications.AddRange(app.Select(role => role.RoleName));
                }
                var notificationService = (INotificationService)DependencyResolver.Current.GetService(typeof(INotificationService));
                var totallUnread = notificationService.GetAllNotification().Where(n => n.IsRead == false && allUserRollsInAllApplications.Contains(n.RoleName)).OrderByDescending(n => n.NotificationId).ToList();
                return totallUnread.Count();
            }
            catch (Exception)
            {
                return 0;
            }
        }




        public static HtmlString GetActiveNotifications(this HtmlHelper helper)
        {
            try
            {
                var accountService = (IUserAccountService)DependencyResolver.Current.GetService(typeof(IUserAccountService));
              
                var user = HttpContext.Current.User.Identity.Name;

                var roles = accountService.GetUserPermissions(user).Select(a => a.Roles).ToList();
                var allUserRollsInAllApplications = new List<string>();

                foreach (var app in roles)
                {
                    allUserRollsInAllApplications.AddRange(app.Select(role => role.RoleName));
                }


                var str = "<ul>";
                var notificationService = (INotificationService)DependencyResolver.Current.GetService(typeof(INotificationService));
                var totallUnread = notificationService.GetAllNotification().Where(n => n.IsRead == false && allUserRollsInAllApplications.Contains(n.RoleName)).ToList();
                int max = 0;

                if (totallUnread.Count < 1)
                    return MvcHtmlString.Create("");
                max = totallUnread.Count > 5 ? 5 : totallUnread.Count;

                for (int i = 0; i < max;i ++ )
                {
                    str = str + "<li>";
                    str = str + "<a href=" + totallUnread[i].Url + ">";
                    str = str + totallUnread[i].Text;
                    str = str + "</li>";
                    str = str + "</a>";
                }
                    
                str = str + "</ul>";
                if (totallUnread.Count > 5)
                {
                    str = str + "<a href=/Home/GetUnreadNotificationDetail>" + "More...</a>";
                }
               
                return MvcHtmlString.Create(str);
            }
            catch (Exception)
            {
                return MvcHtmlString.Create("");
            }
        }

        public static HtmlString GetActiveNotifications()
        {
            try
            {
                var accountService = (IUserAccountService)DependencyResolver.Current.GetService(typeof(IUserAccountService));

                var user = HttpContext.Current.User.Identity.Name;

                var roles = accountService.GetUserPermissions(user).Select(a => a.Roles).ToList();
                var allUserRollsInAllApplications = new List<string>();

                foreach (var app in roles)
                {
                    allUserRollsInAllApplications.AddRange(app.Select(role => role.RoleName));
                }


                var str = "<ul>";
                var notificationService = (INotificationService)DependencyResolver.Current.GetService(typeof(INotificationService));
                var totallUnread = notificationService.GetAllNotification().Where(n => n.IsRead == false && allUserRollsInAllApplications.Contains(n.RoleName)).ToList();
                int max = 0;

                if (totallUnread.Count < 1)
                    return MvcHtmlString.Create("");
                max = totallUnread.Count > 5 ? 5 : totallUnread.Count;

                for (int i = 0; i < max; i++)
                {
                    str = str + "<li>";
                    str = str + "<a href=" + totallUnread[i].Url + ">";
                    str = str + totallUnread[i].Text;
                    str = str + "</li>";
                    str = str + "</a>";
                }

                str = str + "</ul>";
                if (totallUnread.Count > 5)
                {
                    str = str + "<a href=/Home/GetUnreadNotificationDetail>" + "More...</a>";
                }

                return MvcHtmlString.Create(str);
            }
            catch (Exception)
            {
                return MvcHtmlString.Create("");
            }
        }


        public static void MakeNotificationRead(int recordId)
        {
            try
            {
                var notificationService = (INotificationService)DependencyResolver.Current.GetService(typeof(INotificationService));
                //var reliefRequisitionService =
                //    (IReliefRequisitionService)DependencyResolver.Current.GetService(typeof(IReliefRequisitionService));

                var notification = notificationService.FindBy(i => i.RecordId == recordId).Single();
                notification.IsRead = true;
                notificationService.EditNotification(notification); 
            }
            catch (Exception)
            {
                
               
            }
           
        }

    }
}<commit_msg>refacotr notificatoin order<commit_after>﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Policy;
using System.Web;
using System.Web.Mvc;
using Cats.Services.Security;
using Cats.Services.Common;
using Cats.Services.EarlyWarning;
namespace Cats.Helpers
{
    public static class  NotificationHelper
    {
       

        public static int GetUnreadNotifications(this HtmlHelper helper)
        {
            try
            {
                var accountService = (IUserAccountService)DependencyResolver.Current.GetService(typeof(IUserAccountService));
                var user = HttpContext.Current.User.Identity.Name;
                var roles = accountService.GetUserPermissions(user).Select(a => a.Roles).ToList();
                var allUserRollsInAllApplications = new List<string>();

                foreach (var app in roles)
                {
                    allUserRollsInAllApplications.AddRange(app.Select(role => role.RoleName));
                }
                var notificationService = (INotificationService)DependencyResolver.Current.GetService(typeof(INotificationService));
                var totallUnread = notificationService.GetAllNotification().Where(n => n.IsRead == false && allUserRollsInAllApplications.Contains(n.RoleName)).OrderByDescending(n=>n.NotificationId).ToList();
                

                
               

                return totallUnread.Count();
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public static int GetUnreadNotifications()
        {
            try
            {
                var accountService = (IUserAccountService)DependencyResolver.Current.GetService(typeof(IUserAccountService));
                var user = HttpContext.Current.User.Identity.Name;
                var roles = accountService.GetUserPermissions(user).Select(a => a.Roles).ToList();
                var allUserRollsInAllApplications = new List<string>();

                foreach (var app in roles)
                {
                    allUserRollsInAllApplications.AddRange(app.Select(role => role.RoleName));
                }
                var notificationService = (INotificationService)DependencyResolver.Current.GetService(typeof(INotificationService));
                var totallUnread = notificationService.GetAllNotification().Where(n => n.IsRead == false && allUserRollsInAllApplications.Contains(n.RoleName)).OrderByDescending(n => n.NotificationId).ToList();
                return totallUnread.Count();
            }
            catch (Exception)
            {
                return 0;
            }
        }




        public static HtmlString GetActiveNotifications(this HtmlHelper helper)
        {
            try
            {
                var accountService = (IUserAccountService)DependencyResolver.Current.GetService(typeof(IUserAccountService));
              
                var user = HttpContext.Current.User.Identity.Name;

                var roles = accountService.GetUserPermissions(user).Select(a => a.Roles).ToList();
                var allUserRollsInAllApplications = new List<string>();

                foreach (var app in roles)
                {
                    allUserRollsInAllApplications.AddRange(app.Select(role => role.RoleName));
                }


                var str = "<ul>";
                var notificationService = (INotificationService)DependencyResolver.Current.GetService(typeof(INotificationService));
                var totallUnread = notificationService.GetAllNotification().Where(n => n.IsRead == false && allUserRollsInAllApplications.Contains(n.RoleName)).OrderByDescending(n => n.NotificationId).ToList();
                int max = 0;

                if (totallUnread.Count < 1)
                    return MvcHtmlString.Create("");
                max = totallUnread.Count > 5 ? 5 : totallUnread.Count;

                for (int i = 0; i < max;i ++ )
                {
                    str = str + "<li>";
                    str = str + "<a href=" + totallUnread[i].Url + ">";
                    str = str + totallUnread[i].Text;
                    str = str + "</li>";
                    str = str + "</a>";
                }
                    
                str = str + "</ul>";
                if (totallUnread.Count > 5)
                {
                    str = str + "<a href=/Home/GetUnreadNotificationDetail>" + "More...</a>";
                }
               
                return MvcHtmlString.Create(str);
            }
            catch (Exception)
            {
                return MvcHtmlString.Create("");
            }
        }

        public static HtmlString GetActiveNotifications()
        {
            try
            {
                var accountService = (IUserAccountService)DependencyResolver.Current.GetService(typeof(IUserAccountService));

                var user = HttpContext.Current.User.Identity.Name;

                var roles = accountService.GetUserPermissions(user).Select(a => a.Roles).ToList();
                var allUserRollsInAllApplications = new List<string>();

                foreach (var app in roles)
                {
                    allUserRollsInAllApplications.AddRange(app.Select(role => role.RoleName));
                }


                var str = "<ul>";
                var notificationService = (INotificationService)DependencyResolver.Current.GetService(typeof(INotificationService));
                var totallUnread = notificationService.GetAllNotification().Where(n => n.IsRead == false && allUserRollsInAllApplications.Contains(n.RoleName)).ToList();
                int max = 0;

                if (totallUnread.Count < 1)
                    return MvcHtmlString.Create("");
                max = totallUnread.Count > 5 ? 5 : totallUnread.Count;

                for (int i = 0; i < max; i++)
                {
                    str = str + "<li>";
                    str = str + "<a href=" + totallUnread[i].Url + ">";
                    str = str + totallUnread[i].Text;
                    str = str + "</li>";
                    str = str + "</a>";
                }

                str = str + "</ul>";
                if (totallUnread.Count > 5)
                {
                    str = str + "<a href=/Home/GetUnreadNotificationDetail>" + "More...</a>";
                }

                return MvcHtmlString.Create(str);
            }
            catch (Exception)
            {
                return MvcHtmlString.Create("");
            }
        }


        public static void MakeNotificationRead(int recordId)
        {
            try
            {
                var notificationService = (INotificationService)DependencyResolver.Current.GetService(typeof(INotificationService));
                //var reliefRequisitionService =
                //    (IReliefRequisitionService)DependencyResolver.Current.GetService(typeof(IReliefRequisitionService));

                var notification = notificationService.FindBy(i => i.RecordId == recordId).Single();
                notification.IsRead = true;
                notificationService.EditNotification(notification); 
            }
            catch (Exception)
            {
                
               
            }
           
        }

    }
}
{'fraction_non_alphanumeric': 0.09851212718255316, 'fraction_numerical': 0.00190230314559413, 'mean_word_length': 1.6011662837957237, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7037378', 'n_tokens_mistral': 3763, 'n_tokens_neox': 3471, 'n_words': 639}