Example #1
0
        public async Task SendNotificationAsync(ChatLogItemModel model)
        {
            var staff = await _staffRepository.GetStaffIdentityByUniqueId(model.SenderUniqueId);

            if (staff != null)
            {
                string titleTemplate = "New Message from";
                string bodyTemplate  = "About";

                if (!string.IsNullOrEmpty(model.TranslatedLanguageCode) && model.TranslatedLanguageCode != "en")
                {
                    titleTemplate = await _translationProvider.TranslateAsync(new TranslateRequest {
                        FromLangCode    = "en",
                        ToLangCode      = model.TranslatedLanguageCode,
                        TextToTranslate = titleTemplate
                    });

                    bodyTemplate = await _translationProvider.TranslateAsync(new TranslateRequest
                    {
                        FromLangCode    = "en",
                        ToLangCode      = model.TranslatedLanguageCode,
                        TextToTranslate = bodyTemplate
                    });
                }

                var student = await _studentRepository.GetStudentBriefModelAsyncByUniqueId(model.StudentUniqueId);

                var messageBody  = model.TranslatedMessage != null ? model.TranslatedMessage : model.EnglishMessage;
                var notification = new NotificationItemModel
                {
                    personUniqueId = model.RecipientUniqueId,
                    personType     = "Parent",
                    notification   = new Notification
                    {
                        title = $"{titleTemplate}: {staff.FirstName} {staff.LastSurname}",
                        body  = $"{bodyTemplate}: {student.FirstName} - {messageBody}",
                        icon  = "ic_yes_prep"
                    },
                    data = new NotificationData
                    {
                        studentUsi         = model.StudentUniqueId,
                        personTypeId       = model.RecipientTypeId.ToString(),
                        uniqueId           = model.RecipientUniqueId,
                        unreadMessageCount = 1
                    }
                };
                await _pushNotificationProvider.SendNotificationAsync(notification);
            }
        }
        public async Task SendMessage(MessageAbstractionModel model)
        {
            //We remove the html properties
            model.BodyMessage = StripHTML(model.BodyMessage);

            await _pushNotificationProvider.SendNotificationAsync(new NotificationItemModel
            {
                personUniqueId = model.RecipientUniqueId,
                personType     = "Parent",
                notification   = new Notification
                {
                    title = model.Subject,
                    body  = model.BodyMessage
                }
            });
        }
Example #3
0
        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);
        }
Example #4
0
        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);
        }
        private async Task SendMessageToPreferredMethodOfContact(StudentParentAssociationModel parent, string message, string subject, string staffName)
        {
            string to = string.Empty;

            // If SMS is the preferred method of contact
            if (parent.Parent.ParentAlert.PreferredMethodOfContactTypeId.Value == MethodOfContactTypeEnum.SMS.Value && parent.Parent.Telephone != null)
            {
                to = parent.Parent.Telephone + parent.Parent.SMSDomain;
                await _smsProvider.SendMessageAsync(to, subject, message);
            }
            // If Push Notification is the preferred method of contact
            if (parent.Parent.ParentAlert.PreferredMethodOfContactTypeId.Value == MethodOfContactTypeEnum.Notification.Value)
            {
                await _pushNotificationProvider.SendNotificationAsync(new NotificationItemModel
                {
                    personUniqueId = parent.Parent.ParentUniqueId,
                    personType     = "Parent",
                    notification   = new Notification
                    {
                        title = subject,
                        body  = message
                    }
                });
            }
            //If the parent doesn't have a preferred method of contact configured fall back to sending an email
            else if ((parent.Parent.ParentAlert.PreferredMethodOfContactTypeId.Value == MethodOfContactTypeEnum.Email.Value ||
                      parent.Parent.ParentAlert.PreferredMethodOfContactTypeId == null ||
                      parent.Parent.ParentAlert.PreferredMethodOfContactTypeId == 0) && parent.Parent.Email != null)
            {
                string legendBottom  = "Please do not reply to this message, as this email inbox is not monitored. To contact us, visit <a href=\"https://familyportal.yesprep.org\">https://familyportal.yesprep.org</a>";
                string signByDefault = $"This message was sent from the YES Prep Family Portal on behalf of {staffName}";

                var legentBottomTranslate  = new Hashtable();
                var signByDefaultTranslate = new Hashtable();

                if (string.IsNullOrEmpty(parent.Parent.LanguageCode))
                {
                    parent.Parent.LanguageCode = "en";
                }


                if (!legentBottomTranslate.ContainsKey(parent.Parent.LanguageCode))
                {
                    var legentBottomTrans = await TranslateText(legendBottom, parent.Parent.LanguageCode);

                    legentBottomTranslate.Add(parent.Parent.LanguageCode, legentBottomTrans);
                }

                var translatedLegentBottom = legentBottomTranslate[parent.Parent.LanguageCode].ToString();

                if (!signByDefaultTranslate.ContainsKey(parent.Parent.LanguageCode))
                {
                    var signByDefaultTrans = await TranslateText(signByDefault, parent.Parent.LanguageCode);

                    signByDefaultTranslate.Add(parent.Parent.LanguageCode, signByDefaultTrans);
                }

                var translatedSignByDefault = signByDefaultTranslate[parent.Parent.LanguageCode].ToString();


                message = $"{message} <br/> <br/> {translatedLegentBottom} <br/> {translatedSignByDefault}";
                to      = parent.Parent.Email;
                await _messagingProvider.SendMessageAsync(to, null, null, subject, message);
            }
        }