Skip to content

Commit

Permalink
Amend filter that checks a person exists to check TRS first (#1576)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Oct 14, 2024
1 parent e44b082 commit 485caa5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ namespace TeachingRecordSystem.SupportUi.Infrastructure.Filters;
public class CheckPersonExistsFilter(
TrsDbContext dbContext,
ICrmQueryDispatcher crmQueryDispatcher,
IBackgroundJobScheduler backgroundJobScheduler,
bool requireQts = false) : IAsyncResourceFilter
IBackgroundJobScheduler backgroundJobScheduler) : IAsyncResourceFilter
{
private readonly bool _requireQts = requireQts;

public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
{
var personIdParam = context.RouteData.Values["personId"] as string ?? context.HttpContext.Request.Query["personId"];
Expand All @@ -36,48 +33,52 @@ public async Task OnResourceExecutionAsync(ResourceExecutingContext context, Res
return;
}

var person = await crmQueryDispatcher.ExecuteQuery(
new GetActiveContactDetailByIdQuery(
personId,
new ColumnSet(
Contact.Fields.Id,
Contact.Fields.FirstName,
Contact.Fields.MiddleName,
Contact.Fields.LastName,
Contact.Fields.dfeta_StatedFirstName,
Contact.Fields.dfeta_StatedLastName,
Contact.Fields.dfeta_StatedMiddleName,
Contact.Fields.dfeta_QTSDate)));
var person = await dbContext.Persons.SingleOrDefaultAsync(p => p.PersonId == personId);

if (person is null)
if (person is not null)
{
context.Result = new NotFoundResult();
return;
context.HttpContext.SetCurrentPersonFeature(person);
}
else if (_requireQts && person.Contact.dfeta_QTSDate is null)
else
{
context.Result = new BadRequestResult();
return;
}
// If person isn't in the TRS DB it may be because we haven't synced it yet..

context.HttpContext.SetCurrentPersonFeature(person);
var dqtContact = await crmQueryDispatcher.ExecuteQuery(
new GetActiveContactDetailByIdQuery(
personId,
new ColumnSet(
Contact.Fields.Id,
Contact.Fields.FirstName,
Contact.Fields.MiddleName,
Contact.Fields.LastName,
Contact.Fields.dfeta_StatedFirstName,
Contact.Fields.dfeta_StatedLastName,
Contact.Fields.dfeta_StatedMiddleName,
Contact.Fields.dfeta_QTSDate)));

// Ensure we've synced this person into the TRS DB at least once
if (!await dbContext.Persons.AnyAsync(p => p.PersonId == personId))
{
await backgroundJobScheduler.Enqueue<TrsDataSyncHelper>(helper => helper.SyncPerson(personId, /*ignoreInvalid: */ false, /*dryRun:*/ false, CancellationToken.None));
if (dqtContact is not null)
{
context.HttpContext.SetCurrentPersonFeature(dqtContact);

await backgroundJobScheduler.Enqueue<TrsDataSyncHelper>(helper => helper.SyncPerson(personId, /*ignoreInvalid: */ false, /*dryRun:*/ false, CancellationToken.None));
}
else
{
context.Result = new NotFoundResult();
return;
}
}

await next();
}
}

public class CheckPersonExistsFilterFactory(bool requireQts = false) : IFilterFactory, IOrderedFilter
public class CheckPersonExistsFilterFactory : IFilterFactory, IOrderedFilter
{
public bool IsReusable => false;

public int Order => -200;

public IFilterMetadata CreateInstance(IServiceProvider serviceProvider) =>
ActivatorUtilities.CreateInstance<CheckPersonExistsFilter>(serviceProvider, requireQts);
ActivatorUtilities.CreateInstance<CheckPersonExistsFilter>(serviceProvider);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ public static CurrentPersonFeature GetCurrentPersonFeature(this HttpContext cont
public static void SetCurrentPersonFeature(this HttpContext context, CurrentPersonFeature currentPersonInfo) =>
context.Features.Set(currentPersonInfo);

public static void SetCurrentPersonFeature(this HttpContext context, Person person) =>
SetCurrentPersonFeature(
context,
new CurrentPersonFeature(
person.PersonId,
person.FirstName,
person.MiddleName,
person.LastName));

public static void SetCurrentPersonFeature(this HttpContext context, ContactDetail contactDetail) =>
SetCurrentPersonFeature(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ await testData.OrganizationService.ExecuteAsync(new UpdateRequest()
LastName = _updatedName.Value.LastName
}
});

await testData.SyncConfiguration.SyncIfEnabled(helper => helper.SyncPerson(_personId.Value, ignoreInvalid: false));
}
}
}
Expand Down

0 comments on commit 485caa5

Please sign in to comment.