/// <summary> /// Send learning plan list card for selected week. /// </summary> /// <param name="turnContext">Complete learning plan data.</param> /// <param name="userBotInstalledDate">User bot installed date.</param> /// <returns>Learning plan list card as attachment.</returns> public async Task GetWeeklyLearningPlanCardAsync( ITurnContext <IMessageActivity> turnContext, DateTime?userBotInstalledDate) { turnContext = turnContext ?? throw new ArgumentNullException(nameof(turnContext)); DateTime botInstalledDate = userBotInstalledDate ?? throw new ArgumentNullException(nameof(userBotInstalledDate)); // Complete learning plan for weeks. var completeLearningPlan = await this.GetCompleteLearningPlansAsync(); if (completeLearningPlan == null || !completeLearningPlan.Any()) { await turnContext.SendActivityAsync(this.localizer.GetString("CompleteLearningPlanNotExistText")); } else { int currentLearningWeek = ((DateTime.UtcNow - botInstalledDate).Days / 7) + 1; if (currentLearningWeek > this.sharePointOptions.Value.NewHireLearningPlansInWeeks) { await turnContext.SendActivityAsync(this.localizer.GetString("NoCurrentWeekPlanText")); return; } // Send current week learning list card. var learningWeek = $"{BotCommandConstants.LearningPlanWeek} {currentLearningWeek}"; var listCardAttachment = LearningPlanListCard.GetLearningPlanListCard( completeLearningPlan.Where(learningPlan => learningPlan.CompleteBy.ToUpperInvariant() == learningWeek.ToUpperInvariant()), this.localizer, this.localizer.GetString("LearningPlanWeekListCardTitleText", learningWeek), this.botOptions.Value.ManifestId, this.botOptions.Value.AppBaseUri); await turnContext.SendActivityAsync(MessageFactory.Attachment(listCardAttachment)); } return; }
/// <summary> /// Send learning plan notification card to new hire on weekly basis. /// </summary> /// <returns>A task that represents whether weekly notification sent successfully or not.</returns> public async Task <bool> SendWeeklyNotificationAsync() { try { var completeLearningPlan = await this.learningPlanHelper.GetCompleteLearningPlansAsync(); if (completeLearningPlan == null || !completeLearningPlan.Any()) { this.logger.LogError("Learning plan not available."); return(false); } var newHires = await this.userStorageProvider.GetAllUsersAsync((int)UserRole.NewHire); if (newHires == null) { this.logger.LogError("New hires not available."); return(false); } var batchStartDate = DateTime.UtcNow; var learningPlansInDays = 0; for (int i = 1; i <= this.defaultLearningPlanInWeek; i++) { try { // To calculate weekly users list to send learning plan notification. var users = newHires.Where(user => (batchStartDate - user.BotInstalledOn)?.Days > learningPlansInDays && (batchStartDate - user.BotInstalledOn)?.Days <= learningPlansInDays + 7).ToList(); var learningWeek = $"{BotCommandConstants.LearningPlanWeek} {i}"; var listCardAttachment = LearningPlanListCard.GetLearningPlanListCard( completeLearningPlan.Where(learningPlan => learningPlan.CompleteBy.ToUpperInvariant() == learningWeek.ToUpperInvariant()), this.localizer, this.localizer.GetString("LearningPlanWeekListCardTitleText", learningWeek), this.botOptions.Value.ManifestId, this.botOptions.Value.AppBaseUri); if (!users.Any() || listCardAttachment == null) { this.logger.LogWarning($"Learning plan notification card is not available for week {i}."); continue; } // Send weekly learning plan notification to new hire employees. foreach (var userDetail in users) { try { await this.notificationCardHelper.SendProActiveNotificationCardAsync(listCardAttachment, userDetail.ConversationId, userDetail.ServiceUrl); } #pragma warning disable CA1031 // Catching general exceptions that might arise during execution to avoid blocking next run. catch (Exception ex) #pragma warning restore CA1031 // Catching general exceptions that might arise during execution to avoid blocking next run. { this.logger.LogError(ex, $"Error occurred while sending learning plan for user {userDetail.AadObjectId}: {ex.Message} at: {DateTime.UtcNow}"); } } } #pragma warning disable CA1031 // Catching general exceptions that might arise during execution to avoid blocking next run. catch (Exception ex) #pragma warning restore CA1031 // Catching general exceptions that might arise during execution to avoid blocking next run. { this.logger.LogError(ex, $"Error occurred while sending learning plan for week {i}: {ex.Message} at: {DateTime.UtcNow}"); } finally { learningPlansInDays += 7; batchStartDate.AddDays(7); } } return(true); } catch (CloudException ex) { this.logger.LogError(ex, $"Error occurred while accessing user details from storage: {ex.Message} at: {DateTime.UtcNow}"); return(false); } }