/// <summary>
        /// Get team preferences data from Microsoft Azure Table storage.
        /// </summary>
        /// <param name="digestFrequency">Digest frequency text for notification like Monthly/Weekly.</param>
        /// <returns>A task that represent collection to hold team preferences data.</returns>
        public async Task <IEnumerable <TeamPreferenceEntity> > GetTeamPreferencesAsync(DigestFrequency digestFrequency)
        {
            await this.EnsureInitializedAsync();

            var digestFrequencyCondition = TableQuery.GenerateFilterCondition(nameof(TeamPreferenceEntity.DigestFrequency), QueryComparisons.Equal, digestFrequency.ToString());

            TableQuery <TeamPreferenceEntity> query             = new TableQuery <TeamPreferenceEntity>().Where(digestFrequencyCondition);
            TableContinuationToken            continuationToken = null;
            var teamPreferenceCollection = new List <TeamPreferenceEntity>();

            do
            {
                var queryResult = await this.CloudTable.ExecuteQuerySegmentedAsync(query, continuationToken);

                if (queryResult?.Results != null)
                {
                    teamPreferenceCollection.AddRange(queryResult.Results);
                    continuationToken = queryResult.ContinuationToken;
                }
            }while (continuationToken != null);

            return(teamPreferenceCollection);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Send notification in channels on weekly or monthly basis as per the configured preference in different channels.
        /// Fetch data based on the date range and send it accordingly.
        /// </summary>
        /// <param name="startDate">Start date from which data should fetch.</param>
        /// <param name="endDate">End date till when data should fetch.</param>
        /// <param name="digestFrequency">Digest frequency text for notification like Monthly/Weekly.</param>
        /// <returns>A task that sends notification in channel.</returns>
        public async Task SendNotificationInChannelAsync(DateTime startDate, DateTime endDate, DigestFrequency digestFrequency)
        {
            this.logger.LogInformation($"Send notification Timer trigger function executed at: {DateTime.UtcNow}");

            var teamPosts = await this.teamIdeaSearchService.GetTeamIdeasAsync(IdeaSearchScope.FilterPostsAsPerDateRange, searchQuery : null, userObjectId : null);

            var filteredTeamPosts = this.teamIdeaStorageHelper.GetTeamIdeasInDateRangeAsync(teamPosts, startDate, endDate);

            if (filteredTeamPosts.Any())
            {
                var teamPreferences = await this.teamPreferenceStorageProvider.GetTeamPreferencesAsync(digestFrequency);

                var notificationCardTitle = digestFrequency == DigestFrequency.Weekly
                    ? this.localizer.GetString("NotificationCardWeeklyTitleText")
                    : this.localizer.GetString("NotificationCardMonthlyTitleText");

                foreach (var teamPreference in teamPreferences)
                {
                    try
                    {
                        var categoriesFilteredData = this.GetDataAsPerCategories(teamPreference, filteredTeamPosts);

                        if (categoriesFilteredData.Any())
                        {
                            var notificationCard = DigestNotificationListCard.GetNotificationListCard(
                                this.botOptions.Value.AppBaseUri,
                                categoriesFilteredData,
                                notificationCardTitle);

                            var teamDetails = await this.teamStorageProvider.GetTeamDetailAsync(teamPreference.TeamId);

                            if (teamDetails != null)
                            {
                                await this.SendCardToTeamAsync(teamPreference, notificationCard, teamDetails.ServiceUrl);
                            }
                        }
                    }
#pragma warning disable CA1031 // Catching general exception for any errors occurred during background service execution for each teams.
                    catch (Exception ex)
#pragma warning restore CA1031 // Catching general exception for any errors occurred during background service execution for each teams.
                    {
                        this.logger.LogError(ex, $"Error while sending the notification card for team: {teamPreference?.TeamId}.");
                    }
                }
            }
            else
            {
                this.logger.LogInformation($"There is no digest data available to send at this time range from: {0} till {1}", startDate, endDate);
            }
        }