Skip to content

Commit

Permalink
Batch MQ and Alert creation in TestData (#1551)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Oct 3, 2024
1 parent 5f8df7a commit 125c052
Showing 1 changed file with 136 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Xrm.Sdk.Messages;
using Optional;
using Optional.Unsafe;
using TeachingRecordSystem.Core.DataStore.Postgres;
using TeachingRecordSystem.Core.DataStore.Postgres.Models;
using TeachingRecordSystem.Core.Dqt;
using TeachingRecordSystem.Core.Dqt.Models;
Expand Down Expand Up @@ -593,8 +594,47 @@ await testData.SyncConfiguration.SyncIfEnabled(
helper => helper.SyncPerson(contact, ignoreInvalid: false),
_syncEnabledOverride);

var mqs = await Task.WhenAll(_mqBuilders.Select(mqb => mqb.Execute(this, testData)));
var alerts = await Task.WhenAll(_alertBuilders.Select(ab => ab.Execute(this, testData)));
var (mqs, alerts) = await testData.WithDbContext(async dbContext =>
{
return (
await AddMqs(),
await AddAlerts());

async Task<MandatoryQualification[]> AddMqs()
{
var mqIds = new List<Guid>();
foreach (var builder in _mqBuilders)
{
mqIds.Add(await builder.Execute(this, testData, dbContext));
}
await dbContext.SaveChangesAsync();

var mqs = await dbContext.MandatoryQualifications
.Where(mq => mq.PersonId == PersonId)
.Include(mq => mq.Provider)
.ToDictionaryAsync(mq => mq.QualificationId, mq => mq);

return mqIds.Select(id => mqs[id]).ToArray();
}

async Task<Alert[]> AddAlerts()
{
var alertIds = new List<Guid>();
foreach (var builder in _alertBuilders)
{
alertIds.Add(await builder.Execute(this, testData, dbContext));
}
await dbContext.SaveChangesAsync();

var alerts = await dbContext.Alerts
.Where(a => a.PersonId == PersonId)
.Include(a => a.AlertType)
.ThenInclude(at => at.AlertCategory)
.ToDictionaryAsync(a => a.AlertId, a => a);

return alertIds.Select(id => alerts[id]).ToArray();
}
});

return new CreatePersonResult()
{
Expand Down Expand Up @@ -694,7 +734,7 @@ public CreatePersonAlertBuilder WithCreatedByUser(EventModels.RaisedByUserInfo u
return this;
}

internal async Task<Alert> Execute(CreatePersonBuilder createPersonBuilder, TestData testData)
internal async Task<Guid> Execute(CreatePersonBuilder createPersonBuilder, TestData testData, TrsDbContext dbContext)
{
var personId = createPersonBuilder.PersonId;

Expand All @@ -712,40 +752,35 @@ internal async Task<Alert> Execute(CreatePersonBuilder createPersonBuilder, Test
var createdByUser = _createdByUser.ValueOr(EventModels.RaisedByUserInfo.FromUserId(Core.DataStore.Postgres.Models.SystemUser.SystemUserId));
var createdUtc = _createdUtc.ValueOr(testData.Clock.UtcNow);

return await testData.WithDbContext(async dbContext =>
var alert = new Alert()
{
var alert = new Alert()
{
AlertId = AlertId,
PersonId = personId,
AlertTypeId = alertTypeId!.Value,
Details = details,
ExternalLink = externalLink,
StartDate = startDate,
EndDate = endDate,
CreatedOn = createdUtc!.Value,
UpdatedOn = createdUtc!.Value
};

dbContext.Alerts.Add(alert);
AlertId = AlertId,
PersonId = personId,
AlertTypeId = alertTypeId!.Value,
Details = details,
ExternalLink = externalLink,
StartDate = startDate,
EndDate = endDate,
CreatedOn = createdUtc!.Value,
UpdatedOn = createdUtc!.Value
};

var createdEvent = new AlertCreatedEvent()
{
EventId = Guid.NewGuid(),
CreatedUtc = createdUtc!.Value,
RaisedBy = createdByUser,
Alert = TeachingRecordSystem.Core.Events.Models.Alert.FromModel(alert),
PersonId = personId,
Reason = reason,
EvidenceFile = null
};
dbContext.Alerts.Add(alert);

dbContext.AddEvent(createdEvent);
var createdEvent = new AlertCreatedEvent()
{
EventId = Guid.NewGuid(),
CreatedUtc = createdUtc!.Value,
RaisedBy = createdByUser,
Alert = TeachingRecordSystem.Core.Events.Models.Alert.FromModel(alert),
PersonId = personId,
Reason = reason,
EvidenceFile = null
};

await dbContext.SaveChangesAsync();
dbContext.AddEvent(createdEvent);

return alert;
});
return AlertId;
}
}

Expand Down Expand Up @@ -853,7 +888,7 @@ public CreatePersonMandatoryQualificationBuilder WithImportedByDqtUser(EventMode
return this;
}

internal async Task<MandatoryQualification> Execute(CreatePersonBuilder createPersonBuilder, TestData testData)
internal async Task<Guid> Execute(CreatePersonBuilder createPersonBuilder, TestData testData, TrsDbContext dbContext)
{
var personId = createPersonBuilder.PersonId;

Expand All @@ -864,89 +899,84 @@ internal async Task<MandatoryQualification> Execute(CreatePersonBuilder createPe
var endDate = _endDate.ValueOr(status == MandatoryQualificationStatus.Passed ? testData.GenerateDate(min: (startDate ?? new DateOnly(2000, 1, 1)).AddYears(1)) : null);
var createdUtc = _createdUtc.ValueOr(testData.Clock.UtcNow);

return await testData.WithDbContext(async dbContext =>
{
var provider = providerId.HasValue ?
var provider = providerId.HasValue ?
await dbContext.MandatoryQualificationProviders.SingleAsync(p => p.MandatoryQualificationProviderId == providerId) :
null;

var mq = new MandatoryQualification()
{
QualificationId = QualificationId,
CreatedOn = testData.Clock.UtcNow,
UpdatedOn = testData.Clock.UtcNow,
PersonId = personId,
ProviderId = providerId,
Status = status,
Specialism = specialism,
StartDate = startDate,
EndDate = endDate,
DqtSpecialismId = _dqtSpecialismId.ValueOr((Guid?)null),
DqtMqEstablishmentId = _mqEstablishmentId.ValueOr((Guid?)null)
};
var mq = new MandatoryQualification()
{
QualificationId = QualificationId,
CreatedOn = testData.Clock.UtcNow,
UpdatedOn = testData.Clock.UtcNow,
PersonId = personId,
ProviderId = providerId,
Status = status,
Specialism = specialism,
StartDate = startDate,
EndDate = endDate,
DqtSpecialismId = _dqtSpecialismId.ValueOr((Guid?)null),
DqtMqEstablishmentId = _mqEstablishmentId.ValueOr((Guid?)null)
};

dbContext.MandatoryQualifications.Add(mq);
dbContext.MandatoryQualifications.Add(mq);

if (_importedByUser.HasValue)
if (_importedByUser.HasValue)
{
var createdEvent = new MandatoryQualificationDqtImportedEvent()
{
var createdEvent = new MandatoryQualificationDqtImportedEvent()
EventId = Guid.NewGuid(),
CreatedUtc = createdUtc!.Value,
RaisedBy = _importedByUser.ValueOrFailure(),
PersonId = personId,
MandatoryQualification = new()
{
EventId = Guid.NewGuid(),
CreatedUtc = createdUtc!.Value,
RaisedBy = _importedByUser.ValueOrFailure(),
PersonId = personId,
MandatoryQualification = new()
{
QualificationId = QualificationId,
Provider = provider is not null ?
new()
{
MandatoryQualificationProviderId = provider.MandatoryQualificationProviderId,
Name = provider.Name,
} :
null,
Specialism = specialism,
Status = status,
StartDate = startDate,
EndDate = endDate
},
DqtState = 0
};
dbContext.AddEvent(createdEvent);
}
else
{
var createdByUser = _createdByUser.ValueOr(EventModels.RaisedByUserInfo.FromUserId(Core.DataStore.Postgres.Models.SystemUser.SystemUserId));
QualificationId = QualificationId,
Provider = provider is not null ?
new()
{
MandatoryQualificationProviderId = provider.MandatoryQualificationProviderId,
Name = provider.Name,
} :
null,
Specialism = specialism,
Status = status,
StartDate = startDate,
EndDate = endDate
},
DqtState = 0
};
dbContext.AddEvent(createdEvent);
}
else
{
var createdByUser = _createdByUser.ValueOr(EventModels.RaisedByUserInfo.FromUserId(Core.DataStore.Postgres.Models.SystemUser.SystemUserId));

var createdEvent = new MandatoryQualificationCreatedEvent()
var createdEvent = new MandatoryQualificationCreatedEvent()
{
EventId = Guid.NewGuid(),
CreatedUtc = createdUtc!.Value,
RaisedBy = createdByUser,
PersonId = personId,
MandatoryQualification = new()
{
EventId = Guid.NewGuid(),
CreatedUtc = createdUtc!.Value,
RaisedBy = createdByUser,
PersonId = personId,
MandatoryQualification = new()
{
QualificationId = QualificationId,
Provider = provider is not null ?
new()
{
MandatoryQualificationProviderId = provider.MandatoryQualificationProviderId,
Name = provider.Name,
} :
null,
Specialism = specialism,
Status = status,
StartDate = startDate,
EndDate = endDate
}
};
dbContext.AddEvent(createdEvent);
}

await dbContext.SaveChangesAsync();
QualificationId = QualificationId,
Provider = provider is not null ?
new()
{
MandatoryQualificationProviderId = provider.MandatoryQualificationProviderId,
Name = provider.Name,
} :
null,
Specialism = specialism,
Status = status,
StartDate = startDate,
EndDate = endDate
}
};
dbContext.AddEvent(createdEvent);
}

return mq;
});
return QualificationId;
}
}

Expand Down

0 comments on commit 125c052

Please sign in to comment.