private async Task <ChatLogItemModel> SendGroupMessageHandler(StudentParentAssociationModel model, string personUniqueId, string messageTranslated, string subjectTranslated, string messageEnglish, string subjectEnglish, string studentName, string studentUniqueId, string staffName, string LanguageCode) { // Based on current specification the message should be persisted in the chat log // and then to the preferred method of contact. // Fall back logic: Try to send to "preferred method of contact" if none is defined fall back to email. var resultModel = await PersistMessage(new ChatLogItemModel { SenderTypeId = ChatLogPersonTypeEnum.Staff.Value, SenderUniqueId = personUniqueId, RecipientUniqueId = model.Parent.ParentUniqueId, RecipientTypeId = ChatLogPersonTypeEnum.Parent.Value, // By default we should mark them as read in the chat log. RecipientHasRead = true, EnglishMessage = $"{subjectEnglish}: {StripHTML(messageEnglish)}", StudentUniqueId = studentUniqueId, TranslatedMessage = LanguageCode != "en" ? $"{subjectTranslated}: {StripHTML(messageTranslated)}" : null, TranslatedLanguageCode = LanguageCode != "en" ? LanguageCode : null }); var subjectToSend = $"{studentName}: {subjectTranslated}"; await SendMessageToPreferredMethodOfContact(model, messageTranslated, subjectToSend, staffName); return(resultModel); }
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); } }
private async Task SendGroupMessageHandler(StudentParentAssociationModel model, string personUniqueId, string translatedMessage, string translatedSubject, string englishMessage, string englishSubject, string staffName, string studentName, string studentUniqueId, Guid queueId, string LanguageCode) { string ErrorMessage = string.Empty; ChatLogItemModel resultModel = new ChatLogItemModel(); try { // Based on current specification the message should be persisted in the chat log // and then to the preferred method of contact. // Fall back logic: Try to send to "preferred method of contact" if none is defined fall back to email. resultModel = await PersistMessage(new ChatLogItemModel { SenderTypeId = ChatLogPersonTypeEnum.Staff.Value, SenderUniqueId = personUniqueId, RecipientUniqueId = model.Parent.ParentUniqueId, RecipientTypeId = ChatLogPersonTypeEnum.Parent.Value, // By default we should mark them as read in the chat log. RecipientHasRead = true, EnglishMessage = $"{englishSubject}: {StripHTML(englishMessage)}", StudentUniqueId = studentUniqueId, TranslatedMessage = LanguageCode != "en" ? $"{translatedSubject}: {StripHTML(translatedMessage)}" : null, TranslatedLanguageCode = LanguageCode != "en" ? LanguageCode : null }); var subjectToSend = LanguageCode != "en" ? translatedSubject : englishSubject; var messageMetadata = new MessageAbstractionModel { SenderName = staffName, SenderUniqueId = personUniqueId, RecipientUniqueId = model.Parent.ParentUniqueId, RecipientEmail = model.Parent.Email, RecipientTelephone = model.Parent.Telephone, RecipientTelephoneSMSDomain = model.Parent.SMSDomain, Subject = subjectToSend, BodyMessage = LanguageCode != "en" ? translatedMessage : englishMessage, AboutStudent = new StudentMessageAbstractModel { StudentUniqueId = studentUniqueId, StudentName = studentName }, DelivaryMethod = model.Parent.ParentAlert.PreferredMethodOfContactTypeId.Value, LanguageCode = LanguageCode }; //Should use the provider for the Preferred Method of contact by parent var messageProvider = _messageProviders.SingleOrDefault(x => x.DeliveryMethod == messageMetadata.DelivaryMethod); await messageProvider.SendMessage(messageMetadata); } catch (Exception ex) { ErrorMessage = ex.Message; } finally { var chatLogModel = new GroupMessagesChatLogModel { GroupMessagesLogId = queueId, ChatLogId = resultModel.ChatId, Status = GroupMessagesStatusEnum.Sent }; if (!string.IsNullOrEmpty(ErrorMessage)) { chatLogModel.Status = GroupMessagesStatusEnum.Error; chatLogModel.ErrorMessage = ErrorMessage; await _communicationsRepository.PersistChatGroupMessage(chatLogModel); throw new Exception(ErrorMessage); } await _communicationsRepository.PersistChatGroupMessage(chatLogModel); } }