Skip to content
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 @@ -16,6 +16,5 @@ public class UpdateApplicantSummaryDto
public bool? IndigenousOrgInd { get; set; }
public string? UnityApplicantId { get; set; }
public string? FiscalDay { get; set; }
public string? FiscalMonth { get; set; }
public string? ElectoralDistrict { get; set; }
public string? FiscalMonth { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public async Task<GrantApplicationDto> UpdatePartialApplicantInfoAsync(Guid appl
}

// Only update the fields we need to update based on the modified
// Automapper mapping ignores the ElectoralDistrict as this is different from the application level electoral district
ObjectMapper.Map<UpdateApplicantInfoDto, Applications.Application>(input.Data, application);

//-- APPLICANT INFO - SUMMARY
Expand Down Expand Up @@ -190,6 +191,14 @@ public async Task<GrantApplicationDto> UpdatePartialApplicantInfoAsync(Guid appl
await CreateOrUpdateApplicantAddress(application.ApplicantId, input.Data.MailingAddress);
}

//-- APPLICANT ELECTORAL DISTRICT
if (input.Data.ElectoralDistrict != null
&& await AuthorizationService.IsGrantedAsync(UnitySelector.Applicant.Location.Update))
{
// Update the electoral district at the applicant level
application.Applicant.ElectoralDistrict = input.Data.ElectoralDistrict;
}

//-- APPLICANT INFO CUSTOM FIELDS
if (input.Data.CustomFields?.ValueKind != JsonValueKind.Null && input.Data.WorksheetId != Guid.Empty && input.Data.CorrelationId != Guid.Empty)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public GrantManagerApplicationAutoMapperProfile()
CreateMap<UpdateApplicantInfoDto, Applicant>()
.IgnoreNullAndDefaultValues();
CreateMap<UpdateApplicantInfoDto, Application>()
.IgnoreNullAndDefaultValues();
.IgnoreNullAndDefaultValues()
.ForMember(dest => dest.ElectoralDistrict, opt => opt.Ignore()); // Electoral district is handled separately
CreateMap<SigningAuthorityDto, Application>()
.IgnoreNullAndDefaultValues();
CreateMap<UpdateApplicantSummaryDto, Applicant>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DetermineElectoralDistrictHandler(IGeocoderApiService geocoderApiSe
: ILocalEventHandler<ApplicationProcessEvent>, ITransientDependency
{
/// <summary>
/// Determines the Electoral Distrct based on the Address.
/// Determines the Electoral District based on the Address.
/// </summary>
/// <param name="eventData"></param>
/// <returns></returns>
Expand All @@ -30,31 +30,39 @@ public async Task HandleEventAsync(ApplicationProcessEvent eventData)

if (eventData.FormVersion == null)
{
logger.LogWarning("Application data is null in DetermineElectoralDistrictHandler.");
logger.LogWarning("Form version data is null in DetermineElectoralDistrictHandler.");
return;
}

// Check if the electoral district is already mapped for the form submission, if so then no work to be done
if (eventData.FormVersion.HasSubmissionHeaderMapping("ApplicantElectoralDistrict"))
{
logger.LogInformation("Electoral district already determined for application {ApplicationId}. No further action required.",
eventData.Application.Id);
return;
}

// Use local variable to avoid modifying the entity property
var addressType = eventData.Application.ApplicationForm.ElectoralDistrictAddressType ?? GrantApplications.AddressType.PhysicalAddress;
logger.LogInformation("Using electoral district address type: {AddressType} for electoral determination", addressType);

var electoralDistrictAddressType = eventData.Application.ApplicationForm.ElectoralDistrictAddressType;
var applicantAddresses = eventData.Application.Applicant.ApplicantAddresses;

if (applicantAddresses == null || applicantAddresses.Count == 0)
{
logger.LogWarning("Application data is null in DetermineElectoralDistrictHandler.");
logger.LogWarning("Applicant addresses are null or empty in DetermineElectoralDistrictHandler for application {ApplicationId}.",
eventData.Application.Id);
return;
}

// Find the related address type
var matchedAddressType = applicantAddresses
.FirstOrDefault(a => a.AddressType == electoralDistrictAddressType);
.FirstOrDefault(a => a.AddressType == addressType);

if (matchedAddressType == null)
{
logger.LogWarning("No address of type {AddressType} found for application {ApplicationId}.",
electoralDistrictAddressType, eventData.Application.Id);
addressType, eventData.Application.Id);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,46 @@ public async Task<IViewComponentResult> InvokeAsync(Guid applicationId, Guid app
var applicantInfoDto = await applicationAppicantService.GetApplicantInfoTabAsync(applicationId);
var electoralDistrictAddressType = await applicationFormAppService.GetElectoralDistrictAddressTypeAsync(applicantInfoDto.ApplicationFormId);

if (applicantInfoDto == null)
{
throw new InvalidOperationException("Applicant information could not be retrieved.");
if (applicantInfoDto == null)
{
throw new InvalidOperationException("Applicant information could not be retrieved.");
}

ApplicantInfoViewModel viewModel = new()
{
ApplicationId = applicationId,
ApplicationFormId = applicantInfoDto.ApplicationFormId,
ApplicationFormVersionId = applicationFormVersionId,
ApplicantId = applicantInfoDto.ApplicantId,
ApplicantSummary = ObjectMapper.Map<ApplicantSummaryDto, ApplicantSummaryViewModel>(applicantInfoDto.ApplicantSummary ?? new ApplicantSummaryDto()),
ContactInfo = ObjectMapper.Map<ContactInfoDto, ContactInfoViewModel>(applicantInfoDto.ContactInfo ?? new ContactInfoDto()),
SigningAuthority = ObjectMapper.Map<SigningAuthorityDto, SigningAuthorityViewModel>(applicantInfoDto.SigningAuthority ?? new SigningAuthorityDto()),
ApplicantElectoralAddressType = electoralDistrictAddressType,
ApplicantInfoViewModel viewModel = new()
{
ApplicationId = applicationId,
ApplicationFormId = applicantInfoDto.ApplicationFormId,
ApplicationFormVersionId = applicationFormVersionId,
ApplicantId = applicantInfoDto.ApplicantId,
ApplicantSummary = ObjectMapper.Map<ApplicantSummaryDto, ApplicantSummaryViewModel>(applicantInfoDto.ApplicantSummary ?? new ApplicantSummaryDto()),
ContactInfo = ObjectMapper.Map<ContactInfoDto, ContactInfoViewModel>(applicantInfoDto.ContactInfo ?? new ContactInfoDto()),
SigningAuthority = ObjectMapper.Map<SigningAuthorityDto, SigningAuthorityViewModel>(applicantInfoDto.SigningAuthority ?? new SigningAuthorityDto()),
ApplicantElectoralAddressType = electoralDistrictAddressType,
// This is fixed at the applicant level, we need solution this wrt the recent address logic added below
ElectoralDistrict = applicantInfoDto.ElectoralDistrict
};

viewModel.ApplicantSummary.ApplicantId = applicantInfoDto.ApplicantId;

await PopulateSectorsAndSubSectorsAsync(viewModel);
await PopulateElectoralDistrictsAsync(viewModel);

// MAPADDRESSES
if (applicantInfoDto.ApplicantAddresses?.Count > 0)
{
// Map physical address
var physicalAddress = FindMostRecentAddress(applicantInfoDto.ApplicantAddresses, AddressType.PhysicalAddress);
if (physicalAddress is not null)
{
viewModel.PhysicalAddress = ObjectMapper.Map<ApplicantAddressDto, ApplicantAddressViewModel>(physicalAddress);
await PopulateElectoralDistrictsAsync(viewModel);

// MAPADDRESSES
if (applicantInfoDto.ApplicantAddresses?.Count > 0)
{
// Map physical address
var physicalAddress = FindMostRecentAddress(applicantInfoDto.ApplicantAddresses, AddressType.PhysicalAddress);
if (physicalAddress is not null)
{
viewModel.PhysicalAddress = ObjectMapper.Map<ApplicantAddressDto, ApplicantAddressViewModel>(physicalAddress);
}

// Map mailing address
var mailingAddress = FindMostRecentAddress(applicantInfoDto.ApplicantAddresses, AddressType.MailingAddress);
if (mailingAddress is not null)
{
viewModel.MailingAddress = ObjectMapper.Map<ApplicantAddressDto, ApplicantAddressViewModel>(mailingAddress);
}
// Map mailing address
var mailingAddress = FindMostRecentAddress(applicantInfoDto.ApplicantAddresses, AddressType.MailingAddress);
if (mailingAddress is not null)
{
viewModel.MailingAddress = ObjectMapper.Map<ApplicantAddressDto, ApplicantAddressViewModel>(mailingAddress);
}
}

return View(viewModel);
Expand All @@ -76,12 +78,10 @@ public async Task<IViewComponentResult> InvokeAsync(Guid applicationId, Guid app
private static ApplicantAddressDto? FindMostRecentAddress(List<ApplicantAddressDto> applicantAddresses, AddressType addressType)
{
return applicantAddresses
.Where(address => address.AddressType == addressType)
.OrderByDescending(address =>
address.CreationTime < address.LastModificationTime.GetValueOrDefault(DateTime.MinValue)
? address.CreationTime
: address.LastModificationTime.GetValueOrDefault(DateTime.MinValue))
.FirstOrDefault();
.Where(address => address.AddressType == addressType)
.OrderByDescending(address =>
address.LastModificationTime.GetValueOrDefault(address.CreationTime))
.FirstOrDefault();
}

private async Task PopulateElectoralDistrictsAsync(ApplicantInfoViewModel model)
Expand Down