Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ScheduleNotificationDataRepository"/> class.
 /// </summary>
 /// <param name="configuration">Represents the application configuration.</param>
 /// <param name="notificationDataRepository">Notification DataRepository instance.</param>
 /// <param name="scheduleNotificationDelivery">Notification Delivery instance.</param>
 /// <param name="metadataProvider">Meta data Provider instance.</param>
 /// <param name="sendingNotificationCreator">SendingNotification Creator instance.</param>
 /// <param name="isFromAzureFunction">Flag to show if created from Azure Function.</param>
 public ScheduleNotificationDataRepository(
     IConfiguration configuration,
     NotificationDataRepository notificationDataRepository,
     MetadataProvider metadataProvider = null,
     SendingNotificationCreator sendingNotificationCreator     = null,
     ScheduleNotificationDelivery scheduleNotificationDelivery = null,
     bool isFromAzureFunction = false)
     : base(
         configuration,
         PartitionKeyNames.ScheduleNotificationDataTable.TableName,
         PartitionKeyNames.ScheduleNotificationDataTable.ScheduleNotificationsPartition,
         isFromAzureFunction)
 {
     this.notificationDataRepository   = notificationDataRepository;
     this.metadataProvider             = metadataProvider;
     this.sendingNotificationCreator   = sendingNotificationCreator;
     this.scheduleNotificationDelivery = scheduleNotificationDelivery;
 }
        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")));
        }