public async void Run([TimerTrigger("0 0 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            var strFilter          = "PublishOn eq '" + this.RoundAndFormatDateTime() + "'";
            var draftNotifications = await this.notificationDataRepository.GetWithFilterAsync(strFilter);

            foreach (var draftEntity in draftNotifications)
            {
                var newSentNotificationId =
                    await this.notificationDataRepository.MoveDraftToSentPartitionAsync(draftEntity);

                var prepareToSendQueueMessageContent = new PrepareToSendQueueMessageContent
                {
                    NotificationId = newSentNotificationId,
                };
                await this.prepareToSendQueue.SendAsync(prepareToSendQueueMessageContent);

                var forceCompleteDataQueueMessageContent = new DataQueueMessageContent
                {
                    NotificationId       = newSentNotificationId,
                    ForceMessageComplete = true,
                };
                await this.dataQueue.SendDelayedAsync(
                    forceCompleteDataQueueMessageContent,
                    this.forceCompleteMessageDelayInSeconds);
            }
        }
        public async Task <IActionResult> CreateSentNotificationAsync(
            [FromBody] DraftNotification draftNotification)
        {
            if (draftNotification == null)
            {
                throw new ArgumentNullException(nameof(draftNotification));
            }

            // TODO: double-check it
            // draftNotification.Buttons = this.GetButtonTrackingUrl(draftNotification);

            var draftNotificationDataEntity = await this.notificationDataRepository.GetAsync(
                NotificationDataTableNames.DraftNotificationsPartition,
                draftNotification.Id);

            if (draftNotificationDataEntity == null)
            {
                return(this.NotFound($"Draft notification, Id: {draftNotification.Id}, could not be found."));
            }

            var newSentNotificationId =
                await this.notificationDataRepository.MoveDraftToSentPartitionAsync(draftNotificationDataEntity);

            // Ensure the data table needed by the Azure Functions to send the notifications exist in Azure storage.
            await this.sentNotificationDataRepository.EnsureSentNotificationDataTableExistsAsync();

            // Update user app id if proactive installation is enabled.
            await this.UpdateUserAppIdAsync();

            var prepareToSendQueueMessageContent = new PrepareToSendQueueMessageContent
            {
                NotificationId = newSentNotificationId,
            };

            await this.prepareToSendQueue.SendAsync(prepareToSendQueueMessageContent);

            // Send a "force complete" message to the data queue with a delay to ensure that
            // the notification will be marked as complete no matter the counts
            var forceCompleteDataQueueMessageContent = new DataQueueMessageContent
            {
                NotificationId       = newSentNotificationId,
                ForceMessageComplete = true,
            };

            await this.dataQueue.SendDelayedAsync(
                forceCompleteDataQueueMessageContent,
                this.forceCompleteMessageDelayInSeconds);

            return(this.Ok());
        }
Beispiel #3
0
        public async Task PrepareToSendFunctionNotificationEntityNotFoundTest()
        {
            // Arrange
            var activityContext = this.GetPrepareToSendFunction();

            string myQueueItem = "{\"NotificationId\":\"notificationId\"}";
            PrepareToSendQueueMessageContent messageContent = JsonConvert.DeserializeObject <PrepareToSendQueueMessageContent>(myQueueItem);

            this.notificationDataRepository.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <string>())).Returns(Task.FromResult(default(NotificationDataEntity)));

            // Act
            Func <Task> task = async() => await activityContext.Run(myQueueItem, this.starter.Object, this.log.Object);

            // Assert
            await task.Should().NotThrowAsync();

            this.notificationDataRepository.Verify(x => x.GetAsync(It.Is <string>(x => x.Equals(NotificationDataTableNames.SentNotificationsPartition)), It.Is <string>(x => x.Equals(messageContent.NotificationId))), Times.Once());
            this.starter.Verify(x => x.StartNewAsync(It.IsAny <string>(), null /*instanceId*/), Times.Never());
        }
        public async Task <IActionResult> CreateSentNotificationAsync(
            [FromBody] DraftNotification draftNotification)
        {
            var draftNotificationDataEntity = await this.notificationDataRepository.GetAsync(
                NotificationDataTableNames.DraftNotificationsPartition,
                draftNotification.Id);

            if (draftNotificationDataEntity == null)
            {
                return(this.NotFound($"Draft notification, Id: {draftNotification.Id}, could not be found."));
            }

            var newSentNotificationId =
                await this.notificationDataRepository.MoveDraftToSentPartitionAsync(draftNotificationDataEntity);

            // Ensure the data tables needed by the Azure Functions to send the notifications exist in Azure storage.
            await Task.WhenAll(
                this.sentNotificationDataRepository.EnsureSentNotificationDataTableExistsAsync(),
                this.sendBatchesDataRepository.EnsureSendBatchesDataTableExistsAsync());

            var prepareToSendQueueMessageContent = new PrepareToSendQueueMessageContent
            {
                NotificationId = newSentNotificationId,
            };

            await this.prepareToSendQueue.SendAsync(prepareToSendQueueMessageContent);

            // Send a "force complete" message to the data queue with a delay to ensure that
            // the notification will be marked as complete no matter the counts
            var forceCompleteDataQueueMessageContent = new DataQueueMessageContent
            {
                NotificationId       = newSentNotificationId,
                ForceMessageComplete = true,
            };

            await this.dataQueue.SendDelayedAsync(
                forceCompleteDataQueueMessageContent,
                this.forceCompleteMessageDelayInSeconds);

            return(this.Ok());
        }
        private async void SendNotification(string id)
        {
            var draftNotificationDataEntity = await this.notificationDataRepository.GetAsync(
                NotificationDataTableNames.DraftNotificationsPartition,
                id);

            if (draftNotificationDataEntity == null)
            {
                this.smslogger.LogError($"Draft notification, Id: {id}, could not be found.");
            }
            else
            {
                var newSentNotificationId =
                    await this.notificationDataRepository.MoveDraftToSentPartitionAsync(draftNotificationDataEntity);

                // Ensure the data table needed by the Azure Functions to send the notifications exist in Azure storage.
                await this.sentNotificationDataRepository.EnsureSentNotificationDataTableExistsAsync();

                var prepareToSendQueueMessageContent = new PrepareToSendQueueMessageContent
                {
                    NotificationId = newSentNotificationId,
                };

                await this.prepareToSendQueue.SendAsync(prepareToSendQueueMessageContent);

                // Send a "force complete" message to the data queue with a delay to ensure that
                // the notification will be marked as complete no matter the counts.
                var forceCompleteDataQueueMessageContent = new DataQueueMessageContent
                {
                    NotificationId       = newSentNotificationId,
                    ForceMessageComplete = true,
                };
                await this.dataQueue.SendDelayedAsync(
                    forceCompleteDataQueueMessageContent,
                    this.forceCompleteMessageDelayInSeconds);
            }
        }
Beispiel #6
0
        public async Task PrepareToSendFunctionSuccessTest()
        {
            // Arrange
            var activityContext = this.GetPrepareToSendFunction();

            string myQueueItem = "{\"NotificationId\":\"notificationId\"}";
            PrepareToSendQueueMessageContent messageContent             = JsonConvert.DeserializeObject <PrepareToSendQueueMessageContent>(myQueueItem);
            NotificationDataEntity           sentNotificationDataEntity = new NotificationDataEntity()
            {
                Id = "notificationId"
            };

            this.notificationDataRepository.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(sentNotificationDataEntity);
            this.starter.Setup(x => x.StartNewAsync(It.IsAny <string>(), It.IsAny <NotificationDataEntity>())).ReturnsAsync("instanceId");

            // Act
            Func <Task> task = async() => await activityContext.Run(myQueueItem, this.starter.Object, this.log.Object);

            // Assert
            await task.Should().NotThrowAsync();

            this.notificationDataRepository.Verify(x => x.GetAsync(It.Is <string>(x => x.Equals(NotificationDataTableNames.SentNotificationsPartition)), It.Is <string>(x => x.Equals(messageContent.NotificationId))), Times.Once());
            this.starter.Verify(x => x.StartNewAsync(It.Is <string>(x => x.Equals(FunctionNames.PrepareToSendOrchestrator)), It.Is <NotificationDataEntity>(x => x.Id == sentNotificationDataEntity.Id)), Times.Once());
        }