/// <summary> /// Sends e-mails to users with access to any analysis that ended 24 days prior to the current date and will be deleted after 7 days. /// </summary> /// <param name="numberOfDays">The number of days for which an analysis is stored in the database.</param> /// <param name="numberOfDaysLeft">The number of days until the deletion will take place.</param> /// <returns></returns> private async Task AlertDelete(string scheme, HostString host, int numberOfDays = 31, int numberOfDaysLeft = 7) { // Get the limit date. var limitDate = DateTime.Today - TimeSpan.FromDays(numberOfDays - numberOfDaysLeft); // Get the networks and analyses. var networks = _context.Networks .Where(item => item.DateTimeCreated < limitDate); var analyses = _context.Analyses .Where(item => item.Status == AnalysisStatus.Stopped || item.Status == AnalysisStatus.Completed || item.Status == AnalysisStatus.Error) .Where(item => item.DateTimeEnded < limitDate) .Concat(networks .Select(item => item.AnalysisNetworks) .SelectMany(item => item) .Select(item => item.Analysis)) .Distinct(); // Get the users. var networkUsers = networks .Select(item => item.NetworkUsers) .SelectMany(item => item) .Select(item => item.User); var analysisUsers = analyses .Select(item => item.AnalysisUsers) .SelectMany(item => item) .Select(item => item.User); // Get the users that have access to the items. var users = networkUsers .Concat(analysisUsers); // Go over each of the users. foreach (var user in users) { // Send an alert delete analyses e-mail. await _emailSender.SendAlertDeleteEmailAsync(new EmailAlertDeleteViewModel { Email = user.Email, DateTime = DateTime.Today + TimeSpan.FromDays(numberOfDaysLeft), NetworkItems = user.NetworkUsers .Select(item => item.Network) .Where(item => networks.Contains(item)) .Select(item => new EmailAlertDeleteViewModel.ItemModel { Id = item.Id, Name = item.Name, Url = _linkGenerator.GetUriByPage("/Content/Created/Networks/Details/Index", handler: null, values: new { id = item.Id }, scheme: scheme, host: host) }), AnalysisItems = user.AnalysisUsers .Select(item => item.Analysis) .Where(item => analyses.Contains(item)) .Select(item => new EmailAlertDeleteViewModel.ItemModel { Id = item.Id, Name = item.Name, Url = _linkGenerator.GetUriByPage("/Content/Created/Analyses/Details/Index", handler: null, values: new { id = item.Id }, scheme: scheme, host: host) }), ApplicationUrl = _linkGenerator.GetUriByPage("/Index", handler: null, values: null, scheme: scheme, host: host) }); } }