Skip to content

Commit

Permalink
Merge pull request #1817 from bcgov/revert-1812-revert-1806-master
Browse files Browse the repository at this point in the history
Revert "Revert "fix: duplicate check to consider contained date range and minor perfomance improvements""
  • Loading branch information
KyleKayfish authored Sep 5, 2023
2 parents 85bbf03 + a75eddc commit 463f268
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,42 @@ public async Task<IEnumerable<SupportFlag>> CheckCompliance(Support support)
{
if (support.Id == null) throw new ArgumentNullException(nameof(support.Id));

var ctx = essContextFactory.Create();
var ctx = essContextFactory.CreateReadOnly();

var checkedSupport = (await ((DataServiceQuery<era_evacueesupport>)ctx.era_evacueesupports.Where(s => s.era_name == support.Id)).GetAllPagesAsync()).SingleOrDefault();
if (checkedSupport == null) throw new ArgumentException($"Support {support.Id} not found", nameof(support));

ctx.AttachTo(nameof(ctx.era_evacueesupports), checkedSupport);
await ctx.LoadPropertyAsync(checkedSupport, nameof(era_evacueesupport.era_era_householdmember_era_evacueesupport));

var from = checkedSupport.era_validfrom.Value;
var to = checkedSupport.era_validto.Value;
var type = checkedSupport.era_supporttype.Value;
var householdMembers = checkedSupport.era_era_householdmember_era_evacueesupport.ToArray();
var householdMembers = checkedSupport.era_era_householdmember_era_evacueesupport.ToList();

var similarSupports = (await ((DataServiceQuery<era_evacueesupport>)ctx.era_evacueesupports.Where(s =>
s.era_name != checkedSupport.era_name && s.era_supporttype == type &&
s.statuscode != (int)SupportStatus.Cancelled && s.statuscode != (int)SupportStatus.Void &&
((s.era_validfrom >= from && s.era_validfrom <= to) ||
(s.era_validto >= from && s.era_validto <= to))))
.GetAllPagesAsync()).ToArray();
s.statuscode != (int)Resources.Supports.SupportStatus.Cancelled && s.statuscode != (int)Resources.Supports.SupportStatus.Void &&
((s.era_validfrom >= from && s.era_validfrom <= to) || (s.era_validto >= from && s.era_validto <= to) || (s.era_validfrom < from && s.era_validto > to))))
.GetAllPagesAsync()).ToList();

foreach (var similarSupport in similarSupports)
similarSupports.AsParallel().ForAll(s =>
{
await ctx.LoadPropertyAsync(similarSupport, nameof(era_evacueesupport.era_era_householdmember_era_evacueesupport));
}
ctx.AttachTo(nameof(ctx.era_evacueesupports), s);
ctx.LoadProperty(s, nameof(era_evacueesupport.era_era_householdmember_era_evacueesupport));
ctx.Detach(s);
});

Func<era_householdmember, era_householdmember, bool> householdMemberMatcher = (m1, m2) =>
m1.era_firstname.Equals(m2.era_firstname, StringComparison.OrdinalIgnoreCase) &&
m1.era_lastname.Equals(m2.era_lastname, StringComparison.OrdinalIgnoreCase) &&
m1.era_dateofbirth.Equals(m2.era_dateofbirth);
m1.era_firstname.Equals(m2.era_firstname, StringComparison.InvariantCultureIgnoreCase) &&
m1.era_lastname.Equals(m2.era_lastname, StringComparison.InvariantCultureIgnoreCase) &&
m1.era_dateofbirth.Value.Equals(m2.era_dateofbirth);

var duplicateSupports = similarSupports
.Where(s => s.era_era_householdmember_era_evacueesupport.Any(m => householdMembers.Any(cm => householdMemberMatcher(cm, m))))
.ToArray();
.Where(s =>
s.era_era_householdmember_era_evacueesupport.Any(m => householdMembers.Exists(cm => householdMemberMatcher(cm, m))));

var duplicates = duplicateSupports.Select(s => new DuplicateSupportFlag { DuplicatedSupportId = s.era_name }).ToArray();
var duplicates = duplicateSupports.Select(s => new DuplicateSupportFlag { DuplicatedSupportId = s.era_name }).ToList();

ctx.DetachAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal interface ISupportComplianceCheck

internal class SupportComplianceStrategyFactory
{
private IServiceProvider services;
private readonly IServiceProvider services;

public SupportComplianceStrategyFactory(IServiceProvider services)
{
Expand Down

0 comments on commit 463f268

Please sign in to comment.