From e1256dce862001cd123c1afbe63286d134edbeae Mon Sep 17 00:00:00 2001 From: Martyn Whitwell Date: Tue, 14 Jan 2025 18:00:49 +0000 Subject: [PATCH] add creation channel to mailing list endpoint --- .../GetIntoTeaching/MailingListAddMember.cs | 38 ++++++++++++++++++- .../MailingListAddMemberTests.cs | 33 +++++++++++++++- .../MailingListAddMemberValidatorTests.cs | 3 ++ 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/GetIntoTeachingApi/Models/GetIntoTeaching/MailingListAddMember.cs b/GetIntoTeachingApi/Models/GetIntoTeaching/MailingListAddMember.cs index 6aad83644..d3a60b900 100644 --- a/GetIntoTeachingApi/Models/GetIntoTeaching/MailingListAddMember.cs +++ b/GetIntoTeachingApi/Models/GetIntoTeaching/MailingListAddMember.cs @@ -20,6 +20,13 @@ public class MailingListAddMember public int? DegreeStatusId { get; set; } [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; } public string Email { get; set; } public string FirstName { get; set; } @@ -104,9 +111,38 @@ private void ConfigureChannel(Candidate candidate) { if (CandidateId == null) { - candidate.ChannelId = ChannelId ?? (int?)Candidate.Channel.MailingList; + 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.MailingList; + } + } + 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) { diff --git a/GetIntoTeachingApiTests/Models/GetIntoTeaching/MailingListAddMemberTests.cs b/GetIntoTeachingApiTests/Models/GetIntoTeaching/MailingListAddMemberTests.cs index 1f2e3fb64..1165c45b5 100644 --- a/GetIntoTeachingApiTests/Models/GetIntoTeaching/MailingListAddMemberTests.cs +++ b/GetIntoTeachingApiTests/Models/GetIntoTeaching/MailingListAddMemberTests.cs @@ -62,7 +62,7 @@ public void Constructor_WithCandidate_MapsCorrectly() } [Fact] - public void Candidate_MapsCorrectly() + public void ExistingCandidate_MapsCorrectly() { var request = new MailingListAddMember() { @@ -76,7 +76,10 @@ public void Candidate_MapsCorrectly() FirstName = "John", LastName = "Doe", AddressPostcode = "KY11 9YU", - WelcomeGuideVariant = "variant1" + WelcomeGuideVariant = "variant1", + CreationChannelSourceId = 222750003, + CreationChannelServiceId = 222750002, + CreationChannelActivityId = 222750001, }; var candidate = request.Candidate; @@ -112,6 +115,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_MapsCreationChannelCorrectly() + { + var request = new MailingListAddMember() + { + 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] diff --git a/GetIntoTeachingApiTests/Models/GetIntoTeaching/Validators/MailingListAddMemberValidatorTests.cs b/GetIntoTeachingApiTests/Models/GetIntoTeaching/Validators/MailingListAddMemberValidatorTests.cs index a83953e13..ab0d91865 100644 --- a/GetIntoTeachingApiTests/Models/GetIntoTeaching/Validators/MailingListAddMemberValidatorTests.cs +++ b/GetIntoTeachingApiTests/Models/GetIntoTeaching/Validators/MailingListAddMemberValidatorTests.cs @@ -41,6 +41,9 @@ public void Validate_WhenValid_HasNoErrors() FirstName = "John", LastName = "Doe", AddressPostcode = "KY11 9YU", + CreationChannelSourceId = 222750003, + CreationChannelServiceId = 222750002, + CreationChannelActivityId = 222750001, }; var result = _validator.TestValidate(request);