/// <summary>
 /// Initializes a new instance of the <see cref="SentNotificationsController"/> class.
 /// </summary>
 /// <param name="notificationDataRepository">Notification data repository service that deals with the table storage in azure.</param>
 /// <param name="notificationDelivery">Notification delivery service instance.</param>
 /// <param name="teamDataRepository">Team data repository instance.</param>
 /// <param name="scheduleNotificationDataRepository">Schedule notification data repository.</param>
 /// <param name="sentNotificationDataRepository">Sent Notification Data Repository.</param>
 public SentNotificationsController(
     NotificationDataRepository notificationDataRepository,
     NotificationDelivery notificationDelivery,
     TeamDataRepository teamDataRepository,
     ScheduleNotificationDataRepository scheduleNotificationDataRepository,
     SentNotificationDataRepository sentNotificationDataRepository)
 {
     this.notificationDataRepository         = notificationDataRepository;
     this.notificationDelivery               = notificationDelivery;
     this.teamDataRepository                 = teamDataRepository;
     this.scheduleNotificationDataRepository = scheduleNotificationDataRepository;
     this.sentNotificationDataRepository     = sentNotificationDataRepository;
 }
Пример #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ScheduleNotificationDelivery"/> class.
 /// </summary>
 /// <param name="configuration">The configuration.</param>
 /// <param name="notificationDataRepository">Notification repository service.</param>
 /// <param name="metadataProvider">Meta data Provider instance.</param>
 /// <param name="sendingNotificationCreator">SendingNotification creator.</param>
 /// <param name="scheduleNotificationDataRepository">Schedule Notification data repository.</param>
 /// <param name="teamDataRepository">TeamData Repository instance.</param>
 public ScheduleNotificationDelivery(
     IConfiguration configuration,
     NotificationDataRepository notificationDataRepository,
     MetadataProvider metadataProvider,
     SendingNotificationCreator sendingNotificationCreator,
     ScheduleNotificationDataRepository scheduleNotificationDataRepository,
     TeamDataRepository teamDataRepository)
 {
     this.configuration = configuration;
     this.notificationDataRepository         = notificationDataRepository;
     this.metadataProvider                   = metadataProvider;
     this.sendingNotificationCreator         = sendingNotificationCreator;
     this.scheduleNotificationDataRepository = scheduleNotificationDataRepository;
     this.teamDataRepository                 = teamDataRepository;
 }
        public async Task RunAsync(
            [TimerTrigger("0 */30 * * * *")]
            TimerInfo scheduleInitializationTimer,
            ILogger log)
        {
            scheduleInitializationTimer = scheduleInitializationTimer ?? throw new ArgumentNullException(nameof(scheduleInitializationTimer));

            log.LogInformation($"Schedule Initialization Timer executed at: {scheduleInitializationTimer.ToString()}.");

            CompanyCommunicatorScheduleFunction.configuration = CompanyCommunicatorScheduleFunction.configuration ??
                                                                new ConfigurationBuilder()
                                                                .AddEnvironmentVariables()
                                                                .Build();

            CompanyCommunicatorScheduleFunction.adaptiveCardCreator = CompanyCommunicatorScheduleFunction.adaptiveCardCreator
                                                                      ?? new AdaptiveCardCreator();

            CompanyCommunicatorScheduleFunction.tableRowKeyGenerator = CompanyCommunicatorScheduleFunction.tableRowKeyGenerator
                                                                       ?? new TableRowKeyGenerator();

            CompanyCommunicatorScheduleFunction.adGroupsDataRepository = CompanyCommunicatorScheduleFunction.adGroupsDataRepository
                                                                         ?? new ADGroupsDataRepository(CompanyCommunicatorScheduleFunction.configuration, true);

            CompanyCommunicatorScheduleFunction.notificationDataRepository = CompanyCommunicatorScheduleFunction.notificationDataRepository
                                                                             ?? new NotificationDataRepository(CompanyCommunicatorScheduleFunction.configuration, CompanyCommunicatorScheduleFunction.adGroupsDataRepository, tableRowKeyGenerator, true);

            CompanyCommunicatorScheduleFunction.userDataRepository = CompanyCommunicatorScheduleFunction.userDataRepository
                                                                     ?? new UserDataRepository(CompanyCommunicatorScheduleFunction.configuration, true);

            CompanyCommunicatorScheduleFunction.teamDataRepository = CompanyCommunicatorScheduleFunction.teamDataRepository
                                                                     ?? new TeamDataRepository(CompanyCommunicatorScheduleFunction.configuration, true);

            CompanyCommunicatorScheduleFunction.sendingNotificationDataRepository = CompanyCommunicatorScheduleFunction.sendingNotificationDataRepository
                                                                                    ?? new SendingNotificationDataRepository(CompanyCommunicatorScheduleFunction.configuration, true);

            CompanyCommunicatorScheduleFunction.globalSendingNotificationDataRepository = CompanyCommunicatorScheduleFunction.globalSendingNotificationDataRepository
                                                                                          ?? new GlobalSendingNotificationDataRepository(CompanyCommunicatorScheduleFunction.configuration, true);

            CompanyCommunicatorScheduleFunction.sentNotificationDataRepository = CompanyCommunicatorScheduleFunction.sentNotificationDataRepository
                                                                                 ?? new SentNotificationDataRepository(CompanyCommunicatorScheduleFunction.configuration, true);

            CompanyCommunicatorScheduleFunction.metadataProvider = CompanyCommunicatorScheduleFunction.metadataProvider
                                                                   ?? new MetadataProvider(CompanyCommunicatorScheduleFunction.configuration, userDataRepository, teamDataRepository, notificationDataRepository, adGroupsDataRepository);

            CompanyCommunicatorScheduleFunction.sendingNotificationCreator = CompanyCommunicatorScheduleFunction.sendingNotificationCreator
                                                                             ?? new SendingNotificationCreator(CompanyCommunicatorScheduleFunction.configuration, notificationDataRepository, sendingNotificationDataRepository, adaptiveCardCreator);

            CompanyCommunicatorScheduleFunction.scheduleNotificationDelivery = CompanyCommunicatorScheduleFunction.scheduleNotificationDelivery
                                                                               ?? new ScheduleNotificationDelivery(CompanyCommunicatorScheduleFunction.configuration, notificationDataRepository, metadataProvider, sendingNotificationCreator, scheduleNotificationDataRepository, teamDataRepository);

            CompanyCommunicatorScheduleFunction.scheduleNotificationDataRepository = CompanyCommunicatorScheduleFunction.scheduleNotificationDataRepository
                                                                                     ?? new ScheduleNotificationDataRepository(CompanyCommunicatorScheduleFunction.configuration, notificationDataRepository, metadataProvider, sendingNotificationCreator, scheduleNotificationDelivery, true);

            // Generate filter condition to get pending notifications.
            var rowKeyFilter = TableQuery.GenerateFilterConditionForDate(
                nameof(ScheduleNotificationDataEntity.NotificationDate),
                QueryComparisons.LessThanOrEqual,
                DateTime.UtcNow);

            // Get records which are pending to send notification.
            var pendingNotifications = await CompanyCommunicatorScheduleFunction.scheduleNotificationDataRepository.GetWithFilterAsync(
                rowKeyFilter);

            // Repeat all pending notifications.
            foreach (var pendingNotification in pendingNotifications)
            {
                try
                {
                    log.LogInformation($"Schedule notification triggered for " + pendingNotification.NotificationId);
                    string returnMessage = await CompanyCommunicatorScheduleFunction.scheduleNotificationDataRepository.SendNotificationandCreateScheduleAsync(pendingNotification);

                    if (string.IsNullOrEmpty(returnMessage))
                    {
                        log.LogInformation($"Schedule notification success for " + pendingNotification.NotificationId);
                    }
                    else
                    {
                        log.LogInformation($"Schedule notification failed for " + pendingNotification.NotificationId);
                    }
                }
                catch (Exception ex)
                {
                    log.LogError($"Error while sending schedule notification:" + pendingNotification.NotificationId, ex);
                }
            }
        }
        public async Task <IActionResult> RunAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            //  scheduleInitializationTimer = scheduleInitializationTimer ?? throw new ArgumentNullException(nameof(scheduleInitializationTimer));

            //log.LogInformation($"Schedule Initialization Timer executed at: {scheduleInitializationTimer.ToString()}.");

            SchduleNotificationTestFunction.configuration = SchduleNotificationTestFunction.configuration ??
                                                            new ConfigurationBuilder()
                                                            .AddEnvironmentVariables()
                                                            .Build();

            SchduleNotificationTestFunction.adaptiveCardCreator = SchduleNotificationTestFunction.adaptiveCardCreator
                                                                  ?? new AdaptiveCardCreator();

            SchduleNotificationTestFunction.tableRowKeyGenerator = SchduleNotificationTestFunction.tableRowKeyGenerator
                                                                   ?? new TableRowKeyGenerator();

            SchduleNotificationTestFunction.adGroupsDataRepository = SchduleNotificationTestFunction.adGroupsDataRepository
                                                                     ?? new ADGroupsDataRepository(SchduleNotificationTestFunction.configuration, true);

            SchduleNotificationTestFunction.notificationDataRepository = SchduleNotificationTestFunction.notificationDataRepository
                                                                         ?? new NotificationDataRepository(SchduleNotificationTestFunction.configuration, SchduleNotificationTestFunction.adGroupsDataRepository, tableRowKeyGenerator, true);

            SchduleNotificationTestFunction.userDataRepository = SchduleNotificationTestFunction.userDataRepository
                                                                 ?? new UserDataRepository(SchduleNotificationTestFunction.configuration, true);

            SchduleNotificationTestFunction.teamDataRepository = SchduleNotificationTestFunction.teamDataRepository
                                                                 ?? new TeamDataRepository(SchduleNotificationTestFunction.configuration, true);

            SchduleNotificationTestFunction.sendingNotificationDataRepository = SchduleNotificationTestFunction.sendingNotificationDataRepository
                                                                                ?? new SendingNotificationDataRepository(SchduleNotificationTestFunction.configuration, true);

            SchduleNotificationTestFunction.globalSendingNotificationDataRepository = SchduleNotificationTestFunction.globalSendingNotificationDataRepository
                                                                                      ?? new GlobalSendingNotificationDataRepository(SchduleNotificationTestFunction.configuration, true);

            SchduleNotificationTestFunction.sentNotificationDataRepository = SchduleNotificationTestFunction.sentNotificationDataRepository
                                                                             ?? new SentNotificationDataRepository(SchduleNotificationTestFunction.configuration, true);

            SchduleNotificationTestFunction.metadataProvider = SchduleNotificationTestFunction.metadataProvider
                                                               ?? new MetadataProvider(SchduleNotificationTestFunction.configuration, userDataRepository, teamDataRepository, notificationDataRepository, adGroupsDataRepository);

            SchduleNotificationTestFunction.sendingNotificationCreator = SchduleNotificationTestFunction.sendingNotificationCreator
                                                                         ?? new SendingNotificationCreator(SchduleNotificationTestFunction.configuration, notificationDataRepository, sendingNotificationDataRepository, adaptiveCardCreator);

            SchduleNotificationTestFunction.scheduleNotificationDelivery = SchduleNotificationTestFunction.scheduleNotificationDelivery
                                                                           ?? new ScheduleNotificationDelivery(SchduleNotificationTestFunction.configuration, notificationDataRepository, metadataProvider, sendingNotificationCreator, scheduleNotificationDataRepository, teamDataRepository);

            SchduleNotificationTestFunction.scheduleNotificationDataRepository = SchduleNotificationTestFunction.scheduleNotificationDataRepository
                                                                                 ?? new ScheduleNotificationDataRepository(SchduleNotificationTestFunction.configuration, notificationDataRepository, metadataProvider, sendingNotificationCreator, scheduleNotificationDelivery, true);

            // Generate filter condition to get pending notifications.
            var rowKeyFilter = TableQuery.GenerateFilterConditionForDate(
                nameof(ScheduleNotificationDataEntity.NotificationDate),
                QueryComparisons.LessThanOrEqual,
                DateTime.UtcNow);

            // Get records which are pending to send notification.
            var pendingNotifications = await SchduleNotificationTestFunction.scheduleNotificationDataRepository.GetWithFilterAsync(
                rowKeyFilter);

            // Repeat all pending notifications.
            foreach (var pendingNotification in pendingNotifications)
            {
                try
                {
                    log.LogInformation($"Schedule notification triggered for " + pendingNotification.NotificationId);
                    string returnMessage = await SchduleNotificationTestFunction.scheduleNotificationDataRepository.SendNotificationandCreateScheduleAsync(pendingNotification);

                    if (string.IsNullOrEmpty(returnMessage))
                    {
                        log.LogInformation($"Schedule notification success for " + pendingNotification.NotificationId);
                    }
                    else
                    {
                        log.LogInformation($"Schedule notification failed for " + pendingNotification.NotificationId);
                    }
                }
                catch (Exception ex)
                {
                    log.LogError($"Error while sending schedule notification:" + pendingNotification.NotificationId, ex);
                }
            }

            return(await Task.FromResult((ActionResult) new OkObjectResult("OK")));
        }