Skip to content

Commit

Permalink
Merge pull request #352 from DFE-Digital/matchback-ignoring-whitespace
Browse files Browse the repository at this point in the history
Ignore leading/trailing whitespace in match-back
  • Loading branch information
ethax-ross authored Nov 3, 2020
2 parents 1432b00 + 8ca0d4e commit c441ef3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
6 changes: 3 additions & 3 deletions GetIntoTeachingApi/Models/ExistingCandidateRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public string Slugify()

private bool EmailMatchesCandidate(Entity entity)
{
return entity.GetAttributeValue<string>("emailaddress1").Equals(Email, StringComparison.OrdinalIgnoreCase);
return entity.GetAttributeValue<string>("emailaddress1").Trim().Equals(Email, StringComparison.OrdinalIgnoreCase);
}

private string[] AdditionalAttributeValues(string firstName, string lastName, DateTime? dateOfBirth)
Expand All @@ -50,8 +50,8 @@ private bool MinimumAdditionalAttributesMatch(Entity entity)
{
var matches = AdditionalAttributeValues(FirstName, LastName, DateOfBirth).Intersect(
AdditionalAttributeValues(
entity.GetAttributeValue<string>("firstname"),
entity.GetAttributeValue<string>("lastname"),
entity.GetAttributeValue<string>("firstname")?.Trim(),
entity.GetAttributeValue<string>("lastname")?.Trim(),
entity.GetAttributeValue<DateTime>("birthdate")), StringComparer.OrdinalIgnoreCase);

return matches.Count() >= MinimumAdditionalAttributeMatches;
Expand Down
5 changes: 4 additions & 1 deletion GetIntoTeachingApi/Services/CrmService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public Candidate MatchCandidate(ExistingCandidateRequest request)
var entity = _service.CreateQuery("contact", context)
.Where(e =>
e.GetAttributeValue<int>("statecode") == (int)Candidate.Status.Active &&
e.GetAttributeValue<string>("emailaddress1") == request.Email) // Will perform a case-insensitive comparison

// Will perform a case-insensitive comparison.
// Contains is used to ensure we match emails with white space (request.Match does an exact match in-memory).
e.GetAttributeValue<string>("emailaddress1").Contains(request.Email))
.OrderByDescending(e => e.GetAttributeValue<double>("dfe_duplicatescorecalculated"))
.ThenByDescending(e => e.GetAttributeValue<DateTime>("modifiedon"))
.Take(MaximumNumberOfCandidatesToMatch)
Expand Down
12 changes: 11 additions & 1 deletion GetIntoTeachingApiTests/Models/ExistingCandidateRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using Microsoft.Xrm.Sdk;
using Xunit;
using GetIntoTeachingApi.Attributes;

namespace GetIntoTeachingApiTests.Models
{
Expand Down Expand Up @@ -58,6 +57,17 @@ public void Match_WithEmailAndTwoAdditionalAttributes_ReturnsTrue()
_request.Match(entity).Should().BeTrue();
}

[Fact]
public void Match_WithEmailAndTwoAdditionalAttributesContainingWhitespace_ReturnsTrue()
{
var entity = new Entity();
entity["emailaddress1"] = $" {_request.Email} ";
entity["firstname"] = $" {_request.FirstName} ";
entity["lastname"] = $" {_request.LastName} ";

_request.Match(entity).Should().BeTrue();
}

[Fact]
public void Match_WithoutEmailAndWithTwoAdditionalAttributes_ReturnsFalse()
{
Expand Down
1 change: 1 addition & 0 deletions GetIntoTeachingApiTests/Services/CrmServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public void GetCandidate_WithNonExistentId_ReturnsNull()
[InlineData("john@doe.com", "New John", "Doe", "New John")]
[InlineData("JOHN@doe.com", "New John", "Doe", "New John")]
[InlineData("jane@doe.com", "Jane", "Doe", "Jane")]
[InlineData(" jane@doe.com ", " Jane ", " Doe ", "Jane")]
[InlineData("bob@doe.com", "Bob", "Doe", null)]
[InlineData("inactive@doe.com", "Inactive", "Doe", null)]
public void MatchCandidate_WithExistingCandidateRequest_MatchesOnNewestCandidateWithEmail(
Expand Down

0 comments on commit c441ef3

Please sign in to comment.