private void LogEmployeeTasks(Employee employee, EmployeeTasks employeeTasks)
        {
            try
            {
                if (TotalTaskCount(employeeTasks) == 0)
                {
                    Log.Debug(string.Format("{0} {1} has NO Overdue, Completed or DueTomorrow tasks", employee.FullName,
                        employee.GetEmail()));
                }
                else
                {
                    Log.Debug(
                        string.Format(
                            "{0} {1} - {2} Overdue tasks - {3} Completed tasks - {4} DueTomorrow tasks", employee.FullName, employee.GetEmail()

                            , (employeeTasks.GeneralRiskAssessmentTasksOverdue.Count
                               + employeeTasks.PersonalRiskAssessmentTasksOverdue.Count
                               + employeeTasks.FireRiskAssessmentTasksOverdue.Count
                               + employeeTasks.HazadousSubstanceRiskAssessmentTasksOverdue.Count
                               + employeeTasks.RiskAssessmentReviewTasksOverdue.Count
                               + employeeTasks.ResponsibilityTaskOverdue.Count
                               + employeeTasks.ActionTasksOverdue.Count),
                               
                               
                                (employeeTasks.HazardousSubstanceTasksCompleted.Count
                              + employeeTasks.FireRiskAssessmentTasksCompleted.Count
                              + employeeTasks.PersonalRiskAssessmentTasksCompleted.Count
                              + employeeTasks.GeneralRiskAssessmentTasksCompleted.Count
                              + employeeTasks.RiskAssessmentReviewTasksCompleted.Count),
                               
                               (employeeTasks.GeneralRiskAssessmentTasksDueTomorrow.Count
                              + employeeTasks.PersonalRiskAssessmentTasksDueTomorrow.Count
                              + employeeTasks.FireRiskAssessmentTasksDueTomorrow.Count
                              + employeeTasks.HazardousSubstanceTasksDueTomorrow.Count
                              + employeeTasks.RiskAssessmentReviewTasksDueTomorrow.Count
                              + employeeTasks.ResponsibilityTasksDueTomorrow.Count
                              + employeeTasks.ActionTasksDueTomorrow.Count)
                               
                               
                               ));                 
                }
            }
            catch (Exception ex)
            {
                Log.Debug("Exception " + ex.Message);
                throw;
            }
       }
        private long TotalTaskCount( EmployeeTasks tasks)
        {
            try
            {
                //Log.Debug("TotalTaskCount() ");

                return tasks.GeneralRiskAssessmentTasksOverdue.Count
                       + tasks.PersonalRiskAssessmentTasksOverdue.Count
                       + tasks.FireRiskAssessmentTasksOverdue.Count
                       + tasks.HazadousSubstanceRiskAssessmentTasksOverdue.Count
                       + tasks.RiskAssessmentReviewTasksOverdue.Count
                       + tasks.ResponsibilityTaskOverdue.Count
                       + tasks.ActionTasksOverdue.Count
                       + tasks.HazardousSubstanceTasksCompleted.Count
                       + tasks.FireRiskAssessmentTasksCompleted.Count
                       + tasks.PersonalRiskAssessmentTasksCompleted.Count
                       + tasks.GeneralRiskAssessmentTasksCompleted.Count
                       + tasks.RiskAssessmentReviewTasksCompleted.Count
                       + tasks.GeneralRiskAssessmentTasksDueTomorrow.Count
                       + tasks.PersonalRiskAssessmentTasksDueTomorrow.Count
                       + tasks.FireRiskAssessmentTasksDueTomorrow.Count
                       + tasks.HazardousSubstanceTasksDueTomorrow.Count
                       + tasks.RiskAssessmentReviewTasksDueTomorrow.Count
                       + tasks.ResponsibilityTasksDueTomorrow.Count
                       + tasks.ActionTasksDueTomorrow.Count;
            }
            catch (Exception ex)
            {
                Log.Debug("Exception  - TotalTaskCount() " + ex.Message);
                throw;
            }
        }
 public EmployeeTasks GetEmployeeTasks(ISession session, Guid employeeId)
 {
     try
     {
         //Log.Debug("GetEmployeeTasks()");
         
         var employeeNotifications = new EmployeeTasks();
         
         ////Completed Tasks
         employeeNotifications.RiskAssessmentReviewTasksCompleted = _completedRAReviewTasksQuery.Execute(employeeId, session);
         employeeNotifications.GeneralRiskAssessmentTasksCompleted = _completedGRATasksQuery.Execute(employeeId, session);
         employeeNotifications.PersonalRiskAssessmentTasksCompleted = _completedPRATasksQuery.Execute(employeeId, session);
         employeeNotifications.FireRiskAssessmentTasksCompleted = _completedFRATasksQuery.Execute(employeeId, session);
         employeeNotifications.HazardousSubstanceTasksCompleted = _completedHSRATasksQuery.Execute(employeeId, session);
       
         ////DueTomorrow Tasks
         employeeNotifications.GeneralRiskAssessmentTasksDueTomorrow = _dueGraTasksQuery.Execute(employeeId, session);
         employeeNotifications.PersonalRiskAssessmentTasksDueTomorrow = _duePraTasksQuery.Execute(employeeId, session);
         employeeNotifications.FireRiskAssessmentTasksDueTomorrow = _dueFraTasksQuery.Execute(employeeId, session);
         employeeNotifications.HazardousSubstanceTasksDueTomorrow = _dueTomorrowHSRATasksQuery.Execute(employeeId, session);
         employeeNotifications.RiskAssessmentReviewTasksDueTomorrow = _dueRiskAssessmentReviewTasksForEmployeeQuery.Execute(employeeId, session);
         employeeNotifications.ResponsibilityTasksDueTomorrow = _dueResponsibilityTasksForEmployeeQuery.Execute(employeeId, session);
         employeeNotifications.ActionTasksDueTomorrow = _dueActionTasksForEmployee.Execute(employeeId, session);
       
         ////Overdue Tasks
         employeeNotifications.GeneralRiskAssessmentTasksOverdue = _overdueGRATasksQuery.Execute(employeeId, session);
         employeeNotifications.PersonalRiskAssessmentTasksOverdue = _overduePRATasksQuery.Execute(employeeId, session);
         employeeNotifications.FireRiskAssessmentTasksOverdue = _overdueFRATasksQuery.Execute(employeeId, session);
         employeeNotifications.HazadousSubstanceRiskAssessmentTasksOverdue = _overdueHSRATasksQuery.Execute(employeeId, session);
         employeeNotifications.RiskAssessmentReviewTasksOverdue = _overdueRAReviewTasksQuery.Execute(employeeId, session);
         employeeNotifications.ResponsibilityTaskOverdue = _overdueResponsibilitiesTasksForEmployee.Execute(employeeId, session);
         employeeNotifications.ActionTasksOverdue = _overdueActionTasksForEmployeeQuery.Execute(employeeId, session);
       
         return employeeNotifications;
     }
     catch(Exception ex)
     {
         Log.Debug("Exception  - TotalTaskCount() " + ex.Message);
         throw;
     }
 }
        public void UpdateEmployeeNotificationEmailSent(Employee employee, EmployeeTasks employeeNotification)
        {
            try
            {
                UserForAuditing user = null;

                try
                {
                    if (employee.User != null)
                    {
                        user = _userForAuditingRepository.GetByIdAndCompanyId(employee.User.Id, employee.CompanyId);
                    }
                }
                catch (UserNotFoundException ex) 
                {
                    Log.Debug(String.Format("*** User Not Found. Using System User for Auditing *** - UserId: {0} CompanyId: {1} Employee - ID: {2} FullName: {3}", 
                                employee.User.Id, employee.CompanyId, employee.Id, employee.FullName ));               
                }

                if (user == null)
                {
                    user = _userForAuditingRepository.GetSystemUser();
                }

                if (employeeNotification.AnyCompletedTasks(employee))
                {

                    employeeNotification.GeneralRiskAssessmentTasksCompleted
                       .Where(task => EmployeeNotificationsHelper.CanSendTaskCompletedNotification(task, employee))
                       .ForEach(task =>
                       {
                           EmployeeNotificationsHelper.AddEmployeeTaskCompletedNotification(task, employee, user);
                           _tasksRepository.Save(task);
                       });

                    employeeNotification.PersonalRiskAssessmentTasksCompleted
                       .Where(task => EmployeeNotificationsHelper.CanSendTaskCompletedNotification(task, employee))
                       .ForEach(task =>
                       {
                           EmployeeNotificationsHelper.AddEmployeeTaskCompletedNotification(task, employee, user);
                           _tasksRepository.Save(task);
                       });

                    employeeNotification.FireRiskAssessmentTasksCompleted
                        .Where(task => EmployeeNotificationsHelper.CanSendTaskCompletedNotification(task, employee))
                        .ForEach(task =>
                        {
                            EmployeeNotificationsHelper.AddEmployeeTaskCompletedNotification(task, employee, user);
                            _tasksRepository.Save(task);
                        });

                    employeeNotification.HazardousSubstanceTasksCompleted
                        .Where(task => EmployeeNotificationsHelper.CanSendTaskCompletedNotification(task, employee))
                        .ForEach(task =>
                        {
                            EmployeeNotificationsHelper.AddEmployeeTaskCompletedNotification(task, employee, user);
                            _tasksRepository.Save(task);
                        });

                
                    _tasksRepository.Flush();
            }
                  }
            catch (Exception ex)
            {
                Log.Debug("Exception  - UpdateEmployeeNotificationEmailSent() " + ex.Message);
                throw;
            }
        }
        public static SendEmployeeDigestEmail CreateSendEmployeeDigestEmailCommand(Employee employee, EmployeeTasks employeeTasks)
        {
            var employeeDigestEmail = new SendEmployeeDigestEmail()
            {
                RecipientEmail = employee.GetEmail()

                //overdue tasks
                ,GeneralRiskAssessmentsOverdueTasks = employeeTasks.GeneralRiskAssessmentTasksOverdue
                    .Where(task => CanSendTaskOverdueNotification(task,employee))
                    .Select(MapToTaskDetails).ToList()

                ,PersonalRiskAssessmentTasksOverdue = employeeTasks.PersonalRiskAssessmentTasksOverdue
                    .Where(task => CanSendTaskOverdueNotification(task, employee))
                    .Select(MapToTaskDetails).ToList()

                ,FireRiskAssessmentsOverdueTasks = employeeTasks.FireRiskAssessmentTasksOverdue
                     .Where(task => CanSendTaskOverdueNotification(task, employee))
                    .Select(MapToTaskDetails).ToList()

                ,HazardousSubstanceRiskAssessmentTasksOverdue = employeeTasks.HazadousSubstanceRiskAssessmentTasksOverdue
                    .Where(task => CanSendTaskOverdueNotification(task, employee))
                    .Select(MapToTaskDetails).ToList()

                ,RiskAssessmentReviewTasksOverdue = employeeTasks.RiskAssessmentReviewTasksOverdue
                    .Where(task => CanSendReviewTaskOverdueNotification(task, employee))
                    .Select(MapToTaskDetails).ToList()

                , ResponsibilitiesTasksOverdue = employeeTasks.ResponsibilityTaskOverdue
                    .Where(task => CanSendResponsibilityTaskOverdueNotification(task, employee))
                    .Select(MapToTaskDetails).ToList()

                ,ActionTasksOverdue = employeeTasks.ActionTasksOverdue
                    .Where( task => CanSendTaskOverdueNotification(task, employee))
                    .Select(MapToTaskDetails)
                    .ToList()

                //Completed tasks
                ,HazardousSubstanceTasksCompleted = employeeTasks.HazardousSubstanceTasksCompleted
                    .Where(task => CanSendTaskCompletedNotification(task, employee))
                    .Select(MapToTaskDetails)
                    .ToList()

                , FireRiskAssessmentTasksCompleted = employeeTasks.FireRiskAssessmentTasksCompleted
                    .Where(task => CanSendTaskOverdueNotification(task, employee))
                    .Select(MapToTaskDetails).ToList()

               , GeneralRiskAssessmentTasksCompleted = employeeTasks.GeneralRiskAssessmentTasksCompleted
                    .Where(task => CanSendTaskCompletedNotification(task, employee))
                    .Select(MapToTaskDetails)
                    .ToList()

                ,PersonalRiskAssessmentTasksCompleted = employeeTasks.PersonalRiskAssessmentTasksCompleted
                    .Where(task => CanSendTaskCompletedNotification(task, employee))
                    .Select(MapToTaskDetails)
                    .ToList()

                ,RiskAssessmentReviewTasksCompleted = employeeTasks.RiskAssessmentReviewTasksCompleted
                    .Where(task => CanSendTaskCompletedNotification(task, employee))
                    .Select(MapToTaskDetails)
                    .ToList()

                //Due Tomorrow tasks
                ,GeneralRiskAssessmentsTasksDueTomorrow = employeeTasks.GeneralRiskAssessmentTasksDueTomorrow
                    .Where(task => CanSendTaskDueTomorrowNotification(task, employee))
                    .Select(MapToTaskDetails).ToList()

                 ,PersonalRiskAssessmentTasksDueTomorrow = employeeTasks.PersonalRiskAssessmentTasksDueTomorrow
                    .Where(task => CanSendTaskDueTomorrowNotification(task, employee))
                    .Select(MapToTaskDetails).ToList()

                  ,FireRiskAssessmentsTasksDueTomorrow = employeeTasks.FireRiskAssessmentTasksDueTomorrow
                    .Where(task => CanSendTaskDueTomorrowNotification(task, employee))
                    .Select(MapToTaskDetails).ToList()

                  ,HazardousSubstanceRiskAssessmentTasksDueTomorrow = employeeTasks.HazardousSubstanceTasksDueTomorrow
                    .Where(task => CanSendTaskDueTomorrowNotification(task, employee))
                    .Select(MapToTaskDetails).ToList()

                 ,RiskAssessmentReviewTasksDueTomorrow = employeeTasks.RiskAssessmentReviewTasksDueTomorrow
                    .Where(task => CanSendReviewTaskDueTomorrowNotification(task, employee))
                    .Select(MapToTaskDetails).ToList()

                 ,ResponsibilitiesTasksDueTomorrow = employeeTasks.ResponsibilityTasksDueTomorrow
                    .Where(task => CanSendTaskDueTomorrowNotification(task, employee))
                    .Select(MapToTaskDetails).ToList()

                  ,ActionTasksDueTomorrow = employeeTasks.ActionTasksDueTomorrow
                        .Where(task => CanSendActionTaskDueTomorrowNotification(task, employee))
                        .Select(MapToTaskDetails).ToList()
            };

            return employeeDigestEmail;
        }