using LCU.Library.Internal.IncentiveApiModels.Entities; using LCU.Library.Internal.IncentiveApiModels.Enums; using LCU.API.Internal.Operations.IncentiveApi.Services.Rules; using LCU.API.Internal.Operations.IncentiveApi.Models; using LCU.API.Internal.Operations.IncentiveApiLib.Repositories; using LCU.Library.Internal.IncentiveApiModels.Common; namespace LCU.API.Internal.Operations.IncentiveApi.Services; public class DashboardService : IDashboardService { private readonly IDepartmentService _departmentService; private readonly IActivityRepository _activityRepository; private readonly IGoalRepository _goalRepository; private readonly IBusinessRuleService _businessRuleService; public DashboardService(IDepartmentService departmentService, IActivityRepository activityRepository, IGoalRepository goalRepository, IBusinessRuleService businessRuleService) { _departmentService = departmentService; _activityRepository = activityRepository; _goalRepository = goalRepository; _businessRuleService = businessRuleService; } public async Task GetRegionProgressForMonthRangeAsync( Region region, MonthRange monthRange) { var departments = await _departmentService.GetDepartmentsByRegionAsync(region); var regionProgress = new RegionProgress(region, monthRange); foreach (var dept in departments.Where(x => x.State == DepartmentState.Active)) { var progress = await GetDepartmentProgressForMonthRangeAsync(dept.Id.Value, monthRange); regionProgress.Departments.Add(progress); } return regionProgress; } public async Task GetBusinessLineProgressForMonthRangeAsync( BusinessLine businessLine, MonthRange monthRange) { var departments = await _departmentService.GetDepartmentsByBusinessLineAsync(businessLine); var businessLineProgress = new BusinessLineProgress(businessLine, monthRange); foreach (var dept in departments.Where(x => x.State == DepartmentState.Active)) { var progress = await GetDepartmentProgressForMonthRangeAsync(dept.Id.Value, monthRange); businessLineProgress.Departments.Add(progress); } return businessLineProgress; } public async Task GetEmployeeProgressForMonthRangeAsync(AppUser employee, MonthRange monthRange) { var users = new List() { employee }; var progress = new EmployeeProgress(employee, monthRange, new List()); foreach (var dept in employee.Departments) { var employeeGoals = await _goalRepository.FindPersonnelGoalsByMonthRange(dept.DepartmentId, users, monthRange); progress.ApplyGoals(employeeGoals); } progress.ApplyAll(await _activityRepository.FindEmployeeActivityByMonthRange(employee, monthRange)); return progress; } public async Task> GetDepartmentProgressForMonthRangeAsync( List userDepartments, MonthRange monthRange) { var deptProgress = new List(); foreach (var dept in userDepartments) { var perDeptProgress = await GetDepartmentProgressForMonthRangeAsync(dept.DepartmentId, monthRange); if (perDeptProgress == null) continue; deptProgress.Add(perDeptProgress); } return deptProgress; } public async Task GetDepartmentProgressForMonthRangeAsync(int departmentId, MonthRange monthRange) { var employees = await _departmentService.FindDepartmentAssignmentsForRange(departmentId, monthRange); var departmentActivity = await _activityRepository.FindDepartmentActivityByMonthRange( departmentId, monthRange); var departmentGoals = await _goalRepository.FindDepartmentGoalsByMonthRange(departmentId, monthRange); var progress = new DepartmentProgress(departmentId, monthRange, employees.Select(x => x.Employee.EmployeeId).Distinct().ToList(), _businessRuleService.GetRules(PayoutType.Department), _businessRuleService.GetRules(PayoutType.Personal)); progress.ApplyActivity(departmentActivity); progress.ApplyGoals(departmentGoals); var employeeGoals = await _goalRepository.FindPersonnelGoalsByMonthRange(departmentId, progress.EmployeeList.ToList(), monthRange); progress.ApplyGoals(employeeGoals); return progress; } }