public byte[] GetEmployeeFilteredReport(EmployeeFilteredReportListViewModel request, IList <ExcelTableCell> tableHeaders) { var tableNames = tableHeaders.Where(x => x.IsActive).Select(x => x.Name).ToList(); var tableHelper = new ExcelTableHelper(); var headers = tableHelper.GetTeamsByFiltersReportTableHeaders(); var result = new StringBuilder(); result.Append(string.Join(',', tableNames)); result.AppendLine(); var row = 0; foreach (var assignee in request.Assignees) { bool isSettedAssignee = false; foreach (var activity in assignee.ActivityView.Activities) { foreach (var loggedTime in activity.LoggedTimeView.LoggedTimes) { ++row; foreach (var name in tableNames) { if (name == headers[0].Name) { result.Append($"{row}, "); } else if (name == headers[1].Name) { if (isSettedAssignee) { continue; } result.Append($"{assignee.AssigneeName}, "); isSettedAssignee = true; } else if (name == headers[2].Name) { result.Append($"{activity.ProjectName}, "); } else if (name == headers[3].Name) { result.Append($"{activity.ActivityName}, "); } else if (name == headers[4].Name) { result.Append($"{activity.ActivityStatus}, "); } else if (name == headers[5].Name) { result.Append($"{activity.LoggedTimeView.TotalEstimatedTime}, "); } else if (name == headers[6].Name) { result.Append($"{activity.LoggedTimeView.TotalLoggedTime}, "); } } result.AppendLine(); } } } return(Encoding.Unicode.GetBytes(result.ToString())); }
public async Task Handle(TodayLogsNotificationsAdm notification, CancellationToken cancellationToken) { if (DateTime.Now.Hour >= 20 && DateTime.Now.Hour < 23) { try { var service = await _context.ServiceTimeCheckers .FirstOrDefaultAsync(x => x.ServiceName == "DailyLogsAdm", cancellationToken); if (service != null && (!service.ExecutedLastTime.HasValue || (DateTime.Now - service.ExecutedLastTime.Value).Hours >= 22)) { service.ExecutedLastTime = DateTime.Now; _context.ServiceTimeCheckers.Update(service); await _context.SaveChangesAsync(cancellationToken); } else { return; } } catch (Exception exception) { Debug.WriteLine(exception.Message); } #region Recepients var administration = await Task.Run(async() => { var owners = await _userManager.GetUsersInRoleAsync("PM Owner"); var admins = await _userManager.GetUsersInRoleAsync("PM Admin"); var activeOwners = owners?.Where(x => x.Active).ToList() ?? new List <ApplicationUser>(); var activeAdmins = admins?.Where(x => x.Active).ToList() ?? new List <ApplicationUser>(); return(activeOwners.Union(activeAdmins)); }, cancellationToken); #endregion var teamsReport = await _mediator.Send(new GetReportDetailsByNameQuery() { Name = "Teams by filters" }, cancellationToken); var projectsReport = await _mediator.Send(new GetReportDetailsByNameQuery() { Name = "Projects by filters" }, cancellationToken); var now = DateTime.Now; var today = $"{now.Year}-{now.Month}-{now.Day}"; var yesterday = now.AddDays(-1); var tableHelper = new ExcelTableHelper(); var excelReportHelper = new ExcelReportHelper(); foreach (var administrator in administration) { var userTeamsReport = await _mediator.Send(new GetUserReportDetailsQuery { UserId = administrator.Id, ReportId = teamsReport.Id }, cancellationToken); var teamsHtmlContent = new StringBuilder(); #region Team report byte[] teamsAttachment = null; if (userTeamsReport.Active) { var employeeModelForTeams = await _mediator.Send(new GetUserReportGuidListQuery { UserId = administrator.Id, ReportId = teamsReport.Id, FilterType = FilterType.EmployeeIds }, cancellationToken); var projectModelForTeams = await _mediator.Send(new GetUserReportGuidListQuery { UserId = administrator.Id, ReportId = teamsReport.Id, FilterType = FilterType.ProjectIds }, cancellationToken); var activityTypeForTeams = await _mediator.Send(new GetUserReportGuidListQuery { UserId = administrator.Id, ReportId = teamsReport.Id, FilterType = FilterType.ActivityTypeIds }, cancellationToken); var activityStatusesForTeams = await _mediator.Send(new GetUserReportActivityStatusesQuery { UserId = administrator.Id, ReportId = teamsReport.Id }, cancellationToken); var teamsRequest = await _mediator.Send(new GetTeamsByFiltersReportQuery { ProjectIds = projectModelForTeams.GuidList, ActivityStatuses = activityStatusesForTeams.ActivityStatuses, ActivityTypeIds = activityTypeForTeams.GuidList, EmployeeIds = employeeModelForTeams.GuidList, StartDate = new DateTime(yesterday.Year, yesterday.Month, yesterday.Day, 23, 59, 59), DueDate = now }, cancellationToken); var tableHeaders = tableHelper.GetTeamsByFiltersReportTableHeaders(); teamsAttachment = excelReportHelper.GetTeamsByFiltersReport(teamsRequest, tableHeaders); teamsHtmlContent = GetTeamsHtmlContent(teamsRequest); } #endregion var projectsHtmlContent = new StringBuilder(); #region Projects report var userProjectsReport = await _mediator.Send(new GetUserReportDetailsQuery { UserId = administrator.Id, ReportId = projectsReport.Id }, cancellationToken); byte[] projectsAttachment = null; if (userProjectsReport.Active) { var projectModelForProjects = await _mediator.Send(new GetUserReportGuidListQuery { UserId = administrator.Id, ReportId = projectsReport.Id, FilterType = FilterType.ProjectIds }, cancellationToken); var activityTypeForProjects = await _mediator.Send(new GetUserReportGuidListQuery { UserId = administrator.Id, ReportId = projectsReport.Id, FilterType = FilterType.ActivityTypeIds }, cancellationToken); var activityStatusesForProjects = await _mediator.Send( new GetUserReportActivityStatusesQuery { UserId = administrator.Id, ReportId = projectsReport.Id }, cancellationToken); var projectsRequest = await _mediator.Send(new GetProjectsByFiltersReportQuery { ProjectIds = projectModelForProjects.GuidList, ActivityStatuses = activityStatusesForProjects.ActivityStatuses, ActivityTypes = activityTypeForProjects.GuidList, StartDate = new DateTime(yesterday.Year, yesterday.Month, yesterday.Day, 23, 59, 59), DueDate = now }, cancellationToken); var tableHeaders = tableHelper.GetProjectsByFiltersReportTableHeaders(); projectsAttachment = excelReportHelper.GetProjectsByFiltersReport(projectsRequest, tableHeaders); projectsHtmlContent = GetProjectsHtmlContent(projectsRequest); } #endregion var attachments = new List <Attachment>(); if (teamsAttachment != null) { attachments.Add(new Attachment { Content = teamsAttachment, ContentId = Guid.NewGuid().ToString(), Disposition = "attachment", Filename = $"TodayLogs-Employees-YMD-{now.Year}-{now.Month}-{now.Day}.xlsx", Type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }); } if (projectsAttachment != null) { attachments.Add(new Attachment { Content = projectsAttachment, ContentId = Guid.NewGuid().ToString(), Disposition = "attachment", Filename = $"TodayLogs-Projects-YMD-{now.Year}-{now.Month}-{now.Day}.xlsx", Type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }); } if (!attachments.Any()) { continue; } var htmlMessageContent = $"Daily logs report on today for the Administrator {administrator.Email}\n" + teamsHtmlContent + "\n<hr>\n" + projectsHtmlContent; var message = new Message { Subject = $"Daily Logs Tasks Report for {today}", Body = htmlMessageContent, To = administrator.Email, Attachments = attachments, EventName = "DailyLogsNotifications", SentTime = now, From = "*****@*****.**" }; try { await _notification.SendAsync(message); } catch (Exception exception) { Debug.WriteLine(exception); } } } }
private StringBuilder GetTeamsHtmlContent(TeamsFilteredReportListViewModel request) { var result = new StringBuilder(); result.AppendLine("<table name=\"teams-report\">"); result.AppendLine("<tr>"); var tableHelper = new ExcelTableHelper(); var tableHeaders = tableHelper.GetTeamsByFiltersReportTableHeaders(); foreach (var item in tableHeaders) { result.AppendLine($"<th style=\"width:{item.Width}px; alignment:{item.Alignment.ToString().ToLower()};\">{item.Name}</th>"); } var rowOrder = 0; result.AppendLine("</tr>"); var previousAssigneeName = string.Empty; foreach (var assignee in request.Assignees) { var previousProjectName = string.Empty; var previousActivityName = string.Empty; foreach (var activity in assignee.ActivityView.Activities) { var loggedTime = activity.LoggedTimeView.LoggedTimes[0]; result.AppendLine("<tr>"); result.AppendLine($"<td>{++rowOrder}</td>"); var assigneeNameReference = EliminatedDuplicate(assignee.AssigneeName, previousAssigneeName) != string.Empty ? $"<a href=\"{IndrivoAddress}/ApplicationUsers/Details/{assignee.AssigneeId}\">" + $"{EliminatedDuplicate(assignee.AssigneeName, previousAssigneeName)}</a>" : $"{EliminatedDuplicate(assignee.AssigneeName, previousAssigneeName)}"; result.AppendLine( $"<td>{assigneeNameReference}</td>"); var projectNameReference = EliminatedDuplicate(loggedTime.ProjectName, previousProjectName) != string.Empty ? $"<a href=\"{IndrivoAddress}/Activities/{activity.ProjectId}\">" + $"{EliminatedDuplicate(loggedTime.ProjectName, previousProjectName)}</a>" : $"{EliminatedDuplicate(loggedTime.ProjectName, previousProjectName)}"; result.AppendLine( $"<td>" + $"{projectNameReference}</td>"); var activityNameReference = EliminatedDuplicate(activity.ActivityName, previousActivityName) != string.Empty ? $"<a href=\"{IndrivoAddress}/Activities/Details/{activity.ActivityId}\">" + $"{EliminatedDuplicate(activity.ActivityName, previousActivityName)}</a>" : $"{EliminatedDuplicate(activity.ActivityName, previousActivityName)}"; result.AppendLine( $"<td>" + $"{activityNameReference}</td>"); result.AppendLine($"<td>{loggedTime.ActivityStatus}</td>"); result.AppendLine($"<td>{activity.LoggedTimeView.TotalEstimatedTime}</td>"); result.AppendLine($"<td>{activity.LoggedTimeView.TotalLoggedTime}</td>"); result.AppendLine("</tr>"); previousAssigneeName = assignee.AssigneeName; previousActivityName = activity.ActivityName; previousProjectName = loggedTime.ProjectName; } } result.AppendLine("</table>"); return(result); }
public async Task Handle(TodayLogsNotificationsPMs notification, CancellationToken cancellationToken) { if (DateTime.Now.Hour >= 20 && DateTime.Now.Hour < 23) { try { var service = await _context.ServiceTimeCheckers .FirstOrDefaultAsync(x => x.ServiceName == "DailyLogsPm", cancellationToken); if (service != null && (!service.ExecutedLastTime.HasValue || (DateTime.Now - service.ExecutedLastTime.Value).Hours >= 22)) { service.ExecutedLastTime = DateTime.Now; _context.ServiceTimeCheckers.Update(service); await _context.SaveChangesAsync(cancellationToken); } else { return; } } catch (Exception exception) { Debug.WriteLine(exception.Message); } var administration = (await Task.Run(async() => { var owners = await _userManager.GetUsersInRoleAsync("PM Owner"); var admins = await _userManager.GetUsersInRoleAsync("PM Admin"); var ownerEmails = owners?.Where(x => x.Active)?.Select(x => x.Email) ?? new List <string>(); var adminEmails = admins?.Where(x => x.Active)?.Select(x => x.Email) ?? new List <string>(); return(ownerEmails.Union(adminEmails)); }, cancellationToken)).ToList(); var teams = await Task.Run(() => { var projectManagers = _context.Projects .Include(x => x.ProjectManager) .Include(x => x.ProjectMembers) .Where(x => x.ProjectManagerId.HasValue && x.ProjectManagerId.Value != Guid.Empty && x.ProjectManager != null && x.ProjectManager.Active && !administration.Contains(x.ProjectManager.Email)); return(projectManagers .OrderBy(x => x.ProjectManager.Email) .GroupBy(x => x.ProjectManager.Email, TeamMembersDto.Create) .ToDictionary(x => x.Key, x => x)); }, cancellationToken); var activityTypeIds = await _context.ActivityTypes.Select(x => x.Id).ToListAsync(cancellationToken); var now = DateTime.Now; var yesterday = now.AddDays(-1); var today = $"{now.Year}-{now.Month}-{now.Day}"; foreach (var manager in teams) { manager.Deconstruct(out var email, out var projectEmployeeIds); var employeeIds = new List <Guid>(); foreach (var employeeGroup in projectEmployeeIds) { employeeIds.AddRange(employeeGroup.EmployeeIds); } var projectIds = projectEmployeeIds.Select(x => x.ProjectId); TeamsFilteredReportListViewModel request; try { request = await _mediator.Send(new GetTeamsByFiltersReportQuery { ProjectIds = projectIds.ToList(), ActivityStatuses = new List <ActivityStatus> { ActivityStatus.Completed, ActivityStatus.Developed, ActivityStatus.InProgress, ActivityStatus.New, ActivityStatus.Refused, ActivityStatus.Tested }, ActivityTypeIds = activityTypeIds, EmployeeIds = employeeIds, StartDate = now.AddYears(-1), //StartDate = new DateTime(yesterday.Year, yesterday.Month, yesterday.Day, 23, 59, 59), DueDate = now }, cancellationToken); } catch (Exception exception) { Debug.WriteLine(exception.Message); continue; } var tableHelper = new ExcelTableHelper(); var tableHeaders = tableHelper.GetTeamsByFiltersReportTableHeaders(); var excelReportHelper = new ExcelReportHelper(); var result = excelReportHelper.GetTeamsByFiltersReport(request, tableHeaders); var message = new Message { Subject = $"Daily Logs Report", Body = $"Daily logs report for the Project Manager {manager.Key}\n" + GetTeamsHtmlContent(request), To = email, Attachments = new List <Attachment> { new Attachment { Content = result, ContentId = Guid.NewGuid().ToString(), Disposition = "attachment", Filename = $"{today}_Employee-TimeLogged.xlsx", Type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" } }, EventName = "DailyLogsNotifications", SentTime = now }; try { await _notification.SendAsync(message); } catch (Exception exception) { Debug.WriteLine(exception.Message); } } } }