public async Task ProcessAsync(CalendarNotificationCommand command, IQuidjiboProgress progress, CancellationToken cancellationToken) { try { progress.Report(1, $"Starting the {Name} Task"); //await Task.Run(async () => //{ var _calendarService = Bootstrapper.GetKernel().Resolve <ICalendarService>(); var logic = new CalendarNotifierLogic(); var calendarItems = await _calendarService.GetCalendarItemsToNotifyAsync(DateTime.UtcNow); if (calendarItems != null) { _logger.LogInformation("CalendarNotification::Calendar Items to Notify: " + calendarItems.Count); foreach (var calendarItem in calendarItems) { var qi = new CalendarNotifierQueueItem(); qi.CalendarItem = calendarItem; _logger.LogInformation("CalendarNotification::Processing Notification for CalendarId:" + qi.CalendarItem.CalendarItemId); var result = await logic.Process(qi); if (result.Item1) { _logger.LogInformation($"CalendarNotification::Processed Calendar Notification {qi.CalendarItem.CalendarItemId} successfully."); } else { _logger.LogInformation($"CalendarNotification::Failed to Processed Calendar Notification {qi.CalendarItem.CalendarItemId} error {result.Item2}"); } } } else { progress.Report(6, "CalendarNotification::No Calendar Items to Notify"); } //}, cancellationToken); progress.Report(100, $"Finishing the {Name} Task"); } catch (Exception ex) { Resgrid.Framework.Logging.LogException(ex); _logger.LogError(ex.ToString()); } }
public async Task ProcessAsync(CalendarNotificationCommand command, IQuidjiboProgress progress, CancellationToken cancellationToken) { progress.Report(1, $"Starting the {Name} Task"); await Task.Factory.StartNew(() => { var _calendarService = Bootstrapper.GetKernel().Resolve <ICalendarService>(); var logic = new CalendarNotifierLogic(); var calendarItems = _calendarService.GetV2CalendarItemsToNotify(DateTime.UtcNow); if (calendarItems != null) { _logger.LogInformation("CalendarNotification::Calendar Items to Notify: " + calendarItems.Count); foreach (var calendarItem in calendarItems) { var qi = new CalendarNotifierQueueItem(); qi.CalendarItem = calendarItem; _logger.LogInformation("CalendarNotification::Processing Notification for CalendarId:" + qi.CalendarItem.CalendarItemId); var result = logic.Process(qi); if (result.Item1) { _logger.LogInformation($"CalendarNotification::Processed Calendar Notification {qi.CalendarItem.CalendarItemId} successfully."); } else { _logger.LogInformation($"CalendarNotification::Failed to Processed Calendar Notification {qi.CalendarItem.CalendarItemId} error {result.Item2}"); } } } else { progress.Report(6, "CalendarNotification::No Calendar Items to Notify"); } }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); progress.Report(100, $"Finishing the {Name} Task"); }
public Tuple <bool, string> Process(CalendarNotifierQueueItem item) { bool success = true; string result = ""; if (item?.CalendarItem?.Attendees != null && item.CalendarItem.Attendees.Any()) { try { var message = String.Empty; var title = String.Empty; var profiles = _userProfileService.GetSelectedUserProfiles(item.CalendarItem.Attendees.Select(x => x.UserId).ToList()); var departmentNumber = _departmentSettingsService.GetTextToCallNumberForDepartment(item.CalendarItem.DepartmentId); var department = _departmentsService.GetDepartmentById(item.CalendarItem.DepartmentId, false); var adjustedDateTime = item.CalendarItem.Start.TimeConverter(department); title = string.Format("Upcoming: {0}", item.CalendarItem.Title); if (String.IsNullOrWhiteSpace(item.CalendarItem.Location)) { message = $"on {adjustedDateTime.ToShortDateString()} - {adjustedDateTime.ToShortTimeString()}"; } else { message = $"on {adjustedDateTime.ToShortDateString()} - {adjustedDateTime.ToShortTimeString()} at {item.CalendarItem.Location}"; } foreach (var person in item.CalendarItem.Attendees) { var profile = profiles.FirstOrDefault(x => x.UserId == person.UserId); _communicationService.SendNotification(person.UserId, item.CalendarItem.DepartmentId, message, departmentNumber, title, profile); } } catch (Exception ex) { success = false; result = ex.ToString(); Logging.LogException(ex); } _calendarService.MarkAsNotified(item.CalendarItem.CalendarItemId); } else if (!String.IsNullOrWhiteSpace(item?.CalendarItem?.Entities)) { var items = item.CalendarItem.Entities.Split(char.Parse(",")); var message = String.Empty; var title = String.Empty; var profiles = _userProfileService.GetAllProfilesForDepartment(item.CalendarItem.DepartmentId); var departmentNumber = _departmentSettingsService.GetTextToCallNumberForDepartment(item.CalendarItem.DepartmentId); var department = _departmentsService.GetDepartmentById(item.CalendarItem.DepartmentId, false); var adjustedDateTime = item.CalendarItem.Start.TimeConverter(department); title = $"Upcoming: {item.CalendarItem.Title}"; if (String.IsNullOrWhiteSpace(item.CalendarItem.Location)) { message = $"on {adjustedDateTime.ToShortDateString()} - {adjustedDateTime.ToShortTimeString()}"; } else { message = $"on {adjustedDateTime.ToShortDateString()} - {adjustedDateTime.ToShortTimeString()} at {item.CalendarItem.Location}"; } if (items.Any(x => x.StartsWith("D:"))) { // Notify the entire department foreach (var profile in profiles) { _communicationService.SendNotification(profile.Key, item.CalendarItem.DepartmentId, message, departmentNumber, title, profile.Value); } } else { var groups = _departmentGroupsService.GetAllGroupsForDepartment(item.CalendarItem.DepartmentId); foreach (var val in items) { int groupId = 0; if (int.TryParse(val.Replace("G:", ""), out groupId)) { var group = groups.FirstOrDefault(x => x.DepartmentGroupId == groupId); if (group != null) { foreach (var member in group.Members) { if (profiles.ContainsKey(member.UserId)) { _communicationService.SendNotification(member.UserId, item.CalendarItem.DepartmentId, message, departmentNumber, title, profiles[member.UserId]); } else { _communicationService.SendNotification(member.UserId, item.CalendarItem.DepartmentId, message, departmentNumber, title, null); } } } } } } _calendarService.MarkAsNotified(item.CalendarItem.CalendarItemId); } return(new Tuple <bool, string>(success, result)); }