/// <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, string digestFrequency)
        {
            this.logger.LogInformation($"Send notification Timer trigger function executed at: {DateTime.UtcNow}");

            var teamPosts = await this.teamPostSearchService.GetPostsAsync(PostSearchScope.FilterPostsAsPerDateRange, searchQuery : null, userObjectId : null);

            var filteredTeamPosts = this.teamPostStorageHelper.GetTeamPostsForDateRange(teamPosts, startDate, endDate);

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

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

                if (teamPreferences != null)
                {
                    foreach (var teamPreference in teamPreferences)
                    {
                        var tagsFilteredData = this.GetDataAsPerTags(teamPreference, filteredTeamPosts);

                        if (tagsFilteredData.Any())
                        {
                            var notificationCard = DigestNotificationListCard.GetNotificationListCard(
                                tagsFilteredData,
                                this.localizer,
                                notificationCardTitle,
                                this.cardPostTypePair,
                                this.botOptions.Value.ManifestId,
                                this.options.Value.DiscoverTabEntityId,
                                this.options.Value.AppBaseUri);

                            var teamTabConfiguration = await this.teamTagStorageProvider.GetTeamTagAsync(teamPreference.TeamId);

                            if (teamTabConfiguration != null)
                            {
                                await this.SendCardToTeamAsync(teamPreference, notificationCard, teamTabConfiguration.ServiceUrl);
                            }
                        }
                    }
                }
                else
                {
                    this.logger.LogInformation("Unable to fetch team digest preferences.");
                }
            }
            else
            {
                this.logger.LogInformation($"There is no digest data available to send at this time range from: {0} till {1}", startDate, endDate);
            }
        }
Example #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);
            }
        }