コード例 #1
0
        public async Task ProcessAggregateCommunicationRequestAsync(AggregateCommunicationRequest request)
        {
            if (_digestDeliveryFrequencies.Contains(request.Frequency) == false)
            {
                throw new NotSupportedException(CommunicationErrorMessages.InvalidAggregateCommunicationRequestedFrequencyMessage);
            }

            var scheduledMessages = await _repository.GetScheduledMessagesSinceAsync(request.RequestType, request.Frequency, request.FromDateTime, request.ToDateTime);

            var groupedUserMessages = scheduledMessages.GroupBy(cm => cm.Recipient.UserId);

            _logger.LogInformation($"Found {groupedUserMessages.Count()} users to possibly send {request.RequestType} aggregate emails to. For Communication Request: {request.RequestId.ToString()}");

            var tasks = new List <Task>();

            foreach (var userMessages in groupedUserMessages)
            {
                var acr = new AggregateCommunicationRequest(Guid.NewGuid(), request.RequestType, request.Frequency, request.RequestDateTime, request.FromDateTime, request.ToDateTime);
                var tsk = _composerQueue.AddMessageAsync(new AggregateCommunicationComposeRequest(
                                                             userId: userMessages.Key,
                                                             messageIds: userMessages.Select(cm => cm.Id),
                                                             aggregateCommunicationRequest: acr
                                                             ));

                tasks.Add(tsk);
            }

            await Task.WhenAll(tasks);
        }
コード例 #2
0
        public async Task GivenAggregateCommunicationRequestFor5ApplicationsSubmittedFor3Recipients_ShouldQueue3ComposeMessages()
        {
            var recipientOne   = new CommunicationUser("1", "*****@*****.**", "a", "person", UserParticipation.PrimaryUser);
            var recipientTwo   = new CommunicationUser("2", "*****@*****.**", "b", "person", UserParticipation.PrimaryUser);
            var recipientThree = new CommunicationUser("3", "*****@*****.**", "c", "person", UserParticipation.PrimaryUser);
            var messages       = Enumerable.Concat(Enumerable.Concat(Enumerable.Repeat(new CommunicationMessage()
            {
                Recipient = recipientOne
            }, 3),
                                                                     Enumerable.Repeat(new CommunicationMessage()
            {
                Recipient = recipientTwo
            }, 3)),
                                                   Enumerable.Repeat(new CommunicationMessage()
            {
                Recipient = recipientThree
            }, 3));
            var from = DateTime.UtcNow.AddDays(-1);
            var to   = DateTime.UtcNow;

            _mockRepository.Setup(x => x.GetScheduledMessagesSinceAsync(CommunicationConstants.RequestType.ApplicationSubmitted, DeliveryFrequency.Daily, from, to))
            .ReturnsAsync(messages);
            var aggCommRequest = new AggregateCommunicationRequest(Guid.NewGuid(), CommunicationConstants.RequestType.ApplicationSubmitted, DeliveryFrequency.Daily, DateTime.UtcNow, from, to);

            await _sut.ProcessAggregateCommunicationRequestAsync(aggCommRequest);

            _mockRepository.Verify(x => x.GetScheduledMessagesSinceAsync(aggCommRequest.RequestType, aggCommRequest.Frequency, aggCommRequest.FromDateTime, aggCommRequest.ToDateTime), Times.Once);
            _mockComposeQueuePublisher.Verify(x => x.AddMessageAsync(It.IsAny <AggregateCommunicationComposeRequest>()), Times.Exactly(3));
        }
コード例 #3
0
        public async Task <CommunicationMessage> CreateAggregateMessageAsync(AggregateCommunicationRequest request, IEnumerable <CommunicationMessage> messages)
        {
            if (messages.Any() == false)
            {
                return(null);
            }

            var recipient = GetUserFromAggregateMessage(messages);

            var participant = (await GetPreferencesForParticipantsAsync(request.RequestType, new[] { recipient })).Single();

            if (participant.Preferences.Channels == DeliveryChannelPreferences.None)
            {
                return(null);
            }

            var validMessages = await GetValidatedAggregateMessagesAsync(messages);

            if (!validMessages.Any())
            {
                return(null);
            }

            var message = await GenerateMessageForAggregateMessagesAsync(request, validMessages);

            return(message);
        }
コード例 #4
0
        public async Task GivenAggregateCommunicationRequestForImmediateDelivery_ShouldThrowError()
        {
            var aggCommRequest = new AggregateCommunicationRequest(Guid.NewGuid(), CommunicationConstants.RequestType.ApplicationSubmitted,
                                                                   DeliveryFrequency.Immediate, DateTime.UtcNow, DateTime.UtcNow.AddDays(-1), DateTime.UtcNow);
            var ex = await Assert.ThrowsAsync <NotSupportedException>(() => _sut.ProcessAggregateCommunicationRequestAsync(aggCommRequest));

            ex.Message.Should().BeEquivalentTo(CommunicationErrorMessages.InvalidAggregateCommunicationRequestedFrequencyMessage);
        }
        public Task DailyApplicationsSubmittedCreateAggregateRequestAsync([TimerTrigger(Schedules.EightAmDaily)] TimerInfo timerInfo, TextWriter log)
        {
            const int eightAmHour = 8;

            _logger.LogInformation($"Timer trigger {this.GetType().Name} fired");

            var to   = _timeProvider.Today.AddHours(eightAmHour);
            var from = to.AddDays(-1);

            var message = new AggregateCommunicationRequest(Guid.NewGuid(), CommunicationConstants.RequestType.ApplicationSubmitted, DeliveryFrequency.Daily, _timeProvider.Now, from, to);

            return(_queue.AddMessageAsync(message));
        }
コード例 #6
0
        public async Task GivenMatchingMessageIds_UserPreferenceIsNotDaily_ThenShouldReturnNullAggregateMessage(DeliveryFrequency userFrequencyPreference)
        {
            var commPref = new CommunicationUserPreference {
                Frequency = userFrequencyPreference, Channels = DeliveryChannelPreferences.EmailOnly, Scope = NotificationScope.Individual
            };
            var from     = DateTime.UtcNow.AddDays(-1);
            var to       = DateTime.UtcNow;
            var acr      = new AggregateCommunicationRequest(Guid.NewGuid(), TestRequestType, DeliveryFrequency.Daily, DateTime.UtcNow, from, to);
            var messages = Enumerable.Repeat <CommunicationMessage>(GetTestTemplateMessage(), 10);

            _mockUserPreferenceProvider.Setup(x => x.GetUserPreferenceAsync(TestRequestType, It.IsAny <CommunicationUser>()))
            .ReturnsAsync(commPref);
            var aggregateMessage = await _sut.CreateAggregateMessageAsync(acr, messages);

            aggregateMessage.Should().BeNull();
        }
コード例 #7
0
        public async Task GivenMatchingMessageIds_AndOnlySubsetOfMessageAreValidParticipation_ShouldReturnAggregateMessage()
        {
            var commPref = new CommunicationUserPreference {
                Frequency = DeliveryFrequency.Daily, Channels = DeliveryChannelPreferences.EmailOnly, Scope = NotificationScope.Individual
            };
            var from     = DateTime.UtcNow.AddDays(-1);
            var to       = DateTime.UtcNow;
            var acr      = new AggregateCommunicationRequest(Guid.NewGuid(), TestRequestType, DeliveryFrequency.Daily, DateTime.UtcNow, from, to);
            var messages = Enumerable.Repeat <CommunicationMessage>(GetTestTemplateMessage(), 10);

            _mockUserPreferenceProvider.Setup(x => x.GetUserPreferenceAsync(TestRequestType, It.IsAny <CommunicationUser>()))
            .ReturnsAsync(commPref);
            _mockParticipantResolver.Setup(x => x.ValidateParticipantAsync(messages))
            .ReturnsAsync(Enumerable.Repeat <CommunicationMessage>(messages.First(), 3));
            var aggregateMessage = await _sut.CreateAggregateMessageAsync(acr, messages);

            aggregateMessage.Should().NotBeNull();
        }
コード例 #8
0
        private async Task <CommunicationMessage> GenerateMessageForAggregateMessagesAsync(AggregateCommunicationRequest request, IEnumerable <CommunicationMessage> messages)
        {
            var templateMessage = GetTemplateMessageFromMessagesToBeAggregated(messages);

            var aggregatedMessage = new CommunicationMessage
            {
                Id = request.RequestId,
                RequestDateTime          = request.RequestDateTime,
                ParticipantsResolverName = templateMessage.ParticipantsResolverName,
                OriginatingServiceName   = templateMessage.OriginatingServiceName,
                Recipient   = new CommunicationUser(templateMessage.Recipient.UserId, templateMessage.Recipient.Email, templateMessage.Recipient.Name, templateMessage.Recipient.UserType, UserParticipation.PrimaryUser),
                RequestType = request.RequestType,
                Channel     = templateMessage.Channel,
                Frequency   = DeliveryFrequency.Immediate,
                TemplateId  = templateMessage.TemplateId
            };

            var compositeDataItemProvider = _compositeDataItemProviders[templateMessage.RequestType];

            var consolidatedMessageData = await compositeDataItemProvider.GetConsolidatedMessageDataItemsAsync(aggregatedMessage, messages);

            if (consolidatedMessageData != null && consolidatedMessageData.Any())
            {
                aggregatedMessage.DataItems = consolidatedMessageData.ToList();
            }

            return(aggregatedMessage);
        }