public async Task <Unit> Handle(UpdateProvidersCacheRequest request, CancellationToken cancellationToken) { try { switch (request.UpdateType) { case ProvidersCacheUpdateType.InsertProvidersFromApprovalsExtract: await _approvalsExtractRepository.InsertProvidersFromApprovalsExtract(); break; default: await _approvalsExtractRepository.RefreshProviders(); break; } } catch (Exception ex) { _logger.LogError(ex, "Providers cache failed to update successfully."); throw; } return(Unit.Value); }
public async Task When_ProviderNotExist_Then_ProviderInserted() { // Arrange _databaseService.Execute("TRUNCATE TABLE ApprovalsExtract;"); _databaseService.Execute("INSERT INTO ApprovalsExtract (ApprenticeshipId, FirstName, LastName, UKPRN) VALUES (123, 'TestFirstName', 'TestLastName', '10000528');"); _roatpApiClientMock.Setup(m => m.SearchOrganisationByUkprn(It.IsAny <int>())) .ReturnsAsync(new OrganisationSearchResult[] { new OrganisationSearchResult() { ProviderName = "Test Provider Name" } }); // Act await _repository.InsertProvidersFromApprovalsExtract(); // Assert IEnumerable <dynamic> providers; using (var connection = new SqlConnection(_databaseConnection.ConnectionString)) { providers = await connection.QueryAsync("SELECT * FROM Providers;"); } providers.Should().NotBeNull(); providers.Should().HaveCount(1); long ukprn = providers.ElementAt(0).Ukprn; ukprn.Should().Be(10000528); string name = providers.ElementAt(0).Name; name.Should().Be("Test Provider Name"); }
public async Task <Unit> Handle(ImportApprovalsRequest request, CancellationToken cancellationToken) { try { _logger.LogInformation("Commencing approvals import."); // 1. Figure out extract start time. DateTime?extractStartTime = null; _logger.LogInformation($"Calculating Approvals changed date, initially set to null."); DateTime?latestApprovalsExtractTimestamp = await _approvalsExtractRepository.GetLatestExtractTimestamp(); if (null != latestApprovalsExtractTimestamp && latestApprovalsExtractTimestamp.HasValue) { extractStartTime = latestApprovalsExtractTimestamp.Value.AddSeconds(-await GetSettingAsInt(TOLERANCE_SETTING_NAME)); _logger.LogInformation($"Pulling Approvals changed since: {extractStartTime}"); } // 2. Request the extract in batches. int batchSize = await GetSettingAsInt(BATCHSIZE_SETTING_NAME); int batchNumber = 0; int count = 0; GetAllLearnersResponse learnersBatch = null; // 3. Reset Staging Table await _approvalsExtractRepository.ClearApprovalsExtractStaging(); do { batchNumber++; learnersBatch = await _outerApiService.GetAllLearners(extractStartTime, batchNumber, batchSize); if (null == learnersBatch || null == learnersBatch.Learners) { throw new Exception($"Failed to get learners batch: sinceTime={extractStartTime?.ToString("o", System.Globalization.CultureInfo.InvariantCulture)} batchNumber={batchNumber} batchSize={batchSize}"); } // 4. Upsert Batch to ApprovalsExtract_Staging. _logger.LogInformation($"Approvals batch import loop. Starting batch {batchNumber} of {learnersBatch.TotalNumberOfBatches}"); await UpsertApprovalsExtractToStaging(learnersBatch.Learners); count += learnersBatch.Learners.Count; _logger.LogInformation($"Approvals batch import loop. Batch Completed {batchNumber} of {learnersBatch.TotalNumberOfBatches}. Total Inserted: {count}"); } while (batchNumber < learnersBatch.TotalNumberOfBatches); // 5. Run Populate ApprovalsExtract From Staging _logger.LogInformation($"Begin Populating Approvals Extract"); await _approvalsExtractRepository.PopulateApprovalsExtract(); _logger.LogInformation($"Finished Populating Approvals Extract"); // 6. Run Populate Learner _logger.LogInformation($"Begin Running Populate Learner"); var learnerCount = await _approvalsExtractRepository.PopulateLearner(); _logger.LogInformation($"Finished Running Populate Learner"); // 7. Update providers cache await _approvalsExtractRepository.InsertProvidersFromApprovalsExtract(); _logger.LogInformation($"Approvals import completed successfully. {count} record(s) read from outer api, {learnerCount} records inserted to Learner table."); } catch (Exception ex) { _logger.LogError(ex, "Approvals import failed to complete successfully."); throw; } return(Unit.Value); }