public async Task <int> SendAlerts() { var customParameters = _customParametersProvider.GetParameters(); var enabledFeauter = customParameters.featureToggle.Select(x => x.features.Where(i => i.enabled && i.studentAbc != null)).FirstOrDefault().FirstOrDefault().studentAbc.missingAssignments; if (!enabledFeauter) { return(0); } var alertTypes = await _typesRepository.GetAlertTypes(); var alertAssignment = alertTypes.Where(x => x.AlertTypeId == AlertTypeEnum.Assignment.Value).FirstOrDefault(); var assignmentThreshold = alertAssignment.Thresholds.Where(x => x.ThresholdTypeId == ThresholdTypeEnum.Assignment.Value).FirstOrDefault(); var currentSchoolYear = await _alertRepository.getCurrentSchoolYear(); // Find students that have surpassed the threshold. var studentsOverThreshold = await _alertRepository.studentsOverThresholdAssignment(assignmentThreshold.ThresholdValue, customParameters.descriptors.gradeBookMissingAssignmentTypeDescriptors, customParameters.descriptors.missingAssignmentLetterGrade); var alertCountSent = 0; // Send alerts to the parents of these students. foreach (var s in studentsOverThreshold) { var imageUrl = await _imageProvider.GetStudentImageUrlForAlertsAsync(s.StudentUniqueId); // For each parent that wants to receive alerts foreach (var p in s.StudentParentAssociations) { var parentAlert = p.Parent.ParentAlert; var wasSentBefore = await _alertRepository.wasSentBefore(p.Parent.ParentUniqueId, s.StudentUniqueId, s.ValueCount.ToString(), currentSchoolYear, AlertTypeEnum.Assignment.Value); if (parentAlert == null || parentAlert.AlertTypeIds == null || !parentAlert.AlertTypeIds.Contains(AlertTypeEnum.Assignment.Value) || wasSentBefore) { continue; } string to; string template; string subjectTemplate = "Family Portal: Missing Assignment Alert"; if (parentAlert.PreferredMethodOfContactTypeId == MethodOfContactTypeEnum.SMS.Value && p.Parent.Telephone != null) { to = p.Parent.Telephone; subjectTemplate = await TranslateText(p.Parent.LanguageCode, subjectTemplate); template = FillSMSTemplate(s); template = await TranslateText(p.Parent.LanguageCode, template); await _smsProvider.SendMessageAsync(to, subjectTemplate, template); alertCountSent++; } else if (parentAlert.PreferredMethodOfContactTypeId == MethodOfContactTypeEnum.Email.Value && p.Parent.Email != null) { to = p.Parent.Email; subjectTemplate = await TranslateText(p.Parent.LanguageCode, subjectTemplate); template = FillEmailTemplate(s, assignmentThreshold, imageUrl); template = await TranslateText(p.Parent.LanguageCode, template); await _messagingProvider.SendMessageAsync(to, null, null, subjectTemplate, template); alertCountSent++; } else if (parentAlert.PreferredMethodOfContactTypeId == MethodOfContactTypeEnum.Notification.Value) { string pushNoSubjectTemplate = $"Missing Assignment Alert: {s.FirstName} {s.LastSurname}"; string pushNoBodyTemplate = $"Has a new missing assignment, for a total of {s.ValueCount}"; pushNoSubjectTemplate = await TranslateText(p.Parent.LanguageCode, pushNoSubjectTemplate); pushNoBodyTemplate = await TranslateText(p.Parent.LanguageCode, pushNoBodyTemplate); await _pushNotificationProvider.SendNotificationAsync(new NotificationItemModel { personUniqueId = p.Parent.ParentUniqueId, personType = "Parent", notification = new Notification { title = pushNoSubjectTemplate, //Grade Alert: {s.FirstName} {s.LastSurname} body = pushNoBodyTemplate //Has one or more grades below 70 } }); alertCountSent++; } // Save in log await _alertRepository.AddAlertLog(currentSchoolYear, AlertTypeEnum.Assignment.Value, p.Parent.ParentUniqueId, s.StudentUniqueId, s.ValueCount.ToString()); } } // Commit all log entries. await _alertRepository.SaveChanges(); return(alertCountSent); }
public async Task <int> SendAlerts() { return(0); // Disabled for now since we are using Ada Absences var customParameters = _customParametersProvider.GetParameters(); var alertTypes = await _typesRepository.GetAlertTypes(); var alertAbsence = alertTypes.Where(x => x.AlertTypeId == AlertTypeEnum.Absence.Value).FirstOrDefault(); var excusedThreshold = alertAbsence.Thresholds.Where(x => x.ThresholdTypeId == ThresholdTypeEnum.ExcusedAbsence.Value).FirstOrDefault(); var tardyThreshold = alertAbsence.Thresholds.Where(x => x.ThresholdTypeId == ThresholdTypeEnum.Tardy.Value).FirstOrDefault(); var unexcusedThreshold = alertAbsence.Thresholds.Where(x => x.ThresholdTypeId == ThresholdTypeEnum.UnexcusedAbsence.Value).FirstOrDefault(); var currentSchoolYear = await _alertRepository.getCurrentSchoolYear(); // Find students that have surpassed the threshold. var studentsOverThreshold = await _alertRepository.studentsOverThresholdAbsence(excusedThreshold.ThresholdValue, unexcusedThreshold.ThresholdValue, tardyThreshold.ThresholdValue, customParameters.descriptors.excusedAbsenceDescriptorCodeValue, customParameters.descriptors.unexcusedAbsenceDescriptorCodeValue, customParameters.descriptors.tardyDescriptorCodeValue); var alertCountSent = 0; // Send alerts to the parents of these students. foreach (var s in studentsOverThreshold) { var imageUrl = await _imageProvider.GetStudentImageUrlForAlertsAsync(s.StudentUniqueId); // For each parent that wants to receive alerts foreach (var p in s.StudentParentAssociations) { var parentAlert = p.Parent.ParentAlert; var wasSentBefore = await _alertRepository.wasSentBefore(p.Parent.ParentUniqueId, s.StudentUniqueId, s.AbsenceCount.ToString(), currentSchoolYear, AlertTypeEnum.Absence.Value); if (parentAlert == null || parentAlert.AlertTypeIds == null || !parentAlert.AlertTypeIds.Contains(AlertTypeEnum.Absence.Value) || wasSentBefore) { continue; } string to; string template; if (parentAlert.PreferredMethodOfContactTypeId == MethodOfContactTypeEnum.SMS.Value && p.Parent.Telephone != null && p.Parent.SMSDomain != null) { to = p.Parent.Telephone + p.Parent.SMSDomain; template = FillSMSTemplate(s); await _smsProvider.SendMessageAsync(to, "Parent Portal: Attendance Alert", template); alertCountSent++; } else if (p.Parent.Email != null) { to = p.Parent.Email; template = FillEmailTemplate(s, excusedThreshold, unexcusedThreshold, tardyThreshold, imageUrl); await _messagingProvider.SendMessageAsync(to, null, null, "Parent Portal: Attendance Alert", template); alertCountSent++; } // Save in log await _alertRepository.AddAlertLog(currentSchoolYear, AlertTypeEnum.Absence.Value, p.Parent.ParentUniqueId, s.StudentUniqueId, s.AbsenceCount.ToString()); } } // Commit all log entries. await _alertRepository.SaveChanges(); return(alertCountSent); }
public async Task <int> SendAlerts() { var timeToSend = DateTime.Today.AddHours(Double.Parse(_applicationSettingsProvider.GetSetting("unread.message.alert.hour"))); var wasSentBefore = await _alertRepository.unreadMessageAlertWasSentBefore(); if (DateTime.UtcNow < timeToSend.ToUniversalTime() || wasSentBefore) { return(0); } var customParameters = _customParametersProvider.GetParameters(); var alertTypes = await _typesRepository.GetAlertTypes(); var alertAssignment = alertTypes.Where(x => x.AlertTypeId == AlertTypeEnum.Message.Value).FirstOrDefault(); var currentSchoolYear = await _alertRepository.getCurrentSchoolYear(); // Find students that have surpassed the threshold. var parentsAndStaffWithUnreadMessages = await _alertRepository.ParentsAndStaffWithUnreadMessages(); var alertCountSent = 0; // Send alerts to the parents of these students. foreach (var p in parentsAndStaffWithUnreadMessages) { if (p.AlertTypeIds == null || !p.AlertTypeIds.Contains(AlertTypeEnum.Message.Value)) { continue; } string to; string template; string subjectTemplate = "Family Portal: Unread Messages Alert"; if (p.PreferredMethodOfContactTypeId == MethodOfContactTypeEnum.SMS.Value && p.Telephone != null) { to = p.Telephone; subjectTemplate = await TranslateText(p.LanguageCode, subjectTemplate); template = FillSMSTemplate(p); template = await TranslateText(p.LanguageCode, template); await _smsProvider.SendMessageAsync(to, subjectTemplate, template); alertCountSent++; } else if (p.PreferredMethodOfContactTypeId == MethodOfContactTypeEnum.Email.Value && p.Email != null) { to = p.Email; subjectTemplate = await TranslateText(p.LanguageCode, subjectTemplate); template = FillEmailTemplate(p); template = await TranslateText(p.LanguageCode, template); await _messagingProvider.SendMessageAsync(to, null, null, subjectTemplate, template); alertCountSent++; } else if (p.PreferredMethodOfContactTypeId == MethodOfContactTypeEnum.Notification.Value) { string pushNoSubjectTemplate = $"Unread Messages Alert"; string pushNoBodyTemplate = $"You have {p.UnreadMessageCount} unread messages"; pushNoSubjectTemplate = await TranslateText(p.LanguageCode, pushNoSubjectTemplate); pushNoBodyTemplate = await TranslateText(p.LanguageCode, pushNoBodyTemplate); await _pushNotificationProvider.SendNotificationAsync(new NotificationItemModel { personUniqueId = p.PersonUniqueId, personType = p.PersonType, notification = new Notification { title = pushNoSubjectTemplate, body = pushNoBodyTemplate } }); alertCountSent++; } } await _alertRepository.AddAlertLog(currentSchoolYear, AlertTypeEnum.Message.Value, "0", "0", alertCountSent.ToString()); // Commit all log entries. await _alertRepository.SaveChanges(); return(alertCountSent); }