public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]
            HttpRequest req,
            ILogger log,
            [Inject] IApprenticeshipMigrationReportService reportService)
        {
            using (var streamReader = new StreamReader(req.Body))
            {
                var requestBody = await streamReader.ReadToEndAsync();

                ApprenticeshipMigrationReport fromBody = null;

                try
                {
                    fromBody    = JsonConvert.DeserializeObject <ApprenticeshipMigrationReport>(requestBody);
                    fromBody.Id = fromBody.Id == Guid.Empty ? Guid.NewGuid() : fromBody.Id;
                }
                catch (Exception e)
                {
                    return(new BadRequestObjectResult(e));
                }

                try
                {
                    await reportService.CreateApprenticeshipReport(fromBody);
                }
                catch (Exception e)
                {
                    return(new InternalServerErrorObjectResult(e));
                }
            }

            return(new OkResult());
        }
Beispiel #2
0
        public async Task Run([CosmosDBTrigger(
                                   DatabaseName,
                                   CollectionName,
                                   ConnectionStringSetting = ConnectionString,
                                   LeaseCollectionName = LeaseCollectionName,
                                   LeaseCollectionPrefix = LeaseCollectionPrefix,
                                   CreateLeaseCollectionIfNotExists = true
                                   )]
                              IReadOnlyList <Document> documents,
                              ILogger log,
                              [Inject] ReportGenerationServiceResolver reportGenerationServiceResolver)
        {
            _reportGenerationService = reportGenerationServiceResolver(ProcessType.Apprenticeship);

            ApprenticeshipMigrationReport apprenticeshipMigrationReport = null;

            foreach (var document in documents)
            {
                try
                {
                    apprenticeshipMigrationReport = (ApprenticeshipMigrationReport)((dynamic)document);

                    await _reportGenerationService.UpdateReport(apprenticeshipMigrationReport.ProviderUKPRN);
                }
                catch (Exception e)
                {
                    log.LogError($"Unable to Process document with id:  {GetResourceId(apprenticeshipMigrationReport, document)} ");
                }
            }
        }
Beispiel #3
0
        public async Task <IResult> AddApprenticeshipMigrationReportAsync(ApprenticeshipMigrationReport report)
        {
            _logger.LogMethodEnter();
            Throw.IfNull(report, nameof(ApprenticeshipMigrationReport));

            try
            {
                _logger.LogInformationObject("ApprenticeshipReport add object.", report);
                _logger.LogInformationObject("ApprenticeshipReport add URI", _addCourseUri);

                var reportJson = JsonConvert.SerializeObject(report);

                var content  = new StringContent(reportJson, Encoding.UTF8, "application/json");
                var response = await _httpClient.PostAsync(_addReportUri, content);

                _logger.LogHttpResponseMessage("ApprenticeshipReport add service http response", response);

                if (response.IsSuccessStatusCode)
                {
                    var json = await response.Content.ReadAsStringAsync();

                    _logger.LogInformationObject("ApprenticeshipReport add service json response", json);
                    return(Result.Ok());
                }
                else if ((int)response.StatusCode == 429)
                {
                    return(Result.Fail <ApprenticeshipMigrationReport>(
                               "ApprenticeshipReport add service unsuccessful http response - TooManyRequests"));
                }
                else
                {
                    return(Result.Fail <ApprenticeshipMigrationReport>(
                               "ApprenticeshipReport add service unsuccessful http response - ResponseStatusCode: " +
                               response.StatusCode));
                }
            }
            catch (HttpRequestException hre)
            {
                _logger.LogException("ApprenticeshipReport add service http request error", hre);
                return(Result.Fail <ApprenticeshipMigrationReport>("Apprenticeship add service http request error."));
            }
            catch (Exception e)
            {
                _logger.LogException("ApprenticeshipReport add service unknown error.", e);

                return(Result.Fail <ApprenticeshipMigrationReport>("ApprenticeshipReport add service unknown error."));
            }
            finally
            {
                _logger.LogMethodExit();
            }
        }
        public async Task CreateApprenticeshipReport(ApprenticeshipMigrationReport report)
        {
            var client = _cosmosDbHelper.GetClient();
            await _cosmosDbHelper.CreateDatabaseIfNotExistsAsync(client);

            await _cosmosDbHelper.CreateDocumentCollectionIfNotExistsAsync(client,
                                                                           _settings.ApprenticeshipReportCollectionId);

            var result = _cosmosDbHelper.GetDocumentsByUKPRN <ApprenticeshipMigrationReport>(client, _settings.ApprenticeshipReportCollectionId,
                                                                                             report.ProviderUKPRN);

            if (result.Any())
            {
                report.Id = result.FirstOrDefault().Id;

                await _cosmosDbHelper.UpdateDocumentAsync(client, _settings.ApprenticeshipReportCollectionId,
                                                          report);
            }
            else
            {
                var doc = await _cosmosDbHelper.CreateDocumentAsync(client, _settings.ApprenticeshipReportCollectionId, report);
            }
        }
Beispiel #5
0
 private bool HasValidReportOrApprenticeships(IList <Apprenticeship> apprenticeships, ApprenticeshipMigrationReport report)
 {
     return(apprenticeships.Any() || report != null && report.ApprenticeshipsMigrated > 0);
 }
Beispiel #6
0
 private string GetResourceId(ApprenticeshipMigrationReport apprenticeshipMigrationReport, Document document)
 {
     return(apprenticeshipMigrationReport != null?apprenticeshipMigrationReport.ProviderUKPRN.ToString() : document.ResourceId);
 }