Skip to content

Commit

Permalink
add creation channels to remaining endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
martyn-w committed Jan 17, 2025
1 parent e1256dc commit 8ab448e
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Text.Json.Serialization;
using GetIntoTeachingApi.Models.Crm;
using GetIntoTeachingApi.Services;
Expand All @@ -20,6 +21,13 @@ public class GetIntoTeachingCallback
[SwaggerSchema(WriteOnly = true)]
public DateTime? PhoneCallScheduledAt { get; set; }
public string TalkingPoints { get; set; }

[SwaggerSchema(WriteOnly = true)]
public int? CreationChannelSourceId { get; set; }
[SwaggerSchema(WriteOnly = true)]
public int? CreationChannelServiceId { get; set; }
[SwaggerSchema(WriteOnly = true)]
public int? CreationChannelActivityId { get; set; }

[JsonIgnore]
public Candidate Candidate => CreateCandidate();
Expand Down Expand Up @@ -83,9 +91,38 @@ private void ConfigureChannel(Candidate candidate)
{
if (CandidateId == null)
{
candidate.ChannelId = (int?)Candidate.Channel.GetIntoTeachingCallback;
if (CreationChannelSourceId.HasValue)
{
candidate.ChannelId = null;
// NB: CreationChannel should be true only if it is the first ContactChannelCreation record
AddCandidateCreationChannel(candidate, !candidate.ContactChannelCreations.Any());
}
else
{
candidate.ChannelId = (int?)Candidate.Channel.GetIntoTeachingCallback;
}
}
else // Candidate record already exists
{
// NB: we do not update a candidate's ChannelId for an existing record
// NB: CreationChannel should always be false for existing candidates
if (CreationChannelSourceId.HasValue)
{
AddCandidateCreationChannel(candidate, false);
}
}
}

private void AddCandidateCreationChannel(Candidate candidate, bool creationChannel)
{
candidate.ContactChannelCreations.Add(new ContactChannelCreation()
{
CreationChannel = creationChannel,
CreationChannelSourceId = CreationChannelSourceId,
CreationChannelServiceId = CreationChannelServiceId,
CreationChannelActivityId = CreationChannelActivityId,
});
}

private void AcceptPrivacyPolicy(Candidate candidate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public class TeachingEventAddAttendee

[SwaggerSchema(WriteOnly = true)]
public int? ChannelId { get; set; }

[SwaggerSchema(WriteOnly = true)]
public int? CreationChannelSourceId { get; set; }
[SwaggerSchema(WriteOnly = true)]
public int? CreationChannelServiceId { get; set; }
[SwaggerSchema(WriteOnly = true)]
public int? CreationChannelActivityId { get; set; }

[SwaggerSchema(WriteOnly = true)]
public Guid? AcceptedPolicyId { get; set; }
Expand Down Expand Up @@ -140,9 +147,38 @@ private void ConfigureChannel(Candidate candidate)
{
if (CandidateId == null)
{
candidate.ChannelId = ChannelId ?? (int?)Candidate.Channel.Event;
if (CreationChannelSourceId.HasValue)
{
candidate.ChannelId = null;
// NB: CreationChannel should be true only if it is the first ContactChannelCreation record
AddCandidateCreationChannel(candidate, !candidate.ContactChannelCreations.Any());
}
else
{
candidate.ChannelId = ChannelId ?? (int?)Candidate.Channel.Event;
}
}
else // Candidate record already exists
{
// NB: we do not update a candidate's ChannelId for an existing record
// NB: CreationChannel should always be false for existing candidates
if (CreationChannelSourceId.HasValue)
{
AddCandidateCreationChannel(candidate, false);
}
}
}

private void AddCandidateCreationChannel(Candidate candidate, bool creationChannel)
{
candidate.ContactChannelCreations.Add(new ContactChannelCreation()
{
CreationChannel = creationChannel,
CreationChannelSourceId = CreationChannelSourceId,
CreationChannelServiceId = CreationChannelServiceId,
CreationChannelActivityId = CreationChannelActivityId,
});
}

private void AddQualification(Candidate candidate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public class SchoolsExperienceSignUp
public int? DegreeTypeId { get; set; }
public string DegreeSubject { get; set; }
public int? UkDegreeGradeId { get; set; }

[SwaggerSchema(WriteOnly = true)]
public int? CreationChannelSourceId { get; set; }
[SwaggerSchema(WriteOnly = true)]
public int? CreationChannelServiceId { get; set; }
[SwaggerSchema(WriteOnly = true)]
public int? CreationChannelActivityId { get; set; }

[JsonIgnore]
public Candidate Candidate => CreateCandidate();
Expand Down Expand Up @@ -135,8 +142,37 @@ private void ConfigureChannel(Candidate candidate)
{
if (CandidateId == null)
{
candidate.ChannelId = (int?)Candidate.Channel.SchoolsExperience;
if (CreationChannelSourceId.HasValue)
{
candidate.ChannelId = null;
// NB: CreationChannel should be true only if it is the first ContactChannelCreation record
AddCandidateCreationChannel(candidate, !candidate.ContactChannelCreations.Any());
}
else
{
candidate.ChannelId = (int?)Candidate.Channel.SchoolsExperience;
}
}
else // Candidate record already exists
{
// NB: we do not update a candidate's ChannelId for an existing record
// NB: CreationChannel should always be false for existing candidates
if (CreationChannelSourceId.HasValue)
{
AddCandidateCreationChannel(candidate, false);
}
}
}

private void AddCandidateCreationChannel(Candidate candidate, bool creationChannel)
{
candidate.ContactChannelCreations.Add(new ContactChannelCreation()
{
CreationChannel = creationChannel,
CreationChannelSourceId = CreationChannelSourceId,
CreationChannelServiceId = CreationChannelServiceId,
CreationChannelActivityId = CreationChannelActivityId,
});
}

private void AcceptPrivacyPolicy(Candidate candidate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
using GetIntoTeachingApi.Models.Crm;
using GetIntoTeachingApi.Models.GetIntoTeaching;
using System;
using System.Linq;
using Xunit;

namespace GetIntoTeachingApiTests.Models.GetIntoTeaching
{
public class GetIntoTeachingCallbackTests
{
[Fact]
public void Constructor_WithCandidate_MapsCorrectly()
public void Constructor_WithExistingCandidate_MapsCorrectly()
{
var candidate = new Candidate()
{
Expand All @@ -30,7 +31,7 @@ public void Constructor_WithCandidate_MapsCorrectly()
}

[Fact]
public void Candidate_MapsCorrectly()
public void ExistingCandidate_MapsCorrectly()
{
var request = new GetIntoTeachingCallback()
{
Expand All @@ -42,6 +43,9 @@ public void Candidate_MapsCorrectly()
AddressTelephone = "123456789",
PhoneCallScheduledAt = DateTime.UtcNow,
TalkingPoints = "Talking points",
CreationChannelSourceId = 222750003,
CreationChannelServiceId = 222750002,
CreationChannelActivityId = 222750001,
};

var candidate = request.Candidate;
Expand All @@ -61,6 +65,32 @@ public void Candidate_MapsCorrectly()

candidate.PrivacyPolicy.AcceptedPolicyId.Should().Be((Guid)request.AcceptedPolicyId);
candidate.PrivacyPolicy.AcceptedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(30));

var contactChannelCreation = candidate.ContactChannelCreations.First();
contactChannelCreation.CreationChannel.Should().Be(false);
contactChannelCreation.CreationChannelSourceId.Should().Be(request.CreationChannelSourceId);
contactChannelCreation.CreationChannelServiceId.Should().Be(request.CreationChannelServiceId);
contactChannelCreation.CreationChannelActivityId.Should().Be(request.CreationChannelActivityId);
candidate.ChannelId.Should().Be(null);
}

[Fact]
public void NewCandidate_MapsCorrectly()
{
var request = new GetIntoTeachingCallback()
{
CandidateId = null,
CreationChannelSourceId = 222750003,
CreationChannelServiceId = 222750002,
CreationChannelActivityId = 222750001,
};

var contactChannelCreation = request.Candidate.ContactChannelCreations.First();
contactChannelCreation.CreationChannel.Should().Be(true);
contactChannelCreation.CreationChannelSourceId.Should().Be(request.CreationChannelSourceId);
contactChannelCreation.CreationChannelServiceId.Should().Be(request.CreationChannelServiceId);
contactChannelCreation.CreationChannelActivityId.Should().Be(request.CreationChannelActivityId);
request.Candidate.ChannelId.Should().Be(null);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace GetIntoTeachingApiTests.Models.GetIntoTeaching
public class TeachingEventAddAttendeeTests
{
[Fact]
public void Constructor_WithCandidate_MapsCorrectly()
public void Constructor_WithExistingCandidate_MapsCorrectly()
{
var latestQualification = new CandidateQualification()
{
Expand Down Expand Up @@ -64,7 +64,7 @@ public void Constructor_WithCandidate_MapsCorrectly()
}

[Fact]
public void Candidate_MapsCorrectly()
public void ExistingCandidate_MapsCorrectly()
{
var request = new TeachingEventAddAttendee()
{
Expand All @@ -82,6 +82,9 @@ public void Candidate_MapsCorrectly()
AddressPostcode = "KY11 9YU",
SubscribeToMailingList = true,
IsWalkIn = false,
CreationChannelSourceId = 222750003,
CreationChannelServiceId = 222750002,
CreationChannelActivityId = 222750001,
};

var candidate = request.Candidate;
Expand Down Expand Up @@ -121,6 +124,32 @@ public void Candidate_MapsCorrectly()
candidate.Qualifications.First().DegreeStatusId.Should().Be(request.DegreeStatusId);
candidate.Qualifications.First().TypeId.Should().Be((int)CandidateQualification.DegreeType.Degree);
candidate.Qualifications.First().Id.Should().Be(request.QualificationId);

var contactChannelCreation = candidate.ContactChannelCreations.First();
contactChannelCreation.CreationChannel.Should().Be(false);
contactChannelCreation.CreationChannelSourceId.Should().Be(request.CreationChannelSourceId);
contactChannelCreation.CreationChannelServiceId.Should().Be(request.CreationChannelServiceId);
contactChannelCreation.CreationChannelActivityId.Should().Be(request.CreationChannelActivityId);
candidate.ChannelId.Should().Be(null);
}

[Fact]
public void NewCandidate_MapsCorrectly()
{
var request = new TeachingEventAddAttendee()
{
CandidateId = null,
CreationChannelSourceId = 222750003,
CreationChannelServiceId = 222750002,
CreationChannelActivityId = 222750001,
};

var contactChannelCreation = request.Candidate.ContactChannelCreations.First();
contactChannelCreation.CreationChannel.Should().Be(true);
contactChannelCreation.CreationChannelSourceId.Should().Be(request.CreationChannelSourceId);
contactChannelCreation.CreationChannelServiceId.Should().Be(request.CreationChannelServiceId);
contactChannelCreation.CreationChannelActivityId.Should().Be(request.CreationChannelActivityId);
request.Candidate.ChannelId.Should().Be(null);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using FluentAssertions;
using FluentValidation.TestHelper;
using GetIntoTeachingApi.Models.GetIntoTeaching;
using GetIntoTeachingApi.Models.GetIntoTeaching.Validators;
Expand All @@ -20,6 +22,34 @@ public GetIntoTeachingCallbackValidatorTests()
_validator = new GetIntoTeachingCallbackValidator(_mockStore.Object, new DateTimeProvider());
_callback = new GetIntoTeachingCallback();
}

[Fact]
public void Validate_WhenValid_HasNoErrors()
{
// var mockPickListItem = new PickListItem { Id = 123 };
// var mockSubject = new TeachingSubject { Id = Guid.NewGuid() };
// var mockPrivacyPolicy = new PrivacyPolicy { Id = Guid.NewGuid() };

var request = new GetIntoTeachingCallback()
{
FirstName = "John",
LastName = "Doe",
Email = "[email protected]",
AcceptedPolicyId = Guid.NewGuid(),
AddressTelephone = "123456789",
PhoneCallScheduledAt = DateTime.UtcNow.AddDays(1),
TalkingPoints = "Talking points",
CreationChannelSourceId = 222750003,
CreationChannelServiceId = 222750002,
CreationChannelActivityId = 222750001,
};

var result = _validator.TestValidate(request);
// Ensure no validation errors on root object (we expect errors on the Candidate
// properties as we can't mock them).
var propertiesWithErrors = result.Errors.Select(e => e.PropertyName);
propertiesWithErrors.All(p => p.StartsWith("Candidate.")).Should().BeTrue();
}

[Fact]
public void Validate_RequiredFieldsWhenNull_HasError()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public void Validate_WhenValid_HasNoErrors()
AddressPostcode = "KY11 9YU",
IsWalkIn = true,
IsVerified = false,
CreationChannelSourceId = 222750003,
CreationChannelServiceId = 222750002,
CreationChannelActivityId = 222750001,
};

var result = _validator.TestValidate(request);
Expand Down
Loading

0 comments on commit 8ab448e

Please sign in to comment.