public async Task <int> SyncDatabaseAsync(Subplatform subplatform) { InitNonExistingRepo(); // Validation if (subplatform.Settings.FirstOrDefault(ss => ss.SettingName == Setting.Platform.DAYS_TO_KEEP_RECORDS) is null) { throw new Exception("Subplatform has no set period to keep records!"); } if (subplatform.Settings.FirstOrDefault(ss => ss.SettingName == Setting.Platform.SOURCE_API_URL) is null) { throw new Exception("Subplatform has no set API url!"); } // Injects api seed data APICalls restClient = new APICalls { API_URL = subplatform.Settings.FirstOrDefault(ss => ss.SettingName == Setting.Platform.SOURCE_API_URL) .Value }; // Add themes and keywords to dictionary Dictionary <string, string[]> ThemesAndKeywords = new Dictionary <string, string[]>(); ItemRepo.ReadThemes().Where(k => k.SubPlatforms.Contains(subplatform)).ToList().ForEach(t => { ThemesAndKeywords.Add(t.Name, t.Keywords.Select(k => k.Name).ToArray()); }); if (ThemesAndKeywords.Count > 0) { // Call API with request List <JClass> requestedRecords = new List <JClass>(); try { requestedRecords.AddRange(restClient.RequestRecords( since: DateTime.Now.AddDays(-int.Parse(subplatform.Settings .First(s => s.SettingName.Equals(Setting.Platform.DAYS_TO_KEEP_RECORDS)).Value)), themes: ThemesAndKeywords)); } catch (Exception e) { Console.WriteLine(e.GetType().Name + ": " + e.Message); if (e.InnerException != null) { Console.WriteLine("Inner Exception: " + e.InnerException); } throw e; } // Link items to subplatform requestedRecords.ForEach(r => r.Subplatforms.Add(subplatform)); //Convert JClass to Record and persist to database List <Record> newRecords = JClassToRecord(requestedRecords); // Persist items tgo database RecordRepo.CreateRecords(newRecords); } // Save pending changes return(await UowManager.SaveAsync()); }
public async Task <Dictionary <Profile, List <ProfileAlert> > > SendWeeklyReviewsAsync(Subplatform subplatform) { // Get all profiles with at least 1 read profilealert from last week List <Profile> allProfiles = GetProfiles() .Where(p => p.Settings.Find(us => us.SettingName.Equals(Setting.Account.WANTS_WEEKLY_REVIEW_VIA_MAIL)).boolValue&& p.Subscriptions.Count > 0 && p.Email != null && p.ProfileAlerts.FindAll(pa => pa.TimeStamp.Date >= DateTime.Today.AddDays(-7)).Count > 0) .ToList(); // Pick 1 random alert from each day Dictionary <Profile, List <ProfileAlert> > AlertsPerProfile = new Dictionary <Profile, List <ProfileAlert> >(); Random random = new Random(); allProfiles.ForEach(p => { Dictionary <DateTime, List <ProfileAlert> > profileAlertsByDate = p.ProfileAlerts .Where(pa => pa.Alert.Item.SubPlatforms.Contains(subplatform)) .GroupBy(pa => pa.TimeStamp.Date).ToDictionary(kv => kv.Key, kv => kv.ToList()); List <ProfileAlert> profileAlerts = new List <ProfileAlert>(); if (profileAlertsByDate.Count == 0) { return; } profileAlertsByDate.Values.ToList().ForEach(v => { if (v.Count > 0) { profileAlerts.Add(v[random.Next(0, v.Count)]); } }); AlertsPerProfile.Add(p, profileAlerts); }); // Make mail GmailSender sender = new GmailSender { IsHtml = true, Subject = "Weekly Review" }; await Task.Run(() => { AlertsPerProfile.Keys.ToList().ForEach(p => { // Get profileAlerts List <ProfileAlert> profileAlerts = new List <ProfileAlert>(); AlertsPerProfile.TryGetValue(p, out profileAlerts); if (profileAlerts.Count == 0) { return; } // Construct body StringBuilder sbBody = new StringBuilder(GmailSender.WeeklyReviewBody); sbBody.Replace(GmailSender.DefaultUsernameSubstring, p.UserData.FirstName ?? p.UserName); StringBuilder sb = new StringBuilder(); profileAlerts = profileAlerts.OrderByDescending(pa => pa.TimeStamp).ToList(); profileAlerts.ForEach(pa => { StringBuilder sbItem = new StringBuilder(GmailSender.WeeklyReviewListItem); sbItem.Replace(GmailSender.WeeklyReviewListItemIconSubstring, "https://integratieproject.azurewebsites.net" + pa.Alert.Item.IconURL.Substring(1) ?? GmailSender.DefaultItemIcon); sbItem.Replace(GmailSender.DefaultItemLinkSubstring, "#"); //TODO: LINKEN NAAR ITEMDETAIL PAGE sbItem.Replace(GmailSender.WeeklyReviewListItemNameSubstring, pa.Alert.Item.Name); sbItem.Replace(GmailSender.WeeklyReviewListItemDescriptionSubstring, pa.Alert.Item.Name + " " + pa.Alert.Event + " " + pa.Alert.Subject + " - " + pa.TimeStamp.Date.ToShortDateString() + "<br>" + pa.Alert.Text + "." ); sb.Append(sbItem.ToString()); }); List <Person> persons = p.Subscriptions.Where(i => i is Person).Select(i => (Person)i).ToList(); Person person = persons.First(pe => pe.Records.FindAll(r => r.Date.Date >= DateTime.Today.AddDays(-7)).Count == persons.Max(ps => ps.Records.FindAll(r => r.Date.Date >= DateTime.Today.AddDays(-7)).Count)); sbBody.Replace(GmailSender.SubplatformSubstring, subplatform.Name); sbBody.Replace(GmailSender.DefaultItemLinkSubstring, "#"); sbBody.Replace(GmailSender.WeeklyReviewListItemNameSubstring, person.Name); sbBody.Replace(GmailSender.WeeklyReviewListItemDescriptionSubstring, person.Name + " is tijdens de afgelopen week " + person.Records.FindAll(r => r.Date.Date >= DateTime.Today.AddDays(-14)).Count + " aantal keer het onderwerp geweest in iemand zijn/haar tweet."); sbBody.Replace(GmailSender.WeeklyReviewListSubstring, sb.ToString()); // Set mail body sender.Body = sbBody.ToString(); // Set mail recipient sender.ToEmail = p.Email; // Send mail sender.Send(); // Make WeeklyReview and persist if (p.WeeklyReviews == null) { p.WeeklyReviews = new List <WeeklyReview>(); } WeeklyReview weeklyReview = new WeeklyReview { TopPersonId = person.ItemId, Profile = profileAlerts[0].Profile, TimeGenerated = DateTime.Now, TopPersonText = person.Name + " is tijdens de afgelopen week " + person.Records.FindAll(r => r.Date.Date >= DateTime.Today.AddDays(-14)).Count + " aantal keer het onderwerp geweest in iemand zijn/haar tweet.", WeeklyReviewsProfileAlerts = new List <WeeklyReviewProfileAlert>() }; profileAlerts.ForEach(pa => { WeeklyReviewProfileAlert weeklyReviewProfileAlerts = new WeeklyReviewProfileAlert { ProfileAlert = pa, WeeklyReview = weeklyReview }; pa.WeeklyReviewsProfileAlerts.Add(weeklyReviewProfileAlerts); weeklyReview.WeeklyReviewsProfileAlerts.Add(weeklyReviewProfileAlerts); }); p.WeeklyReviews.Add(weeklyReview); }); // Persist changed profiles }); ProfileRepo.UpdateProfiles(allProfiles); await UowManager.SaveAsync(); return(AlertsPerProfile); }