Skip to content

Commit

Permalink
Add support for creating alerts against a person in the TRS database …
Browse files Browse the repository at this point in the history
…to use in tests (#1501)
  • Loading branch information
hortha authored Sep 6, 2024
1 parent 8deb7b0 commit e503f9f
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Services.TrsDataSync;

namespace TeachingRecordSystem.Core.DataStore.Postgres.Models;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using TeachingRecordSystem.Core.Events.Models;

namespace TeachingRecordSystem.Core.Events;

public record class AlertCreatedEvent : EventBase, IEventWithPersonId, IEventWithAlert
{
public required Guid PersonId { get; init; }
public required Alert Alert { get; init; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using TeachingRecordSystem.Core.Events.Models;

namespace TeachingRecordSystem.Core.Events;

public interface IEventWithAlert
{
Alert Alert { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace TeachingRecordSystem.Core.Events.Models;

public record Alert
{
public required Guid AlertId { get; init; }
public required Guid AlertTypeId { get; init; }
public required string? Details { get; init; }
public required string? ExternalLink { get; init; }
public required DateOnly? StartDate { get; init; }
public required DateOnly? EndDate { get; init; }

public static Alert FromModel(DataStore.Postgres.Models.Alert model) => new()
{
AlertId = model.AlertId,
AlertTypeId = model.AlertTypeId,
Details = model.Details,
ExternalLink = model.ExternalLink,
StartDate = model.StartDate,
EndDate = model.EndDate
};
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using TeachingRecordSystem.Core.DataStore.Postgres;
using TeachingRecordSystem.Core.DataStore.Postgres.Models;
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Dqt.Queries;

namespace TeachingRecordSystem.Core.Dqt;
namespace TeachingRecordSystem.Core;

public class ReferenceDataCache : IStartupTask
public class ReferenceDataCache(
ICrmQueryDispatcher crmQueryDispatcher,
IDbContextFactory<TrsDbContext> dbContextFactory) : IStartupTask
{
private readonly ICrmQueryDispatcher _crmQueryDispatcher;
// CRM
private Task<dfeta_mqestablishment[]>? _mqEstablishmentsTask;
private Task<dfeta_sanctioncode[]>? _getSanctionCodesTask;
private Task<Subject[]>? _getSubjectsTask;
Expand All @@ -14,10 +19,9 @@ public class ReferenceDataCache : IStartupTask
private Task<dfeta_hequalification[]>? _getHeQualificationsTask;
private Task<dfeta_hesubject[]>? _getHeSubjectsTask;

public ReferenceDataCache(ICrmQueryDispatcher crmQueryDispatcher)
{
_crmQueryDispatcher = crmQueryDispatcher;
}
// TRS
private Task<AlertCategory[]>? _alertCategoriesTask;
private Task<AlertType[]>? _alertTypesTask;

public async Task<dfeta_sanctioncode> GetSanctionCodeByValue(string value)
{
Expand Down Expand Up @@ -144,48 +148,91 @@ public async Task<dfeta_hesubject> GetHeSubjectByValue(string value)
return heSubjects.First(s => s.dfeta_Value == value, $"Could not find HE subject with value: '{value}'.");
}

public async Task<AlertCategory[]> GetAlertCategories()
{
var alertCategories = await EnsureAlertCategories();
return alertCategories;
}

public async Task<AlertCategory> GetAlertCategoryById(Guid alertCategoryId)
{
var alertCategories = await EnsureAlertCategories();
return alertCategories.Single(ac => ac.AlertCategoryId == alertCategoryId, $"Could not find alert category with ID: '{alertCategoryId}'.");
}

public async Task<AlertType> GetAlertTypeById(Guid alertTypeId)
{
var alertTypes = await EnsureAlertTypes();
return alertTypes.Single(at => at.AlertTypeId == alertTypeId, $"Could not find alert type with ID: '{alertTypeId}'.");
}

public async Task<AlertType[]> GetAlertTypes()
{
var alertTypes = await EnsureAlertTypes();
return alertTypes;
}

private Task<dfeta_sanctioncode[]> EnsureSanctionCodes() =>
LazyInitializer.EnsureInitialized(
ref _getSanctionCodesTask,
() => _crmQueryDispatcher.ExecuteQuery(new GetAllActiveSanctionCodesQuery()));
() => crmQueryDispatcher.ExecuteQuery(new GetAllActiveSanctionCodesQuery()));

private Task<Subject[]> EnsureSubjects() =>
LazyInitializer.EnsureInitialized(
ref _getSubjectsTask,
() => _crmQueryDispatcher.ExecuteQuery(new GetAllSubjectsQuery()));
() => crmQueryDispatcher.ExecuteQuery(new GetAllSubjectsQuery()));

private Task<dfeta_teacherstatus[]> EnsureTeacherStatuses() =>
LazyInitializer.EnsureInitialized(
ref _getTeacherStatusesTask,
() => _crmQueryDispatcher.ExecuteQuery(new GetAllTeacherStatusesQuery()));
() => crmQueryDispatcher.ExecuteQuery(new GetAllTeacherStatusesQuery()));

private Task<dfeta_earlyyearsstatus[]> EnsureEarlyYearsStatuses() =>
LazyInitializer.EnsureInitialized(
ref _getEarlyYearsStatusesTask,
() => _crmQueryDispatcher.ExecuteQuery(new GetAllActiveEarlyYearsStatusesQuery()));
() => crmQueryDispatcher.ExecuteQuery(new GetAllActiveEarlyYearsStatusesQuery()));

private Task<dfeta_specialism[]> EnsureSpecialisms() =>
LazyInitializer.EnsureInitialized(
ref _getSpecialismsTask,
() => _crmQueryDispatcher.ExecuteQuery(new GetAllSpecialismsQuery()));
() => crmQueryDispatcher.ExecuteQuery(new GetAllSpecialismsQuery()));

private Task<dfeta_mqestablishment[]> EnsureMqEstablishments() =>
LazyInitializer.EnsureInitialized(
ref _mqEstablishmentsTask,
() => _crmQueryDispatcher.ExecuteQuery(new GetAllMqEstablishmentsQuery()));
() => crmQueryDispatcher.ExecuteQuery(new GetAllMqEstablishmentsQuery()));

private Task<dfeta_hequalification[]> EnsureHeQualifications() =>
LazyInitializer.EnsureInitialized(
ref _getHeQualificationsTask,
() => _crmQueryDispatcher.ExecuteQuery(new GetAllActiveHeQualificationsQuery()));
() => crmQueryDispatcher.ExecuteQuery(new GetAllActiveHeQualificationsQuery()));

private Task<dfeta_hesubject[]> EnsureHeSubjects() =>
LazyInitializer.EnsureInitialized(
ref _getHeSubjectsTask,
() => _crmQueryDispatcher.ExecuteQuery(new GetAllActiveHeSubjectsQuery()));
() => crmQueryDispatcher.ExecuteQuery(new GetAllActiveHeSubjectsQuery()));

private Task<AlertCategory[]> EnsureAlertCategories() =>
LazyInitializer.EnsureInitialized(
ref _alertCategoriesTask,
() =>
{
using var dbContext = dbContextFactory.CreateDbContext();
return dbContext.AlertCategories.ToArrayAsync();
});

private Task<AlertType[]> EnsureAlertTypes() =>
LazyInitializer.EnsureInitialized(
ref _alertTypesTask,
() =>
{
using var dbContext = dbContextFactory.CreateDbContext();
return dbContext.AlertTypes.ToArrayAsync();
});

async Task IStartupTask.Execute()
{
// CRM
await EnsureSanctionCodes();
await EnsureSubjects();
await EnsureTeacherStatuses();
Expand All @@ -194,5 +241,9 @@ async Task IStartupTask.Execute()
await EnsureMqEstablishments();
await EnsureHeQualifications();
await EnsureHeSubjects();

// TRS
await EnsureAlertCategories();
await EnsureAlertTypes();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public CrmClientFixture(ServiceClient serviceClient, DbFixture dbFixture, IConfi
_memoryCache = memoryCache;
_trnGenerationApiClient = GetTrnGenerationApiClient();
_referenceDataCache = new ReferenceDataCache(
new CrmQueryDispatcher(CreateQueryServiceProvider(_baseServiceClient, referenceDataCache: null),
serviceClientName: null));
new CrmQueryDispatcher(CreateQueryServiceProvider(_baseServiceClient, referenceDataCache: null), serviceClientName: null),
dbFixture.GetDbContextFactory());
}

public IClock Clock { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.PowerPlatform.Dataverse.Client;
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Services.Establishments.Gias;
using TeachingRecordSystem.Core.Services.TrsDataSync;
using Establishment = TeachingRecordSystem.Core.Models.Establishment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Text;
using Microsoft.PowerPlatform.Dataverse.Client;
using TeachingRecordSystem.Core.DataStore.Postgres.Models;
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Services.Establishments.Tps;
using TeachingRecordSystem.Core.Services.TrsDataSync;
using TeachingRecordSystem.Core.Services.WorkforceData;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.PowerPlatform.Dataverse.Client;
using TeachingRecordSystem.Core.DataStore.Postgres.Models;
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Services.PersonMatching;
using TeachingRecordSystem.Core.Services.TrsDataSync;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.PowerPlatform.Dataverse.Client;
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Services.TrsDataSync;

namespace TeachingRecordSystem.Core.Tests.Services.TrsDataSync;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Extensions.Options;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Services.TrsDataSync;

namespace TeachingRecordSystem.Core.Tests.Services.TrsDataSync;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.PowerPlatform.Dataverse.Client;
using TeachingRecordSystem.Core.DataStore.Postgres.Models;
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Services.TrsDataSync;
using TeachingRecordSystem.Core.Services.WorkforceData;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.Extensions.Options;
using Microsoft.PowerPlatform.Dataverse.Client;
using Parquet.Serialization;
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Services.TrsDataSync;
using TeachingRecordSystem.Core.Services.WorkforceData;
using TeachingRecordSystem.Core.Services.WorkforceData.Google;
Expand Down
Loading

0 comments on commit e503f9f

Please sign in to comment.