/// <summary> /// Executes the specified context. /// </summary> /// <param name="context">The context.</param> public virtual void Execute(IJobExecutionContext context) { var rockContext = new RockContext(); var dataMap = context.JobDetail.JobDataMap; var groupType = GroupTypeCache.Get(dataMap.GetString(AttributeKey.GroupType).AsGuid()); var isGroupTypeValid = groupType.TakesAttendance && groupType.SendAttendanceReminder; var results = new StringBuilder(); context.Result = "0 attendance reminders sent."; if (!isGroupTypeValid) { var warning = $"Group Type {groupType.Name} isn't setup to take attendance or send attendance reminders."; results.Append(FormatWarningMessage(warning)); RockLogger.Log.Warning(RockLogDomains.Jobs, warning); context.Result = results.ToString(); throw new RockJobWarningException(warning); } var systemEmailGuid = dataMap.GetString(AttributeKey.SystemEmail).AsGuid(); var systemCommunication = new SystemCommunicationService(rockContext).Get(systemEmailGuid); var jobPreferredCommunicationType = ( CommunicationType )dataMap.GetString(AttributeKey.SendUsingConfiguration).AsInteger(); var isSmsEnabled = MediumContainer.HasActiveSmsTransport() && !string.IsNullOrWhiteSpace(systemCommunication.SMSMessage); if (jobPreferredCommunicationType == CommunicationType.SMS && !isSmsEnabled) { // If sms selected but not usable default to email. var errorMessage = $"The job is setup to send via SMS but either SMS isn't enabled or no SMS message was found in system communication {systemCommunication.Title}."; HandleErrorMessage(context, errorMessage); } if (jobPreferredCommunicationType != CommunicationType.Email && string.IsNullOrWhiteSpace(systemCommunication.SMSMessage)) { var warning = $"No SMS message found in system communication {systemCommunication.Title}. All attendance reminders were sent via email."; results.Append(FormatWarningMessage(warning)); RockLogger.Log.Warning(RockLogDomains.Jobs, warning); jobPreferredCommunicationType = CommunicationType.Email; } var occurrences = GetOccurenceDates(groupType, dataMap, rockContext); var groupIds = occurrences.Where(o => o.Value.Any()).Select(o => o.Key).ToList(); var leaders = GetGroupLeaders(groupIds, rockContext); var attendanceRemindersResults = SendAttendanceReminders(leaders, occurrences, systemCommunication, jobPreferredCommunicationType, isSmsEnabled); results.AppendLine($"{attendanceRemindersResults.MessagesSent} attendance reminders sent."); results.Append(FormatWarningMessages(attendanceRemindersResults.Warnings)); context.Result = results.ToString(); HandleErrorMessages(context, attendanceRemindersResults.Errors); }
/// <summary> /// Executes the specified context. /// </summary> /// <param name="context">The context.</param> public virtual void Execute(IJobExecutionContext context) { var rockContext = new RockContext(); var dataMap = context.JobDetail.JobDataMap; resGroupAttribute = AttributeCache.Get(dataMap.GetString(AttributeKey.GroupAttributeSetting).AsGuid()); reservationLocationEntityTypeId = new EntityTypeService(rockContext).GetNoTracking(com.bemaservices.RoomManagement.SystemGuid.EntityType.RESERVATION_LOCATION.AsGuid()).Id; context.Result = "0 meeting reminders sent."; if (resGroupAttribute == null) { var errorMessages = new List <string> { $"The Reservation Group Attribute job setting is invalid. Please check your Room Registration and KFS Zoom Room plugin configuration." }; HandleErrorMessages(context, errorMessages); } var systemCommunicationGuid = dataMap.GetString(AttributeKey.SystemCommunication).AsGuid(); var systemCommunication = new SystemCommunicationService(rockContext).Get(systemCommunicationGuid); var jobPreferredCommunicationType = ( CommunicationType )dataMap.GetString(AttributeKey.SendUsing).AsInteger(); var isSmsEnabled = MediumContainer.HasActiveSmsTransport() && !string.IsNullOrWhiteSpace(systemCommunication.SMSMessage); var isPushEnabled = MediumContainer.HasActivePushTransport() && !string.IsNullOrWhiteSpace(systemCommunication.PushData); if (jobPreferredCommunicationType == CommunicationType.SMS && !isSmsEnabled) { // If sms selected but not usable default to email. var errorMessages = new List <string> { $"The job is setup to send via SMS but either SMS isn't enabled or no SMS message was found in system communication {systemCommunication.Title}." }; HandleErrorMessages(context, errorMessages); } if (jobPreferredCommunicationType == CommunicationType.PushNotification && !isPushEnabled) { // If push notification selected but not usable default to email. var errorMessages = new List <string> { $"The job is setup to send via Push Notification but either Push Notifications are not enabled or no Push message was found in system communication {systemCommunication.Title}." }; HandleErrorMessages(context, errorMessages); } var results = new StringBuilder(); if (jobPreferredCommunicationType == CommunicationType.SMS && string.IsNullOrWhiteSpace(systemCommunication.SMSMessage)) { var warning = $"No SMS message found in system communication {systemCommunication.Title}. All Zoom meeting reminders will be attempted via email."; results.AppendLine(warning); RockLogger.Log.Warning(RockLogDomains.Jobs, warning); jobPreferredCommunicationType = CommunicationType.Email; } if (jobPreferredCommunicationType == CommunicationType.PushNotification && string.IsNullOrWhiteSpace(systemCommunication.PushMessage)) { var warning = $"No Push message found in system communication {systemCommunication.Title}. All Zoom meeting reminders will be attempted via email."; results.AppendLine(warning); RockLogger.Log.Warning(RockLogDomains.Jobs, warning); jobPreferredCommunicationType = CommunicationType.Email; } // Get upcoming Zoom Room occurrences var roomOccurrenceInfo = GetOccurenceAndGroupData(dataMap, rockContext); // Process reminders var meetingRemindersResults = SendMeetingReminders(context, rockContext, roomOccurrenceInfo, systemCommunication, jobPreferredCommunicationType, isSmsEnabled, isPushEnabled); results.AppendLine($"{notificationEmails + notificationSms + notificationPush } meeting reminders sent."); results.AppendLine(string.Format("- {0} email(s)\n- {1} SMS message(s)\n- {2} push notification(s)", notificationEmails, notificationSms, notificationPush)); results.Append(FormatWarningMessages(meetingRemindersResults.Warnings)); context.Result = results.ToString(); HandleErrorMessages(context, meetingRemindersResults.Errors); }