Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EMBCESSMOD-5208: support task association #2165

Merged
merged 6 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<ItemGroup>
<PackageReference Include="IdentityModel" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.5" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<PackageReference Include="Microsoft.OData.Edm" Version="7.21.1" />
<PackageReference Include="Microsoft.OData.Extensions.Client" Version="1.0.6" />
<PackageReference Include="Microsoft.Spatial" Version="7.21.1" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.5" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.3" />
</ItemGroup>
Expand Down
37 changes: 23 additions & 14 deletions ess/src/API/EMBC.ESS/Managers/Events/EventsManager.Supports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,25 @@ public async System.Threading.Tasks.Task Handle(ProcessPaperSupportsCommand cmd)

public async System.Threading.Tasks.Task Handle(ProcessSelfServeSupportsCommand cmd)
{
var file = (await evacuationRepository.Query(new Resources.Evacuations.EvacuationFilesQuery { FileId = cmd.EvacuationFileId })).Items.SingleOrDefault();
if (file == null) throw new NotFoundException("file not found", cmd.EvacuationFileId);
if (file.NeedsAssessment.EligibilityCheck == null || !file.NeedsAssessment.EligibilityCheck.Eligible) throw new BusinessLogicException($"File {cmd.EvacuationFileId} latest needs assessment doesn't have a valid eligibility check");
var file = (await evacuationRepository.Query(new Resources.Evacuations.EvacuationFilesQuery { FileId = cmd.EvacuationFileNumber })).Items.SingleOrDefault();
if (file == null) throw new NotFoundException("file not found", cmd.EvacuationFileNumber);
if (file.PrimaryRegistrantUserId != cmd.RegistrantUserId) throw new InvalidOperationException($"Registrant {cmd.RegistrantUserId} does not match file {cmd.EvacuationFileNumber} primary registrant which is {file.PrimaryRegistrantUserId}");
if (file.NeedsAssessment.EligibilityCheck == null || !file.NeedsAssessment.EligibilityCheck.Eligible) throw new BusinessLogicException($"File {cmd.EvacuationFileNumber} latest needs assessment doesn't have a valid eligibility check");

var response = (GenerateSelfServeSupportsResponse)await supportingEngine.Generate(new CalculateSelfServeSupports(cmd.Supports, file.NeedsAssessment.HouseholdMembers.Select(hm => new SelfServeHouseholdMember(hm.Id, hm.IsMinor))));

var supports = ((GenerateSelfServeETransferSupportsResponse)await supportingEngine.Generate(
new GenerateSelfServeETransferSupports(file.Id, file.PrimaryRegistrantId, response.Supports, cmd.ETransferDetails, file.NeedsAssessment.EligibilityCheck.From.Value, file.NeedsAssessment.EligibilityCheck.To.Value))).Supports;
var validationResponse = (DigitalSupportsValidationResponse)await supportingEngine.Validate(new DigitalSupportsValidationRequest
{
FileId = cmd.EvacuationFileId,
FileId = cmd.EvacuationFileNumber,
Supports = supports
});
if (!validationResponse.IsValid) throw new BusinessValidationException(string.Join(',', validationResponse.Errors));

await supportingEngine.Process(new ProcessDigitalSupportsRequest
{
FileId = cmd.EvacuationFileId,
FileId = cmd.EvacuationFileNumber,
Supports = supports,
RequestingUserId = null,
IncludeSummaryInReferralsPrintout = false,
Expand Down Expand Up @@ -334,14 +335,19 @@ public async System.Threading.Tasks.Task Handle(ProcessApprovedSupportsCommand _

public async System.Threading.Tasks.Task Handle(OptOutSelfServeCommand cmd)
{
await evacuationRepository.Manage(new Resources.Evacuations.OptoutSelfServe { EvacuationFileNumber = cmd.EvacuationFileId });
var file = (await evacuationRepository.Query(new Resources.Evacuations.EvacuationFilesQuery { FileId = cmd.EvacuationFileNumber })).Items.SingleOrDefault();
if (file == null) throw new NotFoundException("file not found", cmd.EvacuationFileNumber);
if (file.PrimaryRegistrantId != cmd.RegistrantUserId) throw new InvalidOperationException($"Registrant {cmd.RegistrantUserId} does not match file {cmd.EvacuationFileNumber} primary registrant which is {file.PrimaryRegistrantUserId}");

await evacuationRepository.Manage(new Resources.Evacuations.OptoutSelfServe { EvacuationFileNumber = cmd.EvacuationFileNumber });
}

public async Task<DraftSelfServeSupportQueryResponse> Handle(DraftSelfServeSupportQuery query)
{
var file = (await evacuationRepository.Query(new Resources.Evacuations.EvacuationFilesQuery { FileId = query.EvacuationFileId })).Items.SingleOrDefault();
if (file == null) throw new NotFoundException("file not found", query.EvacuationFileId);
if (file.NeedsAssessment.EligibilityCheck == null) throw new BusinessValidationException($"File {query.EvacuationFileId} is not eligible for self serve");
var file = (await evacuationRepository.Query(new Resources.Evacuations.EvacuationFilesQuery { FileId = query.EvacuationFileNumber })).Items.SingleOrDefault();
if (file == null) throw new NotFoundException("file not found", query.EvacuationFileNumber);
if (file.PrimaryRegistrantUserId != query.RegistrantUserId) throw new InvalidOperationException($"Registrant {query.RegistrantUserId} does not match file {query.EvacuationFileNumber} primary registrant which is {file.PrimaryRegistrantUserId}");
if (file.NeedsAssessment.EligibilityCheck == null) throw new BusinessValidationException($"File {query.EvacuationFileNumber} is not eligible for self serve");

var task = (await taskRepository.QueryTask(new TaskQuery { ById = file.NeedsAssessment.EligibilityCheck.TaskNumber, ByStatus = [Resources.Tasks.TaskStatus.Active] })).Items.SingleOrDefault() as EssTask;
if (task == null) throw new NotFoundException("task not found", file.NeedsAssessment.EligibilityCheck.TaskNumber);
Expand Down Expand Up @@ -373,13 +379,15 @@ public async Task<DraftSelfServeSupportQueryResponse> Handle(DraftSelfServeSuppo

public async System.Threading.Tasks.Task<string> Handle(CheckEligibileForSelfServeCommand cmd)
{
var ct = CancellationToken.None;
var file = (await evacuationRepository.Query(new Resources.Evacuations.EvacuationFilesQuery { FileId = cmd.EvacuationFileNumber })).Items.SingleOrDefault();
if (file == null) throw new NotFoundException("file not found", cmd.EvacuationFileNumber);
if (file.PrimaryRegistrantUserId != cmd.RegistrantUserId) throw new InvalidOperationException($"Registrant {cmd.RegistrantUserId} does not match file {cmd.EvacuationFileNumber} primary registrant which is {file.PrimaryRegistrantUserId}");

var eligibilityResult = ((ValidateSelfServeSupportsEligibilityResponse)await supportingEngine.Validate(new ValidateSelfServeSupportsEligibility(cmd.EvacuationFileId), ct)).Eligibility;
var eligibilityResult = ((ValidateSelfServeSupportsEligibilityResponse)await supportingEngine.Validate(new ValidateSelfServeSupportsEligibility(cmd.EvacuationFileNumber), default)).Eligibility;

var response = await evacuationRepository.Manage(new Resources.Evacuations.AddEligibilityCheck
{
EvacuationFileNumber = cmd.EvacuationFileId,
EvacuationFileNumber = cmd.EvacuationFileNumber,
Eligible = eligibilityResult.Eligible,
TaskNumber = eligibilityResult.TaskNumber,
HomeAddressReferenceId = eligibilityResult.HomeAddressReferenceId,
Expand All @@ -393,8 +401,9 @@ public async System.Threading.Tasks.Task<string> Handle(CheckEligibileForSelfSer

public async Task<EligibilityCheckQueryResponse> Handle(EligibilityCheckQuery query)
{
var file = (await evacuationRepository.Query(new Resources.Evacuations.EvacuationFilesQuery { FileId = query.EvacuationFileId })).Items.SingleOrDefault();
if (file == null) throw new NotFoundException("file not found", query.EvacuationFileId);
var file = (await evacuationRepository.Query(new Resources.Evacuations.EvacuationFilesQuery { FileId = query.EvacuationFileNumber })).Items.SingleOrDefault();
if (file == null) throw new NotFoundException("file not found", query.EvacuationFileNumber);
if (file.PrimaryRegistrantUserId != query.RegistrantUserId) throw new InvalidOperationException($"Registrant {query.RegistrantUserId} does not match file {query.EvacuationFileNumber} primary registrant which is {file.PrimaryRegistrantUserId}");

var eligibility = file.NeedsAssessment.EligibilityCheck?.Eligible == true
? new SupportEligibility
Expand Down
78 changes: 43 additions & 35 deletions ess/src/API/EMBC.ESS/Resources/Evacuations/EvacuationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,42 +276,43 @@ private static async Task ParallelLoadEvacuationFileAsync(EssContext ctx, era_ev
{
ctx.AttachTo(nameof(EssContext.era_evacuationfiles), file);

var loadTasks = new[]
var loadTasks = new List<Task>();

if (file.era_era_evacuationfile_era_animal_ESSFileid == null) loadTasks.Add(ctx.LoadPropertyAsync(file, nameof(era_evacuationfile.era_era_evacuationfile_era_animal_ESSFileid), ct));
if (file.era_era_evacuationfile_era_essfilenote_ESSFileID == null) loadTasks.Add(ctx.LoadPropertyAsync(file, nameof(era_evacuationfile.era_era_evacuationfile_era_essfilenote_ESSFileID), ct));
if (file.era_TaskId == null) loadTasks.Add(ctx.LoadPropertyAsync(file, nameof(era_evacuationfile.era_TaskId), ct));
if (file.era_Registrant == null) loadTasks.Add(ctx.LoadPropertyAsync(file, nameof(era_evacuationfile.era_Registrant), ct));
if (file.era_era_evacuationfile_era_evacueesupport_ESSFileId == null) loadTasks.Add(ctx.LoadPropertyAsync(file, nameof(era_evacuationfile.era_era_evacuationfile_era_evacueesupport_ESSFileId), ct));
loadTasks.Add(Task.Run(async () =>
{
ctx.LoadPropertyAsync(file, nameof(era_evacuationfile.era_era_evacuationfile_era_animal_ESSFileid), ct),
ctx.LoadPropertyAsync(file, nameof(era_evacuationfile.era_era_evacuationfile_era_essfilenote_ESSFileID), ct),
ctx.LoadPropertyAsync(file, nameof(era_evacuationfile.era_TaskId), ct),
ctx.LoadPropertyAsync(file, nameof(era_evacuationfile.era_era_evacuationfile_era_evacueesupport_ESSFileId), ct),
Task.Run(async () =>
if (file.era_CurrentNeedsAssessmentid == null) await ctx.LoadPropertyAsync(file, nameof(era_evacuationfile.era_CurrentNeedsAssessmentid), ct);
ctx.AttachTo(nameof(EssContext.era_needassessments), file.era_CurrentNeedsAssessmentid);
if (file.era_CurrentNeedsAssessmentid.era_TaskNumber == null) await ctx.LoadPropertyAsync(file.era_CurrentNeedsAssessmentid, nameof(era_needassessment.era_TaskNumber), ct);
if (file.era_CurrentNeedsAssessmentid.era_EligibilityCheck == null) await ctx.LoadPropertyAsync(file.era_CurrentNeedsAssessmentid, nameof(era_needassessment.era_EligibilityCheck), ct);
if (file.era_CurrentNeedsAssessmentid.era_EligibilityCheck != null)
{
if (file.era_CurrentNeedsAssessmentid == null) await ctx.LoadPropertyAsync(file, nameof(era_evacuationfile.era_CurrentNeedsAssessmentid), ct);
ctx.AttachTo(nameof(EssContext.era_needassessments), file.era_CurrentNeedsAssessmentid);
if (file.era_CurrentNeedsAssessmentid.era_TaskNumber==null) await ctx.LoadPropertyAsync(file.era_CurrentNeedsAssessmentid, nameof(era_needassessment.era_TaskNumber), ct);
if (file.era_CurrentNeedsAssessmentid.era_EligibilityCheck==null) await ctx.LoadPropertyAsync(file.era_CurrentNeedsAssessmentid, nameof(era_needassessment.era_EligibilityCheck), ct);
if (file.era_CurrentNeedsAssessmentid.era_EligibilityCheck != null)
{
ctx.AttachTo(nameof(EssContext.era_eligibilitychecks), file.era_CurrentNeedsAssessmentid.era_EligibilityCheck);
await ctx.LoadPropertyAsync(file.era_CurrentNeedsAssessmentid.era_EligibilityCheck, nameof(era_eligibilitycheck.era_Task));
}

var members = await ctx.era_householdmembers
.Expand(m => m.era_Registrant)
.Where(m => m._era_evacuationfileid_value == file.era_evacuationfileid)
.GetAllPagesAsync(ct);

file.era_era_evacuationfile_era_householdmember_EvacuationFileid = new Collection<era_householdmember>(
members.Where(m => m.era_Registrant == null || m.era_Registrant.statecode == (int)EntityState.Active).ToArray());

var naHouseholdMembers = (await ctx.era_era_householdmember_era_needassessmentset
.Where(m => m.era_needassessmentid == file._era_currentneedsassessmentid_value)
.GetAllPagesAsync(ct))
.ToArray();

file.era_CurrentNeedsAssessmentid.era_era_householdmember_era_needassessment = new Collection<era_householdmember>(
file.era_era_evacuationfile_era_householdmember_EvacuationFileid
.Where(m => naHouseholdMembers.Any(nam => nam.era_householdmemberid == m.era_householdmemberid)).ToArray());
})
};
ctx.AttachTo(nameof(EssContext.era_eligibilitychecks), file.era_CurrentNeedsAssessmentid.era_EligibilityCheck);
await ctx.LoadPropertyAsync(file.era_CurrentNeedsAssessmentid.era_EligibilityCheck, nameof(era_eligibilitycheck.era_Task));
}

var members = await ctx.era_householdmembers
.Expand(m => m.era_Registrant)
.Where(m => m._era_evacuationfileid_value == file.era_evacuationfileid)
.GetAllPagesAsync(ct);

file.era_era_evacuationfile_era_householdmember_EvacuationFileid = new Collection<era_householdmember>(
members.Where(m => m.era_Registrant == null || m.era_Registrant.statecode == (int)EntityState.Active).ToArray());

var naHouseholdMembers = (await ctx.era_era_householdmember_era_needassessmentset
.Where(m => m.era_needassessmentid == file._era_currentneedsassessmentid_value)
.GetAllPagesAsync(ct))
.ToArray();

file.era_CurrentNeedsAssessmentid.era_era_householdmember_era_needassessment = new Collection<era_householdmember>(
file.era_era_evacuationfile_era_householdmember_EvacuationFileid
.Where(m => naHouseholdMembers.Any(nam => nam.era_householdmemberid == m.era_householdmemberid)).ToArray());
}));

await Task.WhenAll(loadTasks);
}

Expand Down Expand Up @@ -378,7 +379,14 @@ private static async Task<IEnumerable<era_evacuationfile>> QueryEvacuationFiles(

if (!shouldQueryFiles) return Array.Empty<era_evacuationfile>();

var filesQuery = ctx.era_evacuationfiles.Expand(f => f.era_CurrentNeedsAssessmentid).Where(f => f.statecode == (int)EntityState.Active);
var filesQuery = ctx.era_evacuationfiles
.Expand(f => f.era_CurrentNeedsAssessmentid)
.Expand(f => f.era_Registrant)
.Expand(f => f.era_era_evacuationfile_era_animal_ESSFileid)
.Expand(f => f.era_era_evacuationfile_era_essfilenote_ESSFileID)
.Expand(f => f.era_TaskId)
.Expand(f => f.era_era_evacuationfile_era_evacueesupport_ESSFileId)
.Where(f => f.statecode == (int)EntityState.Active);

if (!string.IsNullOrEmpty(query.FileId)) filesQuery = filesQuery.Where(f => f.era_name == query.FileId);
if (!string.IsNullOrEmpty(query.ManualFileId)) filesQuery = filesQuery.Where(f => f.era_paperbasedessfile == query.ManualFileId);
Expand Down
Loading