Exemplo n.º 1
0
        public async Task <IHttpActionResult> SetRecipientRead(ChatLogItemModel chatLogItemModel)
        {
            var recipient = SecurityPrincipal.Current;

            chatLogItemModel.RecipientUniqueId = recipient.PersonUniqueId;
            chatLogItemModel.RecipientTypeId   = recipient.PersonTypeId;

            await _communicationsService.SetRecipientRead(chatLogItemModel);

            return(Ok());
        }
        public async Task SetRecipientRead(ChatLogItemModel model)
        {
            var dbModel = _edFiDb.ChatLogs
                          .Where(x => x.RecipientUniqueId == model.RecipientUniqueId &&
                                 x.RecipientTypeId == model.RecipientTypeId &&
                                 x.SenderUniqueId == model.SenderUniqueId &&
                                 x.SenderTypeId == model.SenderTypeId &&
                                 x.StudentUniqueId == model.StudentUniqueId)
                          .OrderByDescending(x => x.DateSent).FirstOrDefault();

            dbModel.RecipientHasRead = true;

            await _edFiDb.SaveChangesAsync();
        }
Exemplo n.º 3
0
        public static bool UpdateClients(ChatLogItemModel model)
        {
            var hubContext = GlobalHost.ConnectionManager.GetHubContext <ChatHub>();
            var camelcase  = JsonConvert.SerializeObject(model, new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });


            hubContext.Clients.Group(model.RecipientUniqueId).messageReceived(camelcase);
            //hubContext.Clients.Group(model.SenderUniqueId).messageReceived(camelcase);
            //hubContext.Clients.All.messageReceived(camelcase);

            return(true);
        }
Exemplo n.º 4
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 <ChatLogItemModel> PersistMessage(ChatLogItemModel model)
        {
            var persistModel = new ChatLog();

            persistModel.RecipientUniqueId = model.RecipientUniqueId;
            persistModel.SenderUniqueId    = model.SenderUniqueId;
            persistModel.StudentUniqueId   = model.StudentUniqueId;
            persistModel.EnglishMessage    = model.EnglishMessage;
            persistModel.OriginalMessage   = model.OriginalMessage;
            persistModel.SenderTypeId      = model.SenderTypeId;
            persistModel.RecipientTypeId   = model.RecipientTypeId;

            _edFiDb.ChatLogs.Add(persistModel);

            await _edFiDb.SaveChangesAsync();

            model.DateSent = persistModel.DateSent;

            return(model);
        }
Exemplo n.º 6
0
        public async Task <IHttpActionResult> PersistMessage(ChatLogItemModel chatLogItemModel)
        {
            var sender = SecurityPrincipal.Current;

            chatLogItemModel.SenderUniqueId = sender.PersonUniqueId;
            chatLogItemModel.SenderTypeId   = sender.PersonTypeId;

            var returnModel = await _communicationsService.PersistMessage(chatLogItemModel);

            // If everything good then update clients
            if (returnModel == null)
            {
                return(NotFound());
            }

            await _notificationsService.SendNotificationAsync(chatLogItemModel);

            ChatHub.UpdateClients(returnModel);
            return(Ok(returnModel));
        }
        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);
            }
        }
 public async Task <ChatLogItemModel> PersistMessage(ChatLogItemModel model)
 {
     return(await _communicationsRepository.PersistMessage(model));
 }
 public async Task SetRecipientRead(ChatLogItemModel model)
 {
     await _communicationsRepository.SetRecipientRead(model);
 }