public async Task <IActionResult> ResendNotifications([FromBody] string[] messageIds)
        {
            var messages = await _notificationMessageService.GetNotificationsMessageByIds(messageIds);

            if (!messages.Any())
            {
                return(NotFound("Messages to send not found"));
            }

            foreach (var message in messages)
            {
                message.Id                  = null;
                message.CreatedDate         = DateTime.UtcNow;
                message.Status              = NotificationMessageStatus.Pending;
                message.SendAttemptCount    = 0;
                message.LastSendAttemptDate = null;
                message.LastSendError       = null;
                message.SendDate            = null;
            }

            await _notificationMessageService.SaveNotificationMessagesAsync(messages);

            foreach (var id in messages.Select(x => x.Id))
            {
                _notificationSender.EnqueueNotificationSending(id);
            }

            return(Ok());
        }
Esempio n. 2
0
        public async Task <NotificationMessageSearchResult> SearchMessageAsync(NotificationMessageSearchCriteria criteria)
        {
            var result = new NotificationMessageSearchResult();

            using (var repository = _repositoryFactory())
            {
                //Optimize performance and CPU usage
                repository.DisableChangesTracking();

                result.Results = new List <NotificationMessage>();
                var query     = BuildQuery(repository, criteria);
                var sortInfos = BuildSortExpression(criteria);

                result.TotalCount = await query.CountAsync();

                if (criteria.Take > 0)
                {
                    var messageIds = await query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id)
                                     .Select(x => x.Id)
                                     .Skip(criteria.Skip).Take(criteria.Take)
                                     .ToArrayAsync();

                    var unorderedResults = await _messageService.GetNotificationsMessageByIds(messageIds);

                    result.Results = unorderedResults.OrderBy(x => Array.IndexOf(messageIds, x.Id)).ToList();
                }
            }

            return(result);
        }
Esempio n. 3
0
        public async Task <NotificationSendResult> TrySendNotificationMessageAsync(string messageId)
        {
            var result = new NotificationSendResult();

            var message = (await _notificationMessageService.GetNotificationsMessageByIds(new[] { messageId })).FirstOrDefault();

            if (message == null)
            {
                result.ErrorMessage = $"Can't find notification message by {messageId}";
                return(result);
            }

            if (message.Status == NotificationMessageStatus.Error)
            {
                result.ErrorMessage = $"Can't send notification message by {messageId}. There are errors.";
                return(result);
            }

            var policy = Policy.Handle <SentNotificationException>().WaitAndRetryAsync(_maxRetryAttempts, retryAttempt => TimeSpan.FromSeconds(Math.Pow(3, retryAttempt)));

            var policyResult = await policy.ExecuteAndCaptureAsync(() =>
            {
                message.LastSendAttemptDate = DateTime.Now;
                message.SendAttemptCount++;
                return(_notificationMessageSenderFactory.GetSender(message).SendNotificationAsync(message));
            });

            if (policyResult.Outcome == OutcomeType.Successful)
            {
                result.IsSuccess = true;
                message.SendDate = DateTime.Now;
                message.Status   = NotificationMessageStatus.Sent;
            }
            else
            {
                result.ErrorMessage   = "Failed to send message.";
                message.LastSendError = policyResult.FinalException?.ToString();
                message.Status        = NotificationMessageStatus.Error;
            }

            await _notificationMessageService.SaveNotificationMessagesAsync(new[] { message });

            return(result);
        }
Esempio n. 4
0
        public async Task <ActionResult <NotificationMessage> > GetObjectNotificationJournal(string id)
        {
            var result = (await _notificationMessageService.GetNotificationsMessageByIds(new[] { id })).FirstOrDefault();

            return(Ok(result));
        }