public async Task RunOnce(ILogger logger) { logger.LogInformation("Begin poll for ShortQuestionSet"); string assessmentType = "short"; var questionSets = await _sitefinity.GetAll <SiteFinityShortQuestionSet>("shortquestionsets"); logger.LogInformation($"Have {questionSets?.Count} question sets to review"); if (questionSets != null) { foreach (var data in questionSets.OrderBy(x => x.LastUpdated)) { logger.LogInformation($"Getting cms data for question set {data.Id} {data.Title}"); // Attempt to load the current version for this assessment type and title var questionSet = await _questionSetRepository.GetLatestQuestionSetByTypeAndKey("short", data.Title); // Determine if an update is required i.e. the last updated datetime stamp has changed bool updateRequired = questionSet == null || (data.LastUpdated > questionSet.LastUpdated); // Nothing to do so log and exit if (!updateRequired) { logger.LogInformation($"Question set {data.Id} {data.Title} is upto date - no changes to be done"); continue; } // Attempt to get the questions for this question set logger.LogInformation($"Getting cms questions for question set {data.Id} {data.Title}"); data.Questions = await _sitefinity.Get <List <SiteFinityShortQuestion> >($"shortquestionsets({data.Id})/Questions?$expand=Trait"); if (data.Questions.Count == 0) { logger.LogInformation($"Question set {data.Id} doesn't have any questions"); continue; } logger.LogInformation( $"Received {data.Questions?.Count} questions for question set {data.Id} {data.Title}"); var latestQuestionSet = await _questionSetRepository.GetCurrentQuestionSet("short"); if (latestQuestionSet != null) { // Change the current question set to be not current logger.LogInformation($"Demoting question set {latestQuestionSet.QuestionSetVersion} from current"); latestQuestionSet.IsCurrent = false; await _questionSetRepository.CreateOrUpdateQuestionSet(latestQuestionSet); } if (questionSet != null && questionSet.IsCurrent) { // Change the current question set to be not current logger.LogInformation($"Demoting question set {questionSet.QuestionSetVersion} from current"); questionSet.IsCurrent = false; await _questionSetRepository.CreateOrUpdateQuestionSet(questionSet); } // Create the new current version int newVersionNumber = questionSet == null ? 1 : questionSet.Version + 1; var newQuestionSet = new QuestionSet() { PartitionKey = "ncs", Title = data.Title, QuestionSetKey = data.Title.ToLower(), Description = data.Description, Version = newVersionNumber, QuestionSetVersion = $"{assessmentType.ToLower()}-{data.Title.ToLower()}-{newVersionNumber}", AssessmentType = assessmentType, IsCurrent = true, LastUpdated = data.LastUpdated, }; string questionPartitionKey = newQuestionSet.QuestionSetVersion; int questionNumber = 1; foreach (var dataQuestion in data.Questions) { var newQuestion = new Question { IsNegative = dataQuestion.IsNegative, Order = questionNumber, QuestionId = questionPartitionKey + "-" + questionNumber, TraitCode = dataQuestion.Trait.Name.ToUpper(), PartitionKey = questionPartitionKey, LastUpdatedDt = dataQuestion.LastUpdatedDt, IsFilterQuestion = false, Texts = new[] { new QuestionText { LanguageCode = "EN", Text = dataQuestion.Title } } }; newQuestionSet.MaxQuestions = questionNumber; questionNumber++; await _questionRepository.CreateQuestion(newQuestion); logger.LogInformation($"Created short question {newQuestion.QuestionId}"); } await _questionSetRepository.CreateOrUpdateQuestionSet(newQuestionSet); logger.LogInformation($"Created Short Question Set {newQuestionSet.Version}"); } } else { logger.LogError("No Short Question sets available"); } logger.LogInformation($"End poll for ShortQuestionSet"); }
private async Task <QuestionSet> UpdateFilteredQuestionSets() { _logger.LogInformation("Begin poll for Filtered Question Sets"); var questionSets = await _sitefinity.GetAll <SiteFinityFilteringQuestionSet>("filteringquestionsets"); _logger.LogInformation($"Have {questionSets?.Count} question sets to review"); QuestionSet questionSet = null; if (questionSets != null) { foreach (var data in questionSets.OrderBy(x => x.LastUpdated)) { _logger.LogInformation($"Getting cms data for question set {data.Id} {data.Title}"); // Attempt to load the current version for this assessment type and title questionSet = await _questionSetRepository.GetCurrentQuestionSet(assessmentType); // Determine if an update is required i.e. the last updated datetime stamp has changed bool updateRequired = questionSet == null || (data.LastUpdated > questionSet.LastUpdated); // Nothing to do so log and exit if (!updateRequired) { _logger.LogInformation($"Filtered Question set {data.Id} {data.Title} is upto date - no changes to be done"); questionSet = null; continue; } // Attempt to get the questions for this question set _logger.LogInformation($"Getting cms questions for question set {data.Id} {data.Title}"); data.Questions = await _sitefinity.Get <List <SiteFinityFilteringQuestion> >($"filteringquestionsets({data.Id})/Questions?$expand=RelatedSkill"); if (data.Questions.Count == 0) { _logger.LogInformation($"Question set {data.Id} doesn't have any questions"); questionSet = null; continue; } _logger.LogInformation( $"Received {data.Questions?.Count} questions for question set {data.Id} {data.Title}"); if (questionSet != null && questionSet.IsCurrent) { // Change the current question set to be not current _logger.LogInformation($"Demoting question set {questionSet.QuestionSetVersion} from current"); questionSet.IsCurrent = false; await _questionSetRepository.CreateOrUpdateQuestionSet(questionSet); } // Create the new current version int newVersionNumber = questionSet == null ? 1 : questionSet.Version + 1; var newQuestionSet = new QuestionSet { PartitionKey = "filtered-" + data.Title.ToLower().Replace(" ", "-"), Title = data.Title, QuestionSetKey = data.Title.ToLower(), Description = data.Description, Version = newVersionNumber, QuestionSetVersion = $"{assessmentType.ToLower()}-{data.Title.ToLower()}-{newVersionNumber.ToString()}", AssessmentType = assessmentType, IsCurrent = true, LastUpdated = data.LastUpdated, }; string questionPartitionKey = newQuestionSet.QuestionSetVersion; int questionNumber = 1; foreach (var dataQuestion in data.Questions) { if (dataQuestion.RelatedSkill == null) { _logger.LogWarning($"Skipping Question {dataQuestion.Title} - No related Skill"); continue; } var newQuestion = new Question { IsNegative = false, Order = questionNumber, QuestionId = questionPartitionKey + "-" + questionNumber.ToString(), PartitionKey = questionPartitionKey, TraitCode = dataQuestion.RelatedSkill.Title.ToLower(), LastUpdatedDt = dataQuestion.LastUpdated, SfId = dataQuestion.Id, IsFilterQuestion = true, Texts = new[] { new QuestionText { LanguageCode = "EN", Text = dataQuestion.QuestionText } } }; newQuestionSet.MaxQuestions = questionNumber; questionNumber++; await _questionRepository.CreateQuestion(newQuestion); _logger.LogInformation($"Created filtering question {newQuestion.QuestionId}"); } questionSet = newQuestionSet; await _questionSetRepository.CreateOrUpdateQuestionSet(newQuestionSet); _logger.LogInformation($"Created Filtering Question Set {newQuestionSet.Version.ToString()}"); } } else { _logger.LogError("No Filtered Question sets available"); } _logger.LogInformation($"End poll for Filtered Question Sets"); return(questionSet); }