Skip to content

Commit

Permalink
Add a Migrated event for MQ records (#1100)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Jan 23, 2024
1 parent 3bfad55 commit 83ec89a
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 109 deletions.
1 change: 0 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ jobs:
dotnet_test_args: >-
--no-build
-e ConnectionStrings__DefaultConnection="Host=localhost;Username=postgres;Password=trs;Database=trs"
--verbosity diag
timeout-minutes: 5

- name: API Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public static async Task<MandatoryQualification> MapFromDqtQualification(dfeta_q
var mqEstablishments = await referenceDataCache.GetMqEstablishments();
var mqSpecialisms = await referenceDataCache.GetMqSpecialisms();

return TrsDataSyncHelper.MapMandatoryQualificationFromDqtQualification(qualification, mqEstablishments, mqSpecialisms);
return TrsDataSyncHelper.MapMandatoryQualificationFromDqtQualification(qualification, mqEstablishments, mqSpecialisms, applyMigrationMappings: true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using TeachingRecordSystem.Core.Events.Models;

namespace TeachingRecordSystem.Core.Events;

public record MandatoryQualificationMigratedEvent : EventBase, IEventWithPersonId, IEventWithMandatoryQualification, IEventWithKey
{
public string? Key { get; init; }
public required Guid PersonId { get; init; }
public required MandatoryQualification MandatoryQualification { get; init; }
public required MandatoryQualificationMigratedEventChanges Changes { get; init; }
}

public enum MandatoryQualificationMigratedEventChanges
{
None = 0,
Provider = 1 << 0,
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public async Task Execute(CancellationToken cancellationToken)
await _trsDataSyncHelper.SyncMandatoryQualifications(
result.Entities.Select(e => e.ToEntity<dfeta_qualification>()).ToArray(),
ignoreInvalid: _syncOptionsAccessor.Value.IgnoreInvalidData,
createdMigratedEvent: false,
cancellationToken);

if (result.MoreRecords)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,20 @@ public static (string EntityLogicalName, string[] AttributeNames) GetEntityInfoF
public static MandatoryQualification MapMandatoryQualificationFromDqtQualification(
dfeta_qualification qualification,
IEnumerable<dfeta_mqestablishment> mqEstablishments,
IEnumerable<dfeta_specialism> mqSpecialisms)
IEnumerable<dfeta_specialism> mqSpecialisms,
bool applyMigrationMappings)
{
if (qualification.dfeta_Type != dfeta_qualification_dfeta_Type.MandatoryQualification)
{
throw new ArgumentException("Qualification is not a mandatory qualification.", nameof(qualification));
}

MandatoryQualificationProvider.TryMapFromDqtMqEstablishment(
mqEstablishments.SingleOrDefault(e => e.Id == qualification.dfeta_MQ_MQEstablishmentId!?.Id), out var provider);
MandatoryQualificationProvider? provider = null;
if (applyMigrationMappings)
{
MandatoryQualificationProvider.TryMapFromDqtMqEstablishment(
mqEstablishments.SingleOrDefault(e => e.Id == qualification.dfeta_MQ_MQEstablishmentId!?.Id), out provider);
}

MandatoryQualificationSpecialism? specialism = qualification.dfeta_MQ_SpecialismId is not null ?
mqSpecialisms.Single(s => s.Id == qualification.dfeta_MQ_SpecialismId.Id).ToMandatoryQualificationSpecialism() :
Expand Down Expand Up @@ -261,50 +266,53 @@ public async Task<bool> SyncMandatoryQualification(Guid qualificationId, IReadOn
var mqEstablishments = await referenceDataCache.GetMqEstablishments();
var mqSpecialisms = await referenceDataCache.GetMqSpecialisms();

var mqs = qualifications.Select(q => MapMandatoryQualificationFromDqtQualification(q, mqEstablishments, mqSpecialisms)).ToArray();
var mqs = qualifications.Select(q => MapMandatoryQualificationFromDqtQualification(q, mqEstablishments, mqSpecialisms, applyMigrationMappings: false)).ToArray();

return await SyncMandatoryQualifications(mqs, events, ignoreInvalid: false, cancellationToken) == 1;
}
else
{
return await SyncMandatoryQualifications(qualifications, ignoreInvalid: false, cancellationToken) == 1;
return await SyncMandatoryQualifications(qualifications, ignoreInvalid: false, createdMigratedEvent: false, cancellationToken) == 1;
}
}

public async Task<bool> SyncMandatoryQualification(
dfeta_qualification entity,
AuditDetailCollection auditDetails,
bool ignoreInvalid,
bool createdMigratedEvent,
CancellationToken cancellationToken = default)
{
var auditDetailsDict = new Dictionary<Guid, AuditDetailCollection>()
{
{ entity.Id, auditDetails }
};

return await SyncMandatoryQualifications(new[] { entity }, auditDetailsDict, ignoreInvalid, cancellationToken) == 1;
return await SyncMandatoryQualifications(new[] { entity }, auditDetailsDict, ignoreInvalid, createdMigratedEvent, cancellationToken) == 1;
}

public async Task<int> SyncMandatoryQualifications(
IReadOnlyCollection<dfeta_qualification> entities,
bool ignoreInvalid,
bool createdMigratedEvent,
CancellationToken cancellationToken)
{
var auditDetails = await GetAuditRecords(dfeta_qualification.EntityLogicalName, entities.Select(q => q.Id), cancellationToken);

return await SyncMandatoryQualifications(entities, auditDetails, ignoreInvalid, cancellationToken);
return await SyncMandatoryQualifications(entities, auditDetails, ignoreInvalid, createdMigratedEvent, cancellationToken);
}

public async Task<int> SyncMandatoryQualifications(
IReadOnlyCollection<dfeta_qualification> entities,
IReadOnlyDictionary<Guid, AuditDetailCollection> auditDetails,
bool ignoreInvalid,
bool createdMigratedEvent,
CancellationToken cancellationToken = default)
{
// Not all dfeta_qualification records are MQs..
var toSync = entities.Where(q => q.dfeta_Type == dfeta_qualification_dfeta_Type.MandatoryQualification);

var (mqs, events) = await MapMandatoryQualificationsAndAudits(toSync, auditDetails);
var (mqs, events) = await MapMandatoryQualificationsAndAudits(toSync, auditDetails, createdMigratedEvent);

return await SyncMandatoryQualifications(mqs, events, ignoreInvalid, cancellationToken);
}
Expand Down Expand Up @@ -816,7 +824,7 @@ WHERE t.dqt_modified_on < EXCLUDED.dqt_modified_on
EntityLogicalName = dfeta_qualification.EntityLogicalName,
AttributeNames = attributeNames,
GetSyncHandler = helper => (entities, ignoreInvalid, ct) =>
helper.SyncMandatoryQualifications(entities.Select(e => e.ToEntity<dfeta_qualification>()).ToArray(), ignoreInvalid, ct),
helper.SyncMandatoryQualifications(entities.Select(e => e.ToEntity<dfeta_qualification>()).ToArray(), ignoreInvalid, createdMigratedEvent: false, ct),
WriteRecord = writeRecord
};
}
Expand Down Expand Up @@ -844,7 +852,8 @@ private static List<Person> MapPersons(IEnumerable<Contact> contacts) => contact

private async Task<(List<MandatoryQualification> MandatoryQualifications, List<EventBase> Events)> MapMandatoryQualificationsAndAudits(
IEnumerable<dfeta_qualification> qualifications,
IReadOnlyDictionary<Guid, AuditDetailCollection> auditDetails)
IReadOnlyDictionary<Guid, AuditDetailCollection> auditDetails,
bool createMigratedEvent)
{
var mqEstablishments = await referenceDataCache.GetMqEstablishments();
var mqSpecialisms = await referenceDataCache.GetMqSpecialisms();
Expand All @@ -854,7 +863,7 @@ private static List<Person> MapPersons(IEnumerable<Contact> contacts) => contact

foreach (var q in qualifications)
{
var mapped = MapMandatoryQualificationFromDqtQualification(q, mqEstablishments, mqSpecialisms);
var mapped = MapMandatoryQualificationFromDqtQualification(q, mqEstablishments, mqSpecialisms, applyMigrationMappings: true);

var audits = auditDetails[q.Id].AuditDetails;
var versions = GetEntityVersions(q, audits, GetModelTypeSyncInfoForMandatoryQualification().AttributeNames);
Expand Down Expand Up @@ -892,6 +901,10 @@ private static List<Person> MapPersons(IEnumerable<Contact> contacts) => contact
$" (qualification ID: '{q.Id}').");
}
}
else if (createMigratedEvent)
{
events.Add(MapMigratedEvent(versions.Last()));
}

mqs.Add(mapped);
}
Expand All @@ -918,7 +931,7 @@ EventBase MapCreatedEvent(EntityVersionInfo<dfeta_qualification> snapshot)
CreatedUtc = snapshot.Timestamp,
RaisedBy = Events.Models.RaisedByUserInfo.FromDqtUser(snapshot.UserId, snapshot.UserName),
PersonId = snapshot.Entity.dfeta_PersonId.Id,
MandatoryQualification = GetEventMandatoryQualification(snapshot.Entity)
MandatoryQualification = GetEventMandatoryQualification(snapshot.Entity, applyMigrationMappings: false)
};
}

Expand All @@ -931,7 +944,7 @@ EventBase MapImportedEvent(EntityVersionInfo<dfeta_qualification> snapshot)
CreatedUtc = snapshot.Timestamp,
RaisedBy = Events.Models.RaisedByUserInfo.FromDqtUser(snapshot.UserId, snapshot.UserName),
PersonId = snapshot.Entity.dfeta_PersonId.Id,
MandatoryQualification = GetEventMandatoryQualification(snapshot.Entity),
MandatoryQualification = GetEventMandatoryQualification(snapshot.Entity, applyMigrationMappings: false),
DqtState = (int)snapshot.Entity.StateCode!
};
}
Expand Down Expand Up @@ -975,7 +988,7 @@ EventBase MapImportedEvent(EntityVersionInfo<dfeta_qualification> snapshot)
CreatedUtc = snapshot.Timestamp,
RaisedBy = Events.Models.RaisedByUserInfo.FromDqtUser(snapshot.UserId, snapshot.UserName),
PersonId = snapshot.Entity.dfeta_PersonId.Id,
MandatoryQualification = GetEventMandatoryQualification(snapshot.Entity)
MandatoryQualification = GetEventMandatoryQualification(snapshot.Entity, applyMigrationMappings: false)
};
}
else
Expand All @@ -987,7 +1000,7 @@ EventBase MapImportedEvent(EntityVersionInfo<dfeta_qualification> snapshot)
CreatedUtc = snapshot.Timestamp,
RaisedBy = Events.Models.RaisedByUserInfo.FromDqtUser(snapshot.UserId, snapshot.UserName),
PersonId = snapshot.Entity.dfeta_PersonId.Id,
MandatoryQualification = GetEventMandatoryQualification(snapshot.Entity)
MandatoryQualification = GetEventMandatoryQualification(snapshot.Entity, applyMigrationMappings: false)
};
}
}
Expand All @@ -1011,31 +1024,52 @@ EventBase MapImportedEvent(EntityVersionInfo<dfeta_qualification> snapshot)
CreatedUtc = snapshot.Timestamp,
RaisedBy = Events.Models.RaisedByUserInfo.FromDqtUser(snapshot.UserId, snapshot.UserName),
PersonId = snapshot.Entity.dfeta_PersonId.Id,
MandatoryQualification = GetEventMandatoryQualification(snapshot.Entity),
OldMandatoryQualification = GetEventMandatoryQualification(previous.Entity),
MandatoryQualification = GetEventMandatoryQualification(snapshot.Entity, applyMigrationMappings: false),
OldMandatoryQualification = GetEventMandatoryQualification(previous.Entity, applyMigrationMappings: false),
ChangeReason = null,
ChangeReasonDetail = null,
EvidenceFile = null,
Changes = changes
};
}

Events.Models.MandatoryQualification GetEventMandatoryQualification(dfeta_qualification snapshot)
EventBase MapMigratedEvent(EntityVersionInfo<dfeta_qualification> snapshot)
{
var eventMandatoryQualification = GetEventMandatoryQualification(snapshot.Entity, applyMigrationMappings: true);

var changes = MandatoryQualificationMigratedEventChanges.None |
(eventMandatoryQualification.Provider?.Name != eventMandatoryQualification.Provider?.DqtMqEstablishmentName ? MandatoryQualificationMigratedEventChanges.Provider : 0);

return new MandatoryQualificationMigratedEvent()
{
EventId = Guid.NewGuid(),
Key = $"{snapshot.Entity.Id}-Migrated",
CreatedUtc = clock.UtcNow,
RaisedBy = Events.Models.RaisedByUserInfo.FromUserId(Core.DataStore.Postgres.Models.SystemUser.SystemUserId),
PersonId = snapshot.Entity.dfeta_PersonId.Id,
MandatoryQualification = eventMandatoryQualification,
Changes = changes
};
}

Events.Models.MandatoryQualification GetEventMandatoryQualification(dfeta_qualification snapshot, bool applyMigrationMappings)
{
var mapped = MapMandatoryQualificationFromDqtQualification(snapshot, mqEstablishments, mqSpecialisms);
var mapped = MapMandatoryQualificationFromDqtQualification(snapshot, mqEstablishments, mqSpecialisms, applyMigrationMappings);

var establishment = snapshot.dfeta_MQ_MQEstablishmentId?.Id is Guid establishmentId ?
mqEstablishments.Single(e => e.Id == establishmentId) :
null;

var provider = MandatoryQualificationProvider.All.SingleOrDefault(p => p.MandatoryQualificationProviderId == mapped.ProviderId);

return new()
{
QualificationId = mapped.QualificationId,
Provider = establishment is not null ?
new()
{
MandatoryQualificationProviderId = null,
Name = null,
MandatoryQualificationProviderId = provider?.MandatoryQualificationProviderId,
Name = provider?.Name,
DqtMqEstablishmentId = establishment?.Id,
DqtMqEstablishmentName = establishment?.dfeta_name
} :
Expand Down
Loading

0 comments on commit 83ec89a

Please sign in to comment.