Skip to content

Commit

Permalink
Merge pull request #336 from DFE-Digital/122999-sharepoint-path-for-s…
Browse files Browse the repository at this point in the history
…chool-financial-docs

Added quartz.net scheduler for running a one time job to fix sharepoi…
  • Loading branch information
paullocknimble authored Mar 10, 2023
2 parents 607f6a2 + 72d9fb1 commit 9893d96
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />
<PackageReference Include="Quartz.AspNetCore" Version="3.6.2" />
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Dfe.Academies.External.Web.Dtos;

public record ApplicationSchoolSharepointServiceModel(
int Id,
string ApplicationReference,
List<SchoolSharepointServiceModel> SchoolSharepointServiceModels);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Dfe.Academies.External.Web.Dtos;

public record SchoolSharepointServiceModel(int Id, string Name, Guid EntityId);
40 changes: 40 additions & 0 deletions Dfe.Academies.External.Web/Jobs/FixSharepointFoldersJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Dfe.Academies.External.Web.Exceptions;
using Dfe.Academies.External.Web.Services;
using Quartz;

namespace Dfe.Academies.External.Web.Jobs;

public class FixSharepointFoldersJob : IJob
{
private readonly IConversionApplicationRetrievalService _conversionApplicationRetrievalService;
private readonly IFileUploadService _fileUploadService;
private readonly ILogger<FixSharepointFoldersJob> _logger;
public FixSharepointFoldersJob(IConversionApplicationRetrievalService conversionApplicationRetrievalService, IFileUploadService fileUploadService, ILogger<FixSharepointFoldersJob> logger)
{
_conversionApplicationRetrievalService = conversionApplicationRetrievalService;
_fileUploadService = fileUploadService;
_logger = logger;
}

public async Task Execute(IJobExecutionContext context)
{
var sharepointApplicationServiceModels = await _conversionApplicationRetrievalService.GetAllApplications();
foreach (var applicationSchoolSharepointServiceModel in sharepointApplicationServiceModels)
{
_logger.LogInformation($"Application sharepoint model: {applicationSchoolSharepointServiceModel.ApplicationReference}");
foreach (var schoolSharepointServiceModel in applicationSchoolSharepointServiceModel.SchoolSharepointServiceModels)
{
_logger.LogInformation($"Fixing folder structure for application: {applicationSchoolSharepointServiceModel.ApplicationReference} with school: {schoolSharepointServiceModel.Name} :: ${schoolSharepointServiceModel.EntityId}");
try
{
await _fileUploadService.FixApplyingSchool(applicationSchoolSharepointServiceModel.ApplicationReference, schoolSharepointServiceModel.EntityId.ToString());
}
catch (FileUploadException ex)
{
_logger.LogError($"Could not fix folder structure for application: {applicationSchoolSharepointServiceModel.ApplicationReference} with school: {schoolSharepointServiceModel.Name} :: ${schoolSharepointServiceModel.EntityId}");
_logger.LogDebug($"{ex.StackTrace}");
}
}
}
}
}
21 changes: 21 additions & 0 deletions Dfe.Academies.External.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Dfe.Academies.External.Web.Extensions;
using Dfe.Academies.External.Web.Factories;
using Dfe.Academies.External.Web.Helpers;
using Dfe.Academies.External.Web.Jobs;
using Dfe.Academies.External.Web.Middleware;
using Dfe.Academies.External.Web.Models.EmailTemplates;
using Dfe.Academies.External.Web.Routing;
Expand All @@ -20,6 +21,7 @@
using Notify.Interfaces;
using Polly;
using Polly.Extensions.Http;
using Quartz;

//using Serilog;
//using Serilog.Events;
Expand Down Expand Up @@ -194,8 +196,27 @@ static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
builder.Services.AddDataProtection()
.PersistKeysToAzureBlobStorage(blobClient);
}


builder.Services.AddQuartz(q => { q.UseMicrosoftDependencyInjectionJobFactory(); });
builder.Services.AddQuartzHostedService(opt => { opt.WaitForJobsToComplete = true; });
var app = builder.Build();

var schedulerFactory = app.Services.GetRequiredService<ISchedulerFactory>();
var scheduler = await schedulerFactory.GetScheduler();

var job = JobBuilder.Create<FixSharepointFoldersJob>()
.WithIdentity("fix-sharepoint")
.Build();

var trigger = TriggerBuilder.Create()
.WithIdentity("fix-sharepoint")
.StartNow()
.Build();


await scheduler.ScheduleJob(job, trigger);

// Configure the HTTP request pipeline.

if (!app.Environment.IsDevelopment())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,36 @@ public async Task<List<ConversionApplicationContributor>> GetConversionApplicati
}
}

public async Task<List<ApplicationSchoolSharepointServiceModel>> GetAllApplications()
{
try
{
// baseaddress has a backslash at the end to be a valid URI !!!
// https://academies-academisation-api-dev.azurewebsites.net/application/99
// endpoint will return 404 if id NOT found !
string apiurl = $"{_httpClient.BaseAddress}application/all?api-version=V1";

JsonSerializerOptions options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Converters = {
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)
},
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

// Get data from Academisation API
var sharepointServiceModels = await _resilientRequestProvider.GetAsync<List<ApplicationSchoolSharepointServiceModel>>(apiurl, options);

return sharepointServiceModels;
}
catch (Exception ex)
{
_logger.LogError("ConversionApplicationRetrievalService::GetAllApplications::Exception - {Message}", ex.Message);
return new ();
}
}

/// <summary>
/// About the conversion = 4 sections - so could return 'In Progress' or Completed or NotStarted
/// Same logic in here as summary page. Should we re-factor?
Expand Down
9 changes: 8 additions & 1 deletion Dfe.Academies.External.Web/Services/FileUploadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface IFileUploadService
Task<List<string>> GetFiles(string entityName, string recordId, string recordName, string fieldName);
Task<string> UploadFile(string entity, string recordId, string recordName, string fieldName, IFormFile file);
Task DeleteFile(string entityName, string recordId, string recordName, string fieldName, string fileName);
Task FixApplyingSchool(string appReference, string schoolEntityId);
}

public class FileUploadService : IFileUploadService
Expand All @@ -30,6 +31,12 @@ public FileUploadService(HttpClient httpClient, IAadAuthorisationHelper aadAutho
_aadAuthorisationHelper = aadAuthorisationHelper;
}

public async Task FixApplyingSchool(string appReference, string schoolEntityId)
{
var url = $"{_httpClient.BaseAddress}/utils/fix-applying-school?appReference={appReference}&applyingSchoolId={schoolEntityId}";
using var request = new HttpRequestMessage(HttpMethod.Put, url);
await DoHttpRequest(request);
}
public async Task<List<string>> GetFiles(string entityName, string recordId, string recordName, string fieldName)
{
var url = $"?entityName={entityName}&recordName={recordName}&recordId={recordId}&fieldName={fieldName}";
Expand Down Expand Up @@ -81,7 +88,7 @@ private async Task<string> DoHttpRequest(HttpRequestMessage request)
var response = await _httpClient.SendAsync(request);

if (!response.IsSuccessStatusCode)
throw new FileUploadException($"The file upload service failed with a status of {response.ReasonPhrase}");
throw new FileUploadException($"The file service failed with a status of {response.ReasonPhrase}");

var receiveStream = await response.Content.ReadAsStreamAsync();
using var readStream = new StreamReader(receiveStream, Encoding.UTF8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public interface IConversionApplicationRetrievalService
/// <returns></returns>
Task<ConversionApplication?> GetApplication(int applicationId);

Task<List<ApplicationSchoolSharepointServiceModel>> GetAllApplications();

/// <summary>
/// Calculate whether all trust sections of application have been filled in.
/// Could return InProgress or Completed or NotStarted because inner method will return that
Expand Down

0 comments on commit 9893d96

Please sign in to comment.